给我一个 transforms.Compose函数的具体使用例子,并提供完整的图片格式转换过程
时间: 2023-03-13 22:51:22 浏览: 316
使用 transforms.Compose函数的具体使用例子如下:import torchvision.transforms as transforms# 将PIL图像(或者ndarray)转换为Tensor:
transform = transforms.Compose([transforms.ToTensor()])# 将Tensor转换为PIL图像:
transform = transforms.Compose([transforms.ToPILImage()])# 将Tensor归一化:
transform = transforms.Compose([transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))])# 将Tensor反归一化:
transform = transforms.Compose([transforms.Normalize(mean=(-0.5, -0.5, -0.5), std=(1.0, 1.0, 1.0))])# 将PIL图像裁剪成指定大小:
transform = transforms.Compose([transforms.Resize(size=(256, 256))])# 将PIL图像填充成指定大小:
transform = transforms.Compose([transforms.Pad(padding=20, fill=(0, 0, 0))])# 将PIL图像水平翻转:
transform = transforms.Compose([transforms.RandomHorizontalFlip(p=1.0)])# 将PIL图像垂直翻转:
transform = transforms.Compose([transforms.RandomVerticalFlip(p=1.0)])# 将PIL图像旋转角度:
transform = transforms.Compose([transforms.RandomRotation(degrees=90)])# 将PIL图像变换大小:
transform = transforms.Compose([transforms.RandomResizedCrop(size=(224, 224))])# 完整的图片格式转换过程可以是:
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)),
transforms.Resize(size=(256, 256)),
transforms.Pad(padding=20, fill=(0, 0, 0)),
transforms.RandomHorizontalFlip(p=1.0),
transforms.RandomVerticalFlip(p=1.0),
transforms.RandomRotation(degrees=90),
transforms.RandomResizedCrop(size=(224, 224))
])
阅读全文