Functional API Keras alternate solution for predict_classes() - python

Please refer here for my previous question for background information. As per answer suggested by Nassim Ben. I trained model of two-path architecture using functional API. Now I feel stuck as I need to predict the class of each pixel. here is the code for the same:
imgs = io.imread(test_img).astype('float').reshape(5,240,240)
plist = []
# create patches from an entire slice
for img in imgs[:-1]:
if np.max(img) != 0:
img /= np.max(img)
p = extract_patches_2d(img, (33,33))
plist.append(p)
patches = np.array(zip(np.array(plist[0]), np.array(plist[1]), np.array(plist[2]), np.array(plist[3])))
# predict classes of each pixel based on model
full_pred = self.model_comp.predict_classes(patches)
fp1 = full_pred.reshape(208,208)
But according to the github-link predict_classes() is unavailable. So my question is there any other alternative that I can try?

Nassim answer is great but I want to share with you the experience I have with similiar tasks:
Never use predict_proba Keras for version. Here you could find why.
Most of methods used for turning predictions into classes doesn't take into account your data statistics. In case of image segmentation - very often detecting an object is more important then detecting a background. For this reason I advise you to use a threshold obtained from a precision-recall curve for each class. In this case - you need to set a threshold value for which precision == recall (or it's as close as possible). After you obtain the thresholds - you need to write your custom function for a class prediction.

Indeed, predict_classes is not available for functionnal models as it might not make sense to use it in some cases.
However, a "one liner" solution exists to this :
y_classes = keras.utils.np_utils.probas_to_classes(self.model_comp.predict(patches))
This works in keras 1.2.2, not sure about keras 2.0, I couldn't find the function in the source code. But there is really nothing shady about this, your model outputs a vector of probabilities to belonging to each class. What the function does is just take the argmax and outputs the class coresponding to the highest probability.
I hope this helps.

Related

Sort trainable parameters in Keras

When I have some trainable parameters, say layer.trainable_weights. I want to sort those weights before feed into other operations, is it possible for me to do that? Can I use something like
import tensorflow as tf
p = layer.trainable_weights
p = tf.sort(p)
or are there any particular ways in Keras?
I'm new to Keras and TensorFlow. Really appreciate if someone can answer my questions, thanks in advance!
EDIT:
For "other operations", I want to feed those sorted trainable weights into another neural network, but that neural network is fixed (not trainable). So what I want to do is something like
import tensorflow as tf
p = model.layer[0].trainable_weights
p = tf.sort(p)
another_model.trainable = False
x = another_model(p)
# x is involved in the loss function of the original model
Hope this is clear, and hope anyone can help me!
(also, can I just use x=another_model.predict(p) instead of x=another_model(p) above?)
Of course, you can do it. Since you have not stated clearly what these downstreaming operations are, it is more difficult to answer your question.
If you only want to do something to monitor the training process, e.g. monitoring a custom metric to measure the cumulative distribution function of the weight matrix of interest, feel free to use tf.sort (you may use K.stop_gradient(W) before you sort it to further ensure no gradient is flowed back from any downstreaming process).
If you want to do things other than monitoring, e.g. computing a custom regularization term to play a part in training optimization, you should rethink how to implement this part both efficiently and effectively, but directly using tf.sort does not help! Why? Because this function is not implemented with back-propagation, (you might want to double check on this to see whether the latest version supports this feature)

Image Classifier with Tensorflow and Keras

I'm trying to get an Image Classifier to work. So far the model does seem to work but now every time I want to test an image to see if it is being recognized appropriately I have to do the whole training all over. I'm very new to this but I suppose there should be another way to only test the images without the training right?
I also have one further question concerning the code itself.
if result [0][0] >= 0.5:
prediction = "cogwheel"
else:
prediction = "not a cogwheel"
print(prediction)
I am trying to differentiate between images that represent cogwheels and those that don't. I understand that if the probability is > 0,5 it is a cogwheel and else it is not. But what does [0][0] here mean?
Thank you so much for your help!
Since you are a beginner, you may not know that you actually do not need to retrain the model in order to test :D. Your hunch is right, and we will see down below how you can do that.
You can save the weights of your model in a specific file format. In Keras, it is a file with the extension .hdf5.
from tensorflow.keras.models import load_model
##Do some stuff, train model
model.save(model_name)
##Do some stuff
loaded_model = load_model(model_name)
Please make sure that "model_name" includes .hdf5. For example, "my_model.hdf5".
Although it is not clear what you used to get the result (I assume result = model.predict(sample), where sample is a test sample), the first index corresponds to the class(label) and the second label corresponds to the probability of that specific class.
Test to see result[0][0] (probability of class 0), result[1][0] (probability of class 1).

How do "de-embed" words in TensorFlow

I am trying to follow the tutorial for Language Modeling on the TensorFlow site. I see it runs and the cost goes down and it is working great, but I do not see any way to actually get the predictions from the model. I tried following the instructions at this answer but the tensors returned from session.run are floating point values like 0.017842259, and the dictionary maps words to integers so that does not work.
How can I get the predicted word from a tensorflow model?
Edit: I found this explanation after searching around, I just am not sure what x and y would be in the context of this example. They don't seem to use the same conventions for this example as they do in the explanation.
The tensor you are mentioning is the loss, which defines how the network is training. For prediction, you need to access the tensor probabilities which contain the probabilities for the next word. If this was classification problem, you'd just do argmax to get the top probability. But, to also give lower probability words a chance of being generated,some kind of sampling is often used.
Edit: I assume the code you used is this. In that case, if you look at line 148 (logits) which can be converted into probabilities by simply applying the softmax function to it -- like shown in the pseudocode in tensorflow website. Hope this helps.
So after going through a bunch of other similar posts I figured this out. First, the code explained in the documentation is not the same as the code on the GitHub repository. The current code works by initializing models with data inside instead of passing data to the model as it goes along.
So basically to accomplish what I was trying to do, I reverted my code to commit 9274f5a (also do the same for reader.py). Then I followed the steps taken in this post to get the probabilities tensor in my run_epoch function. Additionally, I followed this answer to pass the vocabulary to my main function. From there, I inverted the dict using vocabulary = {v: k for k, v in vocabulary.items()} and passed it to run_epoch.
Finally, we can get the predicted word in run_epoch by running current_word = vocabulary[np.argmax(prob, 1)] where prob is the tensor returned from session.run()
Edit: Reverting the code as such should not be a permanent solution and I definitely recommend using #Prophecies answer above to get the probabilities tensor. However, if you want to get the word mapping, you will need to pass the vocabulary as I did here.

Neural network library for true-false based image recognition

I'm in need of an artificial neural network library (preferably in python) for one (simple) task. I want to train it so that it can tell wether a thing is in an image. I would train it by feeding it lots of pictures and telling it wether it contains the thing I'm looking for or not:
These images contain this thing, return True (or probability of it containing the thing)
These images do not contain this thing, return False (or probability of it containing the thing)
Does such a library already exist? I'm fairly new to ANNs and image recognition; although I understand how they both work in principle I find it quite hard to find an adequate library for this task, and even research in this field has proven to be kind of a frustration - any advice towards the right direction is greatly appreciated.
There are several good Neural Network approaches in Python, including TensorFlow, Caffe, Lasagne, and sknn (Sci-kit Neural Network). sknn provides an easy, out of the box solution, although in my opinion it is more difficult to customize and can be slow on large datasets.
One thing to consider is whether you want to use a CNN (Convolutional Neural Network) or a standard ANN. With an ANN you will mostly likely have to "unroll" your images into a vector whereas with a CNN, it expects the image to be a cube (if in color, a square otherwise).
Here is a good resource on CNNs in Python.
However, since you aren't really doing a multiclass image classification (for which CNNs are the current gold standard) and doing more of a single object recognition, you may consider a transformed image approach, such as one using the Histogram of Oriented Gradients (HOG).
In any case, the accuracy of a Neural Network approach, especially when using CNNs, is highly dependent on successful hyperparamter tuning. Unfortunately, there isn't yet any kind of general theory on what hyperparameter values (number and size of layers, learning rate, update rule, dropout percentage, batch size, etc.) are optimal in a given situation. So be prepared to have a nice Training, Validation, and Test set setup in order to fit a robust model.
I am unaware of any library which can do this for you. I use a lot of Caffe and can give you a solution till you find a single library which can do it for you.
I hope you know about ImageNet and that Caffe has a trained model based on ImageNet.
Here is the idea:
Define what the object is. Say object = "laptop".
Use Caffe's ImageNet trained model, change the code to display the required output you want (you mentioned TRUE or FALSE) when the object is in the output labels.
Here is a link to the ImageNet tutorial which I wrote.
Here is what you might try:
Take a look here. It is a stripped down version of the ImageNet program which I used in a prediction engine.
In line 80 you'll get the top-1 predicted output label. In line 86 you'll get the top-5 predicted labels. Write a line of code to check whether object is in the output_label and return TRUE or FALSE according to it.
I understand that you are looking for a specific library, I will look for it, but this is something I would try out in the beginning.

Alex-Net for feature extraction

I try to get reliable features for ImageNet to do further classification on them. To achieve that I would like to use tensorflow with Alexnet, for feature extraction. That means I would like to get the values from the last layer in the CNN. Could someone write a piece of Python code that explains how that works?
As jonrsharpe mentioned, that's not really stackoverflow's MO, but in practice, many people do choose to write code to help explain answers (because it's often easier).
So I'm going to assume that this was just miscommunication, and you really intended to ask one of the following two questions:
How does one grab the values of the last layer of Alexnet in TensorFlow?
How does feature extraction from the last layer of a deep convolutional network like alexnet work?
The answer to the first question is actually very easy. I'll use the cifar10 example code in TensorFlow (which is loosely based on AlexNet) as an example. The forward pass of the network is built in the inference function, which returns a variable representing the output of the softmax layer. To actually get predicted image labels, you just argmax the logits, like this: (I've left out some of the setup code, but if you're already running alexnet, you already have that working)
logits = cifar10.inference(images)
predictions = tf.argmax(logits,1)
# Actually run the computation
labels = session.run([predictions])
So grabbing just the last layer features is literally just as easy as asking for them. The only wrinkle is that, in this case, cifar10 doesn't natively expose them, so you need to modify the cifar10.inference function to return both:
# old code in cifar10.inference:
# return softmax_linear
# new code in cifar10.inference:
return softmax_linear, local4
And then modify all the calls to cifar10.inference, like the one we just showed:
logits,local4 = cifar10.inference(images)
predictions = tf.argmax(logits,1)
# Actually run the computation, this time asking for both answers
labels,last_layer = session.run([predictions, local4])
And that's it. last_layer contains the last layer for all of the inputs you gave the model.
As for the second question, that's a much deeper question, but I'm guessing that's why you want to work on it. I'd suggest starting by reading up on some of the papers published in this area. I'm not an expert here, but I do like Bolei Zhou's work. For instance, try looking at Figure 2 in "Learning Deep Features for Discriminative Localization". It's a localization paper, but it's using very similar techniques (and several of Bolei's papers use it).

Categories

Resources