常见的Transform类

from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms

writer = SummaryWriter("logs")
# 读取图片
img = Image.open("image/tx1.jpg")

1.ToTensor()

作用:PIL Image或numpy.ndarray转换为tensor

代码:

trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("Totensor", img_tensor)

说明:创建一个对象trans_totensor("img")默认调用的是__call__方法 return F.to_tensor(pic)

2.Normalize()

作用:用平均值和标准差对张量图像进行归一化,不支持PIL格式

代码:

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

公式:

[channel] = (input[channel] - mean[channel]) / std[channel]规划的公式

 (input - 0.5) / 0.5 = 2*input – 1

Input的图片像素值[0,1]

最后result:[-1,1]

3.Resize()

作用:将输入图像调整为给定大小

代码:

trans_resize = transforms.Resize((600, 600))
# img 是PIL类型
img_resize = trans_resize(img)
# PIL  -- tensor
img_resize_norm = trans_totensor(img_resize)
writer.add_image("resize", img_resize_norm)

4.Compose()

trans_resize2 = transforms.Resize(520)
# Compose()参数是一个列表[transfroms参数1,transfroms参数2...]
# PIL-PIL-tensor
trans_compose = transforms.Compose([trans_resize2, trans_totensor])
img_resize2 = trans_compose(img)
writer.add_image("Resize2", img_resize2, 1)

5.RandomCrop()随机切割

trans_random = transforms.RandomCrop((500,1000))
trans_compose2 = transforms.Compose([trans_resize2, trans_totensor])
for i in range(10):
    img_crop = trans_compose2(img)
    writer.add_image("RandomCrop", img_crop, i)

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