Skip to content

Commit 79cd9d5

Browse files
instrctions for inference are added
1 parent 4a80a61 commit 79cd9d5

File tree

8 files changed

+69
-20
lines changed

8 files changed

+69
-20
lines changed

README.md

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,30 @@
66
### Introduction
77

88

9-
With the [announcement](https://blog.tensorflow.org/2020/07/tensorflow-2-meets-object-detection-api.html) that Object Detection API is now compatible with Tensorflow 2, I tried to test the new models published in the [TF2 model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md), and train them with my custom data. However, I have faced some problems as the scripts I have for Tensorflow 1 is not working with Tensorflow 2 (which is not surprising!), in addition to having very poor documentation and tutorials from tensorflow models repo. Therefore, in this repo I am sharing my experience, in addition to providing clean codes to run the inference and training object detection models using Tensorflow 2.
9+
With the [announcement](https://blog.tensorflow.org/2020/07/tensorflow-2-meets-object-detection-api.html) that [Object Detection API](https://github.com/tensorflow/models/tree/master/research/object_detection) is now compatible with Tensorflow 2, I tried to test the new models published in the [TF2 model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md), and train them with my custom data.
10+
However, I have faced some problems as the scripts I have for Tensorflow 1 is not working with Tensorflow 2 (which is not surprising!), in addition to having very poor documentation and tutorials from tensorflow models repo.
11+
In this repo I am sharing my experience, in addition to providing clean codes to run the inference and training object detection models using Tensorflow 2.
1012

11-
This tutorial should be useful for those who has experience with the API but cannot find clear documentation or examples for the new changes to use it with Tensorflow 2. However, I will add all the details and working examples for the new comers who are trying to use the object detection api for the first time, so hopefully this tutorial will make it easy for beginners to get started and run their object detection models easily.
13+
This tutorial should be useful for those who have experience with the API but cannot find clear examples for the new changes to use it with Tensorflow 2.
14+
However, I will add all the details and working examples for the new comers who are trying to use the object detection api for the first time, so hopefully this tutorial will make it easy for beginners to get started and run their object detection models easily.
1215

1316

1417
### Roadmap
1518

1619
This tutorial should take you from installation, to running pre-trained detection model, and training/evaluation your models with a custom dataset.
1720

1821
1. [Installation](#installation)
19-
2. Inference with pre-trained models
22+
2. [Inference with pre-trained models](#inference-with-pre-trained-models)
2023
3. Preparing your custom dataset for training
2124
4. Training with your custom data, and exporting trained models for inference
2225

2326
### Installation
2427

2528
The examples in this repo is tested with python 3.6 and Tensorflow 2.2.0, but it is expected to work with other Tensorflow 2.x versions with python version 3.5 or higher.
2629

27-
It is recommended to install [anaconda](https://www.anaconda.com/products/individual) and create new environment for your project:
30+
It is recommended to install [anaconda](https://www.anaconda.com/products/individual) and create new [environment](https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html) for your projects that use the installed packages:
2831

29-
```
32+
```bash
3033
# create new environment
3134
conda create --name py36-tf2 python=3.6
3235

@@ -36,15 +39,15 @@ conda activate py36-tf2
3639

3740
You need first to install tensorflow 2, either with GPU or CPU only support (slow). For Installation with GPU support, you need to have CUDA 10.1 with CUDNN 7.6 to use Tensorflow 2.2.0. You can check the compatible versions of any tensorflow version with cuda and cudnn versions from [here](https://www.tensorflow.org/install/source#tested_build_configurations).
3841

39-
```
42+
```bash
4043
# if you have NVIDIA GPU with cuda 10.1 and cudnn 7.6
4144
pip install tensorflow-gpu==2.2.0
4245
```
4346

4447
A great feature of Anaconda is that it can automatically install a local version of cudatoolkit that is compatible with your tensorflow version (But you should have the proper nvidia gpu drivers installed).
4548

46-
```
47-
# installation from anaconda along with cudatoolkit
49+
```bash
50+
# installation from anaconda along with cudatoolkit (tf2 version 2.2.0)
4851
conda install -c anaconda tensorflow-gpu==2.2.0
4952

5053
# or to install latest version of tensorflow, just type
@@ -53,17 +56,18 @@ conda install -c anaconda tensorflow-gpu
5356

5457
for CPU only support:
5558

56-
```
59+
```bash
5760
# CPU only support (slow)
5861
pip install tensorflow==2.2.0
5962
```
6063

61-
After that, you should install the object detection api itself, which became much easier now after the latest update. The official installation instructions can be found [here](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2.md), but I will add the instruction to install it as a python package.
64+
After that, you should install the object detection api itself, which became much easier now after the latest update.
65+
The official installation instructions can be found [here](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2.md), but I will add here the instruction to install it as a python package.
6266

6367

6468
Clone the TensorFlow Models repository:
6569

66-
```
70+
```bash
6771
git clone https://github.com/tensorflow/models.git
6872
```
6973

@@ -72,7 +76,7 @@ Make sure you have [protobuf compiler](https://grpc.io/docs/protoc-installation/
7276

7377
Then proceed to the python package installation as follows:
7478

75-
```
79+
```bash
7680
cd models/research
7781
# Compile protos.
7882
protoc object_detection/protos/*.proto --python_out=.
@@ -81,19 +85,64 @@ cp object_detection/packages/tf2/setup.py .
8185
python -m pip install .
8286
```
8387

84-
The previous commands installs the object detection api as a python package that will be available in your virtual environmnet (if you created one), and will automatically install all required dependencies if not found.
88+
The previous commands installs the object detection api as a python package that will be available in your virtual environmnet (if you created one), and will automatically install all required dependencies if not already installed.
8589

8690
Finally, to test that your installation is correct, type the following command:
8791

88-
```
92+
```bash
8993
# Test the installation.
9094
python object_detection/builders/model_builder_tf2_test.py
9195
```
9296

97+
For more installation options, please refer to the original [installation guide](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2.md).
98+
9399

94100
### Inference with pre-trained models
95101

96-
TODO
102+
To go through the tutorial, clone this repo and follow the instructions step by step.
103+
104+
```bash
105+
git clone https://github.com/abdelrahman-gaber/tf2-object-detection-api-tutorial.git
106+
```
107+
108+
109+
To get started with the Object Detection API with TF2, let's download one of the models pre-trained with coco dataset from the [tf2 detection model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md), and use it for inference.
110+
111+
You can download any of the models from the table in the [model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md), and place it in the [models/](models) directory. For example, let's download the EfficientDet D0 model.
112+
113+
```bash
114+
cd models/
115+
# download the model
116+
wget http://download.tensorflow.org/models/object_detection/tf2/20200711/efficientdet_d0_coco17_tpu-32.tar.gz
117+
# extract the downloaded file
118+
tar -xzvf efficientdet_d0_coco17_tpu-32.tar.gz
119+
```
120+
121+
In the tensorflow object detection repo, they provide a tutorial for inference in this [notebook](https://github.com/tensorflow/models/blob/master/research/object_detection/colab_tutorials/inference_from_saved_model_tf2_colab.ipynb), but it is not so clean and needs many improvements.
122+
Therefore, I have created a [class for object detection inference](detector.py), along with an [example script](detect_objects.py) to use this class to run the inference with input images, or from a video.
123+
124+
I encourage you to have a look at the [class file](detector.py) and the [example script](detect_objects.py) and adapt it to your application. But let's first see how to use it to get the inference running with the EfficientDet D0 model we just downloaded.
125+
126+
We can provide some argument when running the scripts, first the `--model_path` argument for the path of the trained model, and the `--path_to_labelmap` for the labelmap file of your dataset (here we use the one for coco dataset).
127+
To run the detection with set of images, provide a path to the folder containing the images in the argument `--images_dir`.
128+
129+
```bash
130+
python detect_objects.py --model_path models/efficientdet_d0_coco17_tpu-32/saved_model --path_to_labelmap models/mscoco_label_map.pbtxt --images_dir data/samples/images/
131+
```
132+
133+
Sample output from the detection with EfficientDet D0 model:
134+
135+
![alt-txt-1](data/output/1.jpg) ![alt-txt-2](data/output/4.jpg)
136+
137+
138+
You can also select set of classes to be detected by passing their labels to the argument `--class_ids` as a string with the "," delimiter. For example, using `--class_ids "1,3" ` will do detection for the classes "person" and "car" only as they have id 1 and 3 respectively (you can check the id and labels from the [coco labelmap](models/mscoco_label_map.pbtxt)). Not using this argument will lead to detecting all objects in the provided labelmap.
139+
140+
Let's use video input by enabling the flag `--video_input`, in addition to detecting only people by passing id 1 to the `--class_ids` argument. The video used for testing is downloaded from [here](https://www.youtube.com/watch?v=pk96gqasGBQ).
141+
142+
```bash
143+
python detect_objects.py --video_input --class_ids "1" --threshold 0.3 --video_path data/samples/pedestrian_test.mp4 --model_path models/efficientdet_d0_coco17_tpu-32/saved_model --path_to_labelmap models/mscoco_label_map.pbtxt
144+
```
145+
97146

98147

99148
### Preparing your custom dataset for training

data/output/1.jpg

103 KB
Loading

data/output/2.jpg

104 KB
Loading

data/output/3.jpg

180 KB
Loading

data/output/4.jpg

232 KB
Loading

data/output/5.jpg

208 KB
Loading

data/output/6.jpg

124 KB
Loading

detect_objects.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ def DetectFromVideo(detector, Video_path, save_output=False, output_dir='output/
2626

2727
cv2.imshow('TF2 Detection', img)
2828
if cv2.waitKey(1) == 27: break
29-
#& 0xFF == ord('q'): break
3029

3130
if save_output:
3231
out.write(img)
@@ -57,7 +56,7 @@ def DetectImagesFromFolder(detector, images_dir, save_output=False, output_dir='
5756
if __name__ == "__main__":
5857

5958
parser = argparse.ArgumentParser(description='Object Detection from Images or Video')
60-
parser.add_argument('--model_path', help='Path to frozen detection graph (model from Tensorflow)',
59+
parser.add_argument('--model_path', help='Path to frozen detection model',
6160
default='models/efficientdet_d0_coco17_tpu-32/saved_model')
6261
parser.add_argument('--path_to_labelmap', help='Path to labelmap (.pbtxt) file',
6362
default='models/mscoco_label_map.pbtxt')
@@ -66,9 +65,10 @@ def DetectImagesFromFolder(detector, images_dir, save_output=False, output_dir='
6665
parser.add_argument('--threshold', help='Detection Threshold', type=float, default=0.4)
6766
parser.add_argument('--images_dir', help='Directory to input images)', default='data/samples/images/')
6867
parser.add_argument('--video_path', help='Path to input video)', default='data/samples/pedestrian_test.mp4')
69-
parser.add_argument('--output_directory', help='Path to output images and video)', default='data/output')
70-
parser.add_argument('--video_input', action='store_true') # default is false
71-
parser.add_argument('--save_output', action='store_true') # default is false
68+
parser.add_argument('--output_directory', help='Path to output images and video', default='data/output')
69+
parser.add_argument('--video_input', help='Flag for video input, default: False', action='store_true') # default is false
70+
parser.add_argument('--save_output', help='Flag for save images and video with detections visualized, default: False',
71+
action='store_true') # default is false
7272
args = parser.parse_args()
7373

7474
id_list = None

0 commit comments

Comments
 (0)