目录
1)torchvision.transforms.ToTensor
写这篇文章的初衷,就是同事跑过来问我,pytorch对图像的预处理为什么和caffe的预处理存在差距,我也是第一次注意到这个问题;
1)torchvision.transforms.ToTensor
直接贴代码:
第一段代码:
class ToTensor(object):
"""Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor.
Converts a PIL Image or numpy.ndarray (H x W x C) in the range
[0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0].
"""
def __call__(self, pic):
"""
Args:
pic (PIL Image or numpy.ndarray): Image to be converted to tensor.
Returns:
Tensor: Converted image.
"""
return F.to_tensor(pic)
def __repr__(self):
return self.__class__.__name__ + '()'
第二段代码:
def to_tensor(pic):
"""Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor.
See ``ToTensor`` for more details.
Args:
pic (PIL Image or numpy.ndarray): Image to be converted to tensor.
Returns:
Tensor: Converted image.
"""
if not(_is_pil_image(pic) or _is_numpy_image(pic)):
raise TypeError('pic should be PIL Image or ndarray. Got {}'.format(type(pic)))
if isinstance(pic, np.ndarray):
# handle numpy array
img = torch.from_numpy(pic.transpose((2, 0, 1)))
# backward compatibility
if isinstance(img, torch.ByteTensor):
return img.float().div(255)
else:
return img
在第二段代码中,可以看出图像进来以后,先进行通道转换,然后判断图像类型,若是uint8类型,就除以255;否则返回原图。
在使用opencv读图时,图像读入后的数据类型就是uint8,所以若是自己做实验,想看看transform后的效果,传入随意数据作为图片,记得使用方法如下:
im = np.ones([112, 112, 3])#图像是3*112*112大小,像素值为1,数据类型float64
im = np.array(im, dtype = np.uint8)#将数据float64转换成uint8
2)pytorch的图像预处理和caffe中的图像预处理
在常规使用中,pytorch的图像预处理:
test_transform = transforms.Compose(
[transforms.ToTensor(), # range [0, 255] -> [0.0,1.0]
transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))]) # range [0.0, 1.0] -> [-1.0,1.0]
im_tensor = test_transform(im).to(torch.device("cuda:0" if torch.cuda.is_available() else "cpu")).unsqueeze(0)
对应到caffe中的预处理操作:
scale = 0.0078125
mean_value = 127.5
tempimg = (tempimg - mean_value) * scale # done in imResample function wrapped by python
tempimg = tempimg.transpose(0, 3, 1, 2)
参考:
https://pytorch-cn.readthedocs.io/zh/latest/package_references/Tensor/