torchvision stransform
时间: 2023-11-06 15:59:16 浏览: 34
torchvision.transform 是 PyTorch 中 torchvision 库中用于图像数据预处理的模块。它提供了一系列的图像转换操作,可以方便地对图像进行预处理和数据增强。常用的操作包括图像缩放、裁剪、翻转、旋转、标准化等。
以下是一些常用的 torchvision.transform 操作示例:
- `transforms.Resize(size)`:将图像大小调整为指定的尺寸。
- `transforms.CenterCrop(size)`:对图像进行中心裁剪,裁剪后的尺寸为指定的大小。
- `transforms.RandomCrop(size)`:对图像进行随机裁剪,裁剪后的尺寸为指定的大小。
- `transforms.RandomHorizontalFlip(p)`:以指定的概率对图像进行水平翻转。
- `transforms.RandomRotation(degrees)`:对图像进行随机旋转,旋转角度在指定范围内。
- `transforms.ToTensor()`:将图像转换为张量类型。
- `transforms.Normalize(mean, std)`:对图像进行标准化处理。
这些操作可以通过 torchvision.transforms.Compose() 函数组合在一起,形成一个图像转换的管道。例如:
```python
import torchvision.transforms as transforms
transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
```
这样,通过调用 transform(image) 可以对图像进行一系列的变换操作。