Transformers documentation

Image processors

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v5.3.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Image processors

Image processors convert images into pixel values, tensors that represent image colors and size. The pixel values are inputs to a vision model. To ensure a pretrained model receives the correct input, an image processor can perform the following operations to make sure an image is exactly like the images a model was pretrained on.

  • center-crop or resize an image
  • normalize or rescale pixel values

Use from_pretrained() to load an image processors configuration (image size, whether to normalize and rescale, etc.) from a vision model on the Hugging Face Hub or local directory. The configuration for each pretrained model is saved in a preprocessor_config.json file.

from transformers import AutoImageProcessor

image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")

Pass an image to the image processor to transform it into pixel values, and set return_tensors="pt" to return PyTorch tensors. Feel free to print out the inputs to see what the image looks like as a tensor.

from PIL import Image
import requests

url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/image_processor_example.png"
image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
inputs = image_processor(image, return_tensors="pt")

This guide covers the image processor class and how to preprocess images for vision models.

Image processor classes

Image processors use a backend-based architecture with two backends:

  • TorchvisionBackend — the default torchvision-backed implementation. GPU-accelerated and up to 33x faster than the PIL backend for batches of torch.Tensor inputs. All models support this backend; newer models only support this one.
  • PilBackend — the PIL/NumPy alternative. Portable and CPU-only. Only available for older models, where it is useful to reproduce the exact numerical outputs of the original implementation.

The active backend on a loaded processor can be inspected with its backend attribute (e.g., processor.backend == "torchvision"). Each image processor subclasses ImageProcessingMixin which provides the from_pretrained() and save_pretrained() methods.

There are two ways you can load an image processor: with AutoImageProcessor or directly from a model-specific class.

AutoImageProcessor
model-specific image processor

The AutoClass API provides a convenient method to load an image processor without directly specifying the model the image processor is associated with.

Use from_pretrained() with the backend argument to select the backend. When backend is omitted (the default), torchvision is picked when it is installed and PIL is used otherwise. Note that backend="pil" is only supported for older models; newer models only expose the torchvision backend.

Note: a small set of older models (Chameleon, Flava, Idefics3, SmolVLM) use Lanczos interpolation that torchvision does not support, so they always default to the PIL backend regardless of torchvision availability. Pass backend="torchvision" explicitly to override this.

from transformers import AutoImageProcessor

# Default: picks torchvision if available, otherwise pil
image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")

# Explicitly request the torchvision backend
image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224", backend="torchvision")

# Explicitly request the PIL backend (only for models that support it)
image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224", backend="pil")

Torchvision backend processors

TorchvisionBackend is the default backend. Make sure torchvision is installed, then load it with backend="torchvision" (or simply omit backend, since torchvision is selected automatically when available).

from transformers import AutoImageProcessor

processor = AutoImageProcessor.from_pretrained("facebook/detr-resnet-50", backend="torchvision")

Control which device processing is performed on with the device argument. Processing is performed on the same device as the input by default if the inputs are tensors, otherwise it falls back to CPU. The example below runs processing on a GPU.

from torchvision.io import read_image
from transformers import DetrImageProcessor

images = read_image("image.jpg")
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
images_processed = processor(images, return_tensors="pt", device="cuda")
Benchmarks

The benchmarks are obtained from an AWS EC2 g5.2xlarge instance with a NVIDIA A10G Tensor Core GPU.

Preprocess

Transformers’ vision models expects the input as PyTorch tensors of pixel values. An image processor handles the conversion of images to pixel values, which is represented by the batch size, number of channels, height, and width. To achieve this, an image is resized (center cropped) and the pixel values are normalized and rescaled to the models expected values.

Image preprocessing is not the same as image augmentation. Image augmentation makes changes (brightness, colors, rotatation, etc.) to an image for the purpose of either creating new training examples or prevent overfitting. Image preprocessing makes changes to an image for the purpose of matching a pretrained model’s expected input format.

Typically, images are augmented (to increase performance) and then preprocessed before being passed to a model. You can use any library (Albumentations, Kornia) for augmentation and an image processor for preprocessing.

This guide uses the torchvision transforms module for augmentation.

Start by loading a small sample of the food101 dataset.

from datasets import load_dataset

dataset = load_dataset("ethz/food101", split="train[:100]")

From the transforms module, use the Compose API to chain together RandomResizedCrop and ColorJitter. These transforms randomly crop and resize an image, and randomly adjusts an images colors.

The image size to randomly crop to can be retrieved from the image processor. For some models, an exact height and width are expected while for others, only the shortest_edge is required.

from torchvision.transforms import RandomResizedCrop, ColorJitter, Compose

size = (
    image_processor.size["shortest_edge"]
    if "shortest_edge" in image_processor.size
    else (image_processor.size["height"], image_processor.size["width"])
)
_transforms = Compose([RandomResizedCrop(size), ColorJitter(brightness=0.5, hue=0.5)])

Apply the transforms to the images and convert them to the RGB format. Then pass the augmented images to the image processor to return the pixel values.

The do_resize parameter is set to False because the images have already been resized in the augmentation step by RandomResizedCrop. If you don’t augment the images, then the image processor automatically resizes and normalizes the images with the image_mean and image_std values. These values are found in the preprocessor configuration file.

def transforms(examples):
    images = [_transforms(img.convert("RGB")) for img in examples["image"]]
    examples["pixel_values"] = image_processor(images, do_resize=False, return_tensors="pt")["pixel_values"]
    return examples

Apply the combined augmentation and preprocessing function to the entire dataset on the fly with set_transform.

dataset.set_transform(transforms)

Convert the pixel values back into an image to see how the image has been augmented and preprocessed.

import numpy as np
import matplotlib.pyplot as plt

img = dataset[0]["pixel_values"]
plt.imshow(img.permute(1, 2, 0))
before
after

For other vision tasks like object detection or segmentation, the image processor includes post-processing methods to convert a models raw output into meaningful predictions like bounding boxes or segmentation maps.

Padding

Some models, like DETR, applies scale augmentation during training which can cause images in a batch to have different sizes. Images with different sizes can’t be batched together.

To fix this, pad the images with the special padding token 0. Use the pad method to pad the images, and define a custom collate function to batch them together.

def collate_fn(batch):
    pixel_values = [item["pixel_values"] for item in batch]
    encoding = image_processor.pad(pixel_values, return_tensors="pt")
    labels = [item["labels"] for item in batch]
    batch = {}
    batch["pixel_values"] = encoding["pixel_values"]
    batch["pixel_mask"] = encoding["pixel_mask"]
    batch["labels"] = labels
    return batch
Update on GitHub