2

Using instructions from Link, I have retrained tensorflow inception for new categories.

But I noticed that subsequently if I want to classify a set of images, it goes through the images one by one and then classifies it. If the data set is huge, it takes a long time to finish the classification. E.g 45 minutes for 1000 images.

For the image classification I am using the LabelImage.py available online as below:

import tensorflow as tf
import sys

image_path = sys.argv[1] #Pass the test file as argument

# Read in the image_data
image_data = tf.gfile.FastGFile(image_path, 'rb').read()

# Loads label file (the retained labels from retraining) and strips off carriage return
label_lines = [line.rstrip() for line
                   in tf.gfile.GFile("/tf_files/tf_files/retrained_labels.txt")]

# Unpersists graph from file
with tf.gfile.FastGFile("/tf_files/tf_files/retrained_graph.pb", 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    _ = tf.import_graph_def(graph_def, name='')

with tf.Session() as sess:
    # Feed the image_data as input to the graph and get first prediction i.e. the most likely result
    softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')

    predictions = sess.run(softmax_tensor, \
             {'DecodeJpeg/contents:0': image_data})

    # Sort to show labels of first prediction in order of confidence
    top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]

    for node_id in top_k:
        human_string = label_lines[node_id]
        score = predictions[0][node_id]
        print('%s (score = %.5f)' % (human_string, score))

As you can notice, it processes one image by image.

Is it possible to speed up the process? As I was retraining the library it is not compiled for multiple GPUs. Is there any other ways to speed up the classification process?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
aandroidtest
  • 1,493
  • 8
  • 41
  • 68
  • 2
    Probably your network is trained on `[None, ...]` input, meaning it probably accepts arbitrary number of inputs. You should be able to do it by creating a stack of images and feeding them to layer. You would have to change `predictions[0].argsort()[...]` with `predictions.argsort(axis=1)[: , ...]`. But in short, this kinds of questions are not suitable for SO and are kind of offtopic. You should be able to come up with a solution with the comments above. – Imanol Luengo May 16 '17 at 07:01

2 Answers2

2

To refine the answer given by aandroidtest, you spend the majority of time for re-loading the model, etc. for each and every image, if you are using the above script.

Instead, you should load the model only once, and go over the images one by one inside your script.

Blackberry
  • 161
  • 10
0

The way is to load the library and subsequently process the images. Thus will save on the loading portion for each image.

Found the answer here.

aandroidtest
  • 1,493
  • 8
  • 41
  • 68