当前位置:网站首页>PyTorch②---transforms结构及用法、常见的Transforms
PyTorch②---transforms结构及用法、常见的Transforms
2022-08-02 14:07:00 【伏月三十】
常见的Transforms
PIL类型:用PIL读入,Image.open()
tensor类型:用transforms转,ToTensor()
narrays类型:用opencv读入,cv.imread()
常见的方法
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
writer=SummaryWriter("logs")
img=Image.open("images/3.jpg")
print(img)
'''ToTensor()'''
trans_totensor=transforms.ToTensor()
img_tensor=trans_totensor(img)
writer.add_image("ToTensor",img_tensor)
'''Normalize(torch.nn.Module)'''
#输入两个参数,是平均值和方差,图片是三维的。(input-mean)/std
#output[channel] = (input[channel] - mean[channel]) / std[channel]
print(img_tensor[0][0][0])
trans_norm=transforms.Normalize([1,5,8],[3,2,1])
img_norm=trans_norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("Normalize",img_norm,1)
'''Resize(torch.nn.Module)'''
print(img)
trans_size=transforms.Resize((512,512))
#img PIL->resize->img_resize PIL
img_resize=trans_size(img)
#img_resize PIL->img_resize tensor
img_resize=trans_totensor(img_resize)
print(img_resize)
writer.add_image("Resize",img_resize,1)
'''Compose:对一张图片的多种操作合并成一种'''
trans_resize_2=transforms.Resize(512)
#先进行裁剪,再变成tensor的类型
trans_compose=transforms.Compose([trans_resize_2,trans_totensor])
img_resize_2=trans_compose(img)
writer.add_image("Resize2",img_resize_2,1)
'''RanDomCrop:随机裁剪'''
trans_random=transforms.RandomCrop((200,400))#裁剪正方形的话直接(512),一个括号
#先进行裁剪,再变成tensor的类型
trans_compose_2=transforms.Compose([trans_random,trans_totensor])
for i in range(10):#随机裁剪10步
img_crop=trans_compose_2(img)
writer.add_image("RanDomCrop1",img_crop,i)
writer.close()
边栏推荐
猜你喜欢
随机推荐
数据偏见的背后是什么
Kubernetes核心概念
国内IT市场还有发展吗?有哪些创新好用的IT运维工具可以推荐?
执行npm install有错误error
使用flutter小记
The NDK portal: C
LLVM系列第六章:函数返回值Return
可以拖拽的ViewGroup,仿微信拖拽缩放关闭
标签加id 和 加号 两个文本框 和一个var 赋值
[论文阅读] ACT: An Attentive Convolutional Transformer for Efficient Text Classification
预训练模型 Bert
投资组合理论的简单介绍
Redis-01-Nosql概述
It is not allowed to subscribe with a(n) xxx multiple times.Please create a fresh instance of xxx
Spark_Core
对疫情期间量化策略表现的看法
spark中RDD与DF的关系
LLVM系列第二十一章:写一个简单的Loop Pass
LLVM系列第三章:函数Function
没学好统计学的下场









