Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 992
Konstantinos Monachopoulos Avatar asked Oct 19 '25 10:10

Konstantinos Monachopoulos


1 Answers

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

like image 173
jeandut Avatar answered Oct 21 '25 23:10

jeandut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!