-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
normalize_image
to rten-imageproc and make it customizable
Several examples need to normalize an image with a mean and standard deviation different from the imagenet values. Add a function in rten-imageproc with this flexibility. The function has been added to the rten-imageproc crate because it doesn't have any dependencies on specific image formats. In the process the implementation was also revised to make it more efficient.
- Loading branch information
1 parent
87f0a38
commit 33b845d
Showing
7 changed files
with
57 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use rten_tensor::prelude::*; | ||
use rten_tensor::NdTensorViewMut; | ||
|
||
/// Standard ImageNet normalization mean values, for use with | ||
/// [`normalize_image`]. | ||
pub const IMAGENET_MEAN: [f32; 3] = [0.485, 0.456, 0.406]; | ||
|
||
/// Standard ImageNet normalization standard deviation values, for use with | ||
/// [`normalize_image`]. | ||
pub const IMAGENET_STD_DEV: [f32; 3] = [0.229, 0.224, 0.225]; | ||
|
||
/// Normalize the mean and standard deviation of all pixels in an image. | ||
/// | ||
/// `img` should be a CHW tensor with `C` channels. For each channel `c`, the | ||
/// output pixel values are computed as `y = (x - mean[c]) / std_dev[c]`. | ||
/// | ||
/// This is a common preprocessing step for inputs to machine learning models. | ||
/// Many models use standard "ImageNet" constants ([`IMAGENET_MEAN`], | ||
/// [`IMAGENET_STD_DEV`]), but check the expected values for the model you are | ||
/// using. | ||
pub fn normalize_image<const C: usize>( | ||
mut img: NdTensorViewMut<f32, 3>, | ||
mean: [f32; C], | ||
std_dev: [f32; C], | ||
) { | ||
let n_chans = img.size(0); | ||
assert_eq!( | ||
n_chans, C, | ||
"expected image to have {} channels but found {}", | ||
C, n_chans | ||
); | ||
|
||
for chan in 0..n_chans { | ||
let inv_std_dev = 1. / std_dev[chan]; | ||
img.slice_mut::<2, _>(chan) | ||
.apply(|x| (x - mean[chan]) * inv_std_dev); | ||
} | ||
} |