Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4a80a61

Browse files
committed
added inference scripts + installations instructions
1 parent 05e18e3 commit 4a80a61

File tree

12 files changed

+674
-2
lines changed

12 files changed

+674
-2
lines changed

‎.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
.idea/
3+
14
# Byte-compiled / optimized / DLL files
25
__pycache__/
36
*.py[cod]

‎README.md

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,109 @@
1-
# tf2-object-detection-api-tutorial
2-
Tensorflow 2 Object Detection API Tutorial
1+
# Tensorflow 2 Object Detection API Tutorial
2+
3+
[![Python 3.6](https://img.shields.io/badge/Python-3.6-3776AB)](https://www.python.org/downloads/release/python-360/)
4+
[![TensorFlow 2.2](https://img.shields.io/badge/TensorFlow-2.2-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v2.2.0)
5+
6+
### Introduction
7+
8+
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.
10+
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.
12+
13+
14+
### Roadmap
15+
16+
This tutorial should take you from installation, to running pre-trained detection model, and training/evaluation your models with a custom dataset.
17+
18+
1. [Installation](#installation)
19+
2. Inference with pre-trained models
20+
3. Preparing your custom dataset for training
21+
4. Training with your custom data, and exporting trained models for inference
22+
23+
### Installation
24+
25+
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.
26+
27+
It is recommended to install [anaconda](https://www.anaconda.com/products/individual) and create new environment for your project:
28+
29+
```
30+
# create new environment
31+
conda create --name py36-tf2 python=3.6
32+
33+
# activate your environment before installation or running your scripts
34+
conda activate py36-tf2
35+
```
36+
37+
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).
38+
39+
```
40+
# if you have NVIDIA GPU with cuda 10.1 and cudnn 7.6
41+
pip install tensorflow-gpu==2.2.0
42+
```
43+
44+
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).
45+
46+
```
47+
# installation from anaconda along with cudatoolkit
48+
conda install -c anaconda tensorflow-gpu==2.2.0
49+
50+
# or to install latest version of tensorflow, just type
51+
conda install -c anaconda tensorflow-gpu
52+
```
53+
54+
for CPU only support:
55+
56+
```
57+
# CPU only support (slow)
58+
pip install tensorflow==2.2.0
59+
```
60+
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.
62+
63+
64+
Clone the TensorFlow Models repository:
65+
66+
```
67+
git clone https://github.com/tensorflow/models.git
68+
```
69+
70+
Make sure you have [protobuf compiler](https://grpc.io/docs/protoc-installation/#install-using-a-package-manager) version >= 3.0, by typing `protoc --version`, or install it on Ubuntu by typing `apt install protobuf-compiler`
71+
72+
73+
Then proceed to the python package installation as follows:
74+
75+
```
76+
cd models/research
77+
# Compile protos.
78+
protoc object_detection/protos/*.proto --python_out=.
79+
# Install TensorFlow Object Detection API.
80+
cp object_detection/packages/tf2/setup.py .
81+
python -m pip install .
82+
```
83+
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.
85+
86+
Finally, to test that your installation is correct, type the following command:
87+
88+
```
89+
# Test the installation.
90+
python object_detection/builders/model_builder_tf2_test.py
91+
```
92+
93+
94+
### Inference with pre-trained models
95+
96+
TODO
97+
98+
99+
### Preparing your custom dataset for training
100+
101+
TODO
102+
103+
### Training with your custom data
104+
105+
TODO
106+
107+
108+
109+

‎data/samples/images/1.jpg

84.3 KB
Loading[フレーム]

‎data/samples/images/2.jpg

131 KB
Loading[フレーム]

‎data/samples/images/3.jpg

160 KB
Loading[フレーム]

‎data/samples/images/4.jpg

374 KB
Loading[フレーム]

‎data/samples/images/5.jpg

127 KB
Loading[フレーム]

‎data/samples/images/6.jpg

111 KB
Loading[フレーム]

‎data/samples/pedestrian_test.mp4

6.04 MB
Binary file not shown.

‎detect_objects.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import os
2+
import cv2
3+
import time
4+
import argparse
5+
6+
from detector import DetectorTF2
7+
8+
9+
def DetectFromVideo(detector, Video_path, save_output=False, output_dir='output/'):
10+
11+
cap = cv2.VideoCapture(Video_path)
12+
if save_output:
13+
output_path = os.path.join(output_dir, 'detection_'+ Video_path.split("/")[-1])
14+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
15+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
16+
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), 25, (frame_width, frame_height))
17+
18+
while (cap.isOpened()):
19+
ret, img = cap.read()
20+
if not ret: break
21+
22+
timestamp1 = time.time()
23+
det_boxes = detector.DetectFromImage(img)
24+
elapsed_time = round((time.time() - timestamp1) * 1000) #ms
25+
img = detector.DisplayDetections(img, det_boxes, det_time=elapsed_time)
26+
27+
cv2.imshow('TF2 Detection', img)
28+
if cv2.waitKey(1) == 27: break
29+
#& 0xFF == ord('q'): break
30+
31+
if save_output:
32+
out.write(img)
33+
34+
cap.release()
35+
if save_output:
36+
out.release()
37+
38+
39+
def DetectImagesFromFolder(detector, images_dir, save_output=False, output_dir='output/'):
40+
41+
for file in os.scandir(images_dir):
42+
if file.is_file() and file.name.endswith(('.jpg', '.jpeg', '.png')) :
43+
image_path = os.path.join(images_dir, file.name)
44+
print(image_path)
45+
img = cv2.imread(image_path)
46+
det_boxes = detector.DetectFromImage(img)
47+
img = detector.DisplayDetections(img, det_boxes)
48+
49+
cv2.imshow('TF2 Detection', img)
50+
cv2.waitKey(0)
51+
52+
if save_output:
53+
img_out = os.path.join(output_dir, file.name)
54+
cv2.imwrite(img_out, img)
55+
56+
57+
if __name__ == "__main__":
58+
59+
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)',
61+
default='models/efficientdet_d0_coco17_tpu-32/saved_model')
62+
parser.add_argument('--path_to_labelmap', help='Path to labelmap (.pbtxt) file',
63+
default='models/mscoco_label_map.pbtxt')
64+
parser.add_argument('--class_ids', help='id of classes to detect, expects string with ids delimited by ","',
65+
type=str, default=None) # example input "1,3" to detect person and car
66+
parser.add_argument('--threshold', help='Detection Threshold', type=float, default=0.4)
67+
parser.add_argument('--images_dir', help='Directory to input images)', default='data/samples/images/')
68+
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
72+
args = parser.parse_args()
73+
74+
id_list = None
75+
if args.class_ids is not None:
76+
id_list = [int(item) for item in args.class_ids.split(',')]
77+
78+
if args.save_output:
79+
if not os.path.exists(args.output_directory):
80+
os.makedirs(args.output_directory)
81+
82+
# instance of the class DetectorTF2
83+
detector = DetectorTF2(args.model_path, args.path_to_labelmap, class_id=id_list, threshold=args.threshold)
84+
85+
if args.video_input:
86+
DetectFromVideo(detector, args.video_path, save_output=args.save_output, output_dir=args.output_directory)
87+
else:
88+
DetectImagesFromFolder(detector, args.images_dir, save_output=args.save_output, output_dir=args.output_directory)
89+
90+
print("Done ...")
91+
cv2.destroyAllWindows()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /