pytorch 圖像預(yù)處理之減去均值,除以方差的實(shí)例
更新時間:2020年01月02日 09:58:41 作者:WYXHAHAHA123
今天小編就為大家分享一篇pytorch 圖像預(yù)處理之減去均值,除以方差的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
如下所示:

#coding=gbk
'''
GPU上面的環(huán)境變化太復(fù)雜,這里我直接給出在筆記本CPU上面的運(yùn)行時間結(jié)果
由于方式3需要將tensor轉(zhuǎn)換到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,其次是轉(zhuǎn)換成torch.tensor,最快的是直接使用torchvision.transforms
我現(xiàn)在在GPU上面跑的程序GPU利用率特別低(大多數(shù)時間維持在2%左右,只有很少數(shù)的時間超過80%)
然后設(shè)置打印點(diǎn)調(diào)試程序時發(fā)現(xiàn),getitem()輸出一張圖像的時間在0.1秒的數(shù)量級,這對于GPU而言是非常慢的
因?yàn)镚PU計算速度很快,CPU加載圖像和預(yù)處理圖像的速度趕不上GPU的計算速度,就會導(dǎo)致顯卡大量時間處于空閑狀態(tài)
經(jīng)過對于圖像I/O部分代碼的定位,發(fā)現(xiàn)是使用numpy減去圖像均值除以方差這一操作浪費(fèi)了太多時間,而且輸入圖像的分辨率越大,
所消耗的時間就會更多
原則上,圖像預(yù)處理每個階段的時間需要維持在0.01秒的數(shù)量級
所以,
'''
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)
#輸入文件路徑,輸出的應(yīng)該是轉(zhuǎn)換成torch.tensor的標(biāo)準(zhǔn)形式
#方式一 在numpy中進(jìn)行減去均值除以方差,最后轉(zhuǎn)換成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
#方式二 轉(zhuǎn)換成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
#方式三 轉(zhuǎn)換成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
#方式四 轉(zhuǎn)換成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 圖像預(yù)處理之減去均值,除以方差的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 使用sklearn進(jìn)行對數(shù)據(jù)標(biāo)準(zhǔn)化、歸一化以及將數(shù)據(jù)還原的方法
- pytorch中的自定義數(shù)據(jù)處理詳解
- pytorch 數(shù)據(jù)處理:定義自己的數(shù)據(jù)集合實(shí)例
- Pytorch 神經(jīng)網(wǎng)絡(luò)—自定義數(shù)據(jù)集上實(shí)現(xiàn)教程
- 關(guān)于Pytorch的MNIST數(shù)據(jù)集的預(yù)處理詳解
- 計算pytorch標(biāo)準(zhǔn)化(Normalize)所需要數(shù)據(jù)集的均值和方差實(shí)例
相關(guān)文章
python+opencv實(shí)現(xiàn)動態(tài)物體追蹤
這篇文章主要為大家詳細(xì)介紹了python+opencv實(shí)現(xiàn)動態(tài)物體的追蹤,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
np.meshgrid中的indexing參數(shù)問題解決
本文主要介紹了np.meshgrid中的indexing參數(shù)問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Python 工具類實(shí)現(xiàn)大文件斷點(diǎn)續(xù)傳功能詳解
用python進(jìn)行大文件下載的時候,一旦出現(xiàn)網(wǎng)絡(luò)波動問題,導(dǎo)致文件下載到一半。如果將下載不完全的文件刪掉,那么又需要從頭開始,如果連續(xù)網(wǎng)絡(luò)波動,是不是要頭禿了。本文提供斷點(diǎn)續(xù)傳下載工具方法,希望可以幫助到你2021-10-10

