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

yipeiwu_com6年前Python基础

如下所示:

#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设计】。

相关文章

使用PyInstaller将Pygame库编写的小游戏程序打包为exe文件及出现问题解决方法

使用PyInstaller将Pygame库编写的小游戏程序打包为exe文件及出现问题解决方法

下面看下通过Pyinstaller打包Pygame库写的小游戏程序出现的问题解决方法 # -基于Python的Pygame库的GUI游戏 游戏内容是通过飞船发射子弹来射击外星人 空格键为...

如何搜索查找并解决Django相关的问题

1. 卡住是怎么办 按照以下步骤, 前提是你需要懂点英文: 尽可能自己想办法解决 仔细阅读相关文档, 确保不错过任何相关内容 在Google, 百度, mailing lists或Sta...

python使用in操作符时元组和数组的区别分析

在python中可以使用in符号判断指定的元素是否存在于列表中,但我发现元组和数组存在区别,下面是详细实验结果。 >>> 'jb51.net' in ['haotu...

python matlibplot绘制多条曲线图

python matlibplot绘制多条曲线图

这里我利用的是matplotlib.pyplot.plot的工具来绘制折线图,这里先给出一个段代码和结果图: # -*- coding: UTF-8 -*- import numpy...

基于Python安装pyecharts所遇的问题及解决方法

基于Python安装pyecharts所遇的问题及解决方法

最近学习到数据可视化内容,老师推荐安装pyecharts,于是pip install 了一下,结果...掉坑了,下面是我的跳坑经验,如果你有类似问题,希望对你有所帮助。 第一个坑: 这...