pages tagged TensorFlow http://meng6net.localhost/tag/TensorFlow/ <p><small>Copyright © 2005-2020 by <code>Meng Lu &lt;lumeng3@gmail.com&gt;</code></small></p> Meng Lu's home page ikiwiki Sat, 01 Jul 2017 21:23:24 +0000 Installing and configuring TensorFlow http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_TensorFlow/ http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_TensorFlow/ Docker TensorFlow computing configuration documentation draft installation note software Sat, 01 Jul 2017 21:23:24 +0000 2017-07-01T21:23:24Z <h2>Install TensorFlow using 'tensorflow/tensorflow' Docker image</h2> <h3>Start the Docker virtual machine</h3> <p>Create a virtual machine if not already</p> <pre><code>bash$ docker-machine create default </code></pre> <p>Start the virtual machine <code>default</code>:</p> <pre><code>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. &gt;&gt;&gt; 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)" </code></pre> <h3>Start 'tensorflow/tensorflow' Docker image</h3> <pre><code>$ 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 </code></pre> <h3>Start TensorBoard</h3> <pre> <code>root@30d79c2e5fc3:/tensorflow_test# tensorboard --logdir training_summaries &amp; [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# </code></pre> <p>Open TensorBoard at</p> <pre><code>http://192.168.99.100:6006 </code></pre> <p>in a Web browser, where the IP is the same as <code>DOCKER_HOST</code> in the output of <code>docker-machine env</code>.</p> <h3>Download training data</h3> <pre> <code># 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 </code></pre> <h3>Download training script</h3> <pre> <code># curl -O https://raw.githubusercontent.com/tensorflow/tensorflow/r1.1/tensorflow/examples/image_retraining/retrain.py </code></pre> <h3>Training</h3> <pre><code># 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 </code></pre> <h2>Python script to classify new images of flowers</h2> <p>Create a new file <code>label_image.py</code>:</p> <pre><code>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)) </code></pre> <p>Classify a new image:</p> <pre> <code># 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) </code></pre> <h3>Exit the Docker image and stop the Docker virtual machine</h3> <p>Ctrl+D to exit Docker image and then run</p> <pre><code>$ docker-machine stop </code></pre> <h2>References</h2> <ul> <li> https://codelabs.developers.google.com/codelabs/tensorflow-for-poets</li> </ul>