transforms

1 transform的使用

1.1 头文件

from torchvision import transforms

1.2使用方法

# 1、transfroms该如何使用
tensor_trans = transforms.ToTensor()#(调用函数以)创建自己的工具
tensor_img = tensor_trans(img)#传入图片地址(使用)

1.3 transform的形象解释

形象解释

1.4完整代码

结合上节课的tensorboard的使用:

from tensorboardX import SummaryWriter
from torchvision import transforms
from PIL import Image

img_path = "dataset\\train\\ants\\0013035.jpg"
Image.open(img_path)
img = Image.open(img_path)

writer = SummaryWriter("logs")

# transfroms的使用
tensor_trans = transforms.ToTensor()#(调用函数以)创建自己的工具
tensor_img = tensor_trans(img)#传入图片地址
#tensorboard的使用
writer.add_image("Tensor_img",tensor_img)
writer.close()

使用命令
tensorboard --logdir=logs --port=6007

结果:


结果

2 常见的Transforms

2.1 ToTensor

img_path = "dataset\\train\\ants\\0013035.jpg"
img = Image.open(img_path)
#ToTensor
writer = SummaryWriter("logs")
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("Totensor",img_tensor)

2.2 Normalize

#Normalize 归一化
trans_norm = transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])#mean, std
img_norm = trans_norm(img_tensor)#使用transform.ToTensor()转换过后的tensor格式图片
writer.add_image("Normalize",img_norm,1)#tag, img_tensor

结果:


Normalize

2.3 Resize

#Resize
trans_resize = transforms.Resize((128,128))#注:这里必须带括号
# img PIL -> resize -> img_resize PIL
img_resize = trans_resize(img)
#img_resize PIL -> totensor -> img_resize tensor
img_resize = trans_totensor(img_resize)
writer.add_image("Resize",img_resize,1)
print(img_resize)

结果:


Resize

*注1:如果transforms.Resize()少带了一层括号:

from PIL import Image
img_path = "dataset\\train\\ants\\0013035.jpg"
img = Image.open(img_path)
print(img.size)
(768, 512)

from torchvision import transforms
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
trans_resize = transforms.Resize(512,512)
img_resize = trans_resize(img)
print(img_resize.size)
(768, 512)

trans_resize = transforms.Resize((512,512))
img_resize = trans_resize(img)
print(img_resize.size)
(512, 512)

*注2:trans_resize(img)中输入的图像必须是PIL格式,若输入tensor格式会报错如下:

img_resize = trans_resize(img_tensor)
Traceback (most recent call last):
  File "D:\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3441, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "", line 1, in 
    img_resize = trans_resize(img_tensor)
  File "D:\anaconda3\lib\site-packages\torchvision\transforms\transforms.py", line 195, in __call__
    return F.resize(img, self.size, self.interpolation)
  File "D:\anaconda3\lib\site-packages\torchvision\transforms\functional.py", line 229, in resize
    raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
TypeError: img should be PIL Image. Got 

2.4 RandomCrop随机裁剪

img_path = "dataset\\train\\ants\\0013035.jpg"
img = Image.open(img_path)
writer = SummaryWriter("logs")
#ToTensor
trans_totensor = transforms.ToTensor()
#RandomCrop
trans_random = transforms.RandomCrop(512)
trans_compose_2 = transforms.Compose([trans_random,trans_totensor])
for i in range(10):
    img_crop = trans_compose_2(img)
    writer.add_image("RandomCrop",img_crop,i)
writer.close()
RandomCrop

3 Comopose

Compose()中的参数需要的是一个列表。Python中,列表的表示形式为[数据1,数据2,...]在Compose中,数据需要的是transforms类型,所以得到,Compose([transforms参数1,transforms参数2,...])

#compose
trans_resize_2 = transforms.Resize(256)

#PIL -> PIL -> tensor
#后一个参数所需要的输入必须和前一个参数所产生的输出相匹配
trans_compose = transforms.Compose([trans_resize_2,trans_totensor])

img_resize_2 =trans_compose(img)
writer.add_image("Resize_mid",img_resize_2,1)

修改代码的参数得到的结果:


resize_2

参考视频:
1.https://www.bilibili.com/video/BV1hE411t7RN?p=10

附:学习方法


study_tips

你可能感兴趣的:(transforms)