TensorFlow image classification Tutorial

The development of deep learning algorithms and computer hardware performance, so that researchers and enterprises have made great progress in image recognition, voice recognition, recommendation engine and machine translation and other fields. Six years ago, the visual field of pattern recognition has made its first extraordinary results. Two years ago, Google has developed a brain team TensorFlow, and depth of learning smart used in various fields. Now, TensorFlow is beyond a lot of sophisticated tools for deep learning.

 

 

Use TensorFlow, you can obtain a complex and powerful capabilities, its strong cornerstone comes from the ease of use of TensorFlow.

 

In this two-part series, I'll show you how to quickly create a convolution neural network used in image recognition.

 

TensorFlow calculating step in parallel, the video may be analyzed frame by frame configuration, the time may be analyzed aware video extension.

 

This series of articles directly into the critical part, requires only basic knowledge of the command line and Python, you can create some home to make you excited about the project quickly. This article does not explore in depth the works TensorFlow, if you want to know more, I will provide significant additional reference materials. This series of libraries and tools are all free and open source software.

 

working principle

 

This tutorial is designed to be placed in a pre-trained category of pictures by running a command to identify which belong to the category of the specific image. Step as shown below:

 

 

Mark: Management training data. Such as flowers, daisies image into the next "daisy" directory, will be put under the Rose "Rose" directories, etc., will be as many different kinds of flowers in different categories in different directories. If we do not mark "fern", the classifier will never return "fern." It takes a lot of samples of each type, so this step is very important, and very time consuming. (As used herein, pre-labeled good data to improve efficiency)

 

Training: The mark good data (image) to the model. There is a tool to randomly grab a batch of images, using the model to guess each type of flower, test the accuracy of speculation, repeated until used up most of the training data. The last batch of images is not used for calculating the training model accuracy.

 

Category: Using the model in a new image. For example, input: IMG207.JPG, output: Daisy. This step is quick and simple, and a small measure of price.

 

Training and classification

 

The tutorial training for identifying different types of a flower image classifier. Depth learning requires a lot of training data, therefore, we need a lot of flowers image classified. Thankfully, another model in image collection and classification has done very well, so we use the classified data set with a script, it has a ready and fully trained image classification model, re-training model last several layers in order to achieve the results we want, this technique is called migration study.

 

We re-training model is Inception v3, originally published in the December 2015 paper "Rethinking computer vision Inception architecture" in doing discussed.

 

Until we have done this for about 20 minutes of training, Inception know how to identify daisies and tulips, this is the depth of learning, "learning" section.

 

installation

 

First, install Docker on the selected platform.

 

In many TensorFlow tutorial it is the first and only rely Docker (should indicate that this is a reasonable start). I also prefer this method of installation TensorFlow, because the need to install a series of dependencies, which can keep the host (laptop or desktop) relatively clean.

 

Bootstrap TensorFlow

 

After installing Docker, we are ready to start a container TensorFlow training and classification. Created on a 2GB hard disk free space of the working directory, create a subdirectory named local, and record the full path.

 

docker run -v /path/to/local:/notebooks/local --rm -it --name tensorflow 
tensorflow/tensorflow:nightly /bin/bash

 

Here is the command parsing:

 

-v / path / to / local: / notebooks / local the local directory just created a container mounted to the appropriate location. If you are using RHEL, Fedora, or other support systems SELinux, added: Z allows the container to access the directory.

 

--rm exit seasonal docker delete container

 

-it input and output connections, interact.

 

--name tensorflow name the container tensorflow, rather than sneaky_chowderhead or any random name Docker defined.

 

tensorflow / tensorflow: nightly nightly images from Docker Hub (public image repository) running tensorflow / tensorflow, rather than the latest image (the default is the recent establishment / image available). Using the latest nightly image instead of the image, because (when writing) latest contains a bug will destroy TensorBoard, which is a later data we need visualization tools.

 

/ Bin / bash specify run Bash shell, without running the default command.

 

Trainer

 

Run the following commands in a container, and to download training data integrity checks.

 

curl -O http://download.tensorflow.org/example_images/flower_photos.tgz
echo 'db6b71d5d3afff90302ee17fd1fefc11d57f243f  flower_photos.tgz' | sha1sum -c

 

If you do not see "flower_photos.tgz" information: the documentation is incorrect. If the appeal or curl sha1sum step fails, the training data manually download and unzip (SHA-1 checksum: db6b71d5d3afff90302ee17fd1fefc11d57f243f) to the local directory of the local host.

 

Now put the training data, then download scripts for retraining and integrity checks.

 

mv flower_photos.tgz local/
cd local
curl -O https://raw.githubusercontent.com/tensorflow/tensorflow/10cf65b48e1b2f16eaa82
6d2793cb67207a085d0/tensorflow/examples/image_retraining/retrain.py
echo 'a74361beb4f763dc2d0101cfe87b672ceae6e2f5  retrain.py' | sha1sum -c

 

Confirm retrain.py have the right content, you should see retrain.py: OK ..

 

Finally, start learning! Run retraining script.

 

python retrain.py --image_dir flower_photos --output_graph output_graph.pb 
--output_labels output_labels.txt

 

If you encounter the following error, ignore it:

 

TypeError: not all arguments converted during string formatting Logged from file
tf_logging.py, line 82.

 

As retrain.py operation, the training image will automatically batches training, testing, and validation data set.

 

On the output, we hope to have a higher "training accuracy" and "verify the accuracy" and lower "cross-entropy." For more explanation of these terms, please refer to "How to retrain on a new type of picture of the final layer of Inception." Training on current hardware for about 30 minutes.

 

Please note that the last line of the console output:

 

INFO:tensorflow:Final test accuracy = 89.1% (N=340)

 

This shows that we've got a model: a given image, 10 times out of nine can correctly guess which one of the five types of flowers. Due to different random numbers provide the training process, classification accuracy will be different.

 

classification

 

Add a small script, you can add a new image of flowers into the model, and output test results. This is the image classification.

 

The following script named classify.py stored in the local local directory:

 

import tensorflow as tf, sys
 
image_path = sys.argv[1]
graph_path = 'output_graph.pb'
labels_path = 'output_labels.txt'
 
# Read in the image_data
image_data = tf.gfile.FastGFile(image_path, 'rb').read()
 
# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line
    in tf.gfile.GFile(labels_path)]
 
# Unpersists graph from file
with tf.gfile.FastGFile(graph_path, 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    _ = tf.import_graph_def(graph_def, name='')
 
# Feed the image_data as input to the graph and get first prediction
with tf.Session() as sess:
    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))

 

To test your own images and saved in the local directory and name test.jpg, run (in the container) python classify.py test.jpg. Output:

 

sunflowers (score = 0.78311)
daisy (score = 0.20722)
dandelion (score = 0.00605)
tulips (score = 0.00289)
roses (score = 0.00073)

 

Data says it all! Model determines the image of the flower is the sunflower accuracy of 78.311 percent. The higher the value the higher the degree of matching. Please note that only one match type. Another multi-label classification different approach is required.

 

Classification chart script to load code has been destroyed, where I used graph_def = tf.GraphDef (), etc., as the chart loading code.

Zero-based knowledge and use of some code, we have built a very good flower image classifier, per second on an existing laptop can handle about five images.

END

Bi-mao wonderful classroom courses recommended:

1.Cloudera data analysis courses;

2.Spark Hadoop development and training;

3. Big Data machine learning recommendation systems;

4.Python data analysis and machine learning practical;

For more details, please look at our public number: Bi Mao data - Course Products - Bi-mao classroom

Register now interactive learning was massive currency, a large number of quality courses free delivery!

Guess you like

Origin blog.csdn.net/ShuYunBIGDATA/article/details/90691446