1. 程式人生 > >pytorch.torchvision.transforms

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))