1.Transforms可以对训练图像进行预处理,以提高模型的稳定性,提高泛化能力。其中包含:
中心裁剪、数据标准化、缩放、裁剪、旋转、仿射、反转、填充、噪声添加、灰度变换、线性变换、亮度饱和度以及对比度变换等。
所处理的图像用tensorboard进行显示,代码如下:
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
writer = SummaryWriter("logs")
imgPath = "D:/Windows_temp/fengbo/Pictures/win7-006.jpg"
img = Image.open(imgPath)
#第一个 ToTensor
trans_toTensor = transforms.ToTensor()
imgTensor = trans_toTensor(img)
print(imgTensor[0,0,0])
writer.add_image("ToTensor",imgTensor,0)
#第二个 Normalize
trans_norm = transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
img_norm = trans_norm(imgTensor) #输入必须是tensor
print(img_norm[0][0][0])
#Resize
print(img.size)
trans_size = transforms.Resize((640,640),interpolation=Image.BILINEAR)
imgResize = trans_size(img)
imgResizeTensor = trans_toTensor(imgResize)
writer.add_image("resize",imgResizeTensor)
#进行多种变换
trans_resize_o= transforms.Resize((512,512))
trans_compose = transforms.Compose([trans_resize_o, trans_toTensor])
imgRT = trans_compose(img) #先经过resize尺寸改变,再变换成张量
writer.add_image("compose",imgRT,0)
#中心裁剪
trans_centerCrop = transforms.CenterCrop((512,512))
img_centercrop = trans_toTensor(trans_centerCrop(img))
print(img_centercrop.size)
writer.add_image("centerCrop",img_centercrop,0)
#padding 加黑边
transform_pad = transforms.Compose([
transforms.Pad(padding=100, fill=(0, 0, 0), padding_mode='constant'),
transforms.ToTensor()
])
# 对图像进行填充
padded_image = transform_pad(img)
writer.add_image("pad",padded_image,0)
#调整亮度 饱和度
transform_jit = transforms.Compose([
transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1),
transforms.ToTensor()
])
# 对图像进行颜色调整
adjusted_image = transform_jit(img)
writer.add_image("jit",adjusted_image,0)
transform_RandomGrayscale = transforms.Compose([
transforms.RandomGrayscale(p=0.5),
transforms.ToTensor()
])
# 对图像进行随机灰度转换
RandomGrayscale_image = transform_RandomGrayscale(img)
writer.add_image("RandomGrayscale",RandomGrayscale_image,0)
# 将图像转换为灰度图
transform_Grayscale = transforms.Compose([
transforms.Grayscale(num_output_channels=1),
transforms.ToTensor()
])
grayscale_image = transform_Grayscale(img)
writer.add_image("Grayscale",grayscale_image,0)
#随机仿射
transform_RandomAffine = transforms.Compose([
transforms.RandomAffine(degrees=10, translate=(0.1, 0.1), scale=(0.9, 1.1), shear=10),
transforms.ToTensor()
])
# 对图像进行随机仿射变换
RandomAffine_image = transform_RandomAffine(img)
writer.add_image("RandomAffine",RandomAffine_image,0)
#随即擦除
"""
将以50%的概率对图像进行随机遮挡,遮挡区域的面积在原图面积的 [0.01, 0.33] 之间随机选择,遮挡区域的长宽比在 [0.3, 3.3] 之间随机选择
"""
transform_RandomErasing = transforms.Compose([
transforms.RandomErasing(p=0.5, scale=(0.01, 0.33), ratio=(0.3, 3.3)),
transforms.ToTensor()
])
# 对图像进行随机遮挡
transform_RandomErasing_image = transform_RandomErasing(img)
writer.add_image("RandomErasing",transform_RandomErasing_image,0)
writer.close()
执行完后,使用tensorboard --logdir=logs,得到网址,点击网址,查看结果: