The goal of this project is to build a Tensorflow Lite Object Detection model designed to detect the Cloudera and Hortonworks logo. This project will include the scripts, photos, and instructions to build the models from scatch, and this project will also include the resulting models for quick testing.
This Github repo is designed to be optmized for Cloudera Data Science Workbench (CDSW), but it's not required.
Language: Python
Requirements:
- Python 3.6
- Tensorflow 1.13
- CDSW 1.5 (For quick build)
Author: Ian R Brooks
Follow: LinkedIn - Ian Brooks PhD
- Object Detection Tutorial Link
- Another Object Detection Tutorial
- Yet Another Object Detection Tutorial
- Logo Object Detection Article
- Logo Object Detection Article Using SSD
Image Augmentation For Object Detection:
- Great Read on Data Augmentation for Object Detection
- Image Augmentation Examples in Python
- Image Augmentation Using Keras
Converting Tensorflow Models to Tensorflow Lite Models:
- Exporting Tained Model for Inference
- Convert Tensorflow Model for TPU
- Convert Tensorflow Model to TFLite
- Tensorflow Object Detection API
- Online Edge TPU Compiler
- LabelImg - Free Object Labeling Tool
- Data Augmentation for Object Detection GitHub
- ImageMagick - Free Image Processing Tool
- SSD: Single Shot MultiBox Detector
Using LabelImg, the create object label annotation XML files. Please note, these files have been provided in this project, and they are avaiable in the Images/train/ or Images/test/ directories. Below is a screenshot of the the application.
Using this libray, this project will take the orginally provided photos and create syntetic images for traing to boost model performance. This process is automated by provided scripts, but the user will need to determine the amount of synetic training examples that will be created.
Below are a few different examples, please note the object labels are updated for the image.
Please Note: Any scripts that use '~/' in the path are assuming this is the home directory of the downloaded project. This is the default for CDSW.
-
In CSDW, download the project using the git url for here.
-
Open a new session, run the CDSW-build.sh script at the terminal prompt, which contains the following operating code.
-
Download the project using the git url for here.
-
Run at terminal prompt.
./scripts/setup.sh
./scripts/buildModel.sh
-
Download the project using the git url for here.
pip3 install tensorflow
pip install tensorflow
- Download Tensorflow models using their Git Repo and build the project.
#Clone Tensorflow Model Git Repo
mkdir tensorflow
cd tensorflow
git clone https://github.com/tensorflow/models.git
cd /home/cdsw/tensorflow/models/research
python setup.py build
python setup.py install
- Convert XML image labels to CSV. (Optional - CSV files have been provided in annotations Dir)
#Convert XML Labels to CSV
python ~/scipts/xml_to_csv.py -i Images/train -o ~/annotations/train_labels.csv
python ~/sciptsxml_to_csv.py -i Images/test -o ~/annotations/test_labels.csv
- Convert CSV labels to Tensorflow TF-Record type.
#Convert CSV to TF-Record
python3 ~/scipts/generate_tfrecord.py --label0=Cloudera --label1=Hortonworks --csv_input=~/annotations/train_labels.csv --img_path=Images/train --output_path=annotations/train.record
python3 ~/scipts/generate_tfrecord.py --label0=Cloudera --label1=Hortonworks --csv_input=~/annotations/test_labels.csv --img_path=Images/test --output_path=~/annotations/test.record
- Download original SSD Tensorflow model.
#Download Original SSD Tensorflow Model
cd
mkdir pre-trained-model
cd pre-trained-model
wget http://download.tensorflow.org/models/object_detection/ssd_inception_v2_coco_2018_01_28.tar.gz
tar -xzf ssd_inception_v2_coco_2018_01_28.tar.gz
- Install COCO API.
#COCO API Install
git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
make
cp -r pycocotools ~/tensorflow/models/research/
- Download Google's protobuffer tools.
# From tensorflow/models/research/
cd ~/tensorflow/models/research/
wget -O protobuf.zip https://github.com/google/protobuf/releases/download/v3.0.0/protoc-3.0.0-linux-x86_64.zip
unzip protobuf.zip
- Create protobuffers for Object Dectection model.
# From tensorflow/models/research/
cd ~/tensorflow/models/research/
./bin/protoc object_detection/protos/*.proto --python_out=.
- Export Path to the protobuffer library.
# From tensorflow/models/research/
cd ~/tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
- Retrain Object Detection model to create new Object Detection model.
# From Home Directory
cd
#Update PATH and PYTHONPATH
cd
export PYTHONPATH=$PYTHONPATH:~/tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:~/tensorflow/models/research/slim
export PYTHONPATH=$PYTHONPATH:~/tensorflow/models/research/object_detection
export PATH=$PATH:~/.local/bin
python3 ~/scripts/train.py --logtostderr --train_dir=~/training/ \
--pipeline_config_path=~/training/ssd_inception_v2_coco.config
If Everything goes to plan, then you should see this type of output with steps. Please keep in mind this could take HOURS if using CPU(s) to complete:
You can also check the Tenorboard with this command
tensorboard --logdir=~/training --port=8080
- Find the Highest Ranked Checkpoint File. Make a note of the file’s name, as it will be passed as an argument when we call the export_inference_graph.py script.
ls -t ~/training/model.ckpt*
If training was sucessful, then results will be displayed. Please keep in mind, the numeric values will be different.
$ ls -t ~/training/model.ckpt*
/home/cdsw/training/model.ckpt-4041.meta
/home/cdsw/training/model.ckpt-4041.index
/home/cdsw/training/model.ckpt-4041.data-00000-of-00001
/home/cdsw/training/model.ckpt-3769.meta
/home/cdsw/training/model.ckpt-3769.index
/home/cdsw/training/model.ckpt-3769.data-00000-of-00001
/home/cdsw/training/model.ckpt-3497.meta
/home/cdsw/training/model.ckpt-3497.index
/home/cdsw/training/model.ckpt-3497.data-00000-of-00001
/home/cdsw/training/model.ckpt-3225.meta
/home/cdsw/training/model.ckpt-3225.index
/home/cdsw/training/model.ckpt-3225.data-00000-of-00001
/home/cdsw/training/model.ckpt-2954.meta
/home/cdsw/training/model.ckpt-2954.index
/home/cdsw/training/model.ckpt-2954.data-00000-of-00001
- Export inference graph into Home directory.
cd
python3 ~/scripts/export_inference_graph.py --input_type image_tensor \
--pipeline_config_path ~/training/ssd_inception_v2_coco.config \
--trained_checkpoint_prefix ~/training/model.ckpt-4041 \
--output_directory ~/trained-inference-graphs/output_inference_graph_v1.pb
If this command is successful, then the trained inference graph will be created.
ls ~/trained-inference-graphs/output_inference_graph_v1.pb
- Convert Tensorflow model to Tensorflow Lite model.
python3 ~/tensorflow/models/research/object_detection/export_tflite_ssd_graph.py \
--input_type=image_tensor \
--input_shape={"image_tensor":[1,300,300,3]} \
--pipeline_config_path=~/trained-inference-graphs/output_inference_graph_v1/pipeline.config \
--trained_checkpoint_prefix=~/trained-inference-graphs/output_inference_graph_v1/model.ckpt \
--output_directory=trainedTFLite \
--add_postprocessing_op=true \
--max_detections=10
- Evaulated the Saved Model using CLI tools.
saved_model_cli show --dir ~/trained-inference-graphs/output_inference_graph_v1/saved_model --all
- Convert Tensorflow model to Tensorflow Lite model with TOCO.
#Convert TF Graphs to TFLite Model
toco --output_file=~/LogoObjD.tflite \
--graph_def_file=~/trainedTFLite/tflite_graph.pb \
--input_format=TENSORFLOW_GRAPHDEF \
--inference_input_type=QUANTIZED_UINT8 \
--inference_type=QUANTIZED_UINT8 \
--input_arrays=normalized_input_image_tensor \
--output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3' \
--output_format=TFLITE \
--input_shape=1,300,300,3 \
--input_data_type=QUANTIZED_UINT8 \
--mean_values=128 \
--std_dev_values=128 \
--default_ranges_min=0.0 \
--default_ranges_max=300 \
--allow_custom_ops
Use Online Edge TPU Compiler to prepare LogoObjD.tflite model for TPU. If the compiler finishes, then you should see the screen below. Please note, this project includes the resulting model.