Git Product home page Git Product logo

logotl's Introduction

Transfer Learning on Tensorflow and Tensorflow Lite Models

Data Science

Object Detection on Logos Using Tensorflow

objectdetection

Introduction - Corporate Logo Object Detection

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

Table of contents

  1. Introduction

  2. Links

    1. Object Detection
    2. Image Augmentation For Object Detection
    3. Tools
    4. Converting Tensorflow Models to Tensorflow Lite Models
  3. Image Prep

  4. Data Augmentation

  5. Build Tensorflow Model Instructions

    1. CDSW Run Instructions
    2. Quick Instructions
    3. Step By Step Command Line Instructions
  6. Convert Tensorflow Model to Tensorflow Lite Instructions

  7. Complile Tensorflow Lite Model for Edge TPU

Links

Object Detection:

Image Augmentation For Object Detection:

Converting Tensorflow Models to Tensorflow Lite Models:

Tools:

Preprocessing - Image and Label Preparation (Optional)

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.

LabelImg

Preprocessing - Data Augmentation

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.

AugImg1 AugImg2 AugImg3 AugImg4

Build Tensorflow Object Detection Model Instructions

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.

CDSW Run Instructions

  1. In CSDW, download the project using the git url for here.

  2. Open a new session, run the CDSW-build.sh script at the terminal prompt, which contains the following operating code.

Quick Instructions (If you feel lucky)

  1. Download the project using the git url for here.

  2. Run at terminal prompt.

./scripts/setup.sh
./scripts/buildModel.sh

Step By Step Command Line Instructions

  1. Download the project using the git url for here.

  2. Install Tensorflow

pip3 install tensorflow
pip install tensorflow
  1. 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
  1. 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
  1. 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
  1. 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
  1. Install COCO API.
#COCO API Install
git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
make
cp -r pycocotools ~/tensorflow/models/research/
  1. 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
  1. Create protobuffers for Object Dectection model.
# From tensorflow/models/research/
cd ~/tensorflow/models/research/
./bin/protoc object_detection/protos/*.proto --python_out=.
  1. Export Path to the protobuffer library.
# From tensorflow/models/research/
cd ~/tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
  1. 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:

modelBuild

You can also check the Tenorboard with this command

tensorboard --logdir=~/training --port=8080
  1. 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

Convert Tensorflow Model to Tensorflow Lite Instructions

  1. 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
  1. 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
  1. Evaulated the Saved Model using CLI tools.
saved_model_cli show --dir ~/trained-inference-graphs/output_inference_graph_v1/saved_model --all
  1. 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 

Complile Tensorflow Lite Model for Edge TPU

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.

OnlineCompile

logotl's People

Contributors

brooksian avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.