pytorch.torchvision.transforms

torchvision.transforms可以对数据进行以下处理:

  • PIL.Image/numpy.ndarray与Tensor的相互转化;
  • 归一化;
  • 对PIL.Image进行裁剪、缩放等操作。

代码如下:

transform1 = transforms.Compose([
                    transforms.ToTensor()
                    ])
transform2 = transforms.Compose([
                    transforms.Resize(int(256*1.12), Image.BICUBIC), 
                    transforms.RandomCrop(256), 
                    transforms.RandomHorizontalFlip(),
                    transforms.ToTensor(),
                    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))  # (tensor-mean)/std
                    ])

img_path=r"trainA\n02381460_14.jpg"

# numpy.ndarray
img = Image.open(img_path)
print(np.array(img).shape)

img1 = transform1(img)
print("img1=",img1.size())
print("img type", type(img1))
print("img max", torch.max(img1))
print("img min", torch.min(img1))

# Tensor
img = Image.open(img_path).convert('RGB')
img2 = transform2(img)
print("img2=",img2.size())
print("img2 max", torch.max(img2))
print("img2 min", torch.min(img2))

猜你喜欢

转载自blog.csdn.net/luolinll1212/article/details/82859911