开始使用免费开始使用

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

本练习是课程的一部分

Efficient AI Model Training with PyTorch

查看课程

练习说明

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

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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"])
编辑并运行代码