Install TensorFlow using 'tensorflow/tensorflow' Docker image
Start the Docker virtual machine
Create a virtual machine if not already
bash$ docker-machine create default
Start the virtual machine default
:
bash$ docker-machine start
Starting "default"...
(default) Check network to re-create if needed...
(default) Waiting for an IP...
Machine "default" was started.
Waiting for SSH to be available...
Detecting the provisioner...
Started machines may have new IP addresses. You may need to re-run the `docker-machine env` command.
>>> elapsed time 35s
bash$ docker-machine env
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/meng/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell:
# eval $(docker-machine env)
bash$ eval "$(docker-machine env default)"
Start 'tensorflow/tensorflow' Docker image
$ export MY_WORKSPACE_DIR='/Users/meng/workspace'
$ docker run -it \
--net=host \
--publish 6006:6006 \
--volume ${MY_WORKSPACE_DIR}/tensorflow_test:/tensorflow_test \
--workdir /tensorflow_test \
tensorflow/tensorflow:1.1.0 bash
root@30d79c2e5fc3:/tensorflow_test# pwd
/tensorflow_test
Start TensorBoard
root@30d79c2e5fc3:/tensorflow_test# tensorboard --logdir training_summaries &
[1] 12
root@30d79c2e5fc3:/tensorflow_test# Starting TensorBoard 47 at http://0.0.0.0:6006
(Press CTRL+C to quit)
root@30d79c2e5fc3:/tensorflow_test#
Open TensorBoard at
http://192.168.99.100:6006
in a Web browser, where the IP is the same as
DOCKER_HOST
in the output of docker-machine
env
.
Download training data
# curl -O http://download.tensorflow.org/example_images/flower_photos.tgz
# tar xzf flower_photos.tgz
# find flower_photos -type f | wc -l
3671
# cp -R flower_photos flower_photos_subset
# rm flower_photos_subset/*/[2-9]*
# find flower_photos_subset/ -type f | wc -l
1202
Download training script
# curl -O https://raw.githubusercontent.com/tensorflow/tensorflow/r1.1/tensorflow/examples/image_retraining/retrain.py
Training
# python retrain.py \
--bottleneck_dir=bottlenecks \
--how_many_training_steps=500 \
--model_dir=inception \
--summaries_dir=training_summaries/basic \
--output_graph=retrained_graph.pb \
--output_labels=retrained_labels.txt \
--image_dir=flower_photos_subset
Python script to classify new images of flowers
Create a new file label_image.py
:
import os, sys
import tensorflow as tf
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# change this as you see fit
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("retrained_labels.txt")]
# Unpersists graph from file
with tf.gfile.FastGFile("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))
Classify a new image:
# python label_image.py flower_photos/daisy/99306615_739eb94b9e_m.jpg
daisy (score = 0.47996)
dandelion (score = 0.24667)
sunflowers (score = 0.24508)
tulips (score = 0.02018)
roses (score = 0.00811)
Exit the Docker image and stop the Docker virtual machine
Ctrl+D to exit Docker image and then run
$ docker-machine stop
References
- https://codelabs.developers.google.com/codelabs/tensorflow-for-poets