Using Tensorflow Estimator for LSTMs to Accept Images - python

I would like to use Tensorflow's Estimator to simplify training using LSTM Networks. Apparently, to use tensorflow's Estimator, one must define a model function like so:
def some_model_fn(features, labels, mode):
...
I have no problem using placeholders to get the inputs and labels. How do I turn images into the shape accepted by tensorflow lstms which is [batch_size, num_time_steps, num_features]?

I suggest using numpy to load images to a multi-dimensional array. This does take quite a bit of memory, depending on the image sizes and the number of time steps.

Related

How to implement multi-output losses using the same label in TensorFlow 2.x?

Using Keras in TensorFlow 2.x, I have a model that predicts something on multiple scales.
However, independent of the scale, the predicted content is the same and thus shares the same label that is fed from a tf.data.Dataset to model.fit().
As a consequence, the output of the model is a list of multiple tensors, while the label from the tf.data.Dataset is a single tensor. This results in the following error:
ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected.
How can I make the model aware of the repetition of the label?
Ideally, I don't want to replicate the label inside the tf.data.Dataset (unless there is a way to tile my label without consuming more memory).

how to get a hidden layer of tensorflow hub module

I want to use tensorflow hub to generate features for my images, but it seems that the 2048 features of Inception Module are not enough for my problem because my class images are very similar. so I decided to use the features of a hidden layer of this module, for example:
"module/InceptionV3/InceptionV3/Mixed_7c/concat:0"
so how can I write a function that gives me this ?*8*8*2048 features from my input images?
Please try
module = hub.Module(...) # As before.
outputs = module(dict(images=images),
signature="image_feature_vector",
as_dict=True)
print(outputs.items())
Besides the default output with the final feature vector output, you should see a bunch of intermediate feature maps, under keys starting with InceptionV3/ (or whichever other architecture you select). These are 4D tensors with shape [batch_size, feature_map_height, feature_map_width, num_features], so you might want to remove those middle dimensions by avg- or max-pooling over them before feeding this into classification.

What is the proper way of doing inference on a batch of images in Tensorflow for performance purposes

What I am trying to accomplish is doing inference in Tensorflow with a batch of images at a time instead of a single image. I am wondering what is the most appropriate way of handling processing of multiple images to speed up inference?
Doing inference on a single image is easily done and quite used in most tutorials, but what I have not seen yet is doing that in a batch-like style.
Here's what I am currently using at a high level:
pl = tf.placeholder(tf.float32)
...
sess.run([boxes, confs], feed_dict={pl: image})
I would appreciate any input on this.
Depending on how your model is designed, you can just feed an array of images to pl. The first dimension of your outputs then corresponds to the index of your image in the batch.
Many tensor ops have an implementation for multiple examples in a batch. There are some exceptions though, for example tf.image.decode_jpeg. In this case, you will have to rewrite your network, using tf.map_fn, for example.

Training and validating on images with different resolution in Keras

I'm using Keras to build a convolutional neural net to perform regression from microscopic images to 2D label data (for counting). I'm looking into training the network on smaller patches of the microscopic data (where the patches are the size of the receptive field). The problem is, the fit() method requires validation data to be of the same size as the input. Instead, I'm hoping to be able to validate on entire images (not patches) so that I can validate on my entire validation set and compare the results to other methods I've used so far.
One solution I found was to alternate between fit() and evaluate() each epoch. However, I was hoping to be able to observe these results using Tensorboard. Since evaluate() doesn't take in callbacks, this solution isn't ideal. Does anybody have a good way validating on full-resolution images while training on patches?
You could use fit generator instead of fit and provide a different generator for validation set. As long as the rest of your network is agnostic to the image size, (e.g, fully convolutional layers), you should be fine.
You need to make sure that your network input is of shape (None,None,3), which means your network accepts an input color image of arbitrary size.

How to visualize features classified by tensorflow?

I'm running the default classify_image code of the imagenet model. Is there any way to visualize the features that it has extracted? If I use 'pool_3:0', that gives me the feature vector. Is there any way to overlay this on top of my image to see which features it has picked as important?
Ross Girshick described one way to visualize what a pooling layer has learned: https://www.cs.berkeley.edu/~rbg/papers/r-cnn-cvpr.pdf
Essentially instead of visualizing features, you find a few images that a neuron fires most on. You repeat that for a few or all neurons from your feature vector. The algorithm needs lots of images to choose from of course, e.g. the test set.
I wrote my implementation of this idea for cifar10 model in Tensorflow today, which I want to share (uses OpenCV): https://gist.github.com/kukuruza/bb640cebefcc550f357c
You could use it if you manage to provide the images tensor for reading images by batches, and the pool_3:0 tensor.

Categories