记录pytorch中torchvision.transforms的图像变换.
在这里,只使用transforms实现基本的功能,并没有对函数中的其他参数进行解读。
参考: pytorch中transforms函数的21种使用方法解析
from torchvision import transforms
from torchvision import transforms# 导包
from PIL import Image
img = Image.open(r'image\dogcat\dogcat\train\cat\cat.1.jpg')# 读取原图
Convert a PIL Image
or numpy.ndarray
to tensor.
转换成tensor对象,如果是numpy对象dtype需要是unit8。
tsf = transforms.ToTensor()
img = tsf(img)
tensor to PIL
tsf = transforms.ToPILImage()
img = tsf(img)
img格式可以是PIL或者tensor
tsf = transforms.RandomCrop(224,224)# 指定大小224*224,通道数是不变的
img = tsf(img)
tsf = transforms.CenterCrop((100,100))
img = tsf(img)
按照比例随机裁剪图片后reszie到指定大小
tsf = transforms.RandomResizedCrop((224,224),(0.5,1))
img = tsf(img)
tsf = transforms.FiveCrop((100,100))
img = tsf(img)
for i in img:
i.show()
tsf = transforms.RandomHorizontalFlip(p=0.8) # 镜像旋转,p是旋转操作的概率
img = tsf(img)
tsf = transforms.RandomVerticalFlip(p=0.8)
img = tsf(img)
tsf = transforms.RandomRotation(degrees=(-45,60))# -45°-60°
img = tsf(img)
tsf = transforms.Resize((224,224))
img = tsf(img)
注意这个和裁剪到224*224的区别
先做大小变化然后随机旋转
tsf = transforms.Compose([
transforms.Resize((224,224)),
transforms.RandomRotation(degrees=(-45,60))])
img = tsf(img)
Normalize接受的是tensor格式的数据,PIL会报错
mean,std = 125,50
tsf = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean,std)
])
img = tsf(img)
tsf = transforms.ColorJitter(brightness=1,contrast=2,saturation=4,hue=0)
img = tsf(img)
tsf = transforms.Grayscale(num_output_channels=1)
img = tsf(img)
tsf = transforms.RandomGrayscale(0.8)
img = tsf(img)