Get startedGet started for free

Preprocess image datasets

You're developing a precision agriculture system to help farmers monitor crop health, using a pre-trained transformer model, which you can later fine-tune on agricultural imagery. Preprocess the dataset using AutoImageProcessor to prepare for training!

Some data has been pre-loaded:

  • The AutoImageProcessor class has been imported from transformers
  • model is equal to microsoft/swin-tiny-patch4-window7-224
  • A sample dataset has been defined, with a sample image loaded into the variable image

This exercise is part of the course

Efficient AI Model Training with PyTorch

View Course

Exercise instructions

  • Load a pre-trained image processor from the pre-defined model.
  • Map the image_processor to the entire dataset.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Load a pre-trained image processor
image_processor = ____.____(model)

# Map the image_processor to the entire dataset
dataset = dataset.____(
    lambda examples: {
        "pixel_values": [
            image_processor(image, return_tensors="pt").pixel_values
            for image in examples["img"]
        ]
    },
    batched=True,
)
print(dataset[0]["img"])
Edit and Run Code