1

I have an MXNet NDArray that has image data in it. How do I render the NDArray as image in Jupyter Notebook?

type(data)
mxnet.ndarray.ndarray.NDArray

data.shape
(3, 759, 1012)
Indhu Bharathi
  • 1,437
  • 1
  • 13
  • 22

2 Answers2

3

Here is how to do it:

  1. Convert the MXNet NDArray to numpy array.
  2. Transpose the array to move channel to last dimension.
  3. Convert the array to uint8 (0 to 255).
  4. Render the array using matplotlib.

Here is the code:

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()

You can then render the array by calling render_as_image.

render_as_image(data)
Indhu Bharathi
  • 1,437
  • 1
  • 13
  • 22
1

To display an image or any other plot using Matplotlib first you need to convert MXNet NDArray to NumPy array.

For example, a is your MXNet NDArray to convert it into Numpy we will do this b = a.asnumpy(). Now you can plot/show this b using Matplotlib.

Hadi Mir
  • 4,497
  • 2
  • 29
  • 31