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

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!