pytorch 图像预处理之减去均值,除以方差的实例


Posted in Python onJanuary 02, 2020

如下所示:

pytorch 图像预处理之减去均值,除以方差的实例

#coding=gbk
'''
GPU上面的环境变化太复杂,这里我直接给出在笔记本CPU上面的运行时间结果

由于方式3需要将tensor转换到GPU上面,这一过程很消耗时间,大概需要十秒,故而果断抛弃这样的做法

img (168, 300, 3)
sub div in numpy,time 0.0110
sub div in torch.tensor,time 0.0070
sub div in torch.tensor with torchvision.transforms,time 0.0050
tensor1=tensor2
tensor2=tensor3


img (1079, 1349, 3)
sub div in numpy,time 0.1899
sub div in torch.tensor,time 0.1469
sub div in torch.tensor with torchvision.transforms,time 0.1109
tensor1=tensor2
tensor2=tensor3


耗时最久的是numpy,其次是转换成torch.tensor,最快的是直接使用torchvision.transforms
我现在在GPU上面跑的程序GPU利用率特别低(大多数时间维持在2%左右,只有很少数的时间超过80%)
然后设置打印点调试程序时发现,getitem()输出一张图像的时间在0.1秒的数量级,这对于GPU而言是非常慢的
因为GPU计算速度很快,CPU加载图像和预处理图像的速度赶不上GPU的计算速度,就会导致显卡大量时间处于空闲状态
经过对于图像I/O部分代码的定位,发现是使用numpy减去图像均值除以方差这一操作浪费了太多时间,而且输入图像的分辨率越大,
所消耗的时间就会更多
原则上,图像预处理每个阶段的时间需要维持在0.01秒的数量级

所以,

'''

import numpy as np
import time
import torch
import torchvision.transforms as transforms
import cv2
# img_path='/ssddata2/wyx/detection/ead_stage12/stage12_img/WL_00387.jpg'
img_path='F:\\2\\00004.jpg'
PIXEL_MEANS =(0.485, 0.456, 0.406)  #RGB format mean and variances
PIXEL_STDS = (0.229, 0.224, 0.225)

#输入文件路径,输出的应该是转换成torch.tensor的标准形式

#方式一  在numpy中进行减去均值除以方差,最后转换成torch.tensor
one_start=time.time()
img=cv2.imread(img_path)
img=img[:,:,::-1]
img=img.astype(np.float32, copy=False)
img/=255.0
img-=np.array(PIXEL_MEANS)
img/=np.array(PIXEL_STDS)
tensor1=torch.from_numpy(img.copy())
tensor1=tensor1.permute(2,0,1)
one_end=time.time()
print('sub div in numpy,time {:.4f}'.format(one_end-one_start))

del img

#方式二 转换成torch.tensor,再减去均值除以方差
two_start=time.time()
img=cv2.imread(img_path)
img=img[:,:,::-1]
print('img',img.shape,np.min(img),np.min(img))
tensor2=torch.from_numpy(img.copy()).float()
tensor2/=255.0
tensor2-=torch.tensor(PIXEL_MEANS)
tensor2/=torch.tensor(PIXEL_STDS)
tensor2=tensor2.permute(2,0,1)
two_end=time.time()
print('sub div in torch.tensor,time {:.4f}'.format(two_end-two_start))

del img

#方式三 转换成torch.tensor,再放到GPU上面,最后减去均值除以方差
# three_start=time.time()
# img=cv2.imread(img_path)
# img=img[:,:,::-1]
# tensor3=torch.from_numpy(img.copy()).cuda().float()
# tensor3-=torch.tensor(PIXEL_MEANS).cuda()
# tensor3/=torch.tensor(PIXEL_STDS).cuda()
# three_end=time.time()
# print('sub div in torch.tensor on cuda,time {:.4f}'.format(three_end-three_start))

# del img

#方式四 转换成torch.tensor,使用transform方法减去均值除以方差
four_start=time.time()
img=cv2.imread(img_path)
img=img[:,:,::-1]
transform=transforms.Compose(
  [transforms.ToTensor(),transforms.Normalize(PIXEL_MEANS, PIXEL_STDS)]
)
tensor4=transform(img.copy())
four_end=time.time()
print('sub div in torch.tensor with torchvision.transforms,time {:.4f}'.format(four_end-four_start))

del img

if torch.sum(tensor1-tensor2)<=1e-3:
  print('tensor1=tensor2')
if torch.sum(tensor2-tensor4)==0:
  print('tensor2=tensor3')
# if tensor3==tensor4:
#   print('tensor3=tensor4')

以上这篇pytorch 图像预处理之减去均值,除以方差的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python脚本在Appium库上对移动应用实现自动化测试
Apr 17 Python
详解Python中__str__和__repr__方法的区别
Apr 17 Python
python 出现SyntaxError: non-keyword arg after keyword arg错误解决办法
Feb 14 Python
基于python爬虫数据处理(详解)
Jun 10 Python
详解python校验SQL脚本命名规则
Mar 22 Python
ipython和python区别详解
Jun 26 Python
Python pip 安装与使用(安装、更新、删除)
Oct 06 Python
python不使用for计算两组、多个矩形两两间的iou方式
Jan 18 Python
IntelliJ 中配置 Anaconda的过程图解
Jun 01 Python
keras K.function获取某层的输出操作
Jun 29 Python
Pytorch 扩展Tensor维度、压缩Tensor维度的方法
Sep 09 Python
python绘制云雨图raincloud plot
Aug 05 Python
Linux下升级安装python3.8并配置pip及yum的教程
Jan 02 #Python
pytorch实现focal loss的两种方式小结
Jan 02 #Python
pytorch中交叉熵损失(nn.CrossEntropyLoss())的计算过程详解
Jan 02 #Python
基于torch.where和布尔索引的速度比较
Jan 02 #Python
Python魔法方法 容器部方法详解
Jan 02 #Python
python 图像的离散傅立叶变换实例
Jan 02 #Python
Python加密模块的hashlib,hmac模块使用解析
Jan 02 #Python
You might like
windows下升级PHP到5.3.3的过程及注意事项
2010/10/12 PHP
php&amp;mysql 日期操作小记
2012/02/27 PHP
php中的PHP_EOL换行符详细解析
2013/10/26 PHP
php内嵌函数用法实例
2015/03/20 PHP
PHP常用的排序和查找算法
2015/08/06 PHP
PHP中子类重载父类的方法【parent::方法名】
2016/05/06 PHP
php使用QueryList轻松采集js动态渲染页面方法
2018/09/11 PHP
用 JSON 处理缓存
2007/04/27 Javascript
jquery isEmptyObject判断是否为空对象的函数
2011/02/14 Javascript
jQuery鼠标悬浮链接弹出跟随图片实例代码
2016/01/08 Javascript
JS解决iframe之间通信和自适应高度的问题
2016/08/24 Javascript
bootstrapValidator自定验证方法写法
2016/12/01 Javascript
通过AngularJS实现图片上传及缩略图展示示例
2017/01/03 Javascript
原生js实现下拉框功能(支持键盘事件)
2017/01/13 Javascript
socket.io学习教程之基本应用(二)
2017/04/29 Javascript
js实现轮播图的完整代码
2020/10/26 Javascript
countup.js实现数字动态叠加效果
2019/10/17 Javascript
vue quill editor 使用富文本添加上传音频功能
2020/01/14 Javascript
JS中类的静态方法,静态变量,实例方法,实例变量区别与用法实例分析
2020/03/14 Javascript
[59:26]DOTA2上海特级锦标赛D组资格赛#1 EG VS VP第二局
2016/02/28 DOTA
[10:21]DOTA2-DPC中国联赛 正赛 PSG.LGD vs Aster 选手采访
2021/03/11 DOTA
Python构建网页爬虫原理分析
2017/12/19 Python
Python内置模块hashlib、hmac与uuid用法分析
2018/02/12 Python
使用GitHub和Python实现持续部署的方法
2019/05/09 Python
基于keras输出中间层结果的2种实现方式
2020/01/24 Python
Python Websocket服务端通信的使用示例
2020/02/25 Python
基于python实现数组格式参数加密计算
2020/04/21 Python
如何设置PyCharm中的Python代码模版(推荐)
2020/11/20 Python
大学生创业计划书的格式要求
2013/12/29 职场文书
秋季红领巾广播稿
2014/01/27 职场文书
运动会稿件200字
2014/02/07 职场文书
推广普通话标语
2014/06/27 职场文书
建筑结构施工求职信
2014/07/11 职场文书
股东授权委托书范本
2014/09/13 职场文书
党员倡议书
2015/01/19 职场文书
观后感开头
2015/06/19 职场文书