Implementation:Huggingface Optimum ImageClassificationProcessing
| Knowledge Sources | |
|---|---|
| Domains | Preprocessing, Vision |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for preprocessing image classification datasets with torchvision transforms and image processor normalization provided by the Huggingface Optimum library.
Description
ImageClassificationProcessing is a TaskProcessor subclass for image classification tasks. It accepts a BaseImageProcessor and builds a torchvision transform pipeline (Resize, CenterCrop, ToTensor, Normalize) using the image processor's size and normalization parameters. The default dataset is CIFAR-10.
Usage
Use this processor when benchmarking or evaluating image classification models. It handles image-to-tensor conversion with proper resizing and normalization matching the model's training configuration.
Code Reference
Source Location
- Repository: Huggingface_Optimum
- File: optimum/utils/preprocessing/image_classification.py
- Lines: 1-116
Signature
class ImageClassificationProcessing(TaskProcessor):
ACCEPTED_PREPROCESSOR_CLASSES = (BaseImageProcessor,)
DEFAULT_DATASET_ARGS = "uoft-cs/cifar10"
DEFAULT_DATASET_DATA_KEYS = {"image": "img"}
ALLOWED_DATA_KEY_NAMES = {"image"}
DEFAULT_REF_KEYS = ["answers"]
def __init__(
self,
config: "PretrainedConfig",
preprocessor: "BaseImageProcessor",
preprocessor_kwargs: Optional[Dict[str, Any]] = None,
): ...
def dataset_processing_func(
self, example: Dict[str, Any], data_keys: Dict[str, str], ref_keys: Optional[List[str]] = None
) -> Dict[str, Any]: ...
def try_to_guess_data_keys(self, column_names: List[str]) -> Optional[Dict[str, str]]: ...
def try_to_guess_ref_keys(self, column_names: List[str]) -> Optional[List[str]]: ...
Import
from optimum.utils.preprocessing.image_classification import ImageClassificationProcessing
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| config | PretrainedConfig | Yes | The model configuration |
| preprocessor | BaseImageProcessor | Yes | Image processor with size and normalization info |
| preprocessor_kwargs | Dict[str, Any] | No | Additional preprocessing keyword arguments |
Outputs
| Name | Type | Description |
|---|---|---|
| dataset_processing_func output | Dict | Adds "pixel_values" key as float32 numpy array |
Usage Examples
from transformers import AutoConfig, AutoImageProcessor
from optimum.utils.preprocessing.image_classification import ImageClassificationProcessing
config = AutoConfig.from_pretrained("google/vit-base-patch16-224")
image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")
processor = ImageClassificationProcessing(config, image_processor)
dataset = processor.load_default_dataset(load_smallest_split=True, num_samples=50)