四、tensorforms 常用 类 方法 的 使用(ToTensor、Normalize、Resize、Compose、RandomCrop)

tensorforms常用的类方法的使用

  • 引入要用的模块
  • 读取PIL图片
  • transforms.ToTensor
  • transforms.Normalize
  • transforms.Resize
  • transforms.Compose
  • transforms.RandomCrop
  • 查看绘制的图像
  • 完整代码

首先明确一下常用的图片读取方式,以及读取后output的格式

格式 使用到的库 读取方式
PIL from PIL import Image Image.open()
ndarray opencv-python cv.imread()
tensor from torchvision import transforms transforms.ToTensor()

引入要用的模块

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

writer = SummaryWriter('log')

image_path = 'hymenoptera_data/train/bees/17209602_fe5a5a746f.jpg'

读取PIL图片

PIL格式的图片可以理解为图片的基准类型,在此类型上进行各种的格式转换

img = Image.open(image_path)
print(type(img))
print(img)

transforms.ToTensor

PIL Image 或者 numpy.ndarray 类型的图片格式转换为 tensor格式

# ToTensor
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
print(type(trans_totensor))

writer.add_image('ToTensor', img_tensor)
writer.close()

transforms.Normalize

根据传入的meanstandardtensor格式的图片做归一化处理。只支持tensor格式,不支持PIL格式
处理公式:output[channel] = (input[channel] - mean[channel]) / std[channel]

# Normalize
trans_normal = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
img_normal = trans_normal(img_tensor)

writer.add_image('Normal', img_normal)
writer.close()

transforms.Resize

PIL 或者 Tensor类型图片重新调正大小,可以接受sequence或者int两种类型的参数。
sequence:(H, W)。高H,宽W
int: int。当参数为int时,会将图片W/H中较小边缘将会按int进行调整

# transforms.Resize
print(img.size)
trans_resize = transforms.Resize((100, 512))
img_resize = trans_resize(img)  # pil
img_resize = trans_totensor(img_resize)  # tensor
print(img_resize)

writer.add_image('Resize', img_resize)
writer.close()

transforms.Compose

将transfroms中的各种图像操作串行结合,但是要注意,前一个变换的output的数据类型必须与后一个变换的input的数据类型相匹配

# Compose
trans_resize_2 = transforms.Resize(512)
trans_compose = transforms.Compose([trans_resize_2, trans_totensor])  # 先进行resize,在进行totensor
img_resize_2 = trans_compose(img)
print(type(img_resize_2))

writer.add_image('RandomCrop', img_resize_2)
writer.close()

transforms.RandomCrop

PIL 或者 Tensor类型图片进行随机裁剪,可以接受sequence或者int两种类型的参数
sequence:(H, W)。高H,宽W
int: int。当参数为int时,将会进行int*int的裁剪

# RandomCrop
trans_random_crop = transforms.RandomCrop(400)
trans_compose_2 = transforms.Compose([trans_random_crop, trans_totensor])
img_crop = trans_compose_2(img)

writer.add_image('RandomCrop', img_crop)
writer.close()

查看绘制的图像

在conda的pytorch环境中执行 tensorboard --logdir=log --port=6007
在这里插入图片描述
点击本地连接,即可查看,不再一一进行展示
四、tensorforms 常用 类 方法 的 使用(ToTensor、Normalize、Resize、Compose、RandomCrop)_第1张图片

完整代码

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

writer = SummaryWriter('log')

image_path = 'hymenoptera_data/train/bees/17209602_fe5a5a746f.jpg'

img = Image.open(image_path)
print(type(img))
print(img)

# ToTensor
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
print(type(trans_totensor))

writer.add_image('ToTensor', img_tensor)
writer.close()

# Normalize
trans_normal = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
img_normal = trans_normal(img_tensor)
writer.add_image('Normal', img_normal)
writer.close()

# Resize
print(img.size)
trans_resize = transforms.Resize((100, 512))
img_resize = trans_resize(img)  # PIL
img_resize = trans_totensor(img_resize)  # tensor
print(img_resize)

writer.add_image('Resize', img_resize)
writer.close()

# Compose 将transfroms中的各种图像操作串行结合,但是要注意,前一个变换的output的数据类型必须与后一个变换的input的数据类型相匹配
trans_resize_2 = transforms.Resize(512)
trans_compose = transforms.Compose([trans_resize_2, trans_totensor])
img_resize_2 = trans_compose(img)
print(type(img_resize_2))

writer.add_image('RandomCrop', img_resize_2)
writer.close()

# RandomCrop
trans_random_crop = transforms.RandomCrop(400)
trans_compose_2 = transforms.Compose([trans_random_crop, trans_totensor])
img_crop = trans_compose_2(img)

writer.add_image('RandomCrop', img_crop)
writer.close()

你可能感兴趣的:(pytorch从入门到实战,python,计算机视觉,opencv)