from PIL import Image
import matplotlib.pyplot as plt
img_b = Image.new("RGB",(32,32))
plt.imshow(img_b)
plt.show()
PIL转tensor方法:
- 先转numpy,再用torch.from_numpy();
img_b_tensor = torch.from_numpy(np.asarray(img_b))
img_b_tensor.size()
Out[36]: torch.Size([32, 32, 3])
- 使用torchvision.transforms
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):
img = torch.from_numpy(pic.transpose((2, 0, 1)))
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)
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()))
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)
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_one = img_b_tensor.new(*(5,32,32))
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参数则为空
|