How Can I Make The Inception-v3 Model Pre-trained From Imagenet (classify_image.py) In The Tensorflow Tutorial Importable As A Module?
Solution 1:
1) First question is about how to return the predicted values. Following code snippet does predition on the given image:
top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1]
for node_id in top_k:
human_string = node_lookup.id_to_string(node_id)
score = predictions[node_id]
print('%s (score = %.5f)' % (human_string, score))
Instead of print you can save the result in some data structure and return. By default, 5 top predictions will be returned, if you would like to change this behavior set proper value to --num_top_predictions
.
2) Regarding Model: There are two parts to it-
- You need to have quality dataset as Imagenet is.
- Assuming if you have such quality dataset, the infrastructure to train inception would require very powerful GPUs. Lot of time too.
but if you still want to train your system with your own dataset, I would say initially train with imagenet and later train the final layer(tensor name is 'final_result') with your own dataset. Please find this tutorial.
Solution 2:
In the end I managed to use the code from the SO article reffered to in the update in the original question. I modified the code with the additional im = 2*(im/255.0)-1.0
from the answer of said SO question, some line to fix PIL on my computer plus a function to convert classes to human readable labels (found on github), link to that file below. I made it a callable function that takes a list of images as input and outputs a list of labels and predict values. If you'd like to use it, this is what you have to to:
- Install the latest Tensorflow version (1.0 at the moment, which is needed).
git clone https://github.com/tensorflow/models/
where you want the models.- Put this checkpoint file from the SO question I referred to earlier (needs to be extracted, of course) in the directory of your project.
- Put this text file (the human readable labels) in the directory of your project.
Use this code from the SO question with some modifications from my side, put it in a .py file in your project:
import tensorflow as tf slim = tf.contrib.slim import PIL as pillow from PIL import Image #import Imagefrom inception_resnet_v2 import * import numpy as np withopen('imagenet1000_clsid_to_human.txt','r') as inf: imagenet_classes = eval(inf.read()) defget_human_readable(id): id = id - 1 label = imagenet_classes[id] return label checkpoint_file = './inception_resnet_v2_2016_08_30.ckpt'#Load the model sess = tf.Session() arg_scope = inception_resnet_v2_arg_scope() input_tensor = tf.placeholder(tf.float32, [None, 299, 299, 3]) with slim.arg_scope(arg_scope): logits, end_points = inception_resnet_v2(input_tensor, is_training=False) saver = tf.train.Saver() saver.restore(sess, checkpoint_file) defclassify_image(sample_images): classifications = [] for image in sample_images: im = Image.open(image).resize((299,299)) im = np.array(im) im = im.reshape(-1,299,299,3) im = 2*(im/255.0)-1.0 predict_values, logit_values = sess.run([end_points['Predictions'], logits], feed_dict={input_tensor: im}) #print (np.max(predict_values), np.max(logit_values))#print (np.argmax(predict_values), np.argmax(logit_values)) label = get_human_readable(np.argmax(predict_values)) predict_value = np.max(predict_values) classifications.append({"label":label, "predict_value":predict_value}) return classifications
Solution 3:
In my case just replace [-FLAGS.num_top_predictions:]
with [-5:]
Then replace other FLAG with directory and file the image sit on.
Post a Comment for "How Can I Make The Inception-v3 Model Pre-trained From Imagenet (classify_image.py) In The Tensorflow Tutorial Importable As A Module?"