-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
346 lines (238 loc) · 12.8 KB
/
main.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"""==================================================================================================="""
################### LIBRARIES ###################
### Basic Libraries
import warnings
warnings.filterwarnings("ignore")
import os, sys, numpy as np, argparse, imp, datetime, pandas as pd, copy
import time, pickle as pkl, random, json, collections
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
from tqdm import tqdm
import parameters as par
import pdb
"""==================================================================================================="""
################### INPUT ARGUMENTS ###################
parser = argparse.ArgumentParser()
parser = par.basic_training_parameters(parser)
parser = par.batch_creation_parameters(parser)
parser = par.batchmining_specific_parameters(parser)
parser = par.loss_specific_parameters(parser)
parser = par.wandb_parameters(parser)
##### Read in parameters
opt = parser.parse_args()
"""==================================================================================================="""
if opt.savename=='group_plus_seed':
if opt.log_online:
opt.savename = opt.group+'_s{}'.format(opt.seed)
else:
opt.savename = ''
### If wandb-logging is turned on, initialize the wandb-run here:
if opt.log_online:
if opt.online_backend=='comet_ml':
from comet_ml import Experiment
experiment = Experiment(api_key=opt.comet_api_key, project_name=opt.project, auto_metric_logging=False)
experiment.set_name(opt.savename)
experiment.log_parameters(copy.deepcopy(vars(opt)))
opt.experiment = experiment
elif opt.online_backend=='wandb':
import wandb
_ = os.system('wandb login {}'.format(opt.wandb_key))
os.environ['WANDB_API_KEY'] = opt.wandb_key
wandb.init(project=opt.project, group=opt.group, name=opt.savename, dir=opt.save_path)
wandb.config.update(opt)
"""==================================================================================================="""
### Load Remaining Libraries that neeed to be loaded after comet_ml
import torch, torch.nn as nn
import torch.multiprocessing
torch.multiprocessing.set_sharing_strategy('file_system')
import architectures as archs
import datasampler as dsamplers
import datasets as datasets
import criteria as criteria
import metrics as metrics
import evaluation as eval
from utilities import misc
from utilities import logger
def copyStateDict(state_dict):
if list(state_dict.keys())[0].startswith("module"):
start_idx = 1
else:
start_idx = 0
new_state_dict = dict()
for k, v in state_dict.items():
name = ".".join(k.split(".")[start_idx:])
new_state_dict[name] = v
return new_state_dict
"""==================================================================================================="""
full_training_start_time = time.time()
"""==================================================================================================="""
opt.source_path += '/'+opt.dataset
opt.save_path += '/'+opt.dataset
#Assert that the construction of the batch makes sense, i.e. the division into class-subclusters.
assert not opt.bs%opt.samples_per_class, 'Batchsize needs to fit number of samples per class for distance sampling and margin/triplet loss!'
opt.pretrained = not opt.not_pretrained
"""==================================================================================================="""
################### GPU SETTINGS ###########################
os.environ["CUDA_DEVICE_ORDER"] ="PCI_BUS_ID"
# if not opt.use_data_parallel:
#os.environ["CUDA_VISIBLE_DEVICES"] ="3"
#pdb.set_trace()
"""==================================================================================================="""
#################### SEEDS FOR REPROD. #####################
torch.backends.cudnn.deterministic=True; np.random.seed(opt.seed); random.seed(opt.seed)
torch.manual_seed(opt.seed); torch.cuda.manual_seed(opt.seed); torch.cuda.manual_seed_all(opt.seed)
"""==================================================================================================="""
##################### NETWORK SETUP ##################
opt.device = torch.device('cuda')
model = archs.select(opt.arch, opt)
if opt.fc_lr<0:
to_optim = [{'params':model.parameters(),'lr':opt.lr,'weight_decay':opt.decay}]
else:
all_but_fc_params = [x[-1] for x in list(filter(lambda x: 'last_linear' not in x[0], model.named_parameters()))]
fc_params = model.model.last_linear.parameters()
to_optim = [{'params':all_but_fc_params,'lr':opt.lr,'weight_decay':opt.decay},
{'params':fc_params,'lr':opt.fc_lr,'weight_decay':opt.decay}]
_ = model.to(opt.device)
if opt.resume != None:
model.load_state_dict(copyStateDict(torch.load(opt.resume)['state_dict']))
if opt.multi_gpu:
model=nn.DataParallel(model,device_ids=[0,1])
"""============================================================================"""
#################### DATALOADER SETUPS ##################
dataloaders = {}
datasets = datasets.select(opt.dataset, opt, opt.source_path)
dataloaders['evaluation'] = torch.utils.data.DataLoader(datasets['evaluation'], num_workers=opt.kernels, batch_size=opt.bs, shuffle=False,pin_memory = True)
dataloaders['testing'] = torch.utils.data.DataLoader(datasets['testing'], num_workers=opt.kernels, batch_size=opt.bs, shuffle=False,pin_memory = True)
if opt.use_tv_split:
dataloaders['validation'] = torch.utils.data.DataLoader(datasets['validation'], num_workers=opt.kernels, batch_size=opt.bs,shuffle=False,pin_memory = True)
train_data_sampler = dsamplers.select(opt.data_sampler, opt, datasets['training'].image_dict, datasets['training'].image_list)
if train_data_sampler.requires_storage:
train_data_sampler.create_storage(dataloaders['evaluation'], model, opt.device)
dataloaders['training'] = torch.utils.data.DataLoader(datasets['training'], num_workers=opt.kernels, batch_sampler=train_data_sampler,pin_memory = True)
opt.n_classes = len(dataloaders['training'].dataset.avail_classes)
"""============================================================================"""
#################### CREATE LOGGING FILES ###############
sub_loggers = ['Train', 'Test', 'Model Grad']
if opt.use_tv_split: sub_loggers.append('Val')
LOG = logger.LOGGER(opt, sub_loggers=sub_loggers, start_new=True, log_online=opt.log_online)
"""============================================================================"""
#################### LOSS SETUP ####################
# batchminer = bmine.select(opt.batch_mining, opt)
criterion, to_optim = criteria.select(opt.loss, opt, to_optim)
_ = criterion.to(opt.device)
if 'criterion' in train_data_sampler.name:
train_data_sampler.internal_criterion = criterion
"""============================================================================"""
#################### OPTIM SETUP ####################
if opt.optim == 'adam':
optimizer = torch.optim.Adam(to_optim)
elif opt.optim == 'sgd':
optimizer = torch.optim.SGD(to_optim, momentum=0.9)
else:
raise Exception('Optimizer <{}> not available!'.format(opt.optim))
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=opt.tau, gamma=opt.gamma)
"""============================================================================"""
#################### METRIC COMPUTER ####################
opt.rho_spectrum_embed_dim = opt.embed_dim
metric_computer = metrics.MetricComputer(opt.evaluation_metrics, opt)
"""============================================================================"""
################### Summary #########################3
data_text = 'Dataset:\t {}'.format(opt.dataset.upper())
setup_text = 'Objective:\t {}'.format(opt.loss.upper())
miner_text = 'Batchminer:\t {}'.format(opt.batch_mining if criterion.REQUIRES_BATCHMINER else 'N/A')
arch_text = 'Backbone:\t {} (#weights: {})'.format(opt.arch.upper(), misc.gimme_params(model))
summary = data_text+'\n'+setup_text+'\n'+miner_text+'\n'+arch_text
print(summary)
"""============================================================================"""
################### SCRIPT MAIN ##########################
print('\n-----\n')
iter_count = 0
loss_args = {'batch':None, 'labels':None, 'batch_features':None, 'f_embed':None}
for epoch in range(opt.n_epochs):
epoch_start_time = time.time()
if epoch>0 and opt.data_idx_full_prec and train_data_sampler.requires_storage:
train_data_sampler.full_storage_update(dataloaders['evaluation'], model, opt.device)
opt.epoch = epoch
### Scheduling Changes specifically for cosine scheduling
if opt.scheduler!='none': print('Running with learning rates {}...'.format(' | '.join('{}'.format(x) for x in scheduler.get_lr())))
"""======================================="""
if train_data_sampler.requires_storage:
train_data_sampler.precompute_indices()
"""======================================="""
### Train one epoch
start = time.time()
_ = model.train()
loss_collect = []
data_iterator = tqdm(dataloaders['training'], desc='Epoch {} Training...'.format(epoch))
for i,out in enumerate(data_iterator):
class_labels, input, input_indices = out
### Compute Embedding
input = input.to(opt.device)
model_args = {'x':input.to(opt.device)}
# Needed for MixManifold settings.
if 'mix' in opt.arch: model_args['labels'] = class_labels
embeds = model(**model_args)
if isinstance(embeds, tuple): embeds, (avg_features, features) = embeds
### Compute Loss
loss_args['batch'] = embeds
loss_args['labels'] = class_labels
if opt.multi_gpu:
loss_args['f_embed'] = model.module.model.last_linear
else:
loss_args['f_embed'] = model.model.last_linear
loss_args['batch_features'] = features
loss = criterion(**loss_args)
###
optimizer.zero_grad()
loss.backward()
### Compute Model Gradients and log them!
grads = np.concatenate([p.grad.detach().cpu().numpy().flatten() for p in model.parameters() if p.grad is not None])
grad_l2, grad_max = np.mean(np.sqrt(np.mean(np.square(grads)))), np.mean(np.max(np.abs(grads)))
LOG.progress_saver['Model Grad'].log('Grad L2', grad_l2, group='L2')
LOG.progress_saver['Model Grad'].log('Grad Max', grad_max, group='Max')
### Update network weights!
optimizer.step()
###
loss_collect.append(loss.item())
###
iter_count += 1
if i==len(dataloaders['training'])-1: data_iterator.set_description('Epoch (Train) {0}: Mean Loss [{1:.4f}]'.format(epoch, np.mean(loss_collect)))
"""======================================="""
if train_data_sampler.requires_storage and train_data_sampler.update_storage:
train_data_sampler.replace_storage_entries(embeds.detach().cpu(), input_indices)
result_metrics = {'loss': np.mean(loss_collect)}
####
LOG.progress_saver['Train'].log('epochs', epoch)
for metricname, metricval in result_metrics.items():
LOG.progress_saver['Train'].log(metricname, metricval)
LOG.progress_saver['Train'].log('time', np.round(time.time()-start, 4))
"""======================================="""
### Evaluate Metric for Training & Test (& Validation)
_ = model.eval()
print('\nComputing Testing Metrics...')
eval.evaluate(opt.dataset, LOG, metric_computer, [dataloaders['testing']], model, opt, opt.evaltypes, opt.device, log_key='Test')
#if opt.use_tv_split:
# print('\nComputing Validation Metrics...')
# eval.evaluate(opt.dataset, LOG, metric_computer, [dataloaders['validation']], model, opt, opt.evaltypes, opt.device, log_key='Val')
#print('\nComputing Training Metrics...')
#eval.evaluate(opt.dataset, LOG, metric_computer, [dataloaders['evaluation']], model, opt, opt.evaltypes, opt.device, log_key='Train')
#exit()
LOG.update(all=True)
"""======================================="""
### Learning Rate Scheduling Step
if opt.scheduler != 'none':
scheduler.step()
print('Total Epoch Runtime: {0:4.2f}s'.format(time.time()-epoch_start_time))
print('\n-----\n')
"""======================================================="""
### CREATE A SUMMARY TEXT FILE
summary_text = ''
full_training_time = time.time()-full_training_start_time
summary_text += 'Training Time: {} min.\n'.format(np.round(full_training_time/60,2))
summary_text += '---------------\n'
for sub_logger in LOG.sub_loggers:
metrics = LOG.graph_writer[sub_logger].ov_title
summary_text += '{} metrics: {}\n'.format(sub_logger.upper(), metrics)
with open(opt.save_path+'/training_summary.txt','w') as summary_file:
summary_file.write(summary_text)