paddle 转tensor的方法有两种
分别是 paddle.to_tensor() 和paddle.vision.transforms.to_tensor(pic, data_format='CHW')
1.paddle.to_tensor只是将其他数据类型转化为tensor类型,便于构建计算图。
2.
paddle.vision.transforms.to_tensor是将 PIL.Image 或 numpy.ndarray 转换成 paddle.Tensor。
将形状为 (H x W x C)的输入数据 PIL.Image 或 numpy.ndarray 转换为 (C x H x W)。 如果想保持形状不变,可以将参数 data_format 设置为 'HWC'。
同时,如果输入的 PIL.Image 的 mode 是 (L, LA, P, I, F, RGB, YCbCr, RGBA, CMYK, 1) 其中一种,或者输入的 numpy.ndarray 数据类型是 'uint8',那个会将输入数据从(0-255)的范围缩放到 (0-1)的范围。其他的情况,则保持输入不变。
注意:paddle.vision.transforms.to_tensor 将图像的像素值 从(0-255)的范围缩放到 (0-1)的范围。这就是区别。
看看它们对图像的输出
# paddle.vision.transforms.to_tensor转换的图像数据,(像素值在0-1之间,shape也变了)
Tensor(shape=[3, 48, 40], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[[[0.09411766, 0.09803922, 0.09803922, ..., 0.09019608,
0.08627451, 0.07843138],
[0.08627451, 0.09411766, 0.09019608, ..., 0.07450981,
0.08235294, 0.08235294],
[0.09019608, 0.09019608, 0.09019608, ..., 0.07450981,
0.08627451, 0.07843138],
...,
[0.08235294, 0.07450981, 0.07450981, ..., 0.08235294,
0.07450981, 0.07058824],
[0.07450981, 0.07450981, 0.06666667, ..., 0.07843138,
0.07450981, 0.07450981],
[0.07843138, 0.07450981, 0.07058824, ..., 0.07450981,
0.07843138, 0.07450981]],
[[0.09411766, 0.09803922, 0.09803922, ..., 0.09019608,
0.08627451, 0.07843138],
[0.08627451, 0.09411766, 0.09019608, ..., 0.07450981,
0.08235294, 0.08235294],
[0.09019608, 0.09019608, 0.09019608, ..., 0.07450981,
0.08627451, 0.07843138],
...,
[0.08235294, 0.07450981, 0.07450981, ..., 0.08235294,
0.07450981, 0.07058824],
[0.07450981, 0.07450981, 0.06666667, ..., 0.07843138,
0.07450981, 0.07450981],
[0.07843138, 0.07450981, 0.07058824, ..., 0.07450981,
0.07843138, 0.07450981]],
[[0.09411766, 0.09803922, 0.09803922, ..., 0.09019608,
0.08627451, 0.07843138],
[0.08627451, 0.09411766, 0.09019608, ..., 0.07450981,
0.08235294, 0.08235294],
[0.09019608, 0.09019608, 0.09019608, ..., 0.07450981,
0.08627451, 0.07843138],
...,
[0.08235294, 0.07450981, 0.07450981, ..., 0.08235294,
0.07450981, 0.07058824],
[0.07450981, 0.07450981, 0.06666667, ..., 0.07843138,
0.07450981, 0.07450981],
[0.07843138, 0.07450981, 0.07058824, ..., 0.07450981,
0.07843138, 0.07450981]]])
# paddle.to_tensor()不能直接处理图像数据,先将图像转array数据,然后转tensor
# # 这里的shape 与上面的不同
#paddle.to_tensor() 基本保留了原始数据,没有处理
Tensor(shape=[48, 40, 3], dtype=uint8, place=Place(gpu:0), stop_gradient=True,
[[[24, 24, 24],
[25, 25, 25],
[25, 25, 25],
...,
[23, 23, 23],
[22, 22, 22],
[20, 20, 20]],
[[22, 22, 22],
[24, 24, 24],
[23, 23, 23],
...,
[19, 19, 19],
[21, 21, 21],
[21, 21, 21]],
[[23, 23, 23],
[23, 23, 23],
[23, 23, 23],
...,
[19, 19, 19],
[22, 22, 22],
[20, 20, 20]],
...,
[[21, 21, 21],
[19, 19, 19],
[19, 19, 19],
...,
[21, 21, 21],
[19, 19, 19],
[18, 18, 18]],
[[19, 19, 19],
[19, 19, 19],
[17, 17, 17],
...,
[20, 20, 20],
[19, 19, 19],
[19, 19, 19]],
[[20, 20, 20],
[19, 19, 19],
[18, 18, 18],
...,
[19, 19, 19],
[20, 20, 20],
[19, 19, 19]]])
下面付一下,pytorch相关tensor的不同,
import cv2
import torchvision.transforms.functional as F
import torchvision.transforms as T
image = cv2.imread('00001.jpg')
print(type(image),image.shape)
img1 = F.to_tensor(image)
print(type(img1),img1.shape)
print(img1)
#
transform = T.ToTensor() #()很重要
img2 = transform(image)
print(type(img2),img2.shape)
print(img2)
print(img1.equal(img2)) #判断两个tensor是否相等
(227, 227, 3)
torch.Size([3, 227, 227])
tensor([[[0.5294, 0.5333, 0.5451, ..., 0.5451, 0.5451, 0.5451],
[0.5373, 0.5412, 0.5451, ..., 0.5451, 0.5451, 0.5451],
[0.5529, 0.5490, 0.5490, ..., 0.5451, 0.5451, 0.5451],
...,
[0.5490, 0.5529, 0.5529, ..., 0.5490, 0.5490, 0.5490],
[0.5490, 0.5529, 0.5529, ..., 0.5490, 0.5490, 0.5490],
[0.5490, 0.5529, 0.5529, ..., 0.5490, 0.5490, 0.5490]],
[[0.5098, 0.5137, 0.5255, ..., 0.5412, 0.5412, 0.5412],
[0.5176, 0.5216, 0.5255, ..., 0.5412, 0.5412, 0.5412],
[0.5333, 0.5294, 0.5294, ..., 0.5412, 0.5412, 0.5412],
...,
[0.5373, 0.5412, 0.5412, ..., 0.5451, 0.5451, 0.5451],
[0.5373, 0.5412, 0.5412, ..., 0.5451, 0.5451, 0.5451],
[0.5373, 0.5412, 0.5412, ..., 0.5451, 0.5451, 0.5451]]])
torch.Size([3, 227, 227])
tensor([[[0.5294, 0.5333, 0.5451, ..., 0.5451, 0.5451, 0.5451],
[0.5373, 0.5412, 0.5451, ..., 0.5451, 0.5451, 0.5451],
[0.5529, 0.5490, 0.5490, ..., 0.5451, 0.5451, 0.5451],
...,
[0.5490, 0.5529, 0.5529, ..., 0.5490, 0.5490, 0.5490],
[0.5490, 0.5529, 0.5529, ..., 0.5490, 0.5490, 0.5490],
[0.5490, 0.5529, 0.5529, ..., 0.5490, 0.5490, 0.5490]],
[[0.5098, 0.5137, 0.5255, ..., 0.5412, 0.5412, 0.5412],
[0.5176, 0.5216, 0.5255, ..., 0.5412, 0.5412, 0.5412],
[0.5333, 0.5294, 0.5294, ..., 0.5412, 0.5412, 0.5412],
...,
[0.5373, 0.5412, 0.5412, ..., 0.5451, 0.5451, 0.5451],
[0.5373, 0.5412, 0.5412, ..., 0.5451, 0.5451, 0.5451],
[0.5373, 0.5412, 0.5412, ..., 0.5451, 0.5451, 0.5451]]])
True
参考:
(57条消息) 图片转化为向量Tensor的方法_香博士的博客-CSDN博客_图片转tensor