7
7
# detected before a commit.
8
8
9
9
import glob
10
+ import os
10
11
import os .path as osp
11
12
import re
12
13
import sys
13
14
import warnings
15
+ from functools import reduce
14
16
15
17
import mmcv
16
18
@@ -76,7 +78,7 @@ def get_task_name(md_file):
76
78
"""Get task name from README.md".
77
79
78
80
Args:
79
- md_file: Path to .md file.
81
+ md_file (str) : Path to .md file.
80
82
81
83
Returns:
82
84
Str: Task name.
@@ -88,14 +90,50 @@ def get_task_name(md_file):
88
90
return 'Unknown'
89
91
90
92
93
+ def generate_unique_name (md_file ):
94
+ """Search config files and return the unique name of them.
95
+ For Confin.Name.
96
+
97
+ Args:
98
+ md_file (str): Path to .md file.
99
+ Returns:
100
+ dict: dict of unique name for each config file.
101
+ """
102
+ files = os .listdir (osp .dirname (md_file ))
103
+ config_files = [f [:- 3 ] for f in files if f [- 3 :] == '.py' ]
104
+ config_files .sort ()
105
+ config_files .sort (key = lambda x : len (x ))
106
+ split_names = [f .split ('_' ) for f in config_files ]
107
+ config_sets = [set (f .split ('_' )) for f in config_files ]
108
+ common_set = reduce (lambda x , y : x & y , config_sets )
109
+ unique_lists = [[n for n in name if n not in common_set ]
110
+ for name in split_names ]
111
+
112
+ unique_dict = dict ()
113
+ name_list = []
114
+ for i , f in enumerate (config_files ):
115
+ base = split_names [i ][0 ]
116
+ unique_dict [f ] = base
117
+ if len (unique_lists [i ]) > 0 :
118
+ for unique in unique_lists [i ]:
119
+ candidate_name = f'{ base } _{ unique } '
120
+ if candidate_name not in name_list and base != unique :
121
+ unique_dict [f ] = candidate_name
122
+ name_list .append (candidate_name )
123
+ break
124
+ return unique_dict
125
+
126
+
91
127
def parse_md (md_file ):
92
128
"""Parse .md file and convert it to a .yml file which can be used for MIM.
93
129
94
130
Args:
95
- md_file: Path to .md file.
131
+ md_file (str) : Path to .md file.
96
132
Returns:
97
133
Bool: If the target YAML file is different from the original.
98
134
"""
135
+ unique_dict = generate_unique_name (md_file )
136
+
99
137
collection_name = osp .splitext (osp .basename (md_file ))[0 ]
100
138
collection = dict (
101
139
Name = collection_name ,
@@ -125,7 +163,8 @@ def parse_md(md_file):
125
163
126
164
# parse table
127
165
elif lines [i ][0 ] == '|' and i + 1 < len (lines ) and \
128
- (lines [i + 1 ][:3 ] == '| :' or lines [i + 1 ][:2 ] == '|:' ):
166
+ (lines [i + 1 ][:3 ] == '| :' or lines [i + 1 ][:2 ] == '|:'
167
+ or lines [i + 1 ][:2 ] == '|-' ):
129
168
cols = [col .strip () for col in lines [i ].split ('|' )][1 :- 1 ]
130
169
config_idx = cols .index ('Method' )
131
170
checkpoint_idx = cols .index ('Download' )
@@ -157,8 +196,14 @@ def parse_md(md_file):
157
196
right = line [checkpoint_idx ].index (')' , left )
158
197
checkpoint = line [checkpoint_idx ][left :right ]
159
198
160
- model_name = osp .splitext (config )[0 ].replace (
161
- 'configs/' , '' , 1 ).replace ('/' , '--' )
199
+ name_key = osp .splitext (osp .basename (config ))[0 ]
200
+ if name_key in unique_dict :
201
+ model_name = unique_dict [name_key ]
202
+ else :
203
+ model_name = name_key
204
+ warnings .warn (
205
+ f'Config file of { model_name } is not found,'
206
+ 'please check it again.' )
162
207
163
208
# find dataset in config file
164
209
dataset = 'Others'
0 commit comments