text: TensorFlow is a an open source library for numerical computation, specializing in machine learning applications. In this codelab, you will learn how to install and run TensorFlow on a single machine, and will train a simple classifier to classify images of flowers.
  What are we going to be building? 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.
  What you will learn
  What you need
  There are a lot of options to install TensorFlow. This codelab will cover using both Docker and not using Docker. Native installations can be slightly faster, but for this codelab, Docker is quite usable, and there is a TensorFlow image already built for you to use.
  The Docker for Linux installation instructions are here.
  docker run hello-world
  /Applications/, and which looks like this:
  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#"):
  Before you go further, check to see if your installation is working.
  docker run -it gcr.io/tensorflow/tensorflow:latest-devel # You should see "root@xxxxxxx#" python
  Once you have a python prompt, run the following code:
  # python import tensorflow as tf hello = tf.constant('Hello, TensorFlow!') sess = tf.Session() print(sess.run(hello))
  If you see "Hello, Tensorflow!", it works!
  Before you start any training, you'll need a set of images to teach the network about the new classes you want to recognize. We've created an archive of creative-commons licensed flower photos to use initially.
  Exit your Docker instance (ctrl-D, or type
  exit) and return to your home directory.
  # ctrl-D if you're still in Docker and then: cd $HOME mkdir tf_files cd tf_files curl -O tar xzf flower_photos.tgz # On OS X, see what's in the folder: open flower_photos
  After downloading 218MB, you should now have a copy of the flower photos available in your home directory.
  Let's reduce the number of categories we'll learn, and only tell the difference between roses and daisies for now.
  You can delete them with any method; in a Unix shell, you could do this:
  # At your normal prompt, not inside Docker cd $HOME/tf_files/flower_photos rm -rf dandelion sunflowers tulips # On OS X, make sure the flowers are gone! open .
  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 toplevel directory.
  ls /tf_files/ # Should see: flower_photos flower_photos.tgz
  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
  Your sample code will now be in
  /tensorflow/tensorflow/examples/image_retraining/.
  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/flower_photos
  This script loads the pre-trained Inception v3 model, removes the old final layer, and trains a new one on the flower photos you've downloaded.
  ImageNet was not trained on any of these flower species, originally. above example iterates only 500 times. If you are training all five flower classes instead of two, you will very likely get worse results (i.e. lower accuracy). To get much better results, remove the parameter
  --how_many_training_steps to use the default 4,000 iterations.
  # In Docker python tensorflow/examples/image_retraining/retrain.py \ --bottleneck_dir=/tf_files/bottlenecks \ --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
  This section and the next are to be enjoyed while your classifier is training.
  The first phase analyzes all the images on disk and calculates the bottleneck values for each of them. What's a bottleneck?
  The Inception v3 model is made up of many layers stacked on top of each other (see a picture of it in this paper). These layers are pre-trained and are already very valuable at finding and summarizing information that will help classify most images. For this codelab, you are training only the last layer; the previous layers retain their already-trained state.
  A 'Bottleneck,' then, is an informal term we often use for the layer just before the final output layer that actually does the classification.
  Every image is reused multiple times during training. Calculating the layers behind the bottleneck for each image takes a significant amount of time. By caching the outputs of the lower layers on disk, they don't have to be repeatedly recalculated. By default, they're stored in the
  /tmp/bottleneck directory. If you rerun the script, they'll be reused, so you don't have to wait for this part again.
  You will see the bottlenecks train. Once they are complete, the actual training of the final layer of the network begins.
  You'll see a series of step outputs, each one showing training accuracy, validation accuracy, and the cross entropy:
  A true measure of the performance of the network is to measure its performance on a data set that is not in the training data. This performance is measured using the validation accuracy. If the training accuracy is high but the validation accuracy remains low, that means the network is overfitting, and the network is memorizing particular features in the training images that don't help it classify images more generally.
  The training's objective is to make the cross entropy as small as possible, so you can tell if the learning is working by keeping an eye on whether the loss keeps trending downwards, ignoring the short-term noise.
  By default, this script runs 4,000 training steps. Each step chooses 10 images at random from the training set, finds their bottlenecks from the cache, and feeds them into the final layer to get predictions. Those predictions are then compared against the actual labels to update the final layer's weights through a back-propagation will write out a version of the Inception v3 network with a final layer retrained to your categories to
  tf_files/output_graph.pb and a text file containing the labels to
  tf_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.
  Here is Python that loads your new graph file and predicts with it.
  import tensorflow as tf, sys # change this as you see fit image_path = sys.argv[1] # Read in the image_data image_data = tf.gfile.FastGFile(image_path, ))
  This is a little clumsy to cut-and-paste, so we've made a gist for you.
  Exit your Docker image, and go to
  $HOME/tf_files. Create a file called
  label_image.py and put the above code into it. You can use
  curl to do this for you.
  # ctrl-D to exit Docker and then: curl -L > $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
  And then on a rose:
  # In Docker python /tf_files/label_image.py /tf_files/flower_photos/roses/2414954629_3708a1a04d.jpg
  You might warnings; they not harmful. You will then see a list of flower labels, in most cases with the right it is a daisy, and low confidence for any other label.
  You can use
  label_image.py to choose any image file to classify, either from your downloaded collection, or new ones.
  There are several other parameters you can try adjusting to see if they help your results. The
  --learning_rate controls the magnitude of the updates to the final layer during training. If this rate is smaller, the learning will take longer, but it can help the overall precision. That's not always the case, though, so you need to experiment carefully to see what works for your case.
  The
  --train_batch_size parameter controls the number of images that the script examines during one training step. Because the learning rate is applied per batch, you'll need to reduce this value if you have larger batches to get the same overall effect.
  After you see the script working on the flower example images, you can start looking at teaching the network to recognize categories you care about instead.'s a list of other TensorFlow resources on the TensorFlow site, including a discussion group and whitepaper.
  If you make a trained model that you want to run in production, you should also check out TensorFlow Serving, an open source project that makes it easier to manage TensorFlow projects.
  This codelab is based on Pete Warden's TensorFlow for Poets blog post and this retraining tutorial.