pytorch 图像预处理之减去均值,除以方差的实例
作者:WYXHAHAHA123 发布时间:2022-04-20 13:43:45
标签: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')
来源:https://blog.csdn.net/WYXHAHAHA123/article/details/87924745


猜你喜欢
- 代码如下:'返回某年总共有多少天 Function DayOfYear(ByVal y) DayOfYear = DatePart(
- 一、使用步骤 1.引入库(安装Python环境、PyQt、PyQt-tools)from PyQt5 import QtCore,
- 使用递归查找父元素,知道查到想要的元素,然后return getParentTag(startTag) { var self
- 第三方库 binarytree其使用环境、安装方法及二叉树的相关知识,请见:《Python 初识二叉树,新手也秒懂!》不能导入的请安装:pi
- 昨天装了个SQL2000,打开企业管理器,发现SQL Server组下面没有任何的内容,提示“无项目”。之前sa设置的都是空密码就没碰到这个
- 我就废话不多说了,大家还是直接看代码吧~import pandas as pdimport numpy as npcolumns = [[&
- 本文实例为大家分享了pytorch绘制曲线的具体代码,供大家参考,具体内容如下import torchimport torch.nn.fun
- 前言:对于遍历大数组而言, for 循环能比 for range 循环更高效与稳定,这一点在数组元素为结构体类型更加明显。我们知道,Go 的
- 案例:该数据集的是一个关于每个学生成绩的数据集,接下来我们对该数据集进行分析,判断学生是否适合继续深造数据集特征展示1 GRE
- 前言wx.gird.Gird是实现类似excel表格的库,扩展面很广,本文讲述它添加按钮,按钮响应的内容实现效果图如下:本文基于wxPyth
- 楔子在介绍数据类型的时候我们说过,Python 的数据类型相比 C 来说要更加的通用,但速度却远不及 C。如果你在使用 Cython 加速
- 报错:D:\Program Files\Anaconda3\lib\site-packages\matplotlib\figure.py:4
- 前言在CSDN发的第一篇文章,时隔两年,终于实现了爬微博的自由!本文可以解决微博预登录、识别“展开全文”并爬取完整数据、翻页设置等问题。由于
- 本文实例为大家分享了python实现登录与注册系统的具体代码,供大家参考,具体内容如下实现功能1.调用文本文件里的用户信息2.可以将注册信息
- 今天在修改 淘宝 宝贝详情页面的时候,发现页面在 Firefox 下遇到这样一个问题:链接用图片做背景,text-indent:-9999p
- 安装paramiko后,看下面例子:import paramiko#设置ssh连接的远程主机地址和端口t=paramiko.Transpor
- 该爬虫应用了创建文件夹的功能:#file settingfolder_path = "D:/spider_things/2016.
- 前言:我们的vue代码打包上传到服务器之后,要是数据接口 以后换了域名什么的,是不是需要重新去vue文件里修改接口。能不能生成一个配置文件,
- 一、报错: 「Can't swap PDO instance while within transaction」通过查询 Larav
- Windows•安装lxml最好的安装方式是通过wheel文件来安装,http://www.lfd.uci.edu/~gohlke/pyth