pytorch常用normalization函数
将输入的图像shape记为[N, C, H, W],这几个方法主要的区别就是在,
batchNorm是在batch上,对NHW做归一化,对小batchsize效果不好;
layerNorm在通道方向上,对CHW归一化,主要对RNN作用明显;
instanceNorm在图像像素上,对HW做归一化,用在风格化迁移;
GroupNorm将channel分组,然后再做归一化;
SwitchableNorm是将BN、LN、IN结合,赋予权重,让网络自己去学习归一化层应该使用什么方法。
归一化与反归一化
https://blog.csdn.net/rehe_nofish/article/details/111413690
重点关注
pytorch标准化后的图像数据如果反标准化保存
# coding:utf-8
import os
import torch.nn as nn
import numpy as np
import scipy.misc
import imageio
import matplotlib.pyplot as plt
import torch
def tensor2im(input_image, imtype=np.uint8):
""""将tensor的数据类型转成numpy类型,并反归一化.
Parameters:
input_image (tensor) -- 输入的图像tensor数组
imtype (type) -- 转换后的numpy的数据类型
"""
mean = [0.485,0.456,0.406] #dataLoader中设置的mean参数
std = [0.229,0.224,0.225] #dataLoader中设置的std参数
if not isinstance(input_image, np.ndarray):
if isinstance(input_image, torch.Tensor): #如果传入的图片类型为torch.Tensor,则读取其数据进行下面的处理
image_tensor = input_image.data
else:
return input_image
image_numpy = image_tensor.cpu().float().numpy() # convert it into a numpy array
if image_numpy.shape[0] == 1: # grayscale to RGB
image_numpy = np.tile(image_numpy, (3, 1, 1))
for i in range(len(mean)): #反标准化
image_numpy[i] = image_numpy[i] * std[i] + mean[i]
image_numpy = image_numpy * 255 #反ToTensor(),从[0,1]转为[0,255]
image_numpy = np.transpose(image_numpy, (1, 2, 0)) # 从(channels, height, width)变为(height, width, channels)
else: # 如果传入的是numpy数组,则不做处理
image_numpy = input_image
return image_numpy.astype(imtype)
def save_img(im, path, size):
"""im可是没经过任何处理的tensor类型的数据,将数据存储到path中
Parameters:
im (tensor) -- 输入的图像tensor数组
path (str) -- 图像寻出的路径
size (list/tuple) -- 图像合并的高宽(heigth, width)
"""
scipy.misc.imsave(path, merge(im, size)) #将合并后的图保存到相应path中
def merge(images, size):
"""
将batch size张图像合成一张大图,一行有size张图
:param images: 输入的图像tensor数组,shape = (batch_size, channels, height, width)
:param size: 合并的高宽(heigth, width)
:return: 合并后的图
"""
h, w = images[0].shape[1], images[0].shape[1]
if (images[0].shape[0] in (3,4)): # 彩色图像
c = images[0].shape[0]
img = np.zeros((h * size[0], w * size[1], c))
for idx, image in enumerate(images):
i = idx % size[1]
j = idx // size[1]
image = tensor2im(image)
img[j * h:j * h + h, i * w:i * w + w, :] = image
return img
elif images.shape[3]==1: # 灰度图像
img = np.zeros((h * size[0], w * size[1]))
for idx, image in enumerate(images):
i = idx % size[1]
j = idx // size[1]
image = tensor2im(image)
img[j * h:j * h + h, i * w:i * w + w] = image[:,:,0]
return img
else:
raise ValueError('in merge(images,size) images parameter ''must have dimensions: HxW or HxWx3 or HxWx4')
图片保存:torchvision.utils.save_image(img, imgPath)
https://blog.csdn.net/weixin_43723625/article/details/108159190