transforms.Compose
时间: 2023-10-12 07:16:12 浏览: 95
transformations
transforms.Compose is a PyTorch function that allows you to apply multiple transforms to a given dataset. It takes a list of transforms as input and applies them in sequence to the input data.
For example, if you have an image dataset and you want to apply the following transformations:
- Resize the images to 256x256
- Convert the images to grayscale
- Normalize the pixel values
You can use transforms.Compose to chain these transformations together as follows:
```python
import torchvision.transforms as transforms
transform = transforms.Compose([
transforms.Resize((256, 256)),
transforms.Grayscale(),
transforms.Normalize(mean=[0.5], std=[0.5])
])
```
Then, you can apply this transform to your dataset using the `transform` argument in the dataset constructor.
```python
from torchvision.datasets import ImageFolder
dataset = ImageFolder(root='path/to/dataset', transform=transform)
```
This will resize all the images in the dataset to 256x256, convert them to grayscale, and normalize the pixel values.
阅读全文