|
| 1 | +# Copyright (c) OpenMMLab. All rights reserved. |
| 2 | +import argparse |
| 3 | +import datetime |
| 4 | +import json |
| 5 | +import os |
| 6 | +import os.path as osp |
| 7 | +from collections import OrderedDict |
| 8 | + |
| 9 | +from utils import load_config |
| 10 | + |
| 11 | +# automatically collect all the results |
| 12 | + |
| 13 | +# The structure of the directory: |
| 14 | +# ├── work-dir |
| 15 | +# │ ├── config_1 |
| 16 | +# │ │ ├── time1.log.json |
| 17 | +# │ │ ├── time2.log.json |
| 18 | +# │ │ ├── time3.log.json |
| 19 | +# │ │ ├── time4.log.json |
| 20 | +# │ ├── config_2 |
| 21 | +# │ │ ├── time5.log.json |
| 22 | +# │ │ ├── time6.log.json |
| 23 | +# │ │ ├── time7.log.json |
| 24 | +# │ │ ├── time8.log.json |
| 25 | + |
| 26 | + |
| 27 | +def parse_args(): |
| 28 | + parser = argparse.ArgumentParser(description='extract info from log.json') |
| 29 | + parser.add_argument('config_dir') |
| 30 | + args = parser.parse_args() |
| 31 | + return args |
| 32 | + |
| 33 | + |
| 34 | +def has_keyword(name: str, keywords: list): |
| 35 | + for a_keyword in keywords: |
| 36 | + if a_keyword in name: |
| 37 | + return True |
| 38 | + return False |
| 39 | + |
| 40 | + |
| 41 | +def main(): |
| 42 | + args = parse_args() |
| 43 | + cfg = load_config(args.config_dir) |
| 44 | + work_dir = cfg['work_dir'] |
| 45 | + metric = cfg['metric'] |
| 46 | + log_items = cfg.get('log_items', []) |
| 47 | + ignore_keywords = cfg.get('ignore_keywords', []) |
| 48 | + other_info_keys = cfg.get('other_info_keys', []) |
| 49 | + markdown_file = cfg.get('markdown_file', None) |
| 50 | + json_file = cfg.get('json_file', None) |
| 51 | + |
| 52 | + if json_file and osp.split(json_file)[0] != '': |
| 53 | + os.makedirs(osp.split(json_file)[0], exist_ok=True) |
| 54 | + if markdown_file and osp.split(markdown_file)[0] != '': |
| 55 | + os.makedirs(osp.split(markdown_file)[0], exist_ok=True) |
| 56 | + |
| 57 | + assert not (log_items and ignore_keywords), \ |
| 58 | + 'log_items and ignore_keywords cannot be specified at the same time' |
| 59 | + assert metric not in other_info_keys, \ |
| 60 | + 'other_info_keys should not contain metric' |
| 61 | + |
| 62 | + if ignore_keywords and isinstance(ignore_keywords, str): |
| 63 | + ignore_keywords = [ignore_keywords] |
| 64 | + if other_info_keys and isinstance(other_info_keys, str): |
| 65 | + other_info_keys = [other_info_keys] |
| 66 | + if log_items and isinstance(log_items, str): |
| 67 | + log_items = [log_items] |
| 68 | + |
| 69 | + if not log_items: |
| 70 | + log_items = [ |
| 71 | + item for item in sorted(os.listdir(work_dir)) |
| 72 | + if not has_keyword(item, ignore_keywords) |
| 73 | + ] |
| 74 | + |
| 75 | + experiment_info_list = [] |
| 76 | + for config_dir in log_items: |
| 77 | + preceding_path = os.path.join(work_dir, config_dir) |
| 78 | + log_list = [ |
| 79 | + item for item in os.listdir(preceding_path) |
| 80 | + if item.endswith('.log.json') |
| 81 | + ] |
| 82 | + log_list = sorted( |
| 83 | + log_list, |
| 84 | + key=lambda time_str: datetime.datetime.strptime( |
| 85 | + time_str, '%Y%m%d_%H%M%S.log.json')) |
| 86 | + val_list = [] |
| 87 | + last_iter = 0 |
| 88 | + for log_name in log_list: |
| 89 | + with open(os.path.join(preceding_path, log_name), 'r') as f: |
| 90 | + # ignore the info line |
| 91 | + f.readline() |
| 92 | + all_lines = f.readlines() |
| 93 | + val_list.extend([ |
| 94 | + json.loads(line) for line in all_lines |
| 95 | + if json.loads(line)['mode'] == 'val' |
| 96 | + ]) |
| 97 | + for index in range(len(all_lines) - 1, -1, -1): |
| 98 | + line_dict = json.loads(all_lines[index]) |
| 99 | + if line_dict['mode'] == 'train': |
| 100 | + last_iter = max(last_iter, line_dict['iter']) |
| 101 | + break |
| 102 | + |
| 103 | + new_log_dict = dict( |
| 104 | + method=config_dir, metric_used=metric, last_iter=last_iter) |
| 105 | + for index, log in enumerate(val_list, 1): |
| 106 | + new_ordered_dict = OrderedDict() |
| 107 | + new_ordered_dict['eval_index'] = index |
| 108 | + new_ordered_dict[metric] = log[metric] |
| 109 | + for key in other_info_keys: |
| 110 | + if key in log: |
| 111 | + new_ordered_dict[key] = log[key] |
| 112 | + val_list[index - 1] = new_ordered_dict |
| 113 | + |
| 114 | + assert len(val_list) >= 1, \ |
| 115 | + f"work dir {config_dir} doesn't contain any evaluation." |
| 116 | + new_log_dict['last eval'] = val_list[-1] |
| 117 | + new_log_dict['best eval'] = max(val_list, key=lambda x: x[metric]) |
| 118 | + experiment_info_list.append(new_log_dict) |
| 119 | + print(f'{config_dir} is processed') |
| 120 | + |
| 121 | + if json_file: |
| 122 | + with open(json_file, 'w') as f: |
| 123 | + json.dump(experiment_info_list, f, indent=4) |
| 124 | + |
| 125 | + if markdown_file: |
| 126 | + lines_to_write = [] |
| 127 | + for index, log in enumerate(experiment_info_list, 1): |
| 128 | + lines_to_write.append( |
| 129 | + f"|{index}|{log['method']}|{log['best eval'][metric]}" |
| 130 | + f"|{log['best eval']['eval_index']}|" |
| 131 | + f"{log['last eval'][metric]}|" |
| 132 | + f"{log['last eval']['eval_index']}|{log['last_iter']}|\n") |
| 133 | + with open(markdown_file, 'w') as f: |
| 134 | + f.write(f'|exp_num|method|{metric} best|best index|' |
| 135 | + f'{metric} last|last index|last iter num|\n') |
| 136 | + f.write('|:---:|:---:|:---:|:---:|:---:|:---:|:---:|\n') |
| 137 | + f.writelines(lines_to_write) |
| 138 | + |
| 139 | + print('processed successfully') |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == '__main__': |
| 143 | + main() |
0 commit comments