目錄
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/