torchvision 笔记:transforms.Normalize()
Posted UQI-LIUWJ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了torchvision 笔记:transforms.Normalize()相关的知识,希望对你有一定的参考价值。
一般和transforms.ToTensor()搭配使用
作用就是先将输入归一化到(0,1)【
transforms.ToTensor()】,再使用公式"(x-mean)/std"
,将每个元素分布到(-1,1)
很多CV的代码中,是这样使用这一条语句的:
torchvision.transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
这一组参数是从ImageNet数据集中获得的
在 torchvision 笔记:ToTensor()_UQI-LIUWJ的博客-CSDN博客的代码基础上我们进行修改
ToTensor 中的代码:
from PIL import Image from torchvision import transforms, utils a=Image.open(b+'img/00000.jpg') a
y=transforms.ToTensor() a=y(a) a ''' tensor([[[0.9255, 0.9255, 0.9255, ..., 0.9176, 0.9176, 0.9176], [0.9255, 0.9255, 0.9255, ..., 0.9176, 0.9176, 0.9176], [0.9255, 0.9255, 0.9255, ..., 0.9176, 0.9176, 0.9176], ..., [0.7882, 0.7882, 0.7882, ..., 0.7922, 0.7922, 0.7922], [0.7882, 0.7882, 0.7882, ..., 0.7922, 0.7922, 0.7922], [0.7882, 0.7882, 0.7882, ..., 0.7922, 0.7922, 0.7922]], [[0.9255, 0.9255, 0.9255, ..., 0.9216, 0.9216, 0.9216], [0.9255, 0.9255, 0.9255, ..., 0.9216, 0.9216, 0.9216], [0.9255, 0.9255, 0.9255, ..., 0.9216, 0.9216, 0.9216], ..., [0.7961, 0.7961, 0.7961, ..., 0.7922, 0.7922, 0.7922], [0.7961, 0.7961, 0.7961, ..., 0.7922, 0.7922, 0.7922], [0.7961, 0.7961, 0.7961, ..., 0.7922, 0.7922, 0.7922]], [[0.9255, 0.9255, 0.9255, ..., 0.9294, 0.9294, 0.9294], [0.9255, 0.9255, 0.9255, ..., 0.9294, 0.9294, 0.9294], [0.9255, 0.9255, 0.9255, ..., 0.9294, 0.9294, 0.9294], ..., [0.7922, 0.7922, 0.7922, ..., 0.8000, 0.8000, 0.8000], [0.7922, 0.7922, 0.7922, ..., 0.8000, 0.8000, 0.8000], [0.7922, 0.7922, 0.7922, ..., 0.8000, 0.8000, 0.8000]]]) '''
Normalize的代码
z=transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
a=z(a)
a
'''
tensor([[[1.9235, 1.9235, 1.9235, ..., 1.8893, 1.8893, 1.8893],
[1.9235, 1.9235, 1.9235, ..., 1.8893, 1.8893, 1.8893],
[1.9235, 1.9235, 1.9235, ..., 1.8893, 1.8893, 1.8893],
...,
[1.3242, 1.3242, 1.3242, ..., 1.3413, 1.3413, 1.3413],
[1.3242, 1.3242, 1.3242, ..., 1.3413, 1.3413, 1.3413],
[1.3242, 1.3242, 1.3242, ..., 1.3413, 1.3413, 1.3413]],
[[2.0959, 2.0959, 2.0959, ..., 2.0784, 2.0784, 2.0784],
[2.0959, 2.0959, 2.0959, ..., 2.0784, 2.0784, 2.0784],
[2.0959, 2.0959, 2.0959, ..., 2.0784, 2.0784, 2.0784],
...,
[1.5182, 1.5182, 1.5182, ..., 1.5007, 1.5007, 1.5007],
[1.5182, 1.5182, 1.5182, ..., 1.5007, 1.5007, 1.5007],
[1.5182, 1.5182, 1.5182, ..., 1.5007, 1.5007, 1.5007]],
[[2.3088, 2.3088, 2.3088, ..., 2.3263, 2.3263, 2.3263],
[2.3088, 2.3088, 2.3088, ..., 2.3263, 2.3263, 2.3263],
[2.3088, 2.3088, 2.3088, ..., 2.3263, 2.3263, 2.3263],
...,
[1.7163, 1.7163, 1.7163, ..., 1.7511, 1.7511, 1.7511],
[1.7163, 1.7163, 1.7163, ..., 1.7511, 1.7511, 1.7511],
[1.7163, 1.7163, 1.7163, ..., 1.7511, 1.7511, 1.7511]]])
'''
将tensor反变换回图片,则有
以上是关于torchvision 笔记:transforms.Normalize()的主要内容,如果未能解决你的问题,请参考以下文章
PyTorch学习笔记——图像处理(transforms.Normalize 归一化)
极智AI | OpenCV and torchvision.transforms 实现图像裁剪方法
极智AI | OpenCV and torchvision.transforms 实现图像等比例缩放方法
PyTorch 1.0 中文文档:torchvision.transforms