如何在Jupyter笔记本中将MXNet NDArray显示为图像?

如何在Jupyter笔记本中将MXNet NDArray显示为图像?,mxnet,Mxnet,我有一个MXNet数据库,里面有图像数据。如何在Jupyter笔记本中将NDArray渲染为图像 type(data) mxnet.ndarray.ndarray.NDArray data.shape (3, 759, 1012) 以下是如何做到这一点: 将MXNet NDArray转换为numpy阵列 转置阵列以将通道移动到最后一个维度 将数组转换为uint8(0到255) 使用matplotlib渲染阵列 代码如下: import mxnet as mx import numpy as

我有一个MXNet数据库,里面有图像数据。如何在Jupyter笔记本中将NDArray渲染为图像

type(data)
mxnet.ndarray.ndarray.NDArray

data.shape
(3, 759, 1012)
以下是如何做到这一点:

  • 将MXNet NDArray转换为numpy阵列
  • 转置阵列以将通道移动到最后一个维度
  • 将数组转换为uint8(0到255)
  • 使用matplotlib渲染阵列
  • 代码如下:

    import mxnet as mx
    import numpy as np
    from matplotlib import pyplot as plt
    
    def render_as_image(a):
        img = a.asnumpy() # convert to numpy array
        img = img.transpose((1, 2, 0))  # Move channel to the last dimension
        img = img.astype(np.uint8)  # use uint8 (0-255)
    
        plt.imshow(img)
        plt.show()
    
    然后,可以通过调用
    render\u as\u image
    来渲染数组

    render_as_image(data)
    
    以下是如何做到这一点:

  • 将MXNet NDArray转换为numpy阵列
  • 转置阵列以将通道移动到最后一个维度
  • 将数组转换为uint8(0到255)
  • 使用matplotlib渲染阵列
  • 代码如下:

    import mxnet as mx
    import numpy as np
    from matplotlib import pyplot as plt
    
    def render_as_image(a):
        img = a.asnumpy() # convert to numpy array
        img = img.transpose((1, 2, 0))  # Move channel to the last dimension
        img = img.astype(np.uint8)  # use uint8 (0-255)
    
        plt.imshow(img)
        plt.show()
    
    然后,可以通过调用
    render\u as\u image
    来渲染数组

    render_as_image(data)
    

    要使用Matplotlib显示图像或任何其他绘图,首先需要将
    MXNet-NDArray
    转换为
    NumPy
    数组


    例如,
    a
    是将其转换为Numpy的MXNet数据阵列,我们将执行此操作
    b=a.asnumpy()
    。现在,您可以使用Matplotlib打印/显示此
    b

    要使用Matplotlib显示图像或任何其他打印,首先需要将
    MXNet-NDArray
    转换为
    NumPy
    数组

    例如,
    a
    是将其转换为Numpy的MXNet数据阵列,我们将执行此操作
    b=a.asnumpy()
    。现在,您可以使用Matplotlib打印/显示此
    b