forked from sungnyun/understanding-cdfsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths.py
159 lines (125 loc) · 4.47 KB
/
paths.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
import glob
import os
import re
from argparse import Namespace
import configs
DATASET_KEYS = {
'miniImageNet': 'mini',
'miniImageNet_test': 'mini_test',
'tieredImageNet': 'tiered',
'tieredImageNet_test': 'tiered_test',
'ImageNet': 'imagenet',
'CropDisease': 'crop',
'EuroSAT': 'euro',
'ISIC': 'isic',
'ChestX': 'chest',
'cars': 'cars',
'cub': 'cub',
'places': 'places',
'plantae': 'plantae',
}
BACKBONE_KEYS = {
'resnet10': 'resnet10',
'resnet18': 'resnet18',
'resnet50': 'resnet50',
}
MODEL_KEYS = {
'base': 'base',
'simclr': 'simclr',
'simsiam': 'simsiam',
'moco': 'moco',
'swav': 'swav',
'byol': 'byol',
}
def get_output_directory(params: Namespace, previous=False, makedirs=True):
"""
:param params:
:param previous: get previous output directory for pls, put, pmsl modes
:return:
"""
if previous and not (params.pls or params.put or params.pmsl):
raise ValueError('Invalid arguments for previous=True')
path = configs.save_dir
path = os.path.join(path, 'output')
path = os.path.join(path, DATASET_KEYS[params.source_dataset])
pretrain_specifiers = [BACKBONE_KEYS[params.backbone]]
if previous:
if params.pls:
pretrain_specifiers.append(MODEL_KEYS['base'])
else:
pretrain_specifiers.append(MODEL_KEYS[params.model])
if params.pls:
pretrain_specifiers.append('LS')
elif params.put:
pretrain_specifiers.append('UT')
elif params.pmsl:
pretrain_specifiers.append('LS_UT')
else:
raise AssertionError("Invalid parameters")
pretrain_specifiers.append(params.previous_tag)
else:
pretrain_specifiers.append(MODEL_KEYS[params.model])
if params.pls:
pretrain_specifiers.append('PLS')
if params.put:
pretrain_specifiers.append('PUT')
if params.pmsl:
pretrain_specifiers.append('PMSL')
if params.ls:
pretrain_specifiers.append('LS')
if params.us:
pretrain_specifiers.append('US')
if params.ut:
pretrain_specifiers.append('UT')
pretrain_specifiers.append(params.tag)
path = os.path.join(path, '_'.join(pretrain_specifiers))
if previous:
if params.put or params.pmsl:
path = os.path.join(path, DATASET_KEYS[params.target_dataset])
else:
if params.put or params.pmsl or params.ut:
path = os.path.join(path, DATASET_KEYS[params.target_dataset])
if makedirs:
os.makedirs(path, exist_ok=True)
return path
def get_pretrain_history_path(output_directory):
basename = 'pretrain_history.csv'
return os.path.join(output_directory, basename)
def get_pretrain_state_path(output_directory, epoch=0):
"""
:param output_directory:
:param epoch: Number of completed epochs. I.e., 0 = initial.
:return:
"""
basename = 'pretrain_state_{:04d}.pt'.format(epoch)
return os.path.join(output_directory, basename)
def get_final_pretrain_state_path(output_directory):
glob_pattern = os.path.join(output_directory, 'pretrain_state_*.pt')
paths = glob.glob(glob_pattern)
pattern = re.compile('pretrain_state_(\d{4}).pt')
paths_by_epoch = dict()
for path in paths:
match = pattern.search(path)
if match:
paths_by_epoch[match.group(1)] = path
if len(paths_by_epoch) == 0:
raise FileNotFoundError('Could not find valid pre-train state file in {}'.format(output_directory))
max_epoch = max(paths_by_epoch.keys())
return paths_by_epoch[max_epoch]
def get_pretrain_params_path(output_directory):
return os.path.join(output_directory, 'pretrain_params.json')
def get_ft_output_directory(params, makedirs=True):
path = get_output_directory(params, makedirs=makedirs)
if not params.ut:
path = os.path.join(path, params.target_dataset)
ft_basename = '{:02d}way_{:03d}shot_{}_{}'.format(params.n_way, params.n_shot, params.ft_parts, params.ft_tag)
path = os.path.join(path, ft_basename)
if makedirs:
os.makedirs(path, exist_ok=True)
return path
def get_ft_params_path(output_directory):
return os.path.join(output_directory, 'params.json')
def get_ft_train_history_path(output_directory):
return os.path.join(output_directory, 'train_history.csv')
def get_ft_test_history_path(output_directory):
return os.path.join(output_directory, 'test_history.csv')