您好,登錄后才能下訂單哦!
ToTensor中就有轉到0-1之間了。
# -*- coding:utf-8 -*- import time import torch from torchvision import transforms import cv2 transform_val_list = [ # transforms.Resize(size=(160, 160), interpolation=3), # Image.BICUBIC transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ] trans_compose = transforms.Compose(transform_val_list) if __name__ == '__main__': std= [0.229, 0.224, 0.225] mean=[0.485, 0.456, 0.406] path="d:/2.jpg" data=cv2.imread(path) t1 = time.time() x = trans_compose(data) x[0]=x[0]*std[0]+mean[0] x[1]=x[1]*std[1]+mean[1] x[2]=x[2].mul(std[2])+mean[2] img = x.mul(255).byte() img = img.numpy().transpose((1, 2, 0)) # torch.set_num_threads(3) # img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB) cv2.imshow("sdf", img) cv2.waitKeyEx()
這個測試時間:歸一化與反歸一化都需要7ms左右,
但是在多路攝像頭中,可能比較慢。
std= [0.229, 0.224, 0.225] mean=[0.485, 0.456, 0.406] path="d:/2.jpg" data=cv2.imread(path) t1 = time.time() start = time.time() x = trans_compose(data) print("gui", time.time() - start) for i in range(10): start=time.time() for i in range(len(mean)): # x[i]=x[i]*std[i]+mean[i] x[i]=x[i].mul(std[i])+mean[i] img = x.mul(255).byte() img = img.numpy().transpose((1, 2, 0)) print("fan",time.time()-start) # torch.set_num_threads(3) # img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB) cv2.imshow("sdf", img) cv2.waitKeyEx()
以上這篇pytorch 歸一化與反歸一化實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。