Saturday, June 17, 2017

How to use Inception v3

Low cost image classification by CNN, convolutional neural network

Overview

These days, CNN(convolutional neural network) is almost regarded as the best answer to classify images.
But it has many rules.
  • it needs huge amount of images
  • it takes much time to train
  • it needs slow and gradual steps to find good network model to attein the goal
  • it needs high spec environment to do try-and-error
Of course, there are free data sets and kinda sample network which works easily in short time. But at the case you practically make model to solve some practical problem personaly or oficilally, those restrictions can face with you.
The combination of Inception v3 model and fine tune can solve the point.



Detail

Inception v3 is one of the models for image classification. The network is huge and was trained by huge amount of data.
Fine tune is way to adjust network to the new other data set.

Inception v3

Inception v3 is one of the great models to classify images. It is distributed for free and you can try it so easily.
This model was trained by huge data, probably by using much time in high spec environment. The accuracy is awesome.

Fine tune

Even if Inception v3 is awesome model to classify images, that was not trained our own data and the output label is not our own, meaning it necessarily solves our problem.
Fine tune solves this problem by replace last layer with the new one. This system makes new model by existing model’s major part and new layer. The new layer can be trained by our own data.
The CNN’s convolutional layer is to extract characteristics from images and these have prevalent characteristics. So we can use those as those are and only about the last layer, to say precisely the last or last some layers after convolution and pooling layers, we add new layer adjusted to my own data.
This way can lessen the number of parameters which we need to train, meaning we can dramatically save time, necessary environment spec, and the amount of data.

Use

It’s easy to try inception v3’s fine tune.
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
./configure
bazel build -c opt --copt=-mavx tensorflow/examples/image_retraining:retrain
bazel-bin/tensorflow/examples/image_retraining/retrain --image_dir IMGDIR
The code above gets tensorflow and build the fine tune system, does fine tune. The ‘IMGDIR’ is your own image data directory.
The output is written out to /tmp as default.
import tensorflow as tf
import numpy as np

with tf.gfile.FastGFile('output_graph.pb', 'rb') as f:
    graph = tf.GraphDef()
    graph.ParseFromString(f.read())
    tf.import_graph_def(graph, name='')

with open('output_labels.txt', 'rb') as f:
    lines = f.readlines()
    labels = [str(w).replace("\n", "") for w in lines] 

with tf.Session() as sess:
        image_data = tf.gfile.FastGFile('pug.jpg', 'rb').read()
        softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
        prediction_outcome = sess.run(softmax_tensor,
                                    {'DecodeJpeg/contents:0': image_data})

        # delete empty dimension
        prediction_outcome = np.squeeze(prediction_outcome)

        label = {}

        for node_id in range(len(labels)):
            label_name = labels[node_id]
            # labels
            score = '{:.10f}'.format(prediction_outcome[node_id])
            label.update({label_name: score})
        result = {
            'scores': label
        }
        print(result)
output_graph.pb and output_labels.txt are model files. This time, I used pug image as example.
The variable ‘result’ has the outcome of the image predictions.