Visualize Tensor [1,64,112,112] using matplotlib - matplotlib

I have a output tensor after convolution of dimensions [1,64,112,112]. Is there any way I can visualize this using matplotlib only, keeping in mind that imshow() only accepts upto 3 channels.

Related

How can I tell tensorflow about the shape of a parse_tensor operation?

When I decode a tensor using tf.io.parse_tensor, the shape comes out as shape=<unknown>, which makse sense because tensorflow has no way to know the shape of the data that I will pass into this operation. However, if I know that the data I will provide has a certain shape, such as having exactly 2 dimensions, or having 3 rows and an unknown number of columns, how can I "tell" tensorflow this?
I need to do this because I am using functions like padded_batch later on that behave differently for different shapes (producing a ragged tensor versus a dense tensor).

TensorFlow: How to convert image to 1 dimensional tensor?

I've recently used the mnist data set to build a model for predicting hand-written integers. I now want to use my own images. My images are 28x28 pixels (like the mnist set), but when I try to convert them into a tensor using tf.image.decode_png, I get a a 3D tensor [28, 28, 4]. From reading around, I believe the extra 4 is RGB related. How can I convert to [28, 28] ignoring any colour scale (if that is the actual problem, maybe I'm missing something entirely).
Thank you!
As you correctly said, you got a 3D tensor because your image have 3 RGB channels. You can use something like tf.image.rgb_to_grayscale to get what you want.

How to resize elements in a ragged tensor in TensorFlow

I would like to resize every element in a ragged tensor. For example, if I have a ragged tensor of various sized images, how can I resize each one so that the dimensions are the same?
For example,
digits = tf.ragged.constant([np.zeros((1,60,60,1)), np.zeros((1,46,75,1))])
resize_lambda = lambda x: tf.image.resize(x, (60,60))
res = tf.ragged.map_flat_values(resize_lambda, digits)
I wish res to be a tensor of shape (2,60,60,1). How can I achieve this?
To clarify, this would be useful if within a custom layer we wanted to slice or crop sections from a single image to batch for inference in the next layer. In my case, I am attempting to combine two models (a model to segment an image into multiple cropped images of varying size and a classifier to predict each sub-image). I am also using tf 2.0
You should be able to do the following.
import tensorflow as tf
import numpy as np
digits = tf.ragged.constant([np.zeros((1,60,60,1)), np.zeros((1,46,75,1))])
res = tf.concat(
[tf.image.resize(digits[i].to_tensor(), (60,60)) for i in tf.range(digits.nrows())],
axis=0)

tensorflow reshaping convolutional filters for visualization

I have a 4D tensor of filter/kernel weights (of convolutional layer).
They're being passed to the subsequent operation with shape [5,5,3,32], 32 RGB 5x5 filters.
to collect their values for monitoring/analysis/storage using tf.summary.image I need to reshape this tensor into the shape [32,5,5,3], to then view/store each of the 32 filters as individual images of [5,5,3]
is this possible purely using tf.reshape()? or do I need to do multiple tensor transformations?
You need transpose instead of reshape, tf.transpose(t, (3,0,1,2)) should do what you need (suppose t is your tensor here), which shifts the last axis as the first axis.

How to sample image tensor in tensorflow

I have one image data tensor with shape of B*H*W*C and one position tensor with shape of B*H*W*2. The values in position tensor are pixel coordinates and I want to sample pixels in image data tensor according to these pixel coordinates. I have tried one way to do that like reshaping the tensor to one-dimension tensor, but I think it's really inconvenient. I wonder whether I could implement it by some more convenient approach like matrix mapping(e.g. remap in opencv).
I would first ask if you are sure the position matrix isn't redundant. If the position matrix entries simply correspond to the pixel locations in the image array, then for a given application however you access the position matrix could be used instead on the image data.
Perhaps as a starting point, running
sess = tf.Session()
np_img, np_pos = sess.run([tf_img, tf_pos], feed_dict={...})
will convert tensors to numpy arrays, which may make your operations easier.
Otherwise, a 1D-tensor isn't that bad and there are TF functions for reshaping easily.