paddle 图像转tensor后,再转置回来,重新显示

我命令行一步一步的做的

进入conda 虚拟环境,首先添加依赖库

>>> import paddle
>>> import numpy as np
>>> from paddle.vision.transforms import Transpose
>>> from PIL import Image
>>> from matplotlib import pyplot as plt
>>> from paddle.vision.transforms import functional as F
>>> import os

然后加载图像:

>>> path1 = r'E:\.......\xxxxx\xxxxx\练xx据\1\1a0000000.tif'
>>> image = Image.open(path1)

然后将图像转为 tensor

>>> img1 = F.to_tensor(image)
# 下面自动启动GPU
W1005 20:18:27.351920  1416 gpu_context.cc:278] Please NOTE: device: 0, GPU Compute Capability: 8.6, Driver API Version: 11.6, Runtime API Version: 11.2
W1005 20:18:27.367619  1416 gpu_context.cc:306] device: 0, cuDNN Version: 8.2.

看一下 转化tensor后的图像参数

>>> img1
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]]])
>>>

Tensor(shape=[3, 48, 40], dtype=float32, place=Place(gpu:0), stop_gradient=True,

正常的图像应该是[48, 40,3],很显然进行tensor的时候 自动转置了。如果你想重新显示图像,图tensor 的shape=[3, 48, 40]转置为[48, 40,3],然后转为矩阵,就可以显示图像了。

转置tensor,需要用到transpose函数

>>> transform = Transpose()
# 转置一下,
>>> img1 = transform(img1)
# 看看形状变化
>>> print(img1.shape)
[40, 3, 48]   #  这个形状不是我们需要的,需要再继续转置一下、
>>> img1 = transform(img1)
# 看看第二次转置的情况
>>> print(img1.shape)
[48, 40, 3]   # 这个是自己想要的

下面 就将转置tensor  转换为矩阵

>>> img2=img1.numpy()
# 看看矩阵的内容
>>> img2
array([[[0.09411766, 0.09411766, 0.09411766],
        [0.09803922, 0.09803922, 0.09803922],
        [0.09803922, 0.09803922, 0.09803922],
        ...,
        [0.09019608, 0.09019608, 0.09019608],
        [0.08627451, 0.08627451, 0.08627451],
        [0.07843138, 0.07843138, 0.07843138]],

       [[0.08627451, 0.08627451, 0.08627451],
        [0.09411766, 0.09411766, 0.09411766],
        [0.09019608, 0.09019608, 0.09019608],
        ...,
        [0.07450981, 0.07450981, 0.07450981],
        [0.08235294, 0.08235294, 0.08235294],
        [0.08235294, 0.08235294, 0.08235294]],

       [[0.09019608, 0.09019608, 0.09019608],
        [0.09019608, 0.09019608, 0.09019608],
        [0.09019608, 0.09019608, 0.09019608],
        ...,
        [0.07450981, 0.07450981, 0.07450981],
        [0.08627451, 0.08627451, 0.08627451],
        [0.07843138, 0.07843138, 0.07843138]],

       ...,

       [[0.08235294, 0.08235294, 0.08235294],
        [0.07450981, 0.07450981, 0.07450981],
        [0.07450981, 0.07450981, 0.07450981],
        ...,
        [0.08235294, 0.08235294, 0.08235294],
        [0.07450981, 0.07450981, 0.07450981],
        [0.07058824, 0.07058824, 0.07058824]],

       [[0.07450981, 0.07450981, 0.07450981],
        [0.07450981, 0.07450981, 0.07450981],
        [0.06666667, 0.06666667, 0.06666667],
        ...,
        [0.07843138, 0.07843138, 0.07843138],
        [0.07450981, 0.07450981, 0.07450981],
        [0.07450981, 0.07450981, 0.07450981]],

       [[0.07843138, 0.07843138, 0.07843138],
        [0.07450981, 0.07450981, 0.07450981],
        [0.07058824, 0.07058824, 0.07058824],
        ...,
        [0.07450981, 0.07450981, 0.07450981],
        [0.07843138, 0.07843138, 0.07843138],
        [0.07450981, 0.07450981, 0.07450981]]], dtype=float32)

OK,下面可以图像显示矩阵了

>>> plt.figure()
<Figure size 640x480 with 0 Axes>
>>> plt.imshow(img2)
<matplotlib.image.AxesImage object at 0x000002509504D3D0>
>>> plt.show()

图像经历了加载,读取,转换成tesor ,然后转置,又转换成矩阵,最后显示。

如果对你有用,欢迎点赞收藏、加关注

 参考程序,这个程序缺陷不少我们需要的依赖库,仅仅只能借鉴一下。

import numpy as np
from PIL import Image
from paddle.vision.transforms import functional as F
 
img = (np.random.rand(256, 300, 3) * 255.).astype('uint8')
img = Image.fromarray(img)
padded_img = F.pad(img, padding=1)
print(padded_img.size)#(302, 258)

transpose函数主要是将图片矩阵数据进行转置,如上述代码,将(300,320,3)的矩阵转置到(3,300,320)大小

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值