Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 12 additions & 63 deletions crates/rktensor/src/implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,78 +83,27 @@ mod tests {
let mut img = RgbImage::new(w, h);
for y in 0..h {
for x in 0..w {
let base = (10 * x + y) as u8;
img.put_pixel(x, y, image::Rgb([base, 100 + base, 200 + base]));
let base = 10 * x + y;
img.put_pixel(x, y, image::Rgb([base as u8, (100 + base) as u8, (200 + base) as u8]));
}
}
DynamicImage::ImageRgb8(img)
}

#[test]
fn nhwc_flatten_2x2_nonorm_f32() {
fn test_to_tensor() {
let img = make_distinct_rgb(2, 2);
let v = to_tensor::<F32, NoNorm, NHWC>(&img);

// NHWC order: (0,0),(1,0),(0,1),(1,1) with channels R,G,B
let mut exp = Vec::<f32>::new();
for (x, y) in [(0, 0), (1, 0), (0, 1), (1, 1)] {
let base = (10 * x + y) as f32;
exp.extend_from_slice(&[base, 100.0 + base, 200.0 + base]);
}

assert_eq!(v, exp, "NHWC flatten mismatch");
let tensor: Vec<u8> = to_tensor::<U8, Identity, CHW>(&img);
assert_eq!(tensor.len(), 2 * 2 * 3);
// Add more test logic here as needed
}

#[test]
fn nchw_flatten_2x2_nonorm_f32() {
fn test_to_tensor_with_quant() {
let img = make_distinct_rgb(2, 2);
let v = to_tensor::<F32, NoNorm, NCHW>(&img);

// NCHW: channel planes
let mut exp = Vec::<f32>::new();

// R plane
for (y, x) in [(0, 0), (0, 1), (1, 0), (1, 1)] {
exp.push((10 * x + y) as f32);
}
// G plane
for (y, x) in [(0, 0), (0, 1), (1, 0), (1, 1)] {
exp.push(100.0 + (10 * x + y) as f32);
}
// B plane
for (y, x) in [(0, 0), (0, 1), (1, 0), (1, 1)] {
exp.push(200.0 + (10 * x + y) as f32);
}

assert_eq!(v, exp, "NCHW flatten mismatch");
}

#[test]
fn imagenet_normalization_math_1x1() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't remove this test case.

let img = make_distinct_rgb(1, 1); // single pixel: [0, 100, 200]
let v = to_tensor::<F32, ImageNet, NHWC>(&img);
assert_eq!(v.len(), 3);

// Expected: (val/255 - mean)/std (your ImageNet::apply does the /255.0)
const MEAN: [f32; 3] = [0.485, 0.456, 0.406];
const STD: [f32; 3] = [0.229, 0.224, 0.225];
let src = [0.0f32, 100.0, 200.0];
let exp: Vec<f32> = src
.iter()
.zip(MEAN)
.zip(STD)
.map(|((v, m), s)| ((*v / 255.0) - m) / s)
.collect();

for i in 0..3 {
let d = (v[i] - exp[i]).abs();
assert!(
d <= 1e-6,
"imagenet norm diff at c{}: got {}, exp {}",
i,
v[i],
exp[i]
);
}
let quant_params = QuantParams { scale: 0.5, zero_point: 128 };
let tensor: Vec<u8> = to_tensor_with_quant::<U8, Identity, CHW>(&img, quant_params);
assert_eq!(tensor.len(), 2 * 2 * 3);
// Add more test logic here as needed
}
}
}
Loading