Skip to content

Commit 3cb1a70

Browse files
authored
[Feature]: Support automaticaly gathering metrics to execl and fix total_epochs (#4693)
1 parent 1085e3c commit 3cb1a70

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

.dev_scripts/gather_benchmark_metric.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
import mmcv
66
from gather_models import get_final_results
77

8+
try:
9+
import xlrd
10+
except ImportError:
11+
xlrd = None
12+
try:
13+
import xlutils
14+
from xlutils.copy import copy
15+
except ImportError:
16+
xlutils = None
17+
818

919
def parse_args():
1020
parser = argparse.ArgumentParser(
@@ -19,6 +29,10 @@ def parse_args():
1929
'--out', type=str, help='output path of gathered metrics to be stored')
2030
parser.add_argument(
2131
'--not-show', action='store_true', help='not show metrics')
32+
parser.add_argument(
33+
'--excel', type=str, help='input path of excel to be recorded')
34+
parser.add_argument(
35+
'--ncol', type=int, help='Number of column to be modified or appended')
2236

2337
args = parser.parse_args()
2438
return args
@@ -27,6 +41,26 @@ def parse_args():
2741
if __name__ == '__main__':
2842
args = parse_args()
2943

44+
if args.excel:
45+
assert args.ncol, 'Please specify "--excel" and "--ncol" ' \
46+
'at the same time'
47+
if xlrd is None:
48+
raise RuntimeError(
49+
'xlrd is not installed,'
50+
'Please use “pip install xlrd==1.2.0” to install')
51+
if xlutils is None:
52+
raise RuntimeError(
53+
'xlutils is not installed,'
54+
'Please use “pip install xlutils==2.0.0” to install')
55+
readbook = xlrd.open_workbook(args.excel)
56+
sheet = readbook.sheet_by_name('Sheet1')
57+
sheet_info = {}
58+
total_nrows = sheet.nrows
59+
for i in range(3, sheet.nrows):
60+
sheet_info[sheet.row_values(i)[0]] = i
61+
xlrw = copy(readbook)
62+
table = xlrw.get_sheet(0)
63+
3064
root_path = args.root
3165
metrics_out = args.out
3266
benchmark_json_path = args.benchmark_json
@@ -40,7 +74,7 @@ def parse_args():
4074
if osp.exists(result_path):
4175
# 1 read config
4276
cfg = mmcv.Config.fromfile(config)
43-
total_epochs = cfg.total_epochs
77+
total_epochs = cfg.runner.max_epochs
4478
final_results = cfg.evaluation.metric
4579
if not isinstance(final_results, list):
4680
final_results = [final_results]
@@ -64,7 +98,30 @@ def parse_args():
6498
if model_performance is None:
6599
print(f'log file error: {log_json_path}')
66100
continue
101+
for performance in model_performance:
102+
if performance in ['AR@1000', 'bbox_mAP', 'segm_mAP']:
103+
metric = round(model_performance[performance] * 100, 1)
104+
model_performance[performance] = metric
67105
result_dict[config] = model_performance
106+
107+
# update and append excel content
108+
if args.excel:
109+
if 'AR@1000' in model_performance:
110+
metrics = f'{model_performance["AR@1000"]}(AR@1000)'
111+
elif 'segm_mAP' in model_performance:
112+
metrics = f'{model_performance["bbox_mAP"]}/' \
113+
f'{model_performance["segm_mAP"]}'
114+
else:
115+
metrics = f'{model_performance["bbox_mAP"]}'
116+
117+
row_num = sheet_info.get(config, None)
118+
if row_num:
119+
table.write(row_num, args.ncol, metrics)
120+
else:
121+
table.write(total_nrows, 0, config)
122+
table.write(total_nrows, args.ncol, metrics)
123+
total_nrows += 1
124+
68125
else:
69126
print(f'{config} not exist: {ckpt_path}')
70127
else:
@@ -79,3 +136,7 @@ def parse_args():
79136
for config_name, metrics in result_dict.items():
80137
print(config_name, metrics)
81138
print('===================================')
139+
if args.excel:
140+
filename, sufflx = osp.splitext(args.excel)
141+
xlrw.save(f'{filename}_o{sufflx}')
142+
print(f'>>> Output {filename}_o{sufflx}')

0 commit comments

Comments
 (0)