# torchvision transform库学习总结

1.一个类似数组的操作

class torchvision.transforms.Compose(transforms)


2.各种数据类型的转换

class torchvision.transforms.ToTensor
class torchvision.transforms.ToPILImage


3.对Tensor进行变换

class torchvision.transforms.Normalize(mean, std)


4.对PIL.Image进行变换

class torchvision.transforms.CenterCrop(size)
class torchvision.transforms.RandomHorizontalFlip()
class torchvision.transforms.RandomResizedCrop(size, interpolation=2)


import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image

----------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------


# 下面将按顺序介绍：

## class torchvision.transforms.Compose(transforms)

from torchvision import transforms as transforms
transform = transforms.Compose([
transforms.RandomHorizontalFlip(),  #图像一半的概率翻转，一半的概率不翻转
transforms.RandomRotation((-45,45)), #随机旋转
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.229, 0.224, 0.225)), #R,G,B每层的归一化用到的均值和方差
])


## class torchvision.transforms.ToTensor

输入代码
data = np.random.randint(0, 255, size=300)
img = data.reshape(10,10,3)
print(img.shape)
img_tensor = transforms.ToTensor()(img) # 转换成tensor
print(img_tensor.shape)

输出结果
(10, 10, 3)
torch.Size([3, 10, 10])


## class torchvision.transforms.ToPILImage

输入代码
data = np.random.randint(0, 255, size=300, dtype='uint8')
img = data.reshape(10,10,3)
print(img.shape)
img_PIL = transforms.ToPILImage()(img)
print(img_PIL.size)
print(img_PIL.mode)

输出结果
(10, 10, 3)
(10, 10)
RGB


## class torchvision.transforms.CenterCrop(size)

输入代码
transform = transforms.Compose([transforms.CenterCrop(500), transforms.ToTensor()])

image = Image.open('./data/image.jpg')

print(np.array(image).shape)

img1 = transform(image)
print(img1.shape)

plt.imshow(np.transpose(img1, (1, 2, 0)))
plt.show()

输出
(606, 910, 3)
torch.Size([3, 500, 500])


输入代码

image = Image.open('./data/image.jpg')

print(np.array(image).shape)

img1 = transform(image)
print(img1.shape)

plt.imshow(np.transpose(img1, (1, 2, 0)))
plt.show()

输出结果
(606, 910, 3)
torch.Size([3, 500, 500])


## class torchvision.transforms.RandomHorizontalFlip()

输入代码，这个可以多运行几次，你会发现不一样的
transform = transforms.Compose([transforms.RandomHorizontalFlip(), transforms.ToTensor()])

image = Image.open('./data/image.jpg')

print(np.array(image).shape)

img1 = transform(image)
print(img1.shape)

plt.imshow(np.transpose(img1, (1, 2, 0)))
plt.show()

输出
(606, 910, 3)
torch.Size([3, 606, 910])


## class torchvision.transforms.RandomResizedCrop(size, interpolation=2)

输入代码
transform = transforms.Compose([transforms.RandomResizedCrop(500, interpolation=1), transforms.ToTensor()])

image = Image.open('./data/image.jpg')

print(np.array(image).shape)

img1 = transform(image)
print(img1.shape)

plt.imshow(np.transpose(img1, (1, 2, 0)))
plt.show()

输出结果
(606, 910, 3)
torch.Size([3, 500, 500])