今天看了pytorch、paddle、发现归一化参数设置是不同,所以深入看看到底是怎么使用的。
pytorch:
torchvision.transforms.Normalize(mean, std, inplace=False)
Normalize:是直接减去mean,再除以std
totensor:先除以255,再转成CHW
示例
'''
transforms.ToTensor的作用是将一个PIL Image格式的图片或者是取值范围为[ 0 , 255 ],
形状为[ H × W × C ],numpy.ndarray的数组转换为取值范围为[ 0.0 , 1.0 ],
形状为[ C × H × W ] 的tensor格式图片。
'''
import cv2
import numpy as np
from PIL import Image
import torchvision.transforms as transforms
img_trans = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.5,0.5,0.5], [0.5,0.5,0.5])])
img_bgr = cv2.imread("1".jpg)
img_rgb = cv2.cvtColor(img_bgr,cv2.COLOR_BGR2RGB)
img = Image.fromarray(img.astype(np.uint8))
img = img_trans(img)
albumentations:
Normalize:源码里面会自动乘上255。所以mean,std参数要自己除以255
ToTensorV2:是调整成CHW
示例
import cv2
import numpy as np
import albumentations as A
from albumentations.core.transforms_interface import ImageOnlyTransform
from albumentations.pytorch import ToTensorV2
img_trans = A.Compose([
A.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
ToTensorV2(),
], p=1.0)
img_bgr = cv2.imread("1".jpg)
img_rgb = cv2.cvtColor(img_bgr,cv2.COLOR_BGR2RGB)
img_t = img_trans(image=img_rgb)['image']
paddle:
paddle.vision.transforms.normalize(img, mean, std, data_format='CHW', to_rgb=False)
源码解释:
示例代码:
import cv2
import numpy as np
from PIL import Image
from paddle.vision.transforms import functional as F
img_trans = T.Compose([
T.Normalize(
mean = [127.5, 127.5, 127.5],
std = [127.5, 127.5, 127.5],
data_format='HWC',
to_rgb=True)])
img_bgr = cv2.imread("1".jpg)
img = Image.fromarray(img_bgr)
img = img_trans(img)
numpy版本的也记录一下:
def pre_processing(img_roi):
img = cv2.cvtColor(img_roi,cv2.COLOR_BGR2RGB)
img = cv2.resize(img_roi,(96,96))
img = np.float32(img) / 255.0
# norm
means = np.array([0.5, 0.5, 0.5])
stds = np.array([0.5, 0.5, 0.5])
img -= means
img /= stds
# channel first
img = np.ascontiguousarray(np.transpose(img, (2, 0, 1)))
# add batch axis
img = img[np.newaxis, ...]
return img