import torch
import torchvision.transforms as tf
import numpy as np
raw_input = np.ones((224,224,3),dtype=np.uint8)
tt_tf = tf.ToTensor()
# tt_tf = torch.as_tensor
output_ = tt_tf(raw_input)
官方解释:
Convert a
PIL Image
ornumpy.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] if the PIL Image belongs to one of the modes (L, LA, P, I, F, RGB, YCbCr, RGBA, CMYK, 1) or if the numpy.ndarray has dtype = np.uint8.In the other cases, tensors are returned without scaling.
通俗解释:
- 转换为torch.FloatTensor
- 将通道提前
- 可以将特定类型的图片或者uint8的ndarray归一化(除255),其他不做归一化
tip:如果想只是转换为tensor,不做其他改变,可以使用 torch.as_array()