forked from cwlroda/falldetection_openpifpaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.py
168 lines (140 loc) · 5.49 KB
/
benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""Benchmark."""
import argparse
import datetime
import json
import logging
import os
import subprocess
import pysparkling
from . import __version__
LOG = logging.getLogger(__name__)
DEFAULT_BACKBONES = [
# 'shufflenetv2x1',
'shufflenetv2x2',
'resnet50',
# 'resnext50',
'resnet101',
'resnet152',
]
class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter,
argparse.RawDescriptionHelpFormatter):
pass
def cli():
parser = argparse.ArgumentParser(
prog='python3 -m openpifpaf.benchmark',
description=__doc__,
formatter_class=CustomFormatter,
)
parser.add_argument('--version', action='version',
version='OpenPifPaf {version}'.format(version=__version__))
parser.add_argument('--output', default=None,
help='output file name')
parser.add_argument('--backbones', default=DEFAULT_BACKBONES, nargs='+',
help='backbones to evaluate')
parser.add_argument('--iccv2019-ablation', default=False, action='store_true')
parser.add_argument('--dense-ablation', default=False, action='store_true')
group = parser.add_argument_group('logging')
group.add_argument('--debug', default=False, action='store_true',
help='print debug messages')
args, eval_args = parser.parse_known_args()
logging.basicConfig(level=logging.INFO if not args.debug else logging.DEBUG)
# default eval_args
if not eval_args:
eval_args = ['--all-images', '--loader-workers=8']
if '--all-images' not in eval_args:
LOG.info('adding "--all-images" to the argument list')
eval_args.append('--all-images')
if not any(l.startswith('--loader-workers') for l in eval_args):
LOG.info('adding "--loader-workers=8" to the argument list')
eval_args.append('--loader-workers=8')
# generate a default output filename
if args.output is None:
now = datetime.datetime.now().strftime('%y%m%d-%H%M%S')
args.output = 'outputs/benchmark-{}/'.format(now)
os.makedirs(args.output)
return args, eval_args
def run_eval_coco(output_folder, backbone, eval_args, output_name=None):
if output_name is None:
output_name = backbone
output_name = output_name.replace('/', '-')
out_file = os.path.join(output_folder, output_name)
if os.path.exists(out_file + '.stats.json'):
LOG.warning('Output file %s exists already. Skipping.',
out_file + '.stats.json')
return
LOG.debug('Launching eval for %s.', output_name)
subprocess.run([
'python', '-m', 'openpifpaf.eval_coco',
'--output', out_file,
'--checkpoint', backbone,
] + eval_args, check=True)
def main():
args, eval_args = cli()
if args.iccv2019_ablation:
assert len(args.backbones) == 1
multi_eval_args = [
eval_args,
eval_args + ['--connection-method=blend'],
eval_args + ['--connection-method=blend', '--long-edge=961', '--multi-scale',
'--no-multi-scale-hflip'],
eval_args + ['--connection-method=blend', '--long-edge=961', '--multi-scale'],
]
names = [
'singlescale-max',
'singlescale',
'multiscale-nohflip',
'multiscale',
]
for eval_args_i, name_i in zip(multi_eval_args, names):
run_eval_coco(args.output, args.backbones[0], eval_args_i, output_name=name_i)
elif args.dense_ablation:
multi_eval_args = [
eval_args,
eval_args + ['--dense-connections', '--dense-coupling=1.0'],
eval_args + ['--dense-connections'],
]
for backbone in args.backbones:
names = [
backbone,
'{}.wdense'.format(backbone),
'{}.wdense.whierarchy'.format(backbone),
]
for eval_args_i, name_i in zip(multi_eval_args, names):
run_eval_coco(args.output, backbone, eval_args_i, output_name=name_i)
else:
for backbone in args.backbones:
run_eval_coco(args.output, backbone, eval_args)
sc = pysparkling.Context()
stats = (
sc
.wholeTextFiles(args.output + '*.stats.json')
.mapValues(json.loads)
.map(lambda d: (d[0].replace('.stats.json', '').replace(args.output, ''), d[1]))
.collectAsMap()
)
LOG.debug('all data: %s', stats)
# pretty printing
# pylint: disable=line-too-long
print('| Backbone | AP | APM | APL | t_{total} [ms] | t_{dec} [ms] | size |')
print('|--------------------------:|:--------:|:--------:|:--------:|:---------------:|:------------:|---------:|')
for backbone, data in sorted(stats.items(), key=lambda b_d: b_d[1]['stats'][0]):
print(
'| {backbone: <25} '
'| __{AP:.1f}__ '
'| {APM: <8.1f} '
'| {APL: <8.1f} '
'| {t: <15.0f} '
'| {tdec: <12.0f} '
'| {file_size: >6.1f}MB '
'|'.format(
backbone='['+backbone+']',
AP=100.0 * data['stats'][0],
APM=100.0 * data['stats'][3],
APL=100.0 * data['stats'][4],
t=1000.0 * data['total_time'] / data['n_images'],
tdec=1000.0 * data['decoder_time'] / data['n_images'],
file_size=data['file_size'] / 1024 / 1024,
)
)
if __name__ == '__main__':
main()