Skip to content

Commit b5ab3ff

Browse files
authored
Add model file collector, fix some bugs and add some features (#123)
add model path searcher and remove hardwritten file paths from benchmark configs; pack ppresnet, mobilenet & crnn with labels; fix palm det data; add flags to enable models of different precision separately (#123)
1 parent 3734ceb commit b5ab3ff

37 files changed

+5363
-5463
lines changed

benchmark/README.md

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
11
# OpenCV Zoo Benchmark
22

3-
Benchmarking different models in the zoo.
3+
Benchmarking the speed of OpenCV DNN inferring different models in the zoo. Result of each model includes the time of its preprocessing, inference and postprocessing stages.
44

55
Data for benchmarking will be downloaded and loaded in [data](./data) based on given config.
66

7-
Time is measured from data preprocess (resize is excluded), to a forward pass of a network, and postprocess to get final results. The final time data presented is averaged from a 100-time run.
8-
97
## Preparation
108

119
1. Install `python >= 3.6`.
1210
2. Install dependencies: `pip install -r requirements.txt`.
1311
3. Download data for benchmarking.
1412
1. Download all data: `python download_data.py`
1513
2. Download one or more specified data: `python download_data.py face text`. Available names can be found in `download_data.py`.
16-
3. If download fails, you can download all data from https://pan.baidu.com/s/18sV8D4vXUb2xC9EG45k7bg (code: pvrw). Please place and extract data packages under [./data](./data).
14+
3. You can also download all data from https://pan.baidu.com/s/18sV8D4vXUb2xC9EG45k7bg (code: pvrw). Please place and extract data packages under [./data](./data).
1715

1816
## Benchmarking
1917

20-
Run the following command to benchmark on a given config:
18+
**Linux**:
2119

2220
```shell
2321
export PYTHONPATH=$PYTHONPATH:..
2422
python benchmark.py --cfg ./config/face_detection_yunet.yaml
2523
```
2624

27-
If you are a Windows user and wants to run in CMD/PowerShell, use this command instead:
25+
**Windows**:
2826
- CMD
29-
```shell
30-
set PYTHONPATH=%PYTHONPATH%;..
31-
python benchmark.py --cfg ./config/face_detection_yunet.yaml
32-
```
27+
```shell
28+
set PYTHONPATH=%PYTHONPATH%;..
29+
python benchmark.py --cfg ./config/face_detection_yunet.yaml
30+
```
3331

3432
- PowerShell
35-
```shell
36-
$env:PYTHONPATH=$env:PYTHONPATH+";.."
37-
python benchmark.py --cfg ./config/face_detection_yunet.yaml
38-
```
33+
```shell
34+
$env:PYTHONPATH=$env:PYTHONPATH+";.."
35+
python benchmark.py --cfg ./config/face_detection_yunet.yaml
36+
```
3937
<!--
4038
Omit `--cfg` if you want to benchmark all included models:
4139
```shell

benchmark/benchmark.py

+27-18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
parser = argparse.ArgumentParser("Benchmarks for OpenCV Zoo.")
1212
parser.add_argument('--cfg', '-c', type=str,
1313
help='Benchmarking on the given config.')
14+
parser.add_argument("--fp32", action="store_true", help="Runs models of float32 precision only.")
15+
parser.add_argument("--fp16", action="store_true", help="Runs models of float16 precision only.")
16+
parser.add_argument("--int8", action="store_true", help="Runs models of int8 precision only.")
1417
args = parser.parse_args()
1518

1619
def build_from_cfg(cfg, registery, key=None, name=None):
@@ -24,14 +27,6 @@ def build_from_cfg(cfg, registery, key=None, name=None):
2427
else:
2528
raise NotImplementedError()
2629

27-
def prepend_pythonpath(cfg):
28-
for k, v in cfg.items():
29-
if isinstance(v, dict):
30-
prepend_pythonpath(v)
31-
else:
32-
if 'path' in k.lower():
33-
cfg[k] = os.path.join(os.environ['PYTHONPATH'].split(os.pathsep)[-1], v)
34-
3530
class Benchmark:
3631
def __init__(self, **kwargs):
3732
self._type = kwargs.pop('type', None)
@@ -115,16 +110,30 @@ def printResults(self):
115110
with open(args.cfg, 'r') as f:
116111
cfg = yaml.safe_load(f)
117112

118-
# prepend PYTHONPATH to each path
119-
prepend_pythonpath(cfg)
120-
121-
# Instantiate benchmarking
113+
# Instantiate benchmark
122114
benchmark = Benchmark(**cfg['Benchmark'])
123115

124116
# Instantiate model
125-
model = build_from_cfg(cfg=cfg['Model'], registery=MODELS, key='name')
126-
127-
# Run benchmarking
128-
print('Benchmarking {}:'.format(model.name))
129-
benchmark.run(model)
130-
benchmark.printResults()
117+
model_config = cfg['Model']
118+
model_handler, model_paths = MODELS.get(model_config.pop('name'))
119+
120+
_model_paths = []
121+
if args.fp32 or args.fp16 or args.int8:
122+
if args.fp32:
123+
_model_paths += model_paths['fp32']
124+
if args.fp16:
125+
_model_paths += model_paths['fp16']
126+
if args.int8:
127+
_model_paths += model_paths['int8']
128+
else:
129+
_model_paths = model_paths['fp32'] + model_paths['fp16'] + model_paths['int8']
130+
131+
for model_path in _model_paths:
132+
model = model_handler(*model_path, **model_config)
133+
# Format model_path
134+
for i in range(len(model_path)):
135+
model_path[i] = model_path[i].split('/')[-1]
136+
print('Benchmarking {} with {}'.format(model.name, model_path))
137+
# Run benchmark
138+
benchmark.run(model)
139+
benchmark.printResults()

benchmark/config/face_detection_yunet.yaml

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Benchmark:
22
name: "Face Detection Benchmark"
33
type: "Detection"
44
data:
5-
path: "benchmark/data/face_detection"
5+
path: "data/face_detection"
66
files: ["group.jpg", "concerts.jpg", "dance.jpg"]
77
sizes: # [[w1, h1], ...], Omit to run at original scale
88
- [160, 120]
@@ -16,8 +16,6 @@ Benchmark:
1616

1717
Model:
1818
name: "YuNet"
19-
modelPath: "models/face_detection_yunet/face_detection_yunet_2022mar.onnx"
2019
confThreshold: 0.6
2120
nmsThreshold: 0.3
2221
topK: 5000
23-

benchmark/config/face_recognition_sface.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Benchmark:
22
name: "Face Recognition Benchmark"
33
type: "Recognition"
44
data:
5-
path: "benchmark/data/face_recognition"
5+
path: "data/face_recognition"
66
files: ["Aaron_Tippin_0001.jpg", "Alvaro_Uribe_0028.jpg", "Alvaro_Uribe_0029.jpg", "Jose_Luis_Rodriguez_Zapatero_0001.jpg"]
77
metric: # 'sizes' is omitted since this model requires input of fixed size
88
warmup: 30
@@ -13,4 +13,3 @@ Benchmark:
1313

1414
Model:
1515
name: "SFace"
16-
modelPath: "models/face_recognition_sface/face_recognition_sface_2021dec.onnx"

benchmark/config/facial_expression_recognition.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Benchmark:
22
name: "Facial Expression Recognition Benchmark"
33
type: "Recognition"
44
data:
5-
path: "benchmark/data/facial_expression_recognition/fer_evaluation"
5+
path: "data/facial_expression_recognition/fer_evaluation"
66
files: ["RAF_test_0_61.jpg", "RAF_test_0_30.jpg", "RAF_test_6_25.jpg"]
77
metric: # 'sizes' is omitted since this model requires input of fixed size
88
warmup: 30
@@ -13,4 +13,3 @@ Benchmark:
1313

1414
Model:
1515
name: "FacialExpressionRecog"
16-
modelPath: "models/facial_expression_recognition/facial_expression_recognition_mobilefacenet_2022july.onnx"

benchmark/config/handpose_estimation_mediapipe.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Benchmark:
22
name: "Hand Pose Estimation Benchmark"
33
type: "Recognition"
44
data:
5-
path: "benchmark/data/palm_detection"
5+
path: "data/palm_detection_20230125"
66
files: ["palm1.jpg", "palm2.jpg", "palm3.jpg"]
77
sizes: # [[w1, h1], ...], Omit to run at original scale
88
- [256, 256]
@@ -15,5 +15,4 @@ Benchmark:
1515

1616
Model:
1717
name: "MPHandPose"
18-
modelPath: "models/handpose_estimation_mediapipe/handpose_estimation_mediapipe_2022may.onnx"
1918
confThreshold: 0.9

benchmark/config/human_segmentation_pphumanseg.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Benchmark:
22
name: "Human Segmentation Benchmark"
33
type: "Base"
44
data:
5-
path: "benchmark/data/human_segmentation"
5+
path: "data/human_segmentation"
66
files: ["messi5.jpg", "100040721_1.jpg", "detect.jpg"]
77
sizes: [[192, 192]]
88
toRGB: True
@@ -15,4 +15,3 @@ Benchmark:
1515

1616
Model:
1717
name: "PPHumanSeg"
18-
modelPath: "models/human_segmentation_pphumanseg/human_segmentation_pphumanseg_2021oct.onnx"

benchmark/config/image_classification_mobilenetv1.yaml renamed to benchmark/config/image_classification_mobilenet.yaml

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Benchmark:
22
name: "Image Classification Benchmark"
33
type: "Classification"
44
data:
5-
path: "benchmark/data/image_classification"
5+
path: "data/image_classification"
66
files: ["coffee_mug.jpg", "umbrella.jpg", "wall_clock.jpg"]
77
sizes: [[256, 256]]
88
toRGB: True
@@ -15,6 +15,4 @@ Benchmark:
1515
target: "cpu"
1616

1717
Model:
18-
name: "MobileNetV1"
19-
modelPath: "models/image_classification_mobilenet/image_classification_mobilenetv1_2022apr.onnx"
20-
18+
name: "MobileNet"

benchmark/config/image_classification_mobilenetv2.yaml

-20
This file was deleted.

benchmark/config/image_classification_ppresnet.yaml

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Benchmark:
22
name: "Image Classification Benchmark"
33
type: "Classification"
44
data:
5-
path: "benchmark/data/image_classification"
5+
path: "data/image_classification"
66
files: ["coffee_mug.jpg", "umbrella.jpg", "wall_clock.jpg"]
77
sizes: [[256, 256]]
88
toRGB: True
@@ -16,5 +16,3 @@ Benchmark:
1616

1717
Model:
1818
name: "PPResNet"
19-
modelPath: "models/image_classification_ppresnet/image_classification_ppresnet50_2022jan.onnx"
20-

benchmark/config/license_plate_detection_yunet.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Benchmark:
22
name: "License Plate Detection Benchmark"
33
type: "Detection"
44
data:
5-
path: "benchmark/data/license_plate_detection"
5+
path: "data/license_plate_detection"
66
files: ["1.jpg", "2.jpg", "3.jpg", "4.jpg"]
77
sizes: # [[w1, h1], ...], Omit to run at original scale
88
- [320, 240]
@@ -15,7 +15,6 @@ Benchmark:
1515

1616
Model:
1717
name: "LPD_YuNet"
18-
modelPath: "models/license_plate_detection_yunet/license_plate_detection_lpd_yunet_2022may.onnx"
1918
confThreshold: 0.8
2019
nmsThreshold: 0.3
2120
topK: 5000

benchmark/config/object_detection_nanodet.yaml

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Benchmark:
22
name: "Object Detection Benchmark"
33
type: "Detection"
44
data:
5-
path: "benchmark/data/object_detection"
5+
path: "data/object_detection"
66
files: ["1.png", "2.png", "3.png"]
77
sizes:
88
- [416, 416]
@@ -15,7 +15,5 @@ Benchmark:
1515

1616
Model:
1717
name: "NanoDet"
18-
modelPath: "models/object_detection_nanodet/object_detection_nanodet_2022nov.onnx"
1918
prob_threshold: 0.35
2019
iou_threshold: 0.6
21-

benchmark/config/object_detection_yolox.yaml

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Benchmark:
22
name: "Object Detection Benchmark"
33
type: "Detection"
44
data:
5-
path: "benchmark/data/object_detection"
5+
path: "data/object_detection"
66
files: ["1.png", "2.png", "3.png"]
77
sizes:
88
- [640, 640]
@@ -15,8 +15,6 @@ Benchmark:
1515

1616
Model:
1717
name: "YoloX"
18-
modelPath: "models/object_detection_yolox/object_detection_yolox_2022nov.onnx"
1918
confThreshold: 0.35
2019
nmsThreshold: 0.5
2120
objThreshold: 0.5
22-

benchmark/config/object_tracking_dasiamrpn.yaml

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Benchmark:
33
type: "Tracking"
44
data:
55
type: "TrackingVideoLoader"
6-
path: "benchmark/data/object_tracking"
6+
path: "data/object_tracking"
77
files: ["throw_cup.mp4"]
88
metric:
99
type: "Tracking"
@@ -13,6 +13,3 @@ Benchmark:
1313

1414
Model:
1515
name: "DaSiamRPN"
16-
model_path: "models/object_tracking_dasiamrpn/object_tracking_dasiamrpn_model_2021nov.onnx"
17-
kernel_cls1_path: "models/object_tracking_dasiamrpn/object_tracking_dasiamrpn_kernel_cls1_2021nov.onnx"
18-
kernel_r1_path: "models/object_tracking_dasiamrpn/object_tracking_dasiamrpn_kernel_r1_2021nov.onnx"

benchmark/config/palm_detection_mediapipe.yaml

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Benchmark:
22
name: "Palm Detection Benchmark"
33
type: "Detection"
44
data:
5-
path: "benchmark/data/palm_detection"
5+
path: "data/palm_detection_20230125"
66
files: ["palm1.jpg", "palm2.jpg", "palm3.jpg"]
77
sizes: # [[w1, h1], ...], Omit to run at original scale
88
- [256, 256]
@@ -15,8 +15,6 @@ Benchmark:
1515

1616
Model:
1717
name: "MPPalmDet"
18-
modelPath: "models/palm_detection_mediapipe/palm_detection_mediapipe_2022may.onnx"
1918
scoreThreshold: 0.5
2019
nmsThreshold: 0.3
2120
topK: 1
22-

benchmark/config/person_reid_youtureid.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Benchmark:
22
name: "Person ReID Benchmark"
33
type: "Base"
44
data:
5-
path: "benchmark/data/person_reid"
5+
path: "data/person_reid"
66
files: ["0030_c1_f0056923.jpg", "0042_c5_f0068994.jpg", "0056_c8_f0017063.jpg"]
77
sizes: [[128, 256]]
88
metric:
@@ -14,4 +14,3 @@ Benchmark:
1414

1515
Model:
1616
name: "YoutuReID"
17-
modelPath: "models/person_reid_youtureid/person_reid_youtu_2021nov.onnx"

benchmark/config/qrcode_wechatqrcode.yaml

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Benchmark:
22
name: "QRCode Detection and Decoding Benchmark"
33
type: "Detection"
44
data:
5-
path: "benchmark/data/qrcode"
5+
path: "data/qrcode"
66
files: ["opencv.png", "opencv_zoo.png"]
77
sizes:
88
- [100, 100]
@@ -16,7 +16,3 @@ Benchmark:
1616

1717
Model:
1818
name: "WeChatQRCode"
19-
detect_prototxt_path: "models/qrcode_wechatqrcode/detect_2021nov.prototxt"
20-
detect_model_path: "models/qrcode_wechatqrcode/detect_2021nov.caffemodel"
21-
sr_prototxt_path: "models/qrcode_wechatqrcode/sr_2021nov.prototxt"
22-
sr_model_path: "models/qrcode_wechatqrcode/sr_2021nov.caffemodel"

benchmark/config/text_detection_db.yaml

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Benchmark:
22
name: "Text Detection Benchmark"
33
type: "Detection"
44
data:
5-
path: "benchmark/data/text"
5+
path: "data/text"
66
files: ["1.jpg", "2.jpg", "3.jpg"]
77
sizes: # [[w1, h1], ...], Omit to run at original scale
88
- [640, 480]
@@ -15,8 +15,7 @@ Benchmark:
1515

1616
Model:
1717
name: "DB"
18-
modelPath: "models/text_detection_db/text_detection_DB_TD500_resnet18_2021sep.onnx"
1918
binaryThreshold: 0.3
2019
polygonThreshold: 0.5
2120
maxCandidates: 200
22-
unclipRatio: 2.0
21+
unclipRatio: 2.0

0 commit comments

Comments
 (0)