-
Notifications
You must be signed in to change notification settings - Fork 1
/
maml_ssl_main_LST.py
259 lines (223 loc) · 13.9 KB
/
maml_ssl_main_LST.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
# os.environ['CUDA_VISIBLE_DEVICES'] = '1,2,3,4,5'
import time
import json
import logging
import math
import pickle
import torch
from collections import defaultdict
from configuration import arg_parser
import common_tools as ct
from datasets_meta.dataloader_meta import BatchMetaDataLoader
from maml.datasets_benchmark import get_benchmark_by_name
from maml.metalearners import ModelAgnosticMetaLearningLST
args = arg_parser.parse_args()
ct.set_random_seeds(args.seed)
INTERVAL = 50
INTERVAL_VAL = args.interval_val
def cat_data(result_dict, new_dict):
for key, values in new_dict.items():
result_dict[key] += values
return result_dict
def append_data(result_dict, new_dict):
for key, values in new_dict.items():
result_dict[key].append(values)
return result_dict
def maml_ssl_main(args, device):
benchmark = get_benchmark_by_name(args.dataset,
args.data_folder,
args.scenario,
args.num_ways,
args.num_shots, # shots in support set
args.num_shots_test_meta_train, # shots in query set for meta-train
args.num_shots_test_meta_test, # shots in query set for meta-test
args.num_shots_unlabeled, # num of unlabeled images for meta-train tasks
args.num_shots_unlabeled_evaluate, # num of unlabeled images per class
args.num_classes_distractor, # with distractor
args.num_shots_distractor, # with distractor
args.num_shots_distractor_eval, # with distractor
args.num_unlabel_total, # for "random"
args.num_unlabel_total_evaluate, # for "random"
hidden_size=args.hidden_size)
meta_train_dataloader = BatchMetaDataLoader(benchmark.meta_train_dataset,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers,
pin_memory=False) # possible to avoid the leaking caffe2 warning
meta_valid_dataloader = BatchMetaDataLoader(benchmark.meta_val_dataset,
batch_size=args.batch_size_val,
shuffle=True, # make it to be false to debug
num_workers=args.num_workers,
pin_memory=False)
meta_test_dataloader = BatchMetaDataLoader(benchmark.meta_test_dataset,
batch_size=args.batch_size_test,
shuffle=True, # make it to be false to debug
num_workers=args.num_workers,
pin_memory=False)
meta_optimizer = torch.optim.Adam(benchmark.model.parameters(), lr=args.meta_lr)
optimizer_swn = torch.optim.Adam(benchmark.swn_model.parameters(), lr=args.swn_lr)
# debugging
metalearner = ModelAgnosticMetaLearningLST(benchmark.model,
benchmark.swn_model,
meta_optimizer,
optimizer_swn,
step_size=args.step_size,
first_order=args.first_order,
num_adaptation_steps=args.num_steps,
num_adaptation_steps_test=args.num_steps_evaluate,
loss_function=benchmark.loss_function,
device=device)
best_value = None
real_datasets = ["miniimagenet", "omniglot", "tieredimagenet", "cifarfs"]
results_train = defaultdict(list) # store all results from meta-training
results_valid = defaultdict(list) # store all results from meta-validation
results_test = defaultdict(list)
results_mean_val_tst_epochs = {"mean_loss_val": [],
"mean_accu_val": [],
"mean_loss_tst": [],
"mean_accu_tst": [],
"accus_tst_ci": []} # loss and accu of query set from meta-validation
# Training loop
epoch_desc_train = 'Epoch {{0: <{0}d}} (meta-train)'.format(1 + int(math.log10(args.num_epochs)))
epoch_desc_val = 'Epoch {{0: <{0}d}} (meta-valid)'.format(1 + int(math.log10(args.num_epochs)))
epoch_desc_tst = 'Epoch {{0: <{0}d}} (meta-test)'.format(1 + int(math.log10(args.num_epochs)))
# # load the saved variables to resume the training
# if args.resume:
# # saved results
# with open(path_resume+ "results_train.pkl", "rb") as f:
# results_train = pickle.load(f)
# with open(path_resume+ "results_valid.pkl", "rb") as f:
# results_valid = pickle.load(f)
# with open(path_resume+ "results_test.pkl", "rb") as f:
# results_test = pickle.load(f)
# with open(path_resume+ "results_mean_valid_test.json", "rb") as f:
# results_mean_val_tst_epochs = json.load(f)
start_epoch = 0
for epoch in range(start_epoch+1, args.num_epochs+1):
# meta training
result_train_per_epoch = metalearner.train(meta_train_dataloader,
max_batches=args.num_batches,
batch_size=args.batch_size,
verbose=args.verbose,
progress=epoch,
desc=epoch_desc_train.format(epoch),
)
results_train = cat_data(results_train, result_train_per_epoch)
if epoch % INTERVAL_VAL == 0 and epoch >= 100:
# meta validation
if args.ssl_algo == "VAT":
results_mean_val, results_all_tasks_val, _ = metalearner.evaluate(meta_valid_dataloader,
max_batches=args.num_batches,
batch_size=args.batch_size_val,
verbose=args.verbose,
progress=epoch,
desc=epoch_desc_val.format(epoch))
results_valid = append_data(results_valid, results_all_tasks_val)
results_mean_val_tst_epochs["mean_loss_val"].append(results_mean_val['mean_outer_loss'])
results_mean_val_tst_epochs["mean_accu_val"].append(results_mean_val["accuracies_after"])
else:
results_mean_val, results_all_tasks_val = {}, {}
results_mean_val['accuracies_after'] = 0
results_mean_val['mean_outer_loss'] = 0
# meta test
# results_mean_tst, results_all_tasks_tst = {}, {}
if args.dataset in real_datasets:
results_mean_tst, results_all_tasks_tst, ci95 = metalearner.evaluate(meta_test_dataloader,
max_batches=args.num_batches_eval,
batch_size=args.batch_size_test,
verbose=args.verbose,
progress=epoch,
desc=epoch_desc_tst.format(epoch))
results_test = append_data(results_test, results_all_tasks_tst)
results_mean_val_tst_epochs["mean_loss_tst"].append(results_mean_tst['mean_outer_loss'])
results_mean_val_tst_epochs["mean_accu_tst"].append(results_mean_tst["accuracies_after"])
results_mean_val_tst_epochs["accus_tst_ci"].append(ci95)
else:
results_mean_tst, results_all_tasks_tst = {}, {}
# save the validation acc and loss during each epoch
rst_path_valid_test = os.path.abspath(os.path.join(args.output_subfolder, "results_mean_valid_test.json"))
with open(rst_path_valid_test, "w") as f:
json.dump(results_mean_val_tst_epochs, f, indent=2)
# ### Save the best model based on validation set
save_model = False
if 'accuracies_after' in results_mean_val:
if (best_value is None) or (best_value < results_mean_val['accuracies_after']):
best_value = results_mean_val['accuracies_after']
save_model = True
elif (best_value is None) or (best_value > results_mean_val['mean_outer_loss']):
best_value = results_mean_val['mean_outer_loss']
save_model = True
else:
save_model = False
if save_model and (args.output_folder is not None):
print(f"^^^^^ Best model at Epoch: {epoch}")
best_epoch={"epoch": epoch,
"valid": results_mean_val,
"test": results_mean_tst,
}
with open(f"{args.model_path}.th", 'wb') as f:
# with open(f"{args.model_path}_epoch_{epoch}.th", 'wb') as f:
torch.save(benchmark.model.state_dict(), f)
with open(args.result_path, 'wb') as handle:
pickle.dump(best_epoch, handle, protocol=pickle.HIGHEST_PROTOCOL)
# ###
# save model every interval_val epochs
with open(f"{args.model_path}_epoch_{epoch}.th", 'wb') as f:
torch.save(benchmark.model.state_dict(), f)
# save some intemediate results, overwrite them epoch by epoch
result_train_path = os.path.abspath(os.path.join(args.output_subfolder, f'results_train.pkl'))
with open(result_train_path, "wb") as f:
pickle.dump(results_train, f, protocol=pickle.HIGHEST_PROTOCOL)
result_valid_path = os.path.abspath(os.path.join(args.output_subfolder, f"results_valid.pkl"))
with open(result_valid_path, "wb") as f:
pickle.dump(results_valid, f, protocol=pickle.HIGHEST_PROTOCOL)
result_test_path = os.path.abspath(os.path.join(args.output_subfolder, f"results_test.pkl"))
with open(result_test_path, "wb") as f:
pickle.dump(results_test, f, protocol=pickle.HIGHEST_PROTOCOL)
if hasattr(benchmark.meta_train_dataset, 'close'):
benchmark.meta_train_dataset.close()
benchmark.meta_val_dataset.close()
def main():
start = time.time() # float
ct.create_path(args.output_folder)
if args.dataset == "miniimagenet":
args.data_folder = "/home/cxl173430/data/DATASETS/miniimagenet_test"
else:
args.data_folder = "/home/cxl173430/data/DATASETS"
# base_path/ssl_path/N-way K-shot/specific_model
# base model folder, storing all results of all experiments
base_path = os.path.join(args.output_folder, f"Baseline_LST_firstOrder_{args.first_order}")
base_path = os.path.join(base_path, f"{args.dataset}_{args.scenario}_#way_{args.num_ways}_#shot_{args.num_shots}")
f"Baseline_LST_{args.output_folder}"
# specific model folder, storing the specific model (specific combination in the configure file)
ssl_path = os.path.join(base_path, f"{args.selection_option_LST}_InnerLoopSteps_{args.inStepsSet}")
specific_file_name = '_'.join(['LabelRatio', str(args.ratio),'#OOD', str(args.num_classes_distractor), '#ShotU', str(args.num_shots_unlabeled),
'TopZs', str(args.pl_num_topz), "TrueLabel", str(args.select_true_label),
time.strftime('%Y-%m-%d-%H%M%S')])
specific_model_path = os.path.join(ssl_path, specific_file_name)
ct.create_path(specific_model_path)
args.output_subfolder = os.path.abspath(specific_model_path) # absolute path
ct.set_logger('{}/log_file_outerLossAcc_seed_{}'.format(args.output_subfolder, args.seed), 'err') # log file
# ct.set_logger('{}/log_file_seed_{}'.format(args.output_subfolder, args.seed), 'out') # log file
print('Random Seed: {}'.format(args.seed))
ct.set_random_seeds(args.seed)
device = ct.set_device(args.gpu_id)
args.model_path = os.path.abspath(os.path.join(args.output_subfolder, 'best_model'))
args.result_path = os.path.abspath(os.path.join(args.output_subfolder, 'best_model_valid_test_result.pkl'))
# save the config, json is better here because it is easily to open directly
with open(os.path.join(args.output_subfolder, 'config.json'), 'w') as f:
json.dump(vars(args), f, indent=2)
print("****** Model parameters: \n{")
for key, value in vars(args).items():
print(f"{key} : {value}")
print("} ****** \n")
maml_ssl_main(args, device)
time_used = "{:.2f}".format(time.time() - start)
print(f"Total time used: {time_used}")
with open(os.path.join(args.output_subfolder, 'others.json'), 'w') as fi:
json.dump({"TimeUsed (s)": float(time_used)}, fi, indent=2)
if __name__ == "__main__":
# torch.cuda.set_per_process_memory_fraction(0.9, 0)
main()