Visualize the edge thickness in tensorboard - tensorflow

I want the graph visualizer to label edges with tensor dimensions, and edge thickness to reflect total tensor size. Basically exactly the same as written in this doc:
When the serialized GraphDef includes tensor shapes, the graph
visualizer labels edges with tensor dimensions, and edge thickness
reflects total tensor size. To include tensor shapes in the GraphDef
pass the actual graph object (as in sess.graph) to the FileWriter when
serializing the graph. The images below show the CIFAR-10 model with
tensor shape information:
I pass the graph object to my summary.FileWriter:
writer = tf.summary.FileWriter(_dir_tensorboard, graph=sess.graph, flush_secs=300)
But I do not get any information about the thickness (all the lines are of the same size). I have just information about the shape of a tensor and information about the number of tensors.
How can I achive the same visual effect as the tutorial claims?

There was a regression impacting 1.11 and 1.12. It should be fixed with https://github.com/tensorflow/tensorboard/pull/1544. Sorry about it :( Please file a GitHub issue next time this happens.

Related

Keras/TensorFlow: What is the order of the weight tensor dimensions of a convolutional layer?

In channels_last format, the shape of the data tensor is (batch_size, height, width, channels) and the shape of the weight tensor is apparently (see reference 2) (rows, cols, input_depth, output_depth).
In channels_first format, the shape of the data tensor is (batch_size, channels, height, width) and the shape of the weight tensor is what?
I've looked high and low for the answer to that question. When I run my code and use model.get_weights() to get the weight and bias tensors, it appears that the format of the weight tensors is the same in channels_first as in channels_last. Yet, when I output the weight tensors to a file and read them back into my C/C++ code which is hand-crafted and doesn't use TensorFlow, it doesn't appear to be working. The results are numerically nonsensical. Maybe there is some other problem, but I would like to obtain a definitive answer to this question.
BTW, the reason I'm switching between channels_last and channels_first is that I need to be able to develop my code on a CPU machine and then run large training sessions on a GPU machine.
Any help is appreciated.
References:
Data tensor shape is explained here.
Weight tensor shape is partially explained here.
You can find the answer in source code of TF/keras keras/keras/layers/convolutional/base_conv.py, where data_format=channels_first or data_format=channels_last is working when forward calculation, but in weight definition, the kernel shape is kept as:
kernel_shape = self.kernel_size + (input_channel // self.groups, self.filters)
So, it makes you find the weight format is same in channels_first or channels_last by model.get_weights()。
In detail, convolution op is ultimately performed by conv1d, conv2d, conv3d, etc., in gen_nn_ops which defined and conducted by C/C++. Each of these operation need receive data_format to adjust inputs but not kernels (weights/filters).

Label shapes for model output shape (batch_size,1,1,n_classes) and SparseCategoricalCrossEntropy Loss

I'm using tensorflow/keras to build an image classification model. The labels are provided as integers, & so I'm using tf.keras.losses.SparseCategoricalCrossEntropy as documented at https://www.tensorflow.org/api_docs/python/tf/keras/losses/SparseCategoricalCrossentropy
The model has output shape (batch_size,1,1,n_classes).
What shape do the labels need to have?
It seems like the labels should have the shape (batch_size,1,1,n_classes).
However, from a few tests, it seems like (batch_size,) works too.
Often models have output shape (batch_size,n_classes) and when using SparseCategoricalCrossEntropy the label shape is (batch_size,).

Resnet50 image preprocessing

I am using https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/3 to extract image feature vectors. However, I'm confused when it comes to how to preprocess the images prior to passing them through the module.
Based on the related Github explanation, it's said that the following should be done:
image_path = "path/to/the/jpg/image"
image_string = tf.read_file(image_path)
image = tf.image.decode_jpeg(image_string, channels=3)
image = tf.image.convert_image_dtype(image, tf.float32)
# All other transformations (during training), in my case:
image = tf.random_crop(image, [224, 224, 3])
image = tf.image.random_flip_left_right(image)
# During testing:
image = tf.image.resize_image_with_crop_or_pad(image, 224, 224)
However, using the aforementioned transformation, the results I am getting suggest that something might be wrong. Moreover, the Resnet paper is saying that the images should be preprocessed by:
A 224×224 crop is randomly sampled from an image or its
horizontal flip, with the per-pixel mean subtracted...
which I can't quite understand what is means. Can someone point me in the right direction?
Looking forward to you answers!
The image modules on TensorFlow Hub all expect pixel values in range [0,1], like you get in your code snippet above. This makes it easy and safe to switch between modules.
Inside the module, the input values are scaled to the range that the network was trained for. The module https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/3 has been published from a TF-Slim checkpoint (see documentation), which uses yet another convention for normalizing inputs than He&al. -- but all this is taken care of.
To demystify the language in He&al.: it refers to the mean R, G and B values aggregated over all pixels of the dataset they studied, following the old wisdom that normalizing inputs to zero mean helps neural networks train better. However, later papers on image classification no longer expended this degree of attention to dataset-specific preprocessing.
The citation from the Resnet paper you mentioned is based on the following explanation from the Alexnet paper:
ImageNet consists of variable-resolution images, while our system requires a constant input dimensionality. Therefore, we down-sampled the images to a fixed resolution of256×256. Given a rectangular image, we first rescaled the image such that the shorter side was of length 256, and thencropped out the central 256×256patch from the resulting image. We did not pre-process the images in any other way, except for subtracting the mean activity over the training set from each pixel.
So in the Resnet paper, a similar process consist in taking a of 224x224 pixels part of the image (or of its horizontally flipped version) to ensure the network is given constant-sized images, and then center it by substracting the mean.

Slicing an image Tensor with bounding box Tensor

I have input image Tensor with shape [?, 448, 448, 3] and my network predicts a bounding box with shape [?, 4]. I want to slice my image tensor with the bounding box tensor and re-size the resulting tensor into a fixed size image for further processing.
Is this possible with tensorflow (or even better, natively in Keras)? I have read the relevant questions. E.g, this, and this, but they do not apply to when both the indexing tensor and the original tensor have an unknown first dimension.
Any help in the right direction is much appreciated !
The best way for you should be to use tf.image.crop_and_resize. From the documentation:
Extracts crops from the input image tensor and bilinearly resizes them
(possibly aspect ratio change) to a common output size specified by
crop_size. This is more general than the crop_to_bounding_box op which
extracts a fixed size slice from the input image and does not allow
resizing or aspect ratio change.
Returns a tensor with crops from the input image at positions defined
at the bounding box locations in boxes. The cropped boxes are all
resized (with bilinear interpolation) to a fixed size = [crop_height,
crop_width]. The result is a 4-D tensor [num_boxes, crop_height,
crop_width, depth].

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.