目录
Transeforms的使用
torchvision中的transeforms,主要是对图像进行变换(预处理)。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.将几个变换组合在一起。参数:[Transform对象列表],例如transforms.Compose([transforms.CenterCrop(10),transforms.ToTensor(),...])
ToTensor: Convert a PIL Image or numpy.ndarray to tensor.
ToPILImage: Convert a tensor or an ndarray to PIL Image.
Normalize(torch.nn.Module): Normalize a tensor image with mean and standard deviation.This transform does not support PIL Image.用平均值和标准偏差归一化张量图像。此转换不支持PIL图像。(为n个维度给定mean:(mean[1],...,mean[n])和std:(std[1],...,std[n]),此转换将对每个channel进行归一化)
Resize(torch.nn.Module): Resize the input image (PIL Image or Tensor) to the given size.Return PIL Image or Tensor: Rescaled image.将输入的图像(PIL Image or Tensor)的大小缩放到指定的size尺寸。size (sequence or int),当是sequence时则调整到指定的(h, w);当是int时,就将原图的min(h,w)调整到size大小,然后另一条边进行等比例缩放。
RandomCrop(torch.nn.Module): Crop the given image (PIL Image or Tensor) at a random location.在随机位置裁剪给定的size大小的图像(size的输入要求跟Resize一样)。
用ToTensor()将PIL Image转为tensor
也可以用 ToTensor() 将 numpy.ndarray 转为tensor(用opencv读入的数据类型是numpy.ndarray)
python
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与Tensorboard配合使用
python
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()
常见的transforms
图像的数据类型在不同场景往往不同,很容易出错,需要转换为特定格式才能使用!
-
__call__()
方法的作用:把一个类的实例化对象变成了可调用对象 。调用该实例对象就是执行__call__()
方法中的代码。 -
可以通过内置函数
callable
来判断是否是可调用对象。例如判断p
是否为可调用对象:print(callable(p))
返回 True 或 False。pythonclass 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
pythonfrom 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()