其他分享
首页 > 其他分享> > PyTorch学习:对比CV2和PyTorch的预处理

PyTorch学习:对比CV2和PyTorch的预处理

作者:互联网

验证预处理一致性

import os
import cv2
import numpy as np

import PIL
import torch
from PIL import Image
import torchvision.transforms as transforms


class RGBToBGR():
    def __call__(self, im):
        assert im.mode == 'RGB'
        r, g, b = [im.getchannel(i) for i in range(3)]
        # RGB mode also for BGR, `3x8-bit pixels, true color`, see PIL doc
        im = PIL.Image.merge('RGB', [b, g, r])
        return im

class ScaleIntensities():
    def __init__(self, in_range, out_range):
        """ Scales intensities. For example [-1, 1] -> [0, 255]."""
        self.in_range = in_range
        self.out_range = out_range

    def __oldcall__(self, tensor):
        tensor.mul_(255)
        return tensor

    def __call__(self, tensor):
        tensor = (
            tensor - self.in_range[0]
        ) / (
            self.in_range[1] - self.in_range[0]
        ) * (
            self.out_range[1] - self.out_range[0]
        ) + self.out_range[0]
        return tensor

def make_transform():
    inception_sz_resize = 256
    inception_sz_crop = 224
    inception_mean = [104, 117, 128]
    inception_std = [1, 1, 1]
    inception_transform = transforms.Compose(
       [
        RGBToBGR(),
        transforms.Resize(inception_sz_resize),
        transforms.CenterCrop(inception_sz_crop),
        transforms.ToTensor(),
        ScaleIntensities([0, 1], [0, 255]),
        transforms.Normalize(mean=inception_mean, std=inception_std)
       ])
    return inception_transform

def cv2_transform(image):
    inception_sz_resize = 256
    inception_sz_crop = 224
    inception_mean = [104, 117, 128]
    inception_std = [1, 1, 1]
    image = cv2.resize(image, (inception_sz_resize, inception_sz_resize),interpolation=cv2.INTER_LINEAR)
    #image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = image[16:16+224,16:16+224]
    image = image.astype(np.float32)
    image = image - inception_mean
    image = image / inception_std
    image = image.transpose((2, 0, 1))
    return image

if __name__ == '__main__':
    inception_transform = make_transform()
    image_name = r"./test.jpg"
    
    
    im = PIL.Image.open(image_name)
    result = inception_transform(im)
    result = result.numpy()
    print('result:',result.shape)

    image = cv2.imread(image_name,-1)
    result2 = cv2_transform(image)
    print('result2:',result2.shape)

结论:
  不加Resize和中心裁剪操作,两者一样;
  加了Resize和中心裁剪操作,结果基本一致,不影响实际使用;

标签:__,cv2,image,CV2,PyTorch,self,range,inception,预处理
来源: https://blog.csdn.net/u013841196/article/details/121563944