跟着小土堆学习pytorch(三)——常见transform

文章目录

一、Normalize

把图片进行,归一化
output[channel] = (input[channel] - mean[channel]) / std[channel]

这是Normalize,所使用的公式

python 复制代码
print(img_tensor[0][0][0])
trans_norm transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
img_norm trans_norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("Normalize",img_norm)

因为RGB图片是三通道的,所以需要用中括号,在均值、方差处,输入三个参数

二、Resize

如果输入,两个参数,那么直接规定为长和宽,如果只有一个数字,那么就是等比例缩小

python 复制代码
print(img.size)
trans_resize transforms.Resize((512,512))
img PIL -resize -img_resize PIL
img_resize trans_resize(img)
img_resize PIL -totensor -img_resize tensor
img_resize trans_totensor(img_resize)
writer.add_image("Resize",img_resize,0)
print(img_resize)

三、compose

Compose()中的参数需要是一个列表

Python中,列表的表示形式为数据1,数据2,

在Compose中,数据需要是transforms类型,所以得到,Compose([transforms参数1,transforms参数2,.)

python 复制代码
# Compose-resize-2
trans_resize_2 = transforms.Resize(512)
# PIL -PIL -tensor
trans_compose transforms.Compose([trans_resize_2,trans_totensor])
img_resize_2 trans_compose(img)
writer.add_image("Resize",img_resize_2,1)

trans_compose transforms.Compose([trans_resize_2,trans_totensor])

着重讲一下这一行

首先明确三点

  1. compose的作用就是把transform组合一起,按照前面处理完,再传完下一个
  2. Resize对象输入PIL,输出也是PIL
  3. totensor对象输入时PIL,输出时tensor
    注意:trans_resize_2,trans_totensor,这是已经把对象实例化了
    所以这一行的代码,是先放入trans_resize_2处理,再放入trans_totensor

四、RandomCrop

嗯,随机裁剪

sequence---序列---()

list---列表----\[\]

tuple---元组

RandomCrop可以输入的是序列

compose输入的是列表

五、transform

听到现在为止,transform就是对图片进行一个预处理的过程

关注几个点

1.输入和输出的数据类型

2.多看官方文档

3.关注方法需要什么参数

不知道返回值或者返回值的类型

print()

print(type())

最后转化为tensor,用torchvision看一下效果

相关推荐
私人珍藏库9 小时前
[Android] 会计快题库 -财会职称考试刷题学习
android·人工智能·学习·app·软件·多功能
闻道且行之9 小时前
TurboOCR:基于PP-OCRv6的极速Windows离线OCR工具,深度解析3.4GB依赖背后的技术架构
c++·人工智能·python·qt·机器学习·ocr
许彰午10 小时前
95_Python内存管理与垃圾回收
开发语言·python
骄阳如火11 小时前
Python 性能深度剖析:从“被诟病的慢”到“Rust 重塑”的拐点
python
满怀冰雪11 小时前
03-第一个 Paddle 程序:Tensor 创建、计算与设备管理
人工智能·python·paddle
CClaris11 小时前
大模型量化从0到1(九):用 llama.cpp 把模型转成 GGUF 并跑本地推理
人工智能·pytorch·python·深度学习·llama
学编程的小虎12 小时前
SenseVoice微调
人工智能·python·自然语言处理
诸葛说抛光12 小时前
国内大型汽车改装展览会定展 佛山改装 佛山汽车赛事
python·汽车
chouchuang12 小时前
day-030-综合练习-笔记管理器
开发语言·笔记·python
乖巧的妹子12 小时前
Python基础核心知识点详解:内置函数、运算符、字符串方法、数据结构与类型转换
python