Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Postprocess borrowed detector outputs

laurenspriem6b13b4ed86b7
diff --git a/rust/crates/photos/src/ml/face/detect.rs b/rust/crates/photos/src/ml/face/detect.rs
index d17b2891e9..f8b5e53d65 100644
--- a/rust/crates/photos/src/ml/face/detect.rs
+++ b/rust/crates/photos/src/ml/face/detect.rs
@@ -16,12 +16,18 @@ pub(crate) fn run_face_detection(
input: &YoloInput,
) -> MlResult<Vec<FaceDetection>> {
let mut face_detection = runtime.face_detection_session()?;
- let output_data = onnx::run_prepared_f32_data(
+ onnx::with_prepared_f32_output(
&mut face_detection,
&input.tensor,
[1, 3, INPUT_HEIGHT as i64, INPUT_WIDTH as i64],
- )?;
+ |_output_shape, output_data| postprocess_face_detections(output_data, input),
+ )
+}
+fn postprocess_face_detections(
+ output_data: &[f32],
+ input: &YoloInput,
+) -> MlResult<Vec<FaceDetection>> {
let row_len = 16usize;
if output_data.len() < row_len {
return Err(MlError::Postprocess(
diff --git a/rust/crates/photos/src/ml/onnx.rs b/rust/crates/photos/src/ml/onnx.rs
index 4a30ee7408..5faa34195b 100644
--- a/rust/crates/photos/src/ml/onnx.rs
+++ b/rust/crates/photos/src/ml/onnx.rs
@@ -209,14 +209,16 @@ pub fn run_f32<const N: usize>(
}
}
-/// Run inference using a reusable, borrowed f32 input.
+/// Run inference using a reusable input and process the first output in place.
///
-/// Automatically uses the lazily cached FP16 representation for FP16 models.
-pub(crate) fn run_prepared_f32<const N: usize>(
+/// Native f32 output is borrowed directly from ONNX Runtime. FP16 output uses
+/// a temporary f32 conversion buffer because postprocessing operates on f32.
+pub(crate) fn with_prepared_f32_output<const N: usize, T>(
session: &mut Session,
input: &PreparedF32Input,
input_shape: [i64; N],
-) -> MlResult<(Vec<i64>, Vec<f32>)> {
+ consume: impl FnOnce(&[i64], &[f32]) -> MlResult<T>,
+) -> MlResult<T> {
let outputs = if session_expects_f16(session) {
let input_tensor =
TensorRef::<half::f16>::from_array_view((input_shape, input.f16_data()))?;
@@ -232,49 +234,12 @@ pub(crate) fn run_prepared_f32<const N: usize>(
}
let output = &outputs[0];
if let Ok((tensor_shape, tensor_data)) = output.try_extract_tensor::<f32>() {
- let shape = tensor_shape.iter().copied().collect::<Vec<_>>();
- Ok((shape, tensor_data.to_vec()))
+ consume(tensor_shape, tensor_data)
} else {
let (tensor_shape, tensor_data) = output.try_extract_tensor::<half::f16>()?;
- let shape = tensor_shape.iter().copied().collect::<Vec<_>>();
- let mut data = vec![0.0; tensor_data.len()];
- tensor_data.convert_to_f32_slice(&mut data);
- Ok((shape, data))
- }
-}
-
-pub(crate) fn run_prepared_f32_data<const N: usize>(
- session: &mut Session,
- input: &PreparedF32Input,
- input_shape: [i64; N],
-) -> MlResult<Vec<f32>> {
- run_prepared_f32(session, input, input_shape).map(|(_, data)| data)
-}
-
-pub fn run_f32_data<const N: usize>(
- session: &mut Session,
- input: Vec<f32>,
- input_shape: [i64; N],
-) -> MlResult<Vec<f32>> {
- let outputs = if session_expects_f16(session) {
- let f16_input = Vec::<half::f16>::from_f32_slice(&input);
- let input_tensor = Tensor::<half::f16>::from_array((input_shape, f16_input))?;
- session.run(ort::inputs![input_tensor])?
- } else {
- let input_tensor = Tensor::<f32>::from_array((input_shape, input))?;
- session.run(ort::inputs![input_tensor])?
- };
- if outputs.len() == 0 {
- return Err(MlError::Ort("missing first output tensor".to_string()));
- }
- let output = &outputs[0];
- if let Ok((_tensor_shape, tensor_data)) = output.try_extract_tensor::<f32>() {
- Ok(tensor_data.to_vec())
- } else {
- let (_tensor_shape, tensor_data) = output.try_extract_tensor::<half::f16>()?;
let mut data = vec![0.0; tensor_data.len()];
tensor_data.convert_to_f32_slice(&mut data);
- Ok(data)
+ consume(tensor_shape, &data)
}
}
diff --git a/rust/crates/photos/src/ml/pet/detect.rs b/rust/crates/photos/src/ml/pet/detect.rs
index 1a88715a20..1b12c88500 100644
--- a/rust/crates/photos/src/ml/pet/detect.rs
+++ b/rust/crates/photos/src/ml/pet/detect.rs
@@ -37,12 +37,21 @@ pub(crate) fn run_pet_face_detection(
input: &YoloInput,
) -> MlResult<Vec<PetFaceDetection>> {
let mut pet_face_detection = runtime.pet_face_detection_session()?;
- let (output_shape, output_data) = onnx::run_prepared_f32(
+ onnx::with_prepared_f32_output(
&mut pet_face_detection,
&input.tensor,
[1, 3, INPUT_HEIGHT as i64, INPUT_WIDTH as i64],
- )?;
+ |output_shape, output_data| {
+ postprocess_pet_face_detections(output_shape, output_data, input)
+ },
+ )
+}
+fn postprocess_pet_face_detections(
+ output_shape: &[i64],
+ output_data: &[f32],
+ input: &YoloInput,
+) -> MlResult<Vec<PetFaceDetection>> {
// Row format: [x, y, w, h, conf, lm_x1, lm_y1, lm_x2, lm_y2, lm_x3, lm_y3, cls0, cls1]
// row_len = 4 + 1 + 6 + 2 = 13 for 2-class model
// Use the output shape's last dimension to determine row length reliably.
@@ -158,12 +167,18 @@ pub(crate) fn run_pet_body_detection(
input: &YoloInput,
) -> MlResult<Vec<PetBodyDetection>> {
let mut body_detection = runtime.pet_body_detection_session()?;
- let (_output_shape, output_data) = onnx::run_prepared_f32(
+ onnx::with_prepared_f32_output(
&mut body_detection,
&input.tensor,
[1, 3, INPUT_HEIGHT as i64, INPUT_WIDTH as i64],
- )?;
+ |_output_shape, output_data| postprocess_pet_body_detections(output_data, input),
+ )
+}
+fn postprocess_pet_body_detections(
+ output_data: &[f32],
+ input: &YoloInput,
+) -> MlResult<Vec<PetBodyDetection>> {
// YOLOv5 output format: [x, y, w, h, obj_conf, cls0, cls1, ..., cls79]
// Total columns: 4 + 1 + 80 = 85
let row_len = 85usize;