transforms.Compose
时间: 2023-10-13 10:19:46 浏览: 69
transformations
transforms.Compose is a class in PyTorch's torchvision module that allows the user to group multiple image transformations together into a single transformation pipeline. The transformations are applied in the order they are added to the list of transforms. This is useful when working with large datasets where multiple transformations may need to be applied to each image in a consistent and efficient way.
Example usage:
```
from torchvision import transforms
transform = transforms.Compose([
transforms.Resize((256, 256)),
transforms.RandomCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
# Apply the transformation pipeline to an image
image = Image.open('example.jpg')
transformed_image = transform(image)
```
In this example, the `Compose` class is used to group together four different transformations: `Resize`, `RandomCrop`, `ToTensor`, and `Normalize`. These transformations are then applied to an image in a consistent order to produce a transformed version of the original image.
阅读全文