Using machine learning to identify old logos
Some tasks are easy to automate, such as checking when your site was last modified. Or how old a file is. But how do you check whether the image contains an old logo?
In the past this is a task I would’ve done manually, to ensure every old image is found. But with machine learning it’s possible to train a neural network to identify two different scenarios and then have it detect which image it is closest to.
To accomplish this I started off using a Tensorflow code lab as a starting point:
https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#0
- Install python dependencies. Create a file /requirements.txt and add the list of libraries required:
absl-py==0.7.0
astor==0.7.1
backports.weakref==1.0.post1
enum34==1.1.6
funcsigs==1.0.2
futures==3.2.0
gast==0.2.2
grpcio==1.19.0
h5py==2.9.0
Keras-Applications==1.0.7
Keras-Preprocessing==1.0.9
Markdown==3.0.1
mock==2.0.0
numpy==1.16.2
pbr==5.1.3
protobuf==3.7.0
six==1.12.0
tensorboard==1.13.0
tensorflow==1.13.1
tensorflow-estimator==1.13.0
termcolor==1.1.0
Werkzeug==0.14.1
2. Create a python virtual environment and install the dependencies using:
virtualenv env
source env/bin/activate
pip install -r requirements.txt
3. Prepare your data. This is arguably the biggest task you’ll have, to get enough source files to give the script access to a variety of examples. Create two folders containing old and new examples of the logo.
/src/images/new/
/src/images/old/
4. Next create your training script. This will navigate to each image subdirectory and use the name as a classifier group, in this example two groups ‘old’ and ‘new’. I have prepared a script you can use at /src/retrain.py
5. Run the training script using the following command to train the image model:
python -m src.retrain \
--bottleneck_dir=dist/bottlenecks \
--how_many_training_steps=500 \
--model_dir=dist/models \
--summaries_dir=dist/summaries \
--output_graph=dist/retrained_graph.pb \
--output_labels=dist/retrained_labels.txt \
--architecture="mobilenet_0.50_224" \
--image_dir=src/images \
--learning_rate=0.005
6. Create the classifier script. You can use this script at /src/classify.py
7.Prepare some test images which are not part of the training data set. These will allow you to test the confidence scores of the results on unseen examples:
/src/images_test
8. Run the classifier script using the command:
python -m src.classify \
--graph=dist/retrained_graph.pb \
--labels=dist/retrained_labels.txt \
--image=src/images_test/o1.jpg
Check your results. In my case I get the following:
Hope that helps you get started with image classifiers!
Check out the full source code and example at:
https://github.com/kmturley/tensorflow-detect-logo