【土堆pytorch实战】P9-P13 Transform使用

P9 Transforms使用

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

img_path="D:\Tool\VScode\pythonProject\hymenoptera_data/train/ants_image/6743948_2b8c096dda.jpg"
img=Image.open(img_path)

writer=SummaryWriter("logs")   #logs 日志文件夹

#ToTensor()
tensor_trans=transforms.ToTensor()
tensor_img=tensor_trans(img)
print(tensor_img)  #将图片数据类型转为tensor
writer.add_image("Tensor_img",tensor_img)

#Normalize()
print(tensor_img[0][0][0])
trans_norm=transforms.Normalize([1,3,5],[3,2,1])
img_norm=trans_norm(tensor_img)
print(img_norm[0][0][0])  #第一个层第一行第一列
writer.add_image("Normalize",img_norm,2)  # 2表示step

#Resize()
print(img.size)
trans_resize=transforms.Resize((512,512))  #把尺寸改为512*512
img_resize=trans_resize(img)  #这里的图片是PIL类型
img_resize=tensor_trans(img_resize)  #把PIL转为tensor 以便在tensorboard中打开
writer.add_image("Resize",img_resize)
print(img_resize)

#Compose()  #多种操作组合在一起
trans_resize_2=transforms.Resize(512)
trans_compose=transforms.Compose([trans_resize_2,tensor_trans])  # PIL--> tensor
img_resize_2=trans_compose(img)
writer.add_image("Resize_2",img_resize_2,1)


#RandomCrop() #随机裁剪
trans_random=transforms.RandomCrop([500,300])  #指定宽和高
trans_compose_2=transforms.Compose([trans_random,tensor_trans])
for i in range(10):
    img_crop=trans_compose_2(img)
    writer.add_image("Randomcrop",img_crop,i)

writer.close()
  1. ToTensor()

Convert a PIL Image or numpy.ndarray to tensor. This transform does not support torchscript
attention: tensorboard只能打开tensor或者array

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

img_path="D:\Tool\VScode\pythonProject\hymenoptera_data/train/ants_image/6743948_2b8c096dda.jpg"
img=Image.open(img_path)

writer=SummaryWriter("logs")   #logs 日志文件夹

#ToTensor()
tensor_trans=transforms.ToTensor()
tensor_img=tensor_trans(img)
print(tensor_img)  #将图片数据类型转为tensor
writer.add_image("Tensor_img",tensor_img)
  1. Normalize()

归一化公式:output[channel] = (input[channel] - mean[channel]) / std[channel]

#Normalize()
print(tensor_img[0][0][0])
trans_norm=transforms.Normalize([1,3,5],[3,2,1])
img_norm=trans_norm(tensor_img)
print(img_norm[0][0][0])  #第一个层第一行第一列
writer.add_image("Normalize",img_norm,2)  # 2表示step

【土堆pytorch实战】P9-P13 Transform使用_第1张图片

【土堆pytorch实战】P9-P13 Transform使用_第2张图片
3. Resize()

#Resize()
print(img.size)
trans_resize=transforms.Resize((512,512))  #把尺寸改为512*512
img_resize=trans_resize(img)  #这里的图片是PIL类型
img_resize=tensor_trans(img_resize)  #把PIL转为tensor 以便在tensorboard中打开
writer.add_image("Resize",img_resize)
print(img_resize)
  1. Compose()

Composes several transforms together. 传入的需要是一个列表
transforms.Compose([

transforms.CenterCrop(10),
transforms.PILToTensor(),
transforms.ConvertImageDtype(torch.float),

])

#Compose()  #多种操作组合在一起
trans_resize_2=transforms.Resize(512)
trans_compose=transforms.Compose([trans_resize_2,tensor_trans])  # PIL--> tensor
img_resize_2=trans_compose(img)
writer.add_image("Resize_2",img_resize_2,1)
  1. RandomCrop()
#RandomCrop() #随机裁剪
trans_random=transforms.RandomCrop(300)  #
trans_compose_2=transforms.Compose([trans_random,tensor_trans]) 
for i in range(10):
    img_crop=trans_compose_2(img)
    writer.add_image("Randomcrop",img_crop,i)


#RandomCrop() #随机裁剪
trans_random=transforms.RandomCrop([500,300])  #指定H W
trans_compose_2=transforms.Compose([trans_random,tensor_trans]) 
for i in range(10):
    img_crop=trans_compose_2(img)
    writer.add_image("Randomcrop",img_crop,i)

【土堆pytorch实战】P9-P13 Transform使用_第3张图片

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