use async_trait::async_trait;
use image::imageops::{self, FilterType};
use super::OnnxOptions;
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, Letterbox, RawRegion};
use crate::manifest::{VerifiedModel, VisionTask};
use crate::media::DecodedImage;
use crate::sent::SentImage;
use crate::traits::{DetectQuery, Detector};
const STRIDES: [u32; 3] = [8, 16, 32];
pub struct YuNet {
descriptor: BackendDescriptor,
model: OrtModel,
input: String,
size: (u32, u32),
letterbox: Letterbox,
label: String,
threshold: f32,
nms_iou: f32,
max_detections: usize,
}
impl std::fmt::Debug for YuNet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("YuNet")
.field("descriptor", &self.descriptor)
.field("threshold", &self.threshold)
.finish_non_exhaustive()
}
}
impl YuNet {
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,
"YuNet needs `io.input`, the size its graph takes".into(),
));
};
let label = match manifest.io.labels.as_slice() {
[label] if !label.is_empty() => label.clone(),
_ => {
return Err(manifest_error(
manifest,
"YuNet needs exactly one label in `io.labels`, such as `face`".into(),
));
}
};
let model_session = OrtModel::load(&manifest.id, onnx_file(model, "", "")?, options)?;
let missing: Vec<String> = STRIDES
.iter()
.flat_map(|s| [format!("cls_{s}"), format!("obj_{s}"), format!("bbox_{s}")])
.filter(|name| !model_session.outputs().iter().any(|(n, _)| n == name))
.collect();
if !missing.is_empty() {
return Err(manifest_error(
manifest,
format!("the graph has no output {missing:?}; is it a YuNet 2023mar export?"),
));
}
let input_name = model_session.input_named(&["input"], 0)?;
let descriptor = BackendDescriptor::new(
manifest.id.clone(),
Locality::InProcess,
CoordFrame::SentPixels,
)
.with_manifest(manifest.id.clone());
Ok(Self {
descriptor,
model: model_session,
input: input_name,
size: (input.width, input.height),
letterbox: input.letterbox,
label,
threshold: 0.5,
nms_iou: 0.3,
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
}
fn check_query(&self, query: &DetectQuery) -> Result<()> {
match query {
DetectQuery::Classes(names)
if names.iter().all(|n| n.trim().eq_ignore_ascii_case(&self.label)) =>
{
Ok(())
}
DetectQuery::Classes(names) => Err(VisionError::InvalidInput(format!(
"`{}` detects `{}` only, not {:?}",
self.descriptor.id, self.label, names
))),
DetectQuery::Concepts(_) => Err(VisionError::Unsupported(format!(
"`{}` detects `{}` only; ask for `DetectQuery::Classes`",
self.descriptor.id, self.label
))),
}
}
async fn pass(&self, pixels: &DecodedImage, fit: (u32, u32)) -> Result<Vec<Detection>> {
let (iw, ih) = self.size;
let geometry = self.letterbox.geometry(pixels.size(), fit);
let (rw, rh) = geometry.resized;
let resized;
let source = if (rw, rh) == pixels.size() {
pixels.inner()
} else {
resized = imageops::resize(pixels.inner(), rw, rh, FilterType::Triangle);
&resized
};
let (w, h) = (iw as usize, ih as usize);
let mut tensor = vec![0.0f32; 3 * w * h];
let (left, top) = (geometry.pad_left as usize, geometry.pad_top as usize);
for (x, y, pixel) in source.enumerate_pixels() {
let (tx, ty) = (x as usize + left, y as usize + top);
if tx >= w || ty >= h {
continue;
}
for (plane, channel) in [2usize, 1, 0].into_iter().enumerate() {
tensor[plane * w * h + ty * w + tx] = f32::from(pixel.0[channel]);
}
}
let names: Vec<String> = STRIDES
.iter()
.flat_map(|s| [format!("cls_{s}"), format!("obj_{s}"), format!("bbox_{s}")])
.collect();
let wanted: Vec<&str> = names.iter().map(String::as_str).collect();
let outputs = self
.model
.run(
vec![(
self.input.clone(),
Input::F32(vec![1, 3, i64::from(ih), i64::from(iw)], tensor),
)],
&wanted,
)
.await?;
let mut found = Vec::new();
for stride in STRIDES {
let head = Head {
cls: &outputs[&format!("cls_{stride}")].data,
obj: &outputs[&format!("obj_{stride}")].data,
bbox: &outputs[&format!("bbox_{stride}")].data,
};
found.extend(decode(&head, stride, (iw, ih), self.threshold));
}
let (sx, sy) = (geometry.scale_x as f32, geometry.scale_y as f32);
let (px, py) = (geometry.pad_left as f32, geometry.pad_top as f32);
let (pw, ph) = pixels.size();
Ok(found
.into_iter()
.map(|d| Detection {
score: d.score,
bbox: BBox::new(
(d.bbox.x0 - px) / sx,
(d.bbox.y0 - py) / sy,
(d.bbox.x1 - px) / sx,
(d.bbox.y1 - py) / sy,
)
.clamped(pw, ph),
})
.filter(|d| d.bbox.width() > 0.0 && d.bbox.height() > 0.0)
.collect())
}
}
pub(crate) struct Head<'a> {
pub cls: &'a [f32],
pub obj: &'a [f32],
pub bbox: &'a [f32],
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Detection {
pub score: f32,
pub bbox: BBox,
}
pub(crate) fn decode(
head: &Head<'_>,
stride: u32,
input: (u32, u32),
threshold: f32,
) -> Vec<Detection> {
let cols = (input.0 / stride) as usize;
let rows = (input.1 / stride) as usize;
let anchors = (cols * rows).min(head.cls.len()).min(head.obj.len()).min(head.bbox.len() / 4);
let s = stride as f32;
let mut found = Vec::new();
for i in 0..anchors {
let score = (head.cls[i].clamp(0.0, 1.0) * head.obj[i].clamp(0.0, 1.0)).sqrt();
if score < threshold {
continue;
}
let (r, c) = ((i / cols) as f32, (i % cols) as f32);
let b = &head.bbox[i * 4..i * 4 + 4];
let (cx, cy) = ((c + b[0]) * s, (r + b[1]) * s);
let (w, h) = (b[2].exp() * s, b[3].exp() * s);
found.push(Detection {
score,
bbox: BBox::new(cx - w / 2.0, cy - h / 2.0, cx + w / 2.0, cy + h / 2.0),
});
}
found
}
pub(crate) fn suppress(mut found: Vec<Detection>, iou: f32, max: usize) -> Vec<Detection> {
found.sort_by(|a, b| b.score.total_cmp(&a.score));
let mut kept: Vec<Detection> = Vec::new();
for d in found {
if kept.len() >= max {
break;
}
if kept.iter().all(|k| k.bbox.iou(&d.bbox) < iou) {
kept.push(d);
}
}
kept
}
#[async_trait]
impl Detector for YuNet {
fn descriptor(&self) -> &BackendDescriptor {
&self.descriptor
}
async fn detect(
&self,
cx: &VisionCx,
image: &SentImage,
query: &DetectQuery,
) -> Result<Vec<RawRegion>> {
cx.check()?;
self.check_query(query)?;
let (iw, ih) = self.size;
let mut found = self.pass(image.pixels(), (iw, ih)).await?;
cx.check()?;
found.extend(self.pass(image.pixels(), ((iw / 2).max(1), (ih / 2).max(1))).await?);
let faces = suppress(found, self.nms_iou, self.max_detections);
tracing::debug!(vision.backend = %self.descriptor.id, vision.regions = faces.len(), "yunet detected");
Ok(faces
.into_iter()
.map(|d| RawRegion::boxed(d.bbox, self.label.clone(), d.score))
.collect())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn anchors_decode_as_opencv_does() {
let head = Head {
cls: &[0.25, 0.81],
obj: &[0.25, 1.4],
bbox: &[0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0],
};
let found = decode(&head, 16, (32, 16), 0.5);
assert_eq!(found.len(), 1);
assert!((found[0].score - 0.9).abs() < 1e-6, "{found:?}");
assert_eq!(found[0].bbox, BBox::new(16.0, 0.0, 32.0, 16.0));
assert_eq!(decode(&head, 16, (32, 16), 0.2).len(), 2);
}
#[test]
fn suppression_keeps_the_best_of_overlapping_boxes() {
let d = |score, x0| Detection { score, bbox: BBox::new(x0, 0.0, x0 + 10.0, 10.0) };
let kept = suppress(vec![d(0.6, 1.0), d(0.9, 0.0), d(0.7, 50.0)], 0.3, 10);
assert_eq!(kept, vec![d(0.9, 0.0), d(0.7, 50.0)]);
assert_eq!(suppress(vec![d(0.6, 1.0), d(0.9, 0.0), d(0.7, 50.0)], 0.3, 1).len(), 1);
}
}