Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:LaurentMazare Tch rs Imagenet Load From Dir

From Leeroopedia


Knowledge Sources
Domains Computer_Vision, Data_Loading
Last Updated 2026-02-08 14:00 GMT

Overview

Concrete tool for loading custom image datasets from directory structure provided by the tch vision module.

Description

imagenet::load_from_dir reads images from a train/val directory hierarchy, resizes each to 224x224, applies ImageNet normalization, assigns integer labels based on class directory names, and returns a Dataset struct. Class names are discovered from subdirectories under val/ and sorted alphabetically.

Usage

Use for transfer learning datasets. Ensure your data directory has train/ and val/ subdirectories, each with one subdirectory per class.

Code Reference

Source Location

  • Repository: tch-rs
  • File: src/vision/imagenet.rs
  • Lines: 116-147

Signature

pub fn load_from_dir<T: AsRef<Path>>(dir: T) -> Result<Dataset, TchError>

Import

use tch::vision::imagenet;

I/O Contract

Inputs

Name Type Required Description
dir T: AsRef<Path> Yes Root directory with train/ and val/ subdirectories

Outputs

Name Type Description
Result<Dataset> Dataset train_images [N, 3, 224, 224] f32, train_labels [N] i64, test_images/labels, labels count

Usage Examples

use tch::vision::imagenet;

let dataset = imagenet::load_from_dir("path/to/custom_dataset")?;
println!("Training samples: {:?}", dataset.train_images.size());
println!("Number of classes: {}", dataset.labels);

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment