pytorch torchvision transform

    transforms: 由transform构成的列表.
    例子:

    class torchvision.transforms.Scale(size, interpolation=2)

    将输入的PIL.Image重新改变大小成给定的sizesize是最小边的边长。举个例子,如果原图的height>width,那么改变大小后的图片大小是(size*height/width, size)
    用例:

    1. from torchvision import transforms
    2. from PIL import Image
    3. crop = transforms.Scale(12)
    4. img = Image.open('test.jpg')
    5. print(type(img))
    6. print(img.size)
    7. croped_img=crop(img)
    8. print(type(croped_img))
    9. print(croped_img.size)

    class torchvision.transforms.CenterCrop(size)

    将给定的PIL.Image进行中心切割,得到给定的sizesize可以是tuple(target_height, target_width)。也可以是一个Integer,在这种情况下,切出来的图片的形状是正方形。

    class torchvision.transforms.RandomHorizontalFlip

    随机水平翻转给定的PIL.Image,概率为0.5。即:一半的概率翻转,一半的概率不翻转。

    class torchvision.transforms.RandomSizedCrop(size, interpolation=2)

    先将给定的PIL.Image随机切,然后再resize成给定的size大小。

    将给定的PIL.Image的所有边用给定的pad value填充。
    padding:要填充多少像素
    fill:用什么值填充
    例子:

    1. from torchvision import transforms
    2. padding_img = transforms.Pad(padding=10, fill=0)
    3. img = Image.open('test.jpg')
    4. print(type(img))
    5. print(img.size)
    6. padded_img=padding(img)
    7. print(type(padded_img))

    class torchvision.transforms.Normalize(mean, std)

    class torchvision.transforms.ToTensor

    把一个取值范围是[0,255]PIL.Image或者shape(H,W,C)numpy.ndarray,转换成形状为[C,H,W],取值范围是[0,1.0]torch.FloadTensor

    1. data = np.random.randint(0, 255, size=300)
    2. img = data.reshape(10,10,3)
    3. print(img.shape)
    4. img_tensor = transforms.ToTensor()(img) # 转换成tensor

    shape(C,H,W)Tensorshape(H,W,C)numpy.ndarray转换成,值不变。

    class torchvision.transforms.Lambda(lambd)

    使用lambd作为转换器。