• Stars
    star
    129
  • Rank 279,262 (Top 6 %)
  • Language
    Python
  • License
    MIT License
  • Created about 5 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Use the Tensorflow Object Detection API with Tensorflow 2

Tensorflow Object Detection with Tensorflow 2

Duckies detection

In this repository you can find some examples on how to use the Tensorflow OD API with Tensorflow 2. For more information check out my articles:

Installation

You can install the TensorFlow Object Detection API either with Python Package Installer (pip) or Docker, an open-source platform for deploying and managing containerized applications.

First clone the master branch of the Tensorflow Models repository:

git clone https://github.com/tensorflow/models.git

Docker Installation

# From the root of the git repository (inside the models directory)
docker build -f research/object_detection/dockerfiles/tf2/Dockerfile -t od .
docker run -it od

Python Package Installation

cd models/research
# Compile protos.
protoc object_detection/protos/*.proto --python_out=.
# Install TensorFlow Object Detection API.
cp object_detection/packages/tf2/setup.py .
python -m pip install .

Note: The *.proto designating all files does not work protobuf version 3.5 and higher. If you are using version 3.5, you have to go through each file individually. To make this easier, I created a python script that loops through a directory and converts all proto files one at a time.

import os
import sys
args = sys.argv
directory = args[1]
protoc_path = args[2]
for file in os.listdir(directory):
    if file.endswith(".proto"):
        os.system(protoc_path+" "+directory+"/"+file+" --python_out=.")
python use_protobuf.py <path to directory> <path to protoc file>

To test the installation run:

# Test the installation.
python object_detection/builders/model_builder_tf2_test.py

If everything installed correctly you should see something like:

...
[       OK ] ModelBuilderTF2Test.test_create_ssd_models_from_config
[ RUN      ] ModelBuilderTF2Test.test_invalid_faster_rcnn_batchnorm_update
[       OK ] ModelBuilderTF2Test.test_invalid_faster_rcnn_batchnorm_update
[ RUN      ] ModelBuilderTF2Test.test_invalid_first_stage_nms_iou_threshold
[       OK ] ModelBuilderTF2Test.test_invalid_first_stage_nms_iou_threshold
[ RUN      ] ModelBuilderTF2Test.test_invalid_model_config_proto
[       OK ] ModelBuilderTF2Test.test_invalid_model_config_proto
[ RUN      ] ModelBuilderTF2Test.test_invalid_second_stage_batch_size
[       OK ] ModelBuilderTF2Test.test_invalid_second_stage_batch_size
[ RUN      ] ModelBuilderTF2Test.test_session
[  SKIPPED ] ModelBuilderTF2Test.test_session
[ RUN      ] ModelBuilderTF2Test.test_unknown_faster_rcnn_feature_extractor
[       OK ] ModelBuilderTF2Test.test_unknown_faster_rcnn_feature_extractor
[ RUN      ] ModelBuilderTF2Test.test_unknown_meta_architecture
[       OK ] ModelBuilderTF2Test.test_unknown_meta_architecture
[ RUN      ] ModelBuilderTF2Test.test_unknown_ssd_feature_extractor
[       OK ] ModelBuilderTF2Test.test_unknown_ssd_feature_extractor
----------------------------------------------------------------------
Ran 20 tests in 91.767s

OK (skipped=1)

Running a pre-trained model

The object_detection_tutorial.ipynb notebook walks you through the process of using a pre-trained model to detect objects in an image. To try it out, I recommend to run it inside Google Colab.

Person and Kites detection

Modify code to run on a video stream

The above example can be easily rewritten to work with video streams by replacing the show_inference method with:

import cv2
cap = cv2.VideoCapture(0) # or cap = cv2.VideoCapture("<video-path>")

def run_inference(model, cap):
    while cap.isOpened():
        ret, image_np = cap.read()
        # Actual detection.
        output_dict = run_inference_for_single_image(model, image_np)
        # Visualization of the results of a detection.
        vis_util.visualize_boxes_and_labels_on_image_array(
            image_np,
            output_dict['detection_boxes'],
            output_dict['detection_classes'],
            output_dict['detection_scores'],
            category_index,
            instance_masks=output_dict.get('detection_masks_reframed', None),
            use_normalized_coordinates=True,
            line_thickness=8)
        cv2.imshow('object_detection', cv2.resize(image_np, (800, 600)))
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cap.release()
            cv2.destroyAllWindows()
            break

run_inference(detection_model, cap)

Live Object Detection Example

You can find the code as a notebook or python file.

Few-shot learning

The new release also comes with another notebook showing us how to fine-tune a RetinaNet pre-trained model to detect rubber duckies with only 5 images and <5 minutes of training time in Google Colab.

Duckies detection

Author

Gilbert Tanner

More Repositories

1

Tutorials

Code for some of my articles
Jupyter Notebook
559
star
2

Tensorflow-Object-Detection-API-Train-Model

Train a object detection model with the Tensorflow Object Detection API and Tensorflow 2.
Jupyter Notebook
192
star
3

Object-Detection-and-Image-Segmentation-with-Detectron2

Object Detection and Image Segmentation with Detectron2
Jupyter Notebook
154
star
4

Detectron2-Train-a-Instance-Segmentation-Model

Learn how to train a custom instance segmentation model with Detectron2
Jupyter Notebook
97
star
5

Tensorflow-Object-Detection-API-train-custom-Mask-R-CNN-model

Train a Mask R-CNN model with the Tensorflow Object Detection API
Jupyter Notebook
77
star
6

Machine-Learning-Explained

Learn the theory, math and code behind different machine learning algorithms and techniques.
Python
62
star
7

MaskRCNN-Object-Detection-and-Segmentation

Train your own custom MaskRCNN Object Detection and Instance Segmentation model.
Jupyter Notebook
58
star
8

Tensorflow-2-Object-Counting

Cumulative object counting with Tensorflow 2
Python
46
star
9

Model-Interpretation

Overview of different model interpretability libraries.
Jupyter Notebook
43
star
10

Computer-Vision-Synthetic-Data-Generation

Synthetic data-set generator for Object Detection and Instance Segmentation
Python
32
star
11

TFLite-Object-Detection-with-TFLite-Model-Maker

Custom object detection with the TFLite Model Maker
Jupyter Notebook
31
star
12

Tensorflow-Lite-Object-Detection-with-the-Tensorflow-Object-Detection-API

Run object detection on edge devices using Tensorflow Lite
Jupyter Notebook
24
star
13

Manim-Examples

Examples on how to use Manim
Python
22
star
14

Arduino-Portenta-Examples

Arduino Portenta H7 Examples
C++
21
star
15

Google-Coral-Edge-TPU

Use the Google Coral USB Accelerator for deep learning.
Jupyter Notebook
21
star
16

HackerRank-Solutions

Solutions for Hackerrank problems.
Python
20
star
17

Arduino-Nano-33-BLE-Sense-Code-Collection

Arduino Nano 33 BLE Sense Code Collection
C++
20
star
18

Run-TFLITE-models-on-the-web

Run TFLITE models on the web
JavaScript
11
star
19

YOLO-Tutorials

YOLO Object Detection Tutorials
Jupyter Notebook
11
star
20

Jetson-Nano-Code-Collection

Jetson Nano Code Collection
Jupyter Notebook
11
star
21

Ryze-Tello-Code-Collection

Some example scripts for the Ryze Tello drone
Python
9
star
22

Nuxt-Content-Blog-Starter

Nuxt Content Blog Starter Code
Vue
8
star
23

FastAI-Code-Collection

FastAI V2 and V1 Code Examples
Jupyter Notebook
3
star
24

LoRa-Introduction

Learn LoRa
C++
2
star
25

TannerGilbert

2
star
26

OpenCV-AI-Kit-Code-Collection

OpenCV AI Kit Examples
Jupyter Notebook
2
star
27

Reddit-PennyStock-Scraping

Scraping data from the /r/pennystocks subreddit
Python
1
star