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 归一化与反归一化实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
pytorch,归一化,反归一化

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“pytorch 归一化与反归一化实例”
暂无“pytorch 归一化与反归一化实例”评论...