Implementation:LaurentMazare Tch rs Imagenet Load Image And Resize224
| Knowledge Sources | |
|---|---|
| Domains | Computer_Vision, Data_Preprocessing |
| Last Updated | 2026-02-08 14:00 GMT |
Overview
Concrete tool for loading and preprocessing images for ImageNet-pretrained models provided by the tch vision module.
Description
imagenet::load_image_and_resize224 loads an image from the filesystem, resizes it to 224x224 pixels, and applies ImageNet normalization. Internally it delegates to image::load_and_resize (using the vendored stb_image library) and then normalizes each channel using the ImageNet mean and standard deviation. The output is a float32 tensor of shape [3, 224, 224].
Usage
Use this before running inference with any pretrained ImageNet model. The returned tensor should be unsqueezed to add a batch dimension before being passed to the model.
Code Reference
Source Location
- Repository: tch-rs
- File: src/vision/imagenet.rs
- Lines: 46-48
Signature
pub fn load_image_and_resize224<T: AsRef<Path>>(path: T) -> Result<Tensor, TchError>
Import
use tch::vision::imagenet;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| path | T: AsRef<Path> | Yes | Path to image file (supports jpg, png, tga, bmp) |
Outputs
| Name | Type | Description |
|---|---|---|
| Result<Tensor> | Tensor | Float32 tensor of shape [3, 224, 224], ImageNet-normalized |
Usage Examples
use tch::vision::imagenet;
let image = imagenet::load_image_and_resize224("photo.jpg")?;
println!("{:?}", image.size()); // [3, 224, 224]
// Add batch dimension for model input
let input = image.unsqueeze(0); // [1, 3, 224, 224]