from PIL import Image
import matplotlib.pyplot as plt
img_b = Image.new("RGB",(32,32))
plt.imshow(img_b)
plt.show()
img_b_tensor = torch.from_numpy(np.asarray(img_b))
img_b_tensor.size()
Out[36]: torch.Size([32, 32, 3])
transs = transforms.ToTensor()
img_b_tensor_two = transs(img_b)
img_b_tensor_two.size()
Out[37]: torch.Size([3, 32, 32])
注意:
3. ToTensor()函数会制动将图像size变为chw;
4. 查看ToTensor()函数可知,其思路也是转换成numpy。只不过ToTensor中加入了除以255的步骤,对图像进行了归一化。
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
if accimage is not None and isinstance(pic, accimage.Image):
nppic = np.zeros([pic.channels, pic.height, pic.width], dtype=np.float32)
pic.copyto(nppic)
return torch.from_numpy(nppic)
# handle PIL Image
if pic.mode == 'I':
img = torch.from_numpy(np.array(pic, np.int32, copy=False))
elif pic.mode == 'I;16':
img = torch.from_numpy(np.array(pic, np.int16, copy=False))
elif pic.mode == 'F':
img = torch.from_numpy(np.array(pic, np.float32, copy=False))
elif pic.mode == '1':
img = 255 * torch.from_numpy(np.array(pic, np.uint8, copy=False))
else:
img = torch.ByteTensor(torch.ByteStorage.from_buffer(pic.tobytes()))
# PIL image mode: L, P, I, F, RGB, YCbCr, RGBA, CMYK
if pic.mode == 'YCbCr':
nchannel = 3
elif pic.mode == 'I;16':
nchannel = 1
else:
nchannel = len(pic.mode)
img = img.view(pic.size[1], pic.size[0], nchannel)
# put it from HWC to CHW format
# yikes, this transpose takes 80% of the loading time/CPU
img = img.transpose(0, 1).transpose(0, 2).contiguous()
if isinstance(img, torch.ByteTensor):
return img.float().div(255)
else:
return img
Torch.new()方法用于创建一个新的Tensor,该Tensor的type和device都和原有Tensor一致,且无内容:
tensor_two = img_b_tensor.new((5,32,32,3))
tensor_one = img_b_tensor.new(*(5,32,32,3))
tensor_two.size()
Out[43]: torch.Size([4])
tensor_one.size()
Out[44]: torch.Size([5, 32, 32, 3])
tensor_three = img_b_tensor.new()
tensor_three.size()
Out[46]: torch.Size([0])
这在使用的时候,需要加入size参数,注意*(参数)与(参数)导致的新建tensor的size区别
(参数):返回tensor维度是len((维度))
*(参数):返回tensor维度是(参数)
无size参数则为空
to_Tensor()和as_Tensor()的区别:
一个会设置/255,一个不会设置
一般用在自定义数据集中的def getitem()函数中,对image数据和labe数据进行处理,to_Tensor用于image,给image进行归一化, as_Tensor用于labe,保持原有标签
a = cv2.imread('图像路径')
b = torch.as_tensor(a.copy(), dtype=torch.int64)
b.max()
Out[32]: tensor(199)
c = F.to_tensor(a.copy())
c.max()
Out[34]: tensor(0.7804)
a.max()
Out[35]: 199