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 retrain 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 a model trained on the ImageNet Large Visual Recognition Challenge dataset. These models can differentiate between 1,000 different classes, like Dalmatian or dishwasher. You will have a choice of model architectures, so you can determine the right tradeoff between speed, size and accuracy for your problem.
We will use this same model, but retrain it to tell apart a small number of classes based on our own examples.
What you'll Learn
How to use Python and TensorFlow to train an image classifier
How to classify images with your trained classifier
All the code used in this codelab is contained in this git repository. Clone the repository and cd into it. This is where we will be working.
git clone https://github.com/googlecodelabs/tensorflow-for-poets-2
3. Download the training images
Before you start any training, you'll need a set of images to teach the model about the new classes you want to recognize. We've created an archive of creative-commons licensed flower photos to use initially. Download the photos (218 MB) by invoking the following two commands:
You should now have a copy of the flower photos. Confirm the contents of your working directory by issuing the following command:
The preceding command should display the following objects:
4. (Re)training the network
Configure your MobileNet
In this exercise, we will retrain a MobileNet. MobileNet is a a small efficient convolutional neural network. "Convolutional" just means that the same calculations are performed at each location in the image.
The MobileNet is configurable in two ways:
Input image resolution: 128,160,192, or 224px. Unsurprisingly, feeding in a higher resolution image takes more processing time, but results in better classification accuracy.
The relative size of the model as a fraction of the largest MobileNet: 1.0, 0.75, 0.50, or 0.25.
We will use 224 0.5 for this codelab.
With the recommended settings, it typically takes only a couple of minutes to retrain on a laptop. You will pass the settings inside Linux shell variables. Set those variables in your shell:
The graph below shows the first-choice-accuracies of these configurations (y-axis), vs the number of calculations required (x-axis), and the size of the model (circle area).
16 points are shown for MobileNet. For each of the 4 model sizes (circle area in the figure) there is one point for each image resolution setting. The 128px image size models are represented by the lower-left point in each set, while the 224px models are in the upper right.
This command will fail with the following error if you already have a tensorboard process running:
Investigate the retraining script
The retrain script is from the TensorFlow Hub repo, but it is not installed as part of the pip package. So for simplicity I've included it in the codelab repository. You can run the script using the python command. Take a minute to skim its "help".
python -m scripts.retrain -h
Run the training
As noted in the introduction, ImageNet models are networks with millions of parameters that can differentiate a large number of classes. We're only training the final layer of that network, so training will end in a reasonable amount of time.
Start your retraining with one big command (note the --summaries_dir option, sending training progress reports to the directory that tensorboard is monitoring) :
This script downloads the pre-trained model, adds a new final layer, and trains that layer on the flower photos you've downloaded.
You have likely encountered this bug. Increase your Docker cpu allocation to 4 or more, on OSX you can set this by selecting "Preferences..." from the Docker menu, the setting is on the "advanced" tab.
ImageNet does not include any of these flower species we're training on here. However, the kinds of information that make it possible for ImageNet to differentiate among 1,000 classes are also useful for distinguishing other objects. By using this pre-trained network, we are using that information as input to the final classification layer that distinguishes our flower classes.
Optional: I'm NOT in a hurry!
The first retraining command iterates only 500 times. You can very likely get improved results (i.e. higher accuracy) by training for longer. To get this improvement, remove the parameter --how_many_training_steps to use the default 4,000 iterations.
This section and the next provide background on how this retraining process works.
The first phase analyzes all the images on disk and calculates the bottleneck values for each of them. What's a bottleneck?
These ImageNet models are made up of many layers stacked on top of each other, a simplified picture of Inception V3 from TensorBoard, is shown above (all the details are available in this paper, with a complete picture on page 6). 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 (final_training_ops in the figure below). While all the previous layers retain their already-trained state.
In the above figure, the node labeled "softmax", on the left side, is the output layer of the original model. While all the nodes to the right of the "softmax" were added by the retraining script.
Note that this will only work after the retrain script finished generating the "bottleneck" files.
A bottleneck is an informal term we often use for the layer just before the final output layer that actually does the classification. "Bottleneck" is not used to imply that the layer is slowing down the network. We use the term bottleneck because near the output, the representation is much more compact than in the main body of the network.
Every image is reused multiple times during training. Calculating the layers behind the bottleneck for each image takes a significant amount of time. Since these lower layers of the network are not being modified their outputs can be cached and reused.
So the script is running the constant part of the network, everything below the node labeled Bottlene... above, and caching the results.
The command you ran saves these files to the bottlenecks/ directory. If you rerun the script, they'll be reused, so you don't have to wait for this part again.
5. Using the Retrained Model
The retraining script writes data to the following two files:
tf_files/retrained_graph.pb, which contains a version of the selected network with a final layer retrained on your categories.
tf_files/retrained_labels.txt, which is a text file containing labels.
Classifying an image
The codelab repo also contains a copy of tensorflow's label_image.py example, which you can use to test your network. Take a minute to read the help for this script:
python -m scripts.label_image -h
Now, let's run the script on this image of a daisy: