pytorch 归一化与反归一化

 

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()

 

你可能感兴趣的:(torch)