π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Don't use custom interpolation in preprocessing of ML models
laurenspriemaf2b24fa6266
diff --git a/mobile/apps/photos/rust/src/ml/face/detect.rs b/mobile/apps/photos/rust/src/ml/face/detect.rsindex 2b4f5b499d..2a57ffa43b 100644--- a/mobile/apps/photos/rust/src/ml/face/detect.rs+++ b/mobile/apps/photos/rust/src/ml/face/detect.rs@@ -14,7 +14,8 @@ pub fn run_face_detection(runtime: &mut MlRuntime,decoded: &DecodedImage,) -> MlResult<Vec<FaceDetection>> {- let (input, scaled_width, scaled_height) = preprocess::preprocess_yolo(decoded)?;+ let (input, scaled_width, scaled_height, pad_left, pad_top) =+ preprocess::preprocess_yolo(decoded)?;let face_detection = runtime.face_detection_session_mut()?;let (output_shape, output_data) = onnx::run_f32(face_detection,@@ -63,6 +64,8 @@ pub fn run_face_detection(&mut keypoints,scaled_width,scaled_height,+ pad_left,+ pad_top,);detections.push(FaceDetection {@@ -81,22 +84,35 @@ fn correct_for_maintained_aspect_ratio(keypoints: &mut [[f32; 2]; 5],scaled_width: usize,scaled_height: usize,+ pad_left: usize,+ pad_top: usize,) {- if scaled_width == INPUT_WIDTH as usize && scaled_height == INPUT_HEIGHT as usize {+ if scaled_width == INPUT_WIDTH as usize+ && scaled_height == INPUT_HEIGHT as usize+ && pad_left == 0+ && pad_top == 0+ {return;}- let scale_x = INPUT_WIDTH / scaled_width as f32;- let scale_y = INPUT_HEIGHT / scaled_height as f32;+ let scaled_width = scaled_width as f32;+ let scaled_height = scaled_height as f32;+ let pad_left = pad_left as f32;+ let pad_top = pad_top as f32;- box_xyxy[0] = (box_xyxy[0] * scale_x).clamp(0.0, 1.0);- box_xyxy[1] = (box_xyxy[1] * scale_y).clamp(0.0, 1.0);- box_xyxy[2] = (box_xyxy[2] * scale_x).clamp(0.0, 1.0);- box_xyxy[3] = (box_xyxy[3] * scale_y).clamp(0.0, 1.0);+ let transform_x =+ |x: f32| -> f32 { ((x * INPUT_WIDTH - pad_left) / scaled_width).clamp(0.0, 1.0) };+ let transform_y =+ |y: f32| -> f32 { ((y * INPUT_HEIGHT - pad_top) / scaled_height).clamp(0.0, 1.0) };++ box_xyxy[0] = transform_x(box_xyxy[0]);+ box_xyxy[1] = transform_y(box_xyxy[1]);+ box_xyxy[2] = transform_x(box_xyxy[2]);+ box_xyxy[3] = transform_y(box_xyxy[3]);for point in keypoints.iter_mut() {- point[0] = (point[0] * scale_x).clamp(0.0, 1.0);- point[1] = (point[1] * scale_y).clamp(0.0, 1.0);+ point[0] = transform_x(point[0]);+ point[1] = transform_y(point[1]);}}diff --git a/mobile/apps/photos/rust/src/ml/preprocess.rs b/mobile/apps/photos/rust/src/ml/preprocess.rsindex 0608b603fb..3ad970b1a1 100644--- a/mobile/apps/photos/rust/src/ml/preprocess.rs+++ b/mobile/apps/photos/rust/src/ml/preprocess.rs@@ -13,7 +13,7 @@ const CLIP_INPUT_WIDTH: usize = 256;const CLIP_INPUT_HEIGHT: usize = 256;const PAD_VALUE: f32 = 114.0;-pub fn preprocess_yolo(decoded: &DecodedImage) -> MlResult<(Vec<f32>, usize, usize)> {+pub fn preprocess_yolo(decoded: &DecodedImage) -> MlResult<(Vec<f32>, usize, usize, usize, usize)> {if decoded.dimensions.width == 0 || decoded.dimensions.height == 0 {return Err(MlError::Preprocess("image dimensions cannot be zero".to_string(),@@ -23,28 +23,55 @@ pub fn preprocess_yolo(decoded: &DecodedImage) -> MlResult<(Vec<f32>, usize, usilet src_w = decoded.dimensions.width as f32;let src_h = decoded.dimensions.height as f32;let scale = (YOLO_INPUT_WIDTH as f32 / src_w).min(YOLO_INPUT_HEIGHT as f32 / src_h);- let scaled_width = (src_w * scale).round().clamp(0.0, YOLO_INPUT_WIDTH as f32) as usize;- let scaled_height = (src_h * scale).round().clamp(0.0, YOLO_INPUT_HEIGHT as f32) as usize;+ let scaled_width = (src_w * scale).round().clamp(1.0, YOLO_INPUT_WIDTH as f32) as u32;+ let scaled_height = (src_h * scale).round().clamp(1.0, YOLO_INPUT_HEIGHT as f32) as u32;- let mut output = vec![0f32; 3 * YOLO_INPUT_WIDTH * YOLO_INPUT_HEIGHT];+ let src_image = FirImage::from_vec_u8(+ decoded.dimensions.width,+ decoded.dimensions.height,+ decoded.rgb.clone(),+ PixelType::U8x3,+ )+ .map_err(|e| MlError::Preprocess(format!("failed to create FIR source image: {e}")))?;++ let mut resized_image = FirImage::new(scaled_width, scaled_height, PixelType::U8x3);+ let mut resizer = Resizer::new();+ let options = ResizeOptions::new().resize_alg(ResizeAlg::Interpolation(FilterType::Bilinear));+ resizer+ .resize(&src_image, &mut resized_image, Some(&options))+ .map_err(|e| MlError::Preprocess(format!("failed to resize YOLO image input: {e}")))?;++ let scaled_width_usize = scaled_width as usize;+ let scaled_height_usize = scaled_height as usize;+ let pad_left = (YOLO_INPUT_WIDTH.saturating_sub(scaled_width_usize)) / 2;+ let pad_top = (YOLO_INPUT_HEIGHT.saturating_sub(scaled_height_usize)) / 2;++ let pad_norm = PAD_VALUE / 255.0;+ let mut output = vec![pad_norm; 3 * YOLO_INPUT_WIDTH * YOLO_INPUT_HEIGHT];let green_offset = YOLO_INPUT_WIDTH * YOLO_INPUT_HEIGHT;let blue_offset = 2 * YOLO_INPUT_WIDTH * YOLO_INPUT_HEIGHT;+ let resized = resized_image.buffer();++ for y in 0..scaled_height_usize {+ for x in 0..scaled_width_usize {+ let src_idx = (y * scaled_width_usize + x) * 3;+ let dst_x = x + pad_left;+ let dst_y = y + pad_top;+ let dst_idx = dst_y * YOLO_INPUT_WIDTH + dst_x;- for y in 0..YOLO_INPUT_HEIGHT {- for x in 0..YOLO_INPUT_WIDTH {- let idx = y * YOLO_INPUT_WIDTH + x;- let rgb = if x >= scaled_width || y >= scaled_height {- [PAD_VALUE, PAD_VALUE, PAD_VALUE]- } else {- sample_bilinear_rgb(decoded, x as f32 / scale, y as f32 / scale)- };- output[idx] = rgb[0] / 255.0;- output[idx + green_offset] = rgb[1] / 255.0;- output[idx + blue_offset] = rgb[2] / 255.0;+ output[dst_idx] = resized[src_idx] as f32 / 255.0;+ output[dst_idx + green_offset] = resized[src_idx + 1] as f32 / 255.0;+ output[dst_idx + blue_offset] = resized[src_idx + 2] as f32 / 255.0;}}- Ok((output, scaled_width, scaled_height))+ Ok((+ output,+ scaled_width_usize,+ scaled_height_usize,+ pad_left,+ pad_top,+ ))}pub fn preprocess_clip(decoded: &DecodedImage) -> MlResult<Vec<f32>> {@@ -98,49 +125,3 @@ pub fn preprocess_clip(decoded: &DecodedImage) -> MlResult<Vec<f32>> {Ok(output)}--pub fn sample_bilinear_rgb(decoded: &DecodedImage, fx: f32, fy: f32) -> [f32; 3] {- let max_x = (decoded.dimensions.width.saturating_sub(1)) as f32;- let max_y = (decoded.dimensions.height.saturating_sub(1)) as f32;- let fx = fx.clamp(0.0, max_x);- let fy = fy.clamp(0.0, max_y);-- let x0 = fx.floor() as i32;- let x1 = fx.ceil() as i32;- let y0 = fy.floor() as i32;- let y1 = fy.ceil() as i32;- let dx = fx - x0 as f32;- let dy = fy - y0 as f32;- let dx1 = 1.0 - dx;- let dy1 = 1.0 - dy;-- let p1 = read_rgb(decoded, x0, y0);- let p2 = read_rgb(decoded, x1, y0);- let p3 = read_rgb(decoded, x0, y1);- let p4 = read_rgb(decoded, x1, y1);-- let blend = |v1: f32, v2: f32, v3: f32, v4: f32| -> f32 {- v1 * dx1 * dy1 + v2 * dx * dy1 + v3 * dx1 * dy + v4 * dx * dy- };-- [- blend(p1[0], p2[0], p3[0], p4[0]),- blend(p1[1], p2[1], p3[1], p4[1]),- blend(p1[2], p2[2], p3[2], p4[2]),- ]-}--pub fn read_rgb(decoded: &DecodedImage, x: i32, y: i32) -> [f32; 3] {- let width = decoded.dimensions.width as i32;- let height = decoded.dimensions.height as i32;- if x < 0 || y < 0 || x >= width || y >= height {- return [PAD_VALUE, PAD_VALUE, PAD_VALUE];- }-- let idx = ((y as usize * decoded.dimensions.width as usize) + x as usize) * 3;- [- decoded.rgb[idx] as f32,- decoded.rgb[idx + 1] as f32,- decoded.rgb[idx + 2] as f32,- ]-}