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
AutoImageProcessorclass has been imported fromtransformers modelis equal tomicrosoft/swin-tiny-patch4-window7-224- A sample
datasethas been defined, with a sample image loaded into the variableimage
本练习是课程的一部分
Efficient AI Model Training with PyTorch
练习说明
- Load a pre-trained image processor from the pre-defined
model. - Map the
image_processorto 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"])