Tensorflow for poets

This notebook steps through the Tensorflow for poets tutorial.

First clone the code repository.

In [ ]:
!git clone https://github.com/googlecodelabs/tensorflow-for-poets-2

Move into the new directory.

In [ ]:
cd tensorflow-for-poets-2

Download the flowers dataset.

In [ ]:
!curl http://download.tensorflow.org/example_images/flower_photos.tgz | tar xz -C tf_files
In [ ]:
ls tf_files/flower_photos

Run this in a terminal, Jupyter doesn't allow background processes...

I'm assuming this won't be possible on Binder?

tensorboard --logdir tf_files/training_summaries &

Train the model

In [ ]:
%%bash
IMAGE_SIZE=224
ARCHITECTURE="mobilenet_0.50_${IMAGE_SIZE}"

python -m scripts.retrain \
  --bottleneck_dir=tf_files/bottlenecks \
  --how_many_training_steps=500 \
  --model_dir=tf_files/models/ \
  --summaries_dir=tf_files/training_summaries/"${ARCHITECTURE}" \
  --output_graph=tf_files/retrained_graph.pb \
  --output_labels=tf_files/retrained_labels.txt \
  --architecture="${ARCHITECTURE}" \
  --image_dir=tf_files/flower_photos

Test the trained model

In [ ]:
%%bash
python -m scripts.label_image \
    --graph=tf_files/retrained_graph.pb  \
    --image=tf_files/flower_photos/daisy/21652746_cc379e0eea_m.jpg

More testing

This bit isn't in the tutorial. I just thought it would be good to do some random testing...

In [ ]:
# Make a list of all the flower images
import os
import random
from IPython.display import display, HTML
flowers = []
flower_dir = 'tf_files/flower_photos/'
for img_dir in [d for d in os.listdir(flower_dir) if os.path.isdir(os.path.join(flower_dir, d))]:
    for img in [i for i in os.listdir(os.path.join(flower_dir, img_dir)) if i[-4:] == '.jpg']:
        flowers.append(os.path.join(flower_dir, img_dir, img))    
In [ ]:
# Choose one flower at random
flower = random.sample(flowers, 1)[0]
display(HTML('<img src="tensorflow-for-poets-2/{0}"><br>{0}'.format(flower)))
In [ ]:
!python -m scripts.label_image --graph=tf_files/retrained_graph.pb --image=$flower
In [ ]: