How to Plot and save a tensor as an image in Tensorflow

13,818

This question has been answered multiple times already (see for example my answer).

You first need to evaluate the tensor to get a numpy array using an open session. Once you get that you have to get rid of the extra dimension by doing something like np_array=np_array[:,:,0].

Then you can use matplotlib and do an imshow(np_array) by default it will aply a colormap to it and normalize it.

If you want a binary you can do as you said binary_array=(np_array>0.5).astype("int") then you can dow a final imshow(binary_array).

Share:
13,818

Related videos on Youtube

Konstantinos Monachopoulos
Author by

Konstantinos Monachopoulos

Intelligence. Beautifully engineered

Updated on June 04, 2022

Comments

  • Konstantinos Monachopoulos
    Konstantinos Monachopoulos almost 2 years

    I am inferencing a Fully Convolutional Network in Tensorflow using Python where the output of the network is a tensor that has dimensions (375, 1242, 1). This is the output image that holds the probability of every pixel belonging in a specific class or not (In my example the Road class - KITTY ). The tensor has this format Tensor("Slice:0", shape=(375, 1242, 1), dtype=float32). My question is how can I plot and save this tensor as an image and how can I convert it to binary doing something like this thres=0.5, image = image > thres?