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()
>>> plt.imshow(img2) >>> plt.show()

paddle 图像转tensor后,再转置回来,重新显示_第1张图片

图像经历了加载,读取,转换成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)大小

你可能感兴趣的:(paddlepaddle,paddle,python,numpy)