赞
踩
在PyTorch中,transforms的作用主要是进行数据预处理和增强。这对于深度学习模型的训练非常重要,因为模型的性能很大程度上依赖于输入数据的质量和格式。transforms提供了一系列的工具,使得数据可以在加载到模型之前被适当地转换和标准化。这些转换可能包括但不限于:
_
3. 标准化:_通过标准化处理(Normalize),可以将图像数据的每个通道的像素值缩放到给定的均值和标准差,这有助于模型的收敛和提高训练稳定性。
_
4. 数据增强:通过随机的图像转换(如随机裁剪、旋转、翻转等),可以人为地增加数据集的多样性,这是一种有效的减少过拟合和提高模型泛化能力的方法。
PyTorch通过torchvision.transforms模块提供了这些转换操作的实现。这些操作可以单独使用,也可以通过transforms.Compose组合成一个转换流水线。这样,原始数据可以按照预定义的方式被转换和准备,以便于模型的训练和验证。
我们知道,在Pytorch图像处理中有许多的函数,它的参数要求被传入的图片类型是Numpy,tensor等等,所以就需要我们使用Transtorms将PIL类型的图片转换成符合要求的类型。
from PIl import Image
from torchvisio import transforms
img_path = "F:\\Python\\Learn_Pytorch\\dataset\\train\\ants\\5650366_e22b7e1065.jpg"
img = Image.open(img_path)
tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)
print(type(img))
print(type(tensor_img))
from PIl import Image
from torchvisio import transforms
img_path = "F:\\Python\\Learn_Pytorch\\dataset\\train\\ants\\5650366_e22b7e1065.jpg"
img = Image.open(img_path)
tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)
print(type(img))
print(type(tensor_img))
可以看出来,图像格式由PIL转换成了 Tensor格式;
通过标准化处理(Normalize),可以将图像数据的每个通道的像素值缩放到给定的均值和标准差,这有助于模型的收敛和提高训练稳定性。
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
writer = SummaryWriter('logs')
img_path = "F:\\Python\\Learn_Pytorch\\dataset\\train\\ants\\5650366_e22b7e1065.jpg"
img = Image.open(img_path)
trans_tensor = transforms.ToTensor()
img_tensor = trans_tensor(img)
writer.add_image("Totensor", img_tensor) # 第一个参数是日志名称,第二个是你处理过后得到的图像变量
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)
writer.close()
conda activate pytorch # 我的虚拟环境名叫pytoch
tensorboard --logdir=logs
等待出现链接,点击链接
from PIL import Image from torch.utils.tensorboard import SummaryWriter from torchvision import transforms writer = SummaryWriter('logs') img_path = "F:\\Python\\Learn_Pytorch\\dataset\\train\\ants\\5650366_e22b7e1065.jpg" img = Image.open(img_path) trans_tensor = transforms.ToTensor() img_tensor = trans_tensor(img) writer.add_image("Totensor", img_tensor) # 第一个参数是日志名称,第二个是你处理过后得到的图像变量 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) writer.close()
这些操作不仅可以帮助模型在不同的数据变体上训练,从而提高泛化能力,还可以调整图像数据以符合模型的输入要求。
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
writer = SummaryWriter('test_resize_logs')
img_path = "F:\\Python\\Learn_Pytorch\\dataset\\train\\ants\\5650366_e22b7e1065.jpg"
img = Image.open(img_path)
trans_tensor = transforms.ToTensor()
img_tensor = trans_tensor(img)
writer.add_image("ToTensor", img_tenosr)
trans_resize = transforms.Resize((512, 512))
img_resize = trans_resize(img)
img_resize = trans_tensor(img_resize)
writer.add_image('Resize', img_resize)
writer.close()
可以看出来图片尺寸确实小了
# 导入PIL模块,transforms模块,SummaryWriter模块 from PIL import Image from torch.utils.tensorboard import SummaryWriter from torchvision import transforms # 创建日志文件,导入图像路径,使用open函数打开图像并存入内存中 writer = SummaryWriter('test_resize_logs') img_path = "F:\\Python\\Learn_Pytorch\\dataset\\train\\ants\\5650366_e22b7e1065.jpg" img = Image.open(img_path) # 创建ToTensor实例对象,将图像格式进行转换 trans_tensor = transforms.ToTensor() img_tensor = trans_tensor(img) # ④ 将图像存入日志中 writer.add_image("ToTensor", img_tenosr) # ⑤ 使用resize trans_resize = transforms.Resize((512, 512)) img_resize = trans_resize(img) img_resize = trans_tensor(img_resize) writer.add_image('Resize', img_resize) writer.close()
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
writer = SummaryWriter('test_resize_logs')
img_path = "F:\\Python\\Learn_Pytorch\\dataset\\train\\ants\\5650366_e22b7e1065.jpg"
img = Image.open(img_path)
trans_tensor = transforms.ToTensor()
img_tensor = trans_tensor(img)
writer.add_image("ToTensor", img_tenosr)
trans_random = transforms.RandomCrop(200)
trans_compose = transforms.Compose([trans_random, trans_tensor])
for i in range(10):<br /> img_crop = trans_compose(img)<br /> writer.add_image('RandomCrop', img_crop, i)
writer.close()
# ① 导入PIL模块,transforms模块,SummaryWriter模块 from PIL import Image from torch.utils.tensorboard import SummaryWriter from torchvision import transforms # ② 创建日志文件,导入图像路径,使用open函数打开图像并存入内存中 writer = SummaryWriter('test_resize_logs') img_path = "F:\\Python\\Learn_Pytorch\\dataset\\train\\ants\\5650366_e22b7e1065.jpg" img = Image.open(img_path) # ③ 创建ToTensor实例对象,将图像格式进行转换 trans_tensor = transforms.ToTensor() img_tensor = trans_tensor(img) # ④ 将图像存入日志中 writer.add_image("ToTensor", img_tenosr) # ⑤ 使用RandomCrop trans_random = transforms.RandomCrop(200) trans_compose = transforms.Compose([trans_random, trans_tensor]) for i in range(10): img_crop = trans_compose(img) writer.add_image('RandomCrop', img_crop, i) writer.close()
from PIL import Image from torch.utils.tensorboard import SummaryWriter from torchvision import transforms writer = SummaryWriter('logs') img_path = "F:\\Python\\Learn_Pytorch\\dataset\\train\\ants\\5650366_e22b7e1065.jpg" img = Image.open(img_path) trans_tensor = transforms.ToTensor() img_tensor = trans_tensor(img) writer.add_image("Totensor", img_tensor) # 第一个参数是日志名称,第二个是你处理过后得到的图像变量 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_resize = transforms.Resize((512, 512)) img_resize = trans_resize(img) img_resize = trans_tensor(img_resize) writer.add_image('Resize', img_resize) trans_random = transforms.RandomCrop(200) trans_compose = transforms.Compose([trans_random, trans_tensor]) for i in range(10): img_crop = trans_compose(img) writer.add_image('RandomCrop', img_crop, i) writer.close()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。