use async_trait::async_trait;
use super::OnnxOptions;
use super::preprocess::{Normalize, sigmoid, upsample_bilinear};
use super::session::{Input, OrtModel, check_manifest, manifest_error, onnx_file};
use crate::backend::{BackendDescriptor, Locality};
use crate::cx::VisionCx;
use crate::error::{Result, VisionError};
use crate::geometry::{BBox, CoordFrame, Mask, RawRegion, Shape};
use crate::manifest::{VerifiedModel, VisionTask};
use crate::sent::SentImage;
use crate::traits::{DetectQuery, Detector};
pub struct RfDetr {
descriptor: BackendDescriptor,
model: OrtModel,
input: String,
boxes: String,
logits: String,
masks: Option<String>,
normalize: Normalize,
labels: Vec<String>,
threshold: f32,
max_detections: usize,
}
impl std::fmt::Debug for RfDetr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RfDetr")
.field("descriptor", &self.descriptor)
.field("threshold", &self.threshold)
.field("masks", &self.masks.is_some())
.finish_non_exhaustive()
}
}
impl RfDetr {
pub fn new(model: &VerifiedModel, options: &OnnxOptions) -> Result<Self> {
let manifest = model.manifest();
check_manifest(manifest, VisionTask::Detect)?;
let Some(input) = manifest.io.input else {
return Err(manifest_error(
manifest,
"RF-DETR needs `io.input`, the square it was trained at".into(),
));
};
if manifest.io.labels.iter().all(String::is_empty) {
return Err(manifest_error(
manifest,
"RF-DETR needs `io.labels`, one per logit column".into(),
));
}
let session = OrtModel::load(&manifest.id, onnx_file(model, "", "")?, options)?;
let outputs = session.outputs();
let named = |names: &[&str]| {
outputs.iter().find(|(n, _)| names.contains(&n.as_str())).map(|(n, _)| n.clone())
};
let boxes = named(&["pred_boxes", "dets", "boxes"]).or_else(|| {
outputs.iter().find(|(_, s)| s.len() == 3 && s[2] == 4).map(|(n, _)| n.clone())
});
let logits = named(&["logits", "labels", "scores"]);
let (Some(boxes), Some(logits)) = (boxes, logits) else {
return Err(manifest_error(
manifest,
format!(
"the graph's outputs {:?} hold no boxes and logits pair",
outputs.iter().map(|(n, _)| n).collect::<Vec<_>>()
),
));
};
let masks = named(&["pred_masks", "masks"]).or_else(|| {
outputs
.iter()
.find(|(n, s)| s.len() == 4 && *n != boxes && *n != logits)
.map(|(n, _)| n.clone())
});
let input_name = session.input_named(&["pixel_values", "input", "images"], 0)?;
let normalize =
Normalize::from_io(&manifest.io, (input.width, input.height), input.letterbox);
let descriptor = BackendDescriptor::new(
manifest.id.clone(),
Locality::InProcess,
CoordFrame::ModelInput {
width: input.width,
height: input.height,
letterbox: input.letterbox,
},
)
.with_manifest(manifest.id.clone());
Ok(Self {
descriptor,
model: session,
input: input_name,
boxes,
logits,
masks,
normalize,
labels: manifest.io.labels.clone(),
threshold: 0.5,
max_detections: 300,
})
}
pub fn with_threshold(mut self, threshold: f32) -> Self {
self.threshold = threshold;
self
}
pub fn with_max_detections(mut self, max: usize) -> Self {
self.max_detections = max;
self
}
pub fn segments(&self) -> bool {
self.masks.is_some()
}
fn columns(&self, query: &DetectQuery) -> Result<Vec<bool>> {
let known = |c: usize| !self.labels[c].is_empty();
match query {
DetectQuery::Classes(names) if names.is_empty() => {
Ok((0..self.labels.len()).map(known).collect())
}
DetectQuery::Classes(names) => {
let mut wanted = vec![false; self.labels.len()];
for name in names {
let column = self
.labels
.iter()
.position(|l| !l.is_empty() && l.eq_ignore_ascii_case(name.trim()))
.ok_or_else(|| {
VisionError::InvalidInput(format!(
"`{}` detects {} classes and `{name}` is not one of them, such as {}",
self.descriptor.id,
self.labels.iter().filter(|l| !l.is_empty()).count(),
self.labels
.iter()
.filter(|l| !l.is_empty())
.take(5)
.map(String::as_str)
.collect::<Vec<_>>()
.join(", ")
))
})?;
wanted[column] = true;
}
Ok(wanted)
}
DetectQuery::Concepts(_) => Err(VisionError::Unsupported(format!(
"`{}` detects the closed set of classes its manifest lists; ask for \
`DetectQuery::Classes`, or route open-vocabulary concepts to a concept detector such as `sam-3.1`",
self.descriptor.id
))),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct Detection {
pub query: usize,
pub class: usize,
pub score: f32,
pub bbox: BBox,
}
pub(crate) fn decode(
logits: &[f32],
boxes: &[f32],
classes: usize,
columns: &[bool],
input: (u32, u32),
threshold: f32,
max: usize,
) -> Vec<Detection> {
let queries = boxes.len() / 4;
let (w, h) = (input.0 as f32, input.1 as f32);
let mut found: Vec<Detection> = Vec::new();
for query in 0..queries.min(logits.len() / classes.max(1)) {
for (class, &allowed) in columns.iter().enumerate().take(classes) {
if !allowed {
continue;
}
let score = sigmoid(logits[query * classes + class]);
if score < threshold {
continue;
}
let b = &boxes[query * 4..query * 4 + 4];
let (cx, cy, bw, bh) = (b[0] * w, b[1] * h, b[2] * w, b[3] * h);
let bbox = BBox::new(cx - bw / 2.0, cy - bh / 2.0, cx + bw / 2.0, cy + bh / 2.0);
found.push(Detection { query, class, score, bbox });
}
}
found.sort_by(|a, b| b.score.total_cmp(&a.score));
found.truncate(max);
found
}
pub(crate) fn instance_mask(logits: &[f32], raster: (usize, usize), input: (u32, u32)) -> Mask {
let (w, h) = (input.0 as usize, input.1 as usize);
let up = upsample_bilinear(logits, raster, (w, h));
let bits: Vec<bool> = up.iter().map(|&v| v > 0.0).collect();
Mask::encode(input.0, input.1, &bits)
}
#[async_trait]
impl Detector for RfDetr {
fn descriptor(&self) -> &BackendDescriptor {
&self.descriptor
}
async fn detect(
&self,
cx: &VisionCx,
image: &SentImage,
query: &DetectQuery,
) -> Result<Vec<RawRegion>> {
cx.check()?;
let columns = self.columns(query)?;
let (iw, ih) = self.normalize.size;
let tensor = self.normalize.tensor(image.pixels());
let mut wanted = vec![self.boxes.as_str(), self.logits.as_str()];
if let Some(masks) = &self.masks {
wanted.push(masks);
}
let outputs = self
.model
.run(
vec![(
self.input.clone(),
Input::F32(vec![1, 3, i64::from(ih), i64::from(iw)], tensor),
)],
&wanted,
)
.await?;
let (boxes, logits) = (&outputs[&self.boxes], &outputs[&self.logits]);
let classes = logits.dims().last().copied().unwrap_or(0);
if classes != self.labels.len() {
return Err(self.model.error(format!(
"the graph answers {classes} classes but the manifest labels {}",
self.labels.len()
)));
}
let detections = decode(
&logits.data,
&boxes.data,
classes,
&columns,
(iw, ih),
self.threshold,
self.max_detections,
);
let masks = self.masks.as_ref().map(|name| &outputs[name]);
let regions = detections
.into_iter()
.map(|d| {
let shape = masks
.and_then(|m| {
let dims = m.dims();
let [_, _, mh, mw] = dims.as_slice() else { return None };
let size = mh * mw;
let logits = m.data.get(d.query * size..(d.query + 1) * size)?;
let mask = instance_mask(logits, (*mw, *mh), (iw, ih));
(mask.area() > 0).then_some(Shape::Mask(mask))
})
.unwrap_or(Shape::Box(d.bbox));
RawRegion {
shape,
label: Some(self.labels[d.class].clone()),
score: Some(d.score),
track: None,
}
})
.collect::<Vec<_>>();
tracing::debug!(vision.backend = %self.descriptor.id, vision.regions = regions.len(), "rf-detr detected");
Ok(regions)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn logit(p: f32) -> f32 {
(p / (1.0 - p)).ln()
}
#[test]
fn pairs_over_the_threshold_are_detections_best_first_in_input_pixels() {
let logits = [
logit(0.9),
logit(0.2),
logit(0.7), logit(0.1),
logit(0.95),
logit(0.6), logit(0.3),
logit(0.3),
logit(0.3), ];
let boxes = [
0.5, 0.5, 0.5, 0.25, 0.25, 0.75, 0.1, 0.1, 0.5, 0.5, 1.0, 1.0,
];
let columns = [false, true, true];
let found = decode(&logits, &boxes, 3, &columns, (384, 384), 0.5, 300);
assert_eq!(
found.iter().map(|d| (d.query, d.class)).collect::<Vec<_>>(),
vec![(1, 1), (0, 2), (1, 2)]
);
assert!((found[0].score - 0.95).abs() < 1e-5);
let b = found[1].bbox;
assert!(
(b.x0 - 96.0).abs() < 1e-3
&& (b.y0 - 144.0).abs() < 1e-3
&& (b.x1 - 288.0).abs() < 1e-3
&& (b.y1 - 240.0).abs() < 1e-3,
"{b:?}"
);
assert_eq!(decode(&logits, &boxes, 3, &columns, (384, 384), 0.5, 1).len(), 1);
assert!(found.iter().all(|d| d.class != 0));
}
#[test]
fn instance_masks_are_upsampled_to_the_model_input() {
let mask = instance_mask(&[5.0, -5.0, -5.0, -5.0], (2, 2), (4, 4));
assert_eq!((mask.width, mask.height), (4, 4));
assert_eq!(mask.bbox(), Some(BBox::new(0.0, 0.0, 2.0, 2.0)));
}
}