You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
893 B
23 lines
893 B
4 years ago
|
import os
|
||
2 years ago
|
import pickle
|
||
4 years ago
|
|
||
3 years ago
|
def load_class_map(map_or_filename, root=''):
|
||
|
if isinstance(map_or_filename, dict):
|
||
|
assert dict, 'class_map dict must be non-empty'
|
||
|
return map_or_filename
|
||
|
class_map_path = map_or_filename
|
||
4 years ago
|
if not os.path.exists(class_map_path):
|
||
3 years ago
|
class_map_path = os.path.join(root, class_map_path)
|
||
|
assert os.path.exists(class_map_path), 'Cannot locate specified class map file (%s)' % map_or_filename
|
||
|
class_map_ext = os.path.splitext(map_or_filename)[-1].lower()
|
||
4 years ago
|
if class_map_ext == '.txt':
|
||
|
with open(class_map_path) as f:
|
||
|
class_to_idx = {v.strip(): k for k, v in enumerate(f)}
|
||
2 years ago
|
elif class_map_ext == '.pkl':
|
||
|
with open(class_map_path,'rb') as f:
|
||
|
class_to_idx = pickle.load(f)
|
||
4 years ago
|
else:
|
||
3 years ago
|
assert False, f'Unsupported class map file extension ({class_map_ext}).'
|
||
4 years ago
|
return class_to_idx
|
||
|
|