当前位置: 首页 > news >正文

paddle,图像转tensor 两种方法的不同

paddle 转tensor的方法有两种

分别是  paddle.to_tensor() 和paddle.vision.transforms.to_tensor(picdata_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的不同,

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是否相等

 

<class 'numpy.ndarray'> (227, 227, 3)
<class 'torch.Tensor'> 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]]])
<class 'torch.Tensor'> 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

相关文章:

  • [MySQL]聚合函数与分组
  • Linux Shell - echo 命令输出格式
  • Nginx反向代理和负载均衡实战
  • huawei euleros - 本地安装和远程安装的区别
  • 自然语言处理学习笔记——文本预处理
  • Hadoop 3.x(HDFS)----【HDFS 的 API 操作】
  • [MySQL]流程控制函数加密与解密函数MySQL信息函数其他函数
  • 数据仓库-数据分层理论详解
  • 【JDBC实战】水果库存系统 [代码优化]
  • python+selenium 爬虫
  • JDBC.
  • 推荐系统实战
  • 【opencv-c++】cv::mixChannels通道混合
  • 机器学习笔记(四)
  • 1480_人月神话阅读笔记_开篇
  • 《测绘综合能力》——测绘航空摄影
  • AB变换
  • 【Shell编程】Shell中Bash变量-位置参数变量
  • 安卓环境配置
  • Python脚本,物联网云服务器端口监控