torchvision.transformer
示例图片(224,224,3)
具体操作
Compose
ToTensor
作用:Convert a PIL Image
or numpy.ndarray
to tensor.
我们将图片传入后得到的结果输出为:
PILToTensor
作用:Converts a PIL Image (H x W x C) to a torch.Tensor of shape (C x H x W).
x则为我们对应的PILImage图片
输出结果如下:
与上述的函数功能保持一致
ConvertImageDtype
ToPILImage
Normalize
运行时遇到一个错误:
查询原因:pytorch的版本1.5.0支持,pytorch1.6.0不支持了,需要改相应代码,我选择把pytorch的版本换掉了。
操作前:
操作后:
tensor发生了变化,图片本身没有变化。
Resize
参数:
def __init__(self, size, interpolation=Image.BILINEAR):
interpolation=Image.BILINEAR
BILINEAR解释:
双线性插值(Bilinear interpolation):扩展之后的图像像素坐标映射回原来的坐标空间时, 如果出现了没有对应到整数点的情况。这时候需要做2次线性的插值计算出新的坐标的像素值。
作用:Resize the input PIL Image to the given size.
增广方案:
augs = torchvision.transforms.Compose([
torchvision.transforms.Resize((200, 100)),
])
当尺寸大于图片本身时:
augs = torchvision.transforms.Compose([
torchvision.transforms.Resize((400, 400)),
])
Scale
该函数继承于Resize,作用与其一致。
CenterCrop
作用:Crops the given PIL Image at the center
当希望输出小于图片size时,不进行缩放,只从中心进行裁剪。
augs = torchvision.transforms.Compose([
torchvision.transforms.CenterCrop((200,100)),
])
代码:
augs = torchvision.transforms.Compose([
torchvision.transforms.CenterCrop((400,400)),
])
从显示图片可以观察,该方法并没有对图片进行缩放,当尺寸大于本身时,进行颜色填充。
Pad
作用:Pad the given PIL Image on all sides with the given “pad” value.
相当于对各边进行相应的padding。
参数fill对应的是我们指定的颜色。
padding_mode=‘constant’
当我们将Pad的参数指定为一个四个参数的元组时(10,20,30,40)
augs = torchvision.transforms.Compose([
torchvision.transforms.Pad((10,20,30,40),fill=(19,78,122),padding_mode='constant'),
])
效果图如下:
padding_mode=‘edge’
此时的fill参数不起作用,根据我们图像边缘的颜色进行的填充。
padding_mode=‘reflect’
padding_mode=‘symmetric’
这个和上面的方式,我效果图一样,目前还没有找到区别。但是源代码里面说不一致。这个问题先保留。
Lambda
作用:pply a user-defined lambda as a transform.
这里我用了一个得到PILImage底片的代码:
def _imgInverse(image):
if image.mode == 'RGBA':
r, g, b, a = image.split()
rgb_image = Image.merge('RGB', (r, g, b))
inverted_image = PIL.ImageOps.invert(rgb_image)
r2, g2, b2 = inverted_image.split()
result_img = Image.merge('RGBA', (r2, g2, b2, a))
else:
result_img = PIL.ImageOps.invert(image)
return result_img
增广策略如下:
augs = torchvision.transforms.Compose([
torchvision.transforms.Lambda(_imgInverse)
])
图片效果:
RandomApply
RandomChoice
RandomOrder
RandomCrop
RandomHorizontalFlip
RandomVerticalFlip
RandomResizedCrop
RandomSizedCrop
FiveCrop
作用:Crop the given PIL Image into four corners and the central crop.
代码:
augs = torchvision.transforms.Compose([
torchvision.transforms.FiveCrop(100),
torchvision.transforms.Lambda(lambda crops: torch.stack([torchvision.transforms.ToTensor()(crop) for crop in crops])) # returns a 4D tensor
])
augs1 = torchvision.transforms.Compose([
torchvision.transforms.ToPILImage()
])
img1 = imgs[0][0]
print(augs(img1).size())
print(augs(img1)[0].size())
for i, x in enumerate(augs(img1)[x] for x in range(5)):
plt.subplot(1, 5, i+1)
plt.imshow(augs1(x))
plt.show()
可以看到原图的上下左右中五个部分被裁剪出来:
TenCrop
作用:Crop the given PIL Image into four corners and the central crop plus the flipped version of these (horizontal flipping is used by default)
相比于5个,每张图片增加了镜像翻转。