In this ML lab, we will be using transfer learning, which means we are starting with a model that has been already trained on another problem. We will then be retraining it on a similar problem. Deep learning from scratch can take days, but transfer learning can be done in short order.
We are going to use the Inception v3 network. Inception v3 is a trained for the ImageNet Large Visual Recognition Challenge using the data from 2012, and it can differentiate between 1,000 different classes, like Dalmatian or dishwasher. We will use this same network, but retrain it to tell apart a small number of classes based on our own examples.
Things we need:-
- Linux desktop / VM
- Docker
- Tensorflow
- Image Dataset (large)
- How to install and run TensorFlow Docker images
- How to use Python to train an image classifier
- How to classify images with your trained classifier
Get Started
In this ML Lab, you will learn how to install and run TensorFlow on a single machine, and will train a simple classifier to classify images of flowers. First you will need the large set of images of which you have to classify. We have 5 types of directory which contains different flowers. You can Download it from here or
cd $HOME
mkdir tf_files
cd tf_files
curl -O http://download.tensorflow.org/example_images/flower_photos.tgz
tar xzf flower_photos.tgz
# On OS X, see what's in the folder:
open flower_photos
All directory contains images of flower and labels are important to classify. Add this directories into one main directory. In my case it is "Flower_photos".
Install Docker in Linux.
-
Log into your Ubuntu installation as a user with
sudoprivileges.
-
Update your
APTpackage index.
$ sudo apt-get update -
Install Docker.
$ sudo apt-get install docker-engine -
Start the
dockerdaemon.
$ sudo service docker start -
Verify that
dockeris installed correctly by running thehello-worldimage.
$ sudo docker run hello-world
Install Tensorflow in Docker.
- Start a terminal using Terminal.app
docker run -it gcr.io/tensorflow/tensorflow:latest-devel
Check to see if your TensorFlow works by invoking Python from the container's command line (you'll see "
root@xxxxxxx#"):2. Exit docker by "ctrl+d".
2.Start Docker with local files available
The TensorFlow Docker image doesn't contain the flower data, so we'll make it available by linking it in virtually.docker run -it -v $HOME/tf_files:/tf_files gcr.io/tensorflow/tensorflow:latest-devel
At the Docker prompt, you can see it's linked as a top level directory.
ls /tf_files/ # Should see: flower_photos flower_photos.tgz
Retrieving the training code
The Docker image you are using contains the latest GitHub TensorFlow tools, but not every last sample. You need to retrieve the full sample set this way.- cd /tensorflow
- git pull
(Re)training Inception
At this point, we have a trainer, we have data, so let's train! We will train the Inception v3 network.
As noted in the introduction, Inception is a huge image classification model with millions of parameters that can differentiate a large number of kinds of images. We're only training the final layer of that network, so training will end in a reasonable amount of time.
Start your image retraining with one big command:
# In Docker python tensorflow/examples/image_retraining/retrain.py \ --bottleneck_dir=/tf_files/bottlenecks \ --how_many_training_steps 500 \ --model_dir=/tf_files/inception \ --output_graph=/tf_files/retrained_graph.pb \ --output_labels=/tf_files/retrained_labels.txt \ --image_dir /tf_files/flower_photos
If you have plenty of time remove
how_many_training_steps 500
Using the Retrained Model
The retraining script will write out a version of the Inception v3 network with a final layer retrained to your categories totf_files/output_graph.pband a text file containing the labels totf_files/output_labels.txt.
These files are both in a format that the C++ and Python image classification examples can use, so you can start using your new model immediately.
Classifying an image
Here is Python that loads your new graph file and predicts with it.
label_image.py
import tensorflow as tf, sys
image_path = sys.argv[1]
# 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("/tf_files/retrained_labels.txt")]
# Unpersists graph from file
with tf.gfile.FastGFile("/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
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))
Or you can download it:-
curl -L https://goo.gl/tx3dqg > $HOME/tf_files/label_image.py
Restart your Docker image:
docker run -it -v $HOME/tf_files:/tf_files gcr.io/tensorflow/tensorflow:latest-devel
Now, run the Python file you created, first on a daisy:
# In Docker python /tf_files/label_image.py /tf_files/flower_photos/daisy/21652746_cc379e0eea_m.jpg
Thank you guys, In my next post I will show how to use tensorflow with Webcam images using opencv

very nice tuto
ReplyDeleteI'm stuck at the image retraining, when I try to run this command:
# python tensorflow/examples/image_retraining/retrain.py \
--bottleneck_dir=/tf_files/bottlenecks \
--how_many_training_steps 500 \
--model_dir=/tf_files/inception \
--output_graph=/tf_files/retrained_graph.pb \
--output_labels=/tf_files/retrained_labels.txt \
--image_dir /tf_files/flower_photos
I got an error: "--bottleneck_dir=/tf_files/bottlenecks: No such file or directory"
I have installed TensorFlow ,installation boot2docker windows 7 (64bits)
Send me your terminal screenshots so i can help you
DeleteSince /tf_files/.. is an absolute path you probably have to make sure where this tensorflow folder is located and change the code to the following after chaning the directory with cd :
Deletepython tf_files/tensorflow/examples/image_retraining/retrain.py --bottleneck_dir=tf_files/bottlenecks --how_many_training_steps 500 --model_dir=tf_files/inception --output_graph=tf_files/retrained_graph.pb --output_labels=tf_files/retrained_labels.txt --image_dir tf_files/images
Khemakhem, are you running this in a docker container? if so, you need to make sure you're mounting the directory "/tf_files/" correctly. What if you run just "docker run ls /tf_files"? Do you see what you expect, or do you get an error thst the path can't be found? check your "docker ... --volume" or "docker ... -v" flags to miunt the directory.
Deletethanks
ReplyDeleteHEy man, thanks so much for the work that you do. You've been really helpful and i am sure many people ave benefited from your work here. Great Job!
ReplyDeletethank you !!
DeleteHey Suraj! Are you still planning on posting a followup with a webcam? I'm interested in running tensorflow in a docker container on a macbook pro where the container has access to the webcam. thanks!
ReplyDeleteYes i am interested.
Delete