transforms的使用方法

一、transforms的介绍


在pytorch中,图像的预处理过程中常常需要对图片的格式、尺寸等做一系列的变化,这就需要借助transforms。

__all__ = ["Compose", "ToTensor", "PILToTensor", "ConvertImageDtype", "ToPILImage", "Normalize", "Resize", "Scale",
           "CenterCrop", "Pad", "Lambda", "RandomApply", "RandomChoice", "RandomOrder", "RandomCrop",
           "RandomHorizontalFlip", "RandomVerticalFlip", "RandomResizedCrop", "RandomSizedCrop", "FiveCrop", "TenCrop",
           "LinearTransformation", "ColorJitter", "RandomRotation", "RandomAffine", "Grayscale", "RandomGrayscale",
           "RandomPerspective", "RandomErasing", "GaussianBlur", "InterpolationMode", "RandomInvert", "RandomPosterize",
           "RandomSolarize", "RandomAdjustSharpness", "RandomAutocontrast", "RandomEqualize"]

这是官方文档里所有的transforms下的操作,以下就根据官方文档和我自己的理解介绍几个常用的transforms方法。

1、transforms.ToTensor()

ToTensor就是将 "PIL Image" 或者 "numpy.ndarray" 格式转换为tensor格式,tensor格式的数据可以直接作为网络的输入。

from PIL import Image
from torchvision import transforms


img = Image.open("../**.png")
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)

运行上述范例代码可以很清楚的看到transforms.ToTensor前后的变化。

#原始的图片信息


#transforms.ToTensor后的图片信息
tensor([[[0.2510, 0.2431, 0.2392,  ..., 0.2392, 0.2275, 0.2314],
         [0.2471, 0.2392, 0.2353,  ..., 0.2471, 0.2627, 0.2627],
         [0.2549, 0.2549, 0.2549,  ..., 0.2549, 0.2471, 0.2471],
         ...,
         [0.2784, 0.2902, 0.2784,  ..., 0.9843, 0.9961, 1.0000],
         [0.3020, 0.3059, 0.2824,  ..., 0.9608, 0.9843, 0.9647],
         [0.3176, 0.2902, 0.2863,  ..., 1.0000, 0.9686, 0.9608]],

        [[0.2510, 0.2431, 0.2392,  ..., 0.2392, 0.2275, 0.2314],
         [0.2471, 0.2392, 0.2353,  ..., 0.2471, 0.2627, 0.2627],
         [0.2549, 0.2549, 0.2549,  ..., 0.2549, 0.2471, 0.2471],
         ...,
         [0.2784, 0.2902, 0.2784,  ..., 0.9843, 0.9961, 1.0000],
         [0.3020, 0.3059, 0.2824,  ..., 0.9608, 0.9843, 0.9647],
         [0.3176, 0.2902, 0.2863,  ..., 1.0000, 0.9686, 0.9608]],

        [[0.2510, 0.2431, 0.2392,  ..., 0.2392, 0.2275, 0.2314],
         [0.2471, 0.2392, 0.2353,  ..., 0.2471, 0.2627, 0.2627],
         [0.2549, 0.2549, 0.2549,  ..., 0.2549, 0.2471, 0.2471],
         ...,
         [0.2784, 0.2902, 0.2784,  ..., 0.9843, 0.9961, 1.0000],
         [0.3020, 0.3059, 0.2824,  ..., 0.9608, 0.9843, 0.9647],
         [0.3176, 0.2902, 0.2863,  ..., 1.0000, 0.9686, 0.9608]]])

2、transforms.Normalize

Normalize就是使用均值和标准差对张量进行归一化处理,其输出通道计算方法为:

output[channel] = (input[channel] - mean[channel]) / std[channel]

 举例说明,这里设置均值和标准差都为0.5,

trans_norm = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
img_norm = trans_norm(img_tensor)

那么output=(input-0.5)/0.5=2*input-1,根据这个公式计算出对应的输出,结果为:

tensor([[[-0.4980, -0.5137, -0.5216,  ..., -0.5216, -0.5451, -0.5373],
         [-0.5059, -0.5216, -0.5294,  ..., -0.5059, -0.4745, -0.4745],
         [-0.4902, -0.4902, -0.4902,  ..., -0.4902, -0.5059, -0.5059],
         ...,
         [-0.4431, -0.4196, -0.4431,  ...,  0.9686,  0.9922,  1.0000],
         [-0.3961, -0.3882, -0.4353,  ...,  0.9216,  0.9686,  0.9294],
         [-0.3647, -0.4196, -0.4275,  ...,  1.0000,  0.9373,  0.9216]],

        [[-0.4980, -0.5137, -0.5216,  ..., -0.5216, -0.5451, -0.5373],
         [-0.5059, -0.5216, -0.5294,  ..., -0.5059, -0.4745, -0.4745],
         [-0.4902, -0.4902, -0.4902,  ..., -0.4902, -0.5059, -0.5059],
         ...,
         [-0.4431, -0.4196, -0.4431,  ...,  0.9686,  0.9922,  1.0000],
         [-0.3961, -0.3882, -0.4353,  ...,  0.9216,  0.9686,  0.9294],
         [-0.3647, -0.4196, -0.4275,  ...,  1.0000,  0.9373,  0.9216]],

        [[-0.4980, -0.5137, -0.5216,  ..., -0.5216, -0.5451, -0.5373],
         [-0.5059, -0.5216, -0.5294,  ..., -0.5059, -0.4745, -0.4745],
         [-0.4902, -0.4902, -0.4902,  ..., -0.4902, -0.5059, -0.5059],
         ...,
         [-0.4431, -0.4196, -0.4431,  ...,  0.9686,  0.9922,  1.0000],
         [-0.3961, -0.3882, -0.4353,  ...,  0.9216,  0.9686,  0.9294],
         [-0.3647, -0.4196, -0.4275,  ...,  1.0000,  0.9373,  0.9216]]])

这里可以稍做验证,取第一个数0.2510,计算“(0.2510-0.5)/0.5=-0.4980”,发现与结果吻合,其他同理,可自行挑选验证。

3、transforms.Resize

Resize就是对图像的尺寸进行变换,这种变换并不会改变图像的格式。

trans_resize = transforms.Resize((224, 224))
img_resize = trans_resize(img)

输出结果为:

4、transforms.Compose

Compose的功能就是将多个transforms组合起来,这里举例来说明,还是对图片的尺寸进行变换,然后转换为tensor形式

trans_resize_2 = transforms.Resize((224, 224))
trans_compose = transforms.Compose([trans_resize_2, trans_totensor])
img_resize_2 = trans_compose(img)

输出结果为:

tensor([[[0.2471, 0.2588, 0.2510,  ..., 0.2392, 0.2431, 0.2431],
         [0.2510, 0.2471, 0.2471,  ..., 0.2471, 0.2471, 0.2471],
         [0.2510, 0.2510, 0.2510,  ..., 0.2510, 0.2392, 0.2510],
         ...,
         [0.2824, 0.2902, 0.3020,  ..., 0.3255, 0.7608, 0.9882],
         [0.2863, 0.2784, 0.2784,  ..., 0.5686, 0.9412, 0.9922],
         [0.3020, 0.2863, 0.2863,  ..., 0.8471, 0.9804, 0.9725]],

        [[0.2471, 0.2588, 0.2510,  ..., 0.2392, 0.2431, 0.2431],
         [0.2510, 0.2471, 0.2471,  ..., 0.2471, 0.2471, 0.2471],
         [0.2510, 0.2510, 0.2510,  ..., 0.2510, 0.2392, 0.2510],
         ...,
         [0.2824, 0.2902, 0.3020,  ..., 0.3255, 0.7608, 0.9882],
         [0.2863, 0.2784, 0.2784,  ..., 0.5686, 0.9412, 0.9922],
         [0.3020, 0.2863, 0.2863,  ..., 0.8471, 0.9804, 0.9725]],

        [[0.2471, 0.2588, 0.2510,  ..., 0.2392, 0.2431, 0.2431],
         [0.2510, 0.2471, 0.2471,  ..., 0.2471, 0.2471, 0.2471],
         [0.2510, 0.2510, 0.2510,  ..., 0.2510, 0.2392, 0.2510],
         ...,
         [0.2824, 0.2902, 0.3020,  ..., 0.3255, 0.7608, 0.9882],
         [0.2863, 0.2784, 0.2784,  ..., 0.5686, 0.9412, 0.9922],
         [0.3020, 0.2863, 0.2863,  ..., 0.8471, 0.9804, 0.9725]]])

5、transforms.RandomCrop

RandomCrop是对图像进行随机裁剪,这里可以用来进行数据增强等,具体实现代码如范例程序。

trans_random = transforms.RandomCrop(224)
trans_compose_2 = transforms.Compose([trans_random, trans_totensor])
for i in range(10):
    img_crop = trans_compose_2(img)

以上就是一些常见的transforms操作,为了更加直观的看出图像预处里过程,可以使用tensorboard。


以上就是我关于tansforms中一些常见操作的介绍,写的不是很规范,欢迎大家批评与讨论,我们一起进步!

你可能感兴趣的:(关于pytorch的tip,pytorch,深度学习,python)