Skip to content

Repository files navigation

TensorFlow For Poets

Retraining one of Google's CNN image classification models to new categories using Transfer Learning.
This can be an much faster (in a few minutes) than training from scratch (Inception V3 took Google, 2 weeks).

Based on Codelab (updated May 30 2018)

Contents

Install Tensorflow
Download Flowers
Speedup Training
(Re)Training
Classifying an Image
Training on Your Own Categories
Benchmarks
Compiled
Build from Source

Install Tensorflow:

sudo pip install -U pip sudo pip install tensorflow 

Download Flowers:

mkdir tf_files
cd tf_files
curl -O http://download.tensorflow.org/example_images/flower_photos.tgz
tar xzf flower_photos.tgz

Speedup Training

reduce the number of images by ~70%

ls flower_photos/roses | wc -l
rm flower_photos/*/[3-9]*

also only use 2 flowers e.g. roses and sunflowers

rm flower_photos/daisy/ flower_photos/dandelion/ flower_photos/tulips/ -r
  • Precompiled bottlenecks also included as tgz.
  • Floydhubrun.shIntel Xeon® · 2 Cores 8 GB RAM
  • colabK80 12GBcolab.sh

colab code:

!wget https://raw.githubusercontent.com/EN10/TensorFlowForPoets/master/colab.sh
!bash colab.sh

(Re)Training

Download retrain file:

wget https://raw.githubusercontent.com/EN10/TensorFlow-For-Poets/master/retrain.py

MobileNet 0.5: Faster (< 2m) Less Accurate (Top-1 64%)

python retrain.py \
--bottleneck_dir=tf_files/bottlenecks \
--how_many_training_steps=500 \
--model_dir=tf_files/models/ \
--summaries_dir=tf_files/training_summaries/mobilenet_0.50_224 \
--output_graph=tf_files/retrained_graph.pb \
--output_labels=tf_files/retrained_labels.txt \
--architecture=mobilenet_0.50_224 \
--image_dir=tf_files/flower_photos

mobilenet can be configured from 0.50 to 1.0, more accuracy but slower.

Inception V3: Slower (5 - 20m) More Accurate (Top-1 78%)

python retrain.py \
--bottleneck_dir=tf_files/bottlenecks \
--how_many_training_steps=500 \
--model_dir=tf_files/models/inception_v3 \
--output_graph=tf_files/retrained_graph.pb \
--output_labels=tf_files/retrained_labels.txt \
--image_dir=tf_files/flower_photos

Classifying an Image

Download label_image file:

wget https://raw.githubusercontent.com/EN10/TensorFlow-For-Poets/master/label_image.py

Predict image label:

python label_image.py \
--graph=tf_files/retrained_graph.pb \
--image=image.jpg

Training on Your Own Categories

retrain.py uses --image_dir as the root folder for training.
Each sub-folder is named after one of your categories and contains only images from that category.

For training to work well, you should gather at least a hundred photos of each kind of object you want to recognize.

Tool to download images for training:

Benchmarks:

rm [3-9]* & 2 Flowers: roses and sunflowers (aka slim)
282 roses & 304 sunfowers i.e. 586 bottlenecks

ModelPAASRAMOSTensorflowCPUPerformanceNotes
MobileNet 0.5cs50.io512MB14.04.51.4.1Not Compiled1m20s
MobileNet 0.5cs50.io1024MB14.04.51.4.1Not Compiled1m20s
MobileNet 0.5colab1024MB17.101.6.0rc1Compiled43s
MobileNet 0.5colab12GB17.101.6.0rc1K8024s
Inception v3cs50.io512MB14.04.51.4.1Not Compiled15m
Inception v3cs50.io512MB14.04.51.2.1Compiled6m30s
Inception v3floydhub8GB16.04.21.1.0Compiled4m30swith install
Inception v3floydhub8GB16.04.21.1.0Compiled4mwith datasets
Inception v3codenvy.io3072MB16.041.2.1Not Compiled6m45s
Inception v3codenvy.io3072MB16.041.2.1Compiled3m20s
Inception v3codenvy.io2048MB16.041.2.1Compiled3m20s
Inception v3colab1024MB17.101.6.0rc1Compiled3m38s
Inception v3colab12GB17.101.6.0rc1K801m23s

Performance = Bottlenecks + Training where Training ~ 1 Min
CPU: TensorFlow binary compiled to use: SSE4.1 SSE4.2 AVX
Precompiled Bottlenecks in /inception_bottlenecks (slim see above)

Compiled

Precompiled with FMA, AVX, AVX2, SSE4.1, SSE4.2:

wget https://github.com/EN10/BuildTF/raw/771df48529285c69ef760327121e996750b3916e/tensorflow-1.4.0-cp27-none-linux_x86_64.whl
sudo pip install --ignore-installed --upgrade tensorflow-1.4.0-cp27-none-linux_x86_64.whl

OS: Ubuntu 14.04.5 LTS - GCC version 4.8.4 - Python: 2.7.6 - Tensorflow 1.2.1

Build from Source

Build:

bazel build --config=opt --copt=-msse4.2 --copt=-mavx2 --copt=-mfma //tensorflow/tools/pip_package:build_pip_package

Releases

Packages

Contributors

Languages