Datasets
  • 🌍GET STARTED
    • Datasets
    • Quickstart
    • Installation
  • 🌍TUTORIALS
    • Overview
    • Load a dataset from the Hub
    • Know your dataset
    • Preprocess
    • Evaluate predictions
    • Create a data
    • Share a dataset to the Hub
  • 🌍HOW-TO GUIDES
    • Overview
    • 🌍GENERAL USAGE
      • Load
      • Process
      • Stream
      • Use with TensorFlow
      • Use with PyTorch
      • Use with JAX
      • Use with Spark
      • Cache management
      • Cloud storage
      • Search index
      • Metrics
      • Beam Datasets
    • 🌍AUDIO
      • Load audio data
      • Process audio data
      • Create an audio dataset
    • 🌍VISION
      • Load image data
      • Process image data
      • Create an image dataset
      • Depth estimation
      • Image classification
      • Semantic segmentation
      • Object detection
    • 🌍TEXT
      • Load text data
      • Process text data
    • 🌍TABULAR
      • Load tabular data
    • 🌍DATASET REPOSITORY
      • Share
      • Create a dataset card
      • Structure your repository
      • Create a dataset loading script
  • 🌍CONCEPTUAL GUIDES
    • Datasets with Arrow
    • The cache
    • Dataset or IterableDataset
    • Dataset features
    • Build and load
    • Batch mapping
    • All about metrics
  • 🌍REFERENCE
    • Main classes
    • Builder classes
    • Loading methods
    • Table Classes
    • Logging methods
    • Task templates
Powered by GitBook
On this page
  • Process image data
  • Map
  • Apply transforms
  1. HOW-TO GUIDES
  2. VISION

Process image data

PreviousLoad image dataNextCreate an image dataset

Last updated 1 year ago

Process image data

This guide shows specific methods for processing image datasets. Learn how to:

  • Use with image dataset.

  • Apply data augmentations to a dataset with .

For a guide on how to process any type of dataset, take a look at the .

Map

The function can apply transforms over an entire dataset.

For example, create a basic function:

Copied

>>> def transforms(examples):
...     examples["pixel_values"] = [image.convert("RGB").resize((100,100)) for image in examples["image"]]
...     return examples

Now use the function to resize the entire dataset, and set batched=True to speed up the process by accepting batches of examples. The transform returns pixel_values as a cacheable PIL.Image object:

Copied

>>> dataset = dataset.map(transforms, remove_columns=["image"], batched=True)
>>> dataset[0]
{'label': 6,
 'pixel_values': <PIL.PngImagePlugin.PngImageFile image mode=RGB size=100x100 at 0x7F058237BB10>}

Apply transforms

For example, if you’d like to change the color properties of an image randomly:

Copied

>>> from torchvision.transforms import Compose, ColorJitter, ToTensor

>>> jitter = Compose(
...     [
...          ColorJitter(brightness=0.25, contrast=0.25, saturation=0.25, hue=0.7),
...          ToTensor(),
...     ]
... )

Create a function to apply the ColorJitter transform:

Copied

>>> def transforms(examples):
...     examples["pixel_values"] = [jitter(image.convert("RGB")) for image in examples["image"]]
...     return examples

Copied

>>> dataset.set_transform(transforms)

The cache file saves time because you don’t have to execute the same transform twice. The function is best for operations you only run once per training - like resizing an image - instead of using it for operations executed for each epoch, like data augmentations.

takes up some memory, but you can reduce its memory requirements with the following parameters:

determines the number of examples that are processed in one call to the transform function.

determines the number of processed examples that are kept in memory before they are stored away.

Both parameter values default to 1000, which can be expensive if you are storing images. Lower these values to use less memory when you use .

🌍 Datasets applies data augmentations from any library or package to your dataset. Transforms can be applied on-the-fly on batches of data with , which consumes less disk space.

The following example uses , but feel free to use other data augmentation libraries like , , and .

Apply the transform with the function:

🌍
🌍
map()
set_transform()
general process guide
map()
Resize
map()
map()
map()
batch_size
writer_batch_size
map()
set_transform()
torchvision
Albumentations
Kornia
imgaug
set_transform()