-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
389 lines (321 loc) · 13.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import torch
import utils
import argparse
from torch.utils.tensorboard import SummaryWriter
import os
import torchvision
import tqdm
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
from data_process.pfedb import PfeDB
from data_process.expdb import ExpDB
from data_process.posecondb import PoseConDB
from itertools import cycle
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--config_file',required=True,type=str)
parser.add_argument('--local_rank',type=int,default=-1)
parser.add_argument('--use_ddp',action='store_true',default=False)
def train(config,pfe_train_loader,exp_train_loader,pose_train_loader,model,logger,step):
running_dic = None
count = 0
#total_num = len(train_loader)
warm_up = config['warm_up'] if config['warm_up'] is not None else -1
total_num = len(pfe_train_loader)
pfe_train_loader = cycle(pfe_train_loader)
exp_train_loader = cycle(exp_train_loader)
pose_train_loader = cycle(pose_train_loader)
exp_loss = 0.
pose_loss = 0.
for i in tqdm.tqdm(range(total_num)):
# pfe train
data = next(pfe_train_loader)
data['epoch'] = step
data['state'] = 'pfe'
dic = model.optimize_parameters(data)
# exp train
if step > warm_up:
data = next(exp_train_loader)
data['epoch']= step
data['state'] = 'exp'
dic.update(model.optimize_parameters(data))
data = next(pose_train_loader)
data['epoch'] = step
data['state'] = 'pose'
dic.update(model.optimize_parameters(data))
exp_loss += dic['exp_loss']
pose_loss += dic['pose_loss']
count += 1
if running_dic == None:
running_dic = {}
for k, v in dic.items():
if k != 'train_print_img':
running_dic[k] = v
else:
for k, v in dic.items():
if k != 'train_print_img' and k != 'recon_weight':
running_dic[k] += v
if i % config['print_loss'] == 0:
txt = 'epoch: {},\t step: {},\t'.format(step, i)
for k in list(dic.keys()):
if k != 'train_print_img':
txt += ',{}: {},\t'.format(k, dic[k])
print(txt)
if config['print_img'] != None and i % config['print_img'] == 0 and 'train_print_img' in dic and dic['train_print_img'] != None:
print_img = dic['train_print_img']
grid = torchvision.utils.make_grid(print_img,nrow=1)
logger.add_image('train_img',grid,global_step=total_num * step + i)
exp_loss /= count
pose_loss /= count
if 'train_loss' in running_dic.keys():
running_dic['train_loss'] /= count
if 'train_acc1' in running_dic.keys():
running_dic['train_acc1'] /= count
if 'train_acc5' in running_dic.keys():
running_dic['train_acc5'] /= count
if 'pose_acc1' in running_dic.keys():
running_dic['pose_acc1'] /= count
if 'pose_acc5' in running_dic.keys():
running_dic['pose_acc5'] /= count
if 'exp_acc1' in running_dic.keys():
running_dic['exp_acc1'] /= count
if 'exp_acc5' in running_dic.keys():
running_dic['exp_acc5'] /= count
if 'train_acc1_exp' in running_dic.keys():
running_dic['train_acc1_exp'] /= count
if 'train_acc5_exp' in running_dic.keys():
running_dic['train_acc5_exp'] /= count
if 'train_acc1_pose' in running_dic.keys():
running_dic['train_acc1_pose'] /= count
if 'train_acc5_pose' in running_dic.keys():
running_dic['train_acc5_pose'] /= count
if 'train_acc1_flip' in running_dic.keys():
running_dic['train_acc1_flip'] /= count
if 'train_acc5_flip' in running_dic.keys():
running_dic['train_acc5_flip'] /= count
for k, v in running_dic.items():
logger.add_scalar(k, v, global_step=step)
return exp_loss,pose_loss,running_dic['exp_acc1'],running_dic['pose_acc1']
def eval(config,val_loader,model,logger,step):
running_dic = None
count = 0
total_num = len(val_loader)
for i, data in tqdm.tqdm(enumerate(val_loader)):
dic = model.eval(data)
count += 1
if running_dic == None:
running_dic = {}
for k, v in dic.items():
if k != 'eval_print_img':
running_dic[k] = v
else:
for k, v in dic.items():
if k != 'eval_print_img':
running_dic[k] += v
if i % config['print_loss'] == 0:
txt = 'epoch: {},\t step: {},\t'.format(step, i)
for k in list(dic.keys()):
if k != 'eval_print_img':
txt += ',{}: {},\t'.format(k, dic[k])
print(txt)
if config['print_img'] != None and i % config['print_img'] == 0:
print_img = dic['eval_print_img']
grid = torchvision.utils.make_grid(print_img,nrow=1)
logger.add_image('test_img',grid,global_step=total_num * step + i)
running_dic['eval_loss'] /= count
for k, v in running_dic.items():
logger.add_scalar(k, v, global_step=step)
return running_dic['eval_loss']
def linear_eval(config,train_loader,val_loader,model,logger):
count = 0
linear_classifier = torch.nn.Linear(in_features=config['linear_dim'],out_features=config['classes_num']).cuda()
linear_classifier.weight.data.normal_(mean=0.0,std=0.01)
linear_classifier.bias.data.zero_()
optimizer = torch.optim.Adam(linear_classifier.parameters(),lr=config['linear_lr'])
lr_schduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer,T_max=config['eval_epochs'])
criterizer = torch.nn.CrossEntropyLoss().cuda()
best_knn_acc = 0.
best_linear_acc = 0.
knn_not_use = False
for eval_step in range(config['eval_epochs']):
train_count = 0
train_acc_count = 0
train_loss = 0.
train_fea_list = []
train_label_list = []
for i,data in tqdm.tqdm(enumerate(train_loader)):
label = data['label'].cuda()
fea = model.linear_eval(data)
count += 1
pred = linear_classifier(fea)
loss = criterizer(pred,label)
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_acc_count += (torch.max(pred,dim=1)[1] == label).sum()
train_count += label.size(0)
train_loss += loss.item()
################### knn #######################
if knn_not_use:
b = label.size(0)
fea_array = fea.detach().cpu().numpy()
for j in range(b):
train_fea_list.append(fea_array[j])
train_label_list.append(label[j].item())
train_acc = train_acc_count / train_count
train_loss = train_loss / train_count
lr_schduler.step()
test_acc_count = 0
test_knn_acc=0
test_count = 0
test_loss = 0.
linear_classifier.eval()
test_fea_list = []
test_label_list = []
for i,data in tqdm.tqdm(enumerate(val_loader)):
label = data['label'].cuda()
fea = model.linear_eval(data)
with torch.no_grad():
pred = linear_classifier(fea)
################### knn #######################
if knn_not_use:
b = label.size(0)
fea_array = fea.detach().cpu().numpy()
for j in range(b):
test_fea_list.append(fea_array[j])
test_label_list.append(label[j].item())
loss = criterizer(pred,label)
test_acc_count += (torch.max(pred,dim=1)[1] == label).sum()
test_count += label.size(0)
test_loss += loss.item()
test_linear_acc = test_acc_count / test_count
test_linear_loss = test_loss / test_count
############ knn #############
if knn_not_use:
print('knn')
train_fea = np.array(train_fea_list)
test_fea = np.array(test_fea_list)
neigh = KNeighborsClassifier(n_neighbors=5)
neigh = neigh.fit(train_fea, train_label_list)
test_pred_list = neigh.predict(test_fea)
test_count = len(test_label_list)
test_label_array = np.array(test_label_list)
test_pred_array = np.array(test_pred_list)
test_knn_acc_count = (test_pred_array == test_label_array).sum()
test_knn_acc = test_knn_acc_count / test_count
best_knn_acc = test_knn_acc
knn_not_use = False
if eval_step % config['print_loss'] == 0:
txt = 'eval step: {},\t linear train acc: {},\t linear train loss: {},\t' \
'eval linear acc: {},\t eval linear loss: {},\t knn acc: {}'.format(
eval_step,train_acc,train_loss,test_linear_acc,test_linear_loss,test_knn_acc
)
print(txt)
logger.add_scalar('linear_train_acc',train_acc,eval_step)
logger.add_scalar('linear_train_loss',train_loss,eval_step)
logger.add_scalar('linear_eval_acc', test_linear_acc, eval_step)
logger.add_scalar('linear_eval_loss', test_linear_loss, eval_step)
#logger.add_scalar('knn_eval_acc', test_knn_acc, eval_step)
#best_linear_acc = max(best_linear_acc,test_linear_acc)
if best_linear_acc < test_linear_acc:
best_linear_acc = test_linear_acc
model_state = model.model.state_dict()
linear_state = linear_classifier.state_dict()
model_state['linear_classifier.weight'] = linear_state['weight']
model_state['linear_classifier.bias'] = linear_state['bias']
utils.save_checkpoint({
'epoch': eval_step + 1,
'state_dict': model_state,
}, config)
return best_linear_acc,best_knn_acc
def main(config,logger):
model = utils.create_model(config)
#train_dataset = utils.create_dataset(config,'train')
#val_dataset = utils.create_dataset(config,'val')
if config['eval']:
test_dataset = utils.create_dataset(config,'test')
train_dataset = utils.create_dataset(config,'train')
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=config['batch_size'],
num_workers=config['num_threads'],
pin_memory=True,
drop_last=False,
shuffle=False
)
test_loader = torch.utils.data.DataLoader(
test_dataset,
batch_size=config['batch_size'],
num_workers=config['num_threads'],
pin_memory=True,
drop_last=False,
shuffle=False
)
linear_acc,knn_acc = linear_eval(config,train_loader,test_loader,model,logger)
print('config name: {},\t test linear acc is : {},\t knn acc is : {}\t'.format(config['experiment_name'],linear_acc,knn_acc))
exit(0)
pfe_train_dataset = PfeDB(config,phase='train')
exp_train_dataset = ExpDB(config,phase='train')
pose_train_dataset= PoseConDB(config,phase='train')
pfe_train_loader = torch.utils.data.DataLoader(
pfe_train_dataset,
batch_size=config['batch_size'],
num_workers=config['num_threads'],
pin_memory=True,
drop_last=True,
shuffle=True
)
exp_train_loader = torch.utils.data.DataLoader(
exp_train_dataset,
batch_size=config['batch_size'],
num_workers=config['num_threads'],
pin_memory=True,
drop_last=True,
shuffle=True
)
pose_train_loader = torch.utils.data.DataLoader(
pose_train_dataset,
batch_size=10,
num_workers=config['num_threads'],
pin_memory=True,
drop_last=True,
shuffle=True
)
lr_schduler = torch.optim.lr_scheduler.CosineAnnealingLR(model.optimizer, T_max=config['epochs'])
best_metric = None
if config['use_dwa']:
avg_cost = np.zeros([config['epochs'],2],dtype=np.float32)
dwa_t = config['dwa_T']
dwa_start_epoch = config['warm_up'] + 3 if config['warm_up'] !=-1 else 2
for step in range(config['start_epochs'],config['epochs']+1):
if config['use_dwa']:
if step > dwa_start_epoch:
exp_w = avg_cost[step - 1,0] / avg_cost[step-2,0]
pose_w = avg_cost[step-1,1] / avg_cost[step-2,1]
model.exp_weight = 2 * np.exp(exp_w / dwa_t) / (np.exp(exp_w / dwa_t) + np.exp(pose_w / dwa_t))
model.pose_weight = 2 * np.exp(pose_w / dwa_t) / (np.exp(exp_w / dwa_t) + np.exp(pose_w / dwa_t))
logger.add_scalar('exp_weight', model.exp_weight, step)
logger.add_scalar('pose_weight',model.pose_weight,step)
exp_loss,pose_loss,exp_acc,pose_acc = train(config,pfe_train_loader,exp_train_loader,pose_train_loader,model,logger,step)
print(exp_loss,pose_loss)
logger.add_scalar('exp_loss_total', exp_loss, step)
logger.add_scalar('pose_loss_total',pose_loss,step)
if config['use_dwa']:
avg_cost[step,0] = exp_loss
avg_cost[step,1] = pose_loss
#metric = eval(config,val_loader,model,logger,step)
lr_schduler.step()
#flag,cur_best = model.metric_better(metric,best_metric)
if step % config['save_epoch'] == 0:
#best_metric = cur_best
utils.save_checkpoint({
'epoch': step + 1,
'state_dict': model.model.state_dict(),
},config)
if __name__ == '__main__':
opt = parser.parse_args()
config = utils.read_config(opt.config_file)
utils.init(config,opt.local_rank,opt.use_ddp)
logger = SummaryWriter(log_dir=os.path.join(config['log_path'], config['experiment_name']),
comment=config['experiment_name'])
main(config, logger)
logger.close()