use ndarray::{Array4, ArrayView3};
pub fn patchify_cpu(
fmap: ArrayView3<'_, f32>,
coords: &[(f32, f32)],
radius: usize,
) -> Array4<f32> {
let (channels, height, width) = fmap.dim();
let num_patches = coords.len();
let window = 2 * radius + 2;
let out_size = 2 * radius + 1;
let mut raw = Array4::<f32>::zeros((num_patches, channels, window, window));
for (patch_index, &(cx, cy)) in coords.iter().enumerate() {
let floor_x = cx.floor();
let floor_y = cy.floor();
for wy in 0..window {
let offset_y = wy as isize - radius as isize;
let sample_y = clamp_isize(floor_y as isize + offset_y, height);
for wx in 0..window {
let offset_x = wx as isize - radius as isize;
let sample_x = clamp_isize(floor_x as isize + offset_x, width);
for c in 0..channels {
raw[(patch_index, c, wy, wx)] = fmap[(c, sample_y, sample_x)];
}
}
}
}
let mut out = Array4::<f32>::zeros((num_patches, channels, out_size, out_size));
for (patch_index, &(cx, cy)) in coords.iter().enumerate() {
let dx = cx - cx.floor();
let dy = cy - cy.floor();
let w00 = (1.0 - dy) * (1.0 - dx);
let w01 = (1.0 - dy) * dx;
let w10 = dy * (1.0 - dx);
let w11 = dy * dx;
for c in 0..channels {
for oy in 0..out_size {
for ox in 0..out_size {
let value = w00 * raw[(patch_index, c, oy, ox)]
+ w01 * raw[(patch_index, c, oy, ox + 1)]
+ w10 * raw[(patch_index, c, oy + 1, ox)]
+ w11 * raw[(patch_index, c, oy + 1, ox + 1)];
out[(patch_index, c, oy, ox)] = value;
}
}
}
}
out
}
fn clamp_isize(value: isize, len: usize) -> usize {
value.clamp(0, len as isize - 1) as usize
}
#[cfg(test)]
mod tests {
use super::*;
use ndarray::Array3;
#[test]
fn integer_centroid_is_a_plain_crop() {
let mut fmap = Array3::<f32>::zeros((1, 5, 5));
for row in 0..5 {
for col in 0..5 {
fmap[(0, row, col)] = (row * 5 + col) as f32;
}
}
let coords = vec![(2.0_f32, 2.0_f32)]; let patches = patchify_cpu(fmap.view(), &coords, 1);
assert_eq!(patches.shape(), &[1, 1, 3, 3]);
let expected = [[6.0, 7.0, 8.0], [11.0, 12.0, 13.0], [16.0, 17.0, 18.0]];
for oy in 0..3 {
for ox in 0..3 {
assert!(
(patches[(0, 0, oy, ox)] - expected[oy][ox]).abs() < 1e-6,
"mismatch at ({oy},{ox}): got {}, want {}",
patches[(0, 0, oy, ox)],
expected[oy][ox]
);
}
}
}
#[test]
fn half_pixel_centroid_averages_two_by_two_neighbourhood() {
let mut fmap = Array3::<f32>::zeros((1, 5, 5));
for row in 0..5 {
for col in 0..5 {
fmap[(0, row, col)] = col as f32;
}
}
let coords = vec![(2.5_f32, 2.5_f32)];
let patches = patchify_cpu(fmap.view(), &coords, 1);
for oy in 0..3 {
for ox in 0..3 {
let expected = ox as f32 + 1.5;
assert!(
(patches[(0, 0, oy, ox)] - expected).abs() < 1e-5,
"mismatch at ({oy},{ox}): got {}, want {expected}",
patches[(0, 0, oy, ox)]
);
}
}
}
#[test]
fn near_border_centroid_clamps_instead_of_panicking() {
let mut fmap = Array3::<f32>::zeros((1, 3, 3));
for row in 0..3 {
for col in 0..3 {
fmap[(0, row, col)] = (row * 3 + col) as f32;
}
}
let coords = vec![(0.0_f32, 0.0_f32)]; let patches = patchify_cpu(fmap.view(), &coords, 1);
assert!((patches[(0, 0, 0, 0)] - 0.0).abs() < 1e-6);
}
}