Train and Deploy Object detection models using TensorFlow Object Detection API. This Repository uses Object detection API to train and export models to required formats along with documentation. This repository also contains some scripts for collecting and preprocessing dataset to required format.
There is no specific requirements for inferencing from pretrained models but require GPU for training or finetuning a detection model.
Python 3.6 or higher. Tested with Python 3.7, 3.9 in Windows 10 and Linux.
tensorflow-gpu>=2.0
ortensorflow>=2.0
numpy
absl-py
opencv-python
This implementation is tested with tensorflow cpu and gpu 2.0, 2.2, 2.7 in Windows 10 and Linux
There are two ways to install python in windows using Python 3 installer or Anaconda. Installing python with anaconda or miniconda is recommended. In linux Python 3 is installed by default but we can also install miniconda or conda into linux.
Create a new python virtual environment using conda or venv and activate it. If Anaconda or Miniconda is installed use conda
else use venv
to create virtual environments.
- Using conda
conda create --name tfod
conda activate tfod
conda install pip
- Using venv in linux
python3 -m venv path/to/create/env/tfod
source path/to/create/env/tfod/bin/activate
- Using venv in windows
python -m venv path\to\create\env\tfod
path\to\create\env\tfod\Scripts\activate
The command below will install the required dependencies from requirements.txt
file.
pip install -r requirements.txt
- Clone the tensorflow model repository fro github into desired location. Tested with commit has
439d515a059740ba926ec2442299300fe52aa101
git clone https://github.com/tensorflow/models
- Run the commands below into terminal to install
cd models/research/
protoc object_detection/protos/*.proto --python_out=.
cp object_detection/packages/tf2/setup.py .
python -m pip install .
- Download latest protoc release from here
- Add
<PATH_TO_Protobuf>\bin
to your Path environment variable. - Open cmd and run
cd models/research/
# in windows cmd
for /f %i in ('dir /b object_detection\protos\*.proto') do protoc object_detection\protos\%i --python_out=.
# in windows powershell
Get-ChildItem object_detection/protos/*.proto | foreach {protoc "object_detection/protos/$($_.Name)" --python_out=.}
Object Detection model trained using TensorFlow object detection API can be downloaded from TensorFlow Object Detection Models Zoo with coco weights which we can finetune or train from scratch.
If get stuck in installation part follow these additional resources
python detect_image.py \
--model PATH/TO/MODEL \
--image PATH/TO/IMAGE \
--output PATH/TO/SAVE/OUTPUT/IMAGE
optional argument threshold
which neglects all detections with probability score less that that. Defaults to 0.5
python detect_video.py \
--model PATH/TO/MODEL \
--video PATH/TO/VIDEO \
--output PATH/TO/SAVE/OUTPUT/VIDEO
optional argument threshold
which neglects all detections with probability score less that that. Defaults to 0.5
python detect_webcam.py \
--model PATH/TO/MODEL \
--cam 0 \
--output PATH/TO/SAVE/OUTPUT/VIDEO
optional argument threshold
which neglects all detections with probability score less that that. Defaults to 0.5
Training script is taken from model/research/object-detection
python train_model_tfrecords.py \
--model_dir "PATH/TO/SAVE/CHECKPOINT/AND/EVENTS" \
--pipeline_config_path "PATH/TO/PIPELINE/CONFIG" \
--num_train_steps 100 \
--checkpoint_dir "PATH/TO/CHECKPOINT/DIR" \
--eval_training_data True \
--sample_1_of_n_eval_examples 1 \
--sample_1_of_n_eval_on_train_examples 5 \
--max_eval_retries 0
sample_1_of_n_eval_on_train_examples
Will sample one of every n train input examples for evaluation, This is only used ifeval_training_data
is True. Default is 5sample_1_of_n_eval_examples
Will sample one of every n eval input examples. Default is 1max_eval_retries
If running continuous eval, the maximum number of retries upon encountering tf.errors.InvalidArgumentError. If negative, will always retry the evaluation.
python train_model_images.py \
--train_annotations_csv "PATH/TO/TRAIN/CSV" \
--test_annotations_csv "PATH/TO/TEST/CSV"
--train_images_dir "PATH/TO/TRAIN/IMAGES" \
--test_images_dir "PATH/TO/TEST/IMAGES" \
--labelstxt_path "PATH/TO/LABELS/TXT/FILE" \
--pipeline_config_path "PATH/TO/PIPELINE/CONFIG" \
--num_train_steps 100 \
--checkpoint_path "PATH/TO/CHECKPOINT/.ckpt" \
--batch_size 32 \
--early_stopping_patience 5 \
--learning_rate 0.01 \
--model_name "MODEL_NAME" \
--finetune True \
--image_width 320 \
--image_height 320
script Taken from model/research/object-detection
python export_model.py \
--input_type image_tensor \
--pipeline_config_path "path/to/ssd_model_pipeline.config" \
--trained_checkpoint_prefix "path/to/model.ckpt" \
--output_directory "path/to/exported_model_directory"
script taken from model/research/object-detection
python export_tflite.py \
--pipeline_config_path "path/to/ssd_model/pipeline.config" \
--trained_checkpoint_dir "path/to/ssd_model/checkpoint" \
--output_directory "path/to/exported_model_directory"
python download_images.py \
--config PATH/TO/DOWNLOADER/JSON
config
Path to config file of downloader. Defaults toimage_downloader/config.json
Contributions are highly welcome that will improve quality of project.