-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
195 lines (163 loc) · 7.44 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
import os.path
import warnings
import torch
import torch.nn as nn
import time
import networks
import logging
import numpy as np
from tensorboardX import SummaryWriter
from utils import get_config,set_env,set_logger,set_outdir,get_dataloader
from utils import get_train_setting,load_checkpoint,get_proc,save_checkpoint
from coding_functions.target_criterion_2d import HadamardTargetCoding, LearnableTargetCoding
def main(conf):
# logger
# tf_writer = SummaryWriter(log_dir=os.path.join('log', conf['outdir']))
tf_writer = SummaryWriter(log_dir=conf['outdir'])
warnings.filterwarnings("ignore")
best_score = 0.
epoch_start = 0
# dataloader
train_loader, val_loader, ds_train = get_dataloader(conf)
# device setting
device = (torch.device('cuda')
if torch.cuda.is_available()
else torch.device('cpu'))
print("Cuda is available!")
# model
model = networks.get_model(conf)
model = nn.DataParallel(model).cuda()
if conf.weightfile is not None:
wmodel = networks.get_model(conf)
wmodel = nn.DataParallel(wmodel).cuda()
checkpoint_dict = load_checkpoint(wmodel, conf.weightfile)
if 'best_score' in checkpoint_dict:
print('best score: {}'.format(best_score))
else:
wmodel = model
# training setting
criterion, optimizer, scheduler = get_train_setting(model,conf)
criterion_test = criterion
if conf.HTC is True:
train_reg = HadamardTargetCoding(gamma_=conf.gamma_,
code_length=conf.code_length,
classes_num=conf.num_class,
).cuda()
elif conf.LTC is True:
train_reg = LearnableTargetCoding(gamma_=conf.gamma_,
lambda_=conf.lambda_,
beta_=conf.beta_,
code_length=conf.code_length,
classes_num=conf.num_class,
active_type=conf.active_type,
margin_ratio=conf.margin_ratio
).cuda()
optimizer.add_param_group({'params': train_reg.target_labels, 'lr': 0.1})
else:
train_reg = None
# training and evaluate process for each epoch
train, validate = get_proc(conf)
if conf.resume:
checkpoint_dict = load_checkpoint(model, conf.resume)
epoch_start = checkpoint_dict['epoch']
if 'best_score' in checkpoint_dict:
best_score = checkpoint_dict['best_score']
print('best score: {}'.format(best_score))
print('Resuming training process from epoch {}...'.format(epoch_start))
optimizer.load_state_dict(checkpoint_dict['optimizer'])
scheduler.load_state_dict(checkpoint_dict['scheduler'])
print('Resuming lr scheduler')
print(checkpoint_dict['scheduler'])
if conf.HTC or conf.LTC:
print('Resuming target_labels')
train_reg.target_labels.data = checkpoint_dict['target_codes']
if conf.evaluate:
print(validate(val_loader, model, criterion_test, conf))
return
detach_epoch = conf.epochs + 1
if 'detach_epoch' in conf:
detach_epoch = conf.detach_epoch
start_eval = 0
if 'start_eval' in conf:
start_eval = conf.start_eval
## ------main loop-----
for epoch in range(epoch_start, conf.epochs):
# setting just for imbalanced data learning
if conf.dataset == 'iNaturalist18' or conf.dataset == 'Imagenet-LT':
cls_num_list = ds_train.get_cls_num_list()
if conf.train_rule == 'None':
per_cls_weights = None
elif conf.train_rule == 'DRW':
if conf.dataset == 'iNaturalist18':
idx = epoch // 160 # for total 200 epochs
else:
idx = epoch // 80 # for total 100 epochs
betas = [0, 0.9999]
effective_num = 1.0 - np.power(betas[idx], cls_num_list)
per_cls_weights = (1.0 - betas[idx]) / np.array(effective_num)
per_cls_weights = per_cls_weights / np.sum(per_cls_weights) * len(cls_num_list)
per_cls_weights = torch.FloatTensor(per_cls_weights).cuda()
else:
warnings.warn('Sample rule is not listed')
criterion = nn.CrossEntropyLoss(weight=per_cls_weights).cuda()
start_time = time.time()
lr0 = optimizer.param_groups[0]['lr']
lr1 = optimizer.param_groups[1]['lr']
lr2 = optimizer.param_groups[-1]['lr']
logging.info("Epoch: [{} | {} LR: {} {} {}".format(epoch+1,conf.epochs,lr0, lr1, lr2))
if epoch == detach_epoch:
model.module.set_detach(False)
tmp_loss = train(train_loader, model, criterion, optimizer, train_reg, conf, wmodel)
infostr = {'Epoch: {} train_loss: {}'.format(epoch+1,tmp_loss)}
logging.info(infostr)
scheduler.step()
if epoch > start_eval and (epoch+1) % 1 == 0:
with torch.no_grad():
val_score, val_score_top5,val_loss,mscore, ascore = validate(val_loader, model, criterion_test, conf)
comscore = val_score
if 'midlevel' in conf:
if conf.midlevel:
comscore = ascore
is_best = comscore > best_score
best_score = max(comscore, best_score)
print('Epoch: {:.4f} loss: {:.4f},gs: {:.4f},gs_acc5: {:.4f},ms:{:.4f},as:{:.4f},bs:{:.4f}'.format(
epoch+1,val_loss,val_score,val_score_top5,mscore,ascore,best_score))
infostr = {'Epoch: {:.4f} loss: {:.4f},gs: {:.4f},gs_acc5: {:.4f},ms:{:.4f},as:{:.4f},bs:{:.4f}'.format(
epoch+1,val_loss,val_score,val_score_top5,mscore,ascore,best_score)}
logging.info(infostr)
tf_writer.add_scalar('acc/test_top1', comscore, epoch)
tf_writer.add_scalar('acc/test_top1_best', best_score, epoch)
tf_writer.add_scalar('acc/test_top5', val_score_top5, epoch)
state_dict = {'epoch': epoch + 1,
'state_dict': model.module.state_dict(),
'optimizer' : optimizer.state_dict(),
'scheduler' : scheduler.state_dict(),
'best_score': best_score
}
if conf.HTC or conf.LTC:
state_dict['target_codes'] = train_reg.target_labels.data
save_checkpoint(state_dict, is_best, outdir=conf['outdir'], iteral=conf.iteral)
end_time = time.time()
seconds = end_time - start_time
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
infostr = {"Epoch Time %02d:%02d:%02d" % (h, m, s)}
logging.info(infostr)
logging.info({'Best val acc: {}'.format(best_score)})
print('Best val acc: {}'.format(best_score))
# return 0
if __name__ == '__main__':
start_time = time.time()
# get configs and set envs
conf = get_config()
set_env(conf)
# generate outdir name
set_outdir(conf)
# Set the logger
set_logger(conf)
main(conf)
end_time = time.time()
seconds = end_time - start_time
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print("During Time %02d:%02d:%02d" % (h, m, s))