Table of contents
Use of Transeforms
The transeforms in torchvision mainly transform (preprocess) the image.from torchvision import transforms
transeforms中常用的就是以下几种方法:(Alt+7可唤出左侧的Structure结构)
“Compose”, “ToTensor”, “PILToTensor”, “ConvertImageDtype”, “ToPILImage”, “Normalize”, “Resize”, “Scale”,“CenterCrop”
Compose: Composes several transforms together. Args: list of transforms to compose. Combine several transforms together. Parameters: [Transform object list], for example transforms.Compose([transforms.CenterCrop(10),transforms.ToTensor(),…])
ToTensor: Convert a PIL Image or numpy.ndarray to tensor.
ToPILImage: Convert a tensor or a graph to a PIL Image.
Normalize(torch.nn.Module): Normalize a tensor image with mean and standard deviation. This transform does not support PIL Image. Normalize a tensor image with mean and standard deviation. This conversion does not support PIL images. (Given mean:(mean[1],…,mean[n]) and std:(std[1],…,std[n]) for n dimensions, this transformation will normalize each channel )
Resize(torch.nn.Module): Resize the input image (PIL Image or Tensor) to the given size. Return PIL Image or Tensor: Rescaled image. Resize the input image (PIL Image or Tensor) to the given size size. size (sequence or int), when it is a sequence, adjust it to the specified (h, w); when it is an int, adjust the min(h, w) of the original image to the size, and then make the other side proportional zoom.
RandomCrop(torch.nn.Module): Crop the given image (PIL Image or Tensor) at a random location. Crop an image of a given size at a random location (the input requirements for size are the same as Resize).
Use ToTensor() to convert PIL Image to tensor
You can also use ToTensor() to convert numpy.ndarray to tensor ( the data type read by opencv is numpy.ndarray)
import numpy as np
from torchvision import transforms
from PIL import Image
image_path = 'hymenoptera_data/train/ants/0013035.jpg'
image = Image.open(image_path)
# 1.transforms该如何使用(python)
tensor_trans = transforms.ToTensor() # ToTensor()中不带参数
tensor_img = tensor_trans(image) # 不能直接写成transforms.ToTensor(image)
print(np.array(image).shape) # (512, 768, 3)
print(tensor_img.shape) # torch.Size([3, 512, 768]),通道数变到第0维了
ToTensor works with Tensorboard
import numpy as np
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from PIL import Image
image_path = 'hymenoptera_data/train/ants/0013035.jpg'
image = Image.open(image_path)
# 1.transforms该如何使用(python)
tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(image)
print(np.array(image).shape)
print(tensor_img.shape)
# 写入tensorboard
writer = SummaryWriter('logs')
writer.add_image('tag', tensor_img, 1)
writer.close()
Common transforms
The data type of the image is often different in different scenarios, it is easy to make mistakes, and it needs to be converted to a specific format before it can be used!
__call__()
The role of the method: to turn an instantiated object of a class into a callable object . Calling the instance object is to execute__call__()
the code in the method.- You can use the built-in function
callable
to determine whether it is a callable object. For example, to judgep
whether it is a callable object:print(callable(p))
return True or False.class Person: def __call__(self, name): print('__call__' + ' Hello ' + name) def hello(self, name): print('hello ' + name) person = Person() # 实例化一个对象person person('zhangsan') # 像调用函数一样调用person对象 person.__call__('zhangshan_2') # 也可像调用类函数调用 person.hello('wangwu') # 调用类函数person # __call__ Hello zhangsan # __call__ Hello zhangshan_2 # hello wangwu
from torch.utils.tensorboard import SummaryWriter from torchvision import transforms from PIL import Image image_path = 'hymenoptera_data/train/ants/0013035.jpg' image = Image.open(image_path) writer = SummaryWriter('logs') # 1.Totensor trans_totensor = transforms.ToTensor() img_tensor = trans_totensor(image) writer.add_image('ToTensor', img_tensor) # 这里只传入了tag和image_tensor,没有写入第3个参数global_step,则会默认是第0步 # 2.Normalize 可以改变色调 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) trans_norm = transforms.Normalize([1, 3, 5], [3, 2, 1]) img_norm_2 = trans_norm(img_tensor) writer.add_image('Normalize', img_norm_2, 1) trans_norm = transforms.Normalize([2, 0.5, 3], [5, 2.6, 1.5]) img_norm_3 = trans_norm(img_tensor) writer.add_image('Normalize', img_norm_3, 2) # 3.Resize 将PIL或者tensor缩放为指定大小然后输出PIL或者tensor w, h = image.size # PIL.Image的size先表示的宽再表示的高 trans_resize = transforms.Resize(min(w, h) // 2) # 缩放为原来的1/2 img_resize = trans_resize(image) # 对PIL进行缩放 writer.add_image('Resize', trans_totensor(img_resize)) # 因为在tensorboard中显示,所以需要转换为tensor或numpy类型 trans_resize = transforms.Resize(min(w, h) // 4) # 缩放为原来的1/4 img_resize_tensor = trans_resize(img_tensor) writer.add_image('Resize', img_resize_tensor, 1) # 4.compose 组合这些操作 trans_compose = transforms.Compose( [transforms.Resize(min(w, h) // 2), transforms.ToTensor(), transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])]) img_campose = trans_compose(image) # image是PIL.Image格式 writer.add_image('Compose', img_campose) # 5.Randomcrop 随机裁剪 trans_randomcrop = transforms.RandomCrop(min(w, h) // 4) # 从原图中任意位置裁剪1/4 # img_ranomcrop = trans_randomcrop(img_tensor) for i in range(10): img_ranomcrop = trans_randomcrop(img_tensor) writer.add_image('RandomCrop', img_ranomcrop, i) # close()一定要记得写啊! writer.close()