forked from zijundeng/BDRAR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
150 lines (121 loc) · 5.85 KB
/
train.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
import datetime
import os
import torch
from torch import nn
from torch import optim
from torch.autograd import Variable
from torch.backends import cudnn
from torch.utils.data import DataLoader
from torchvision import transforms
import joint_transforms
from config import sbu_training_root
from dataset import ImageFolder
from misc import AvgMeter, check_mkdir
from model import BDRAR
cudnn.benchmark = True
torch.cuda.set_device(0)
ckpt_path = './ckpt'
exp_name = 'BDRAR'
# batch size of 8 with resolution of 416*416 is exactly OK for the GTX 1080Ti GPU
args = {
'iter_num': 3000,
'train_batch_size': 8,
'last_iter': 0,
'lr': 5e-3,
'lr_decay': 0.9,
'weight_decay': 5e-4,
'momentum': 0.9,
'snapshot': '',
'scale': 416
}
joint_transform = joint_transforms.Compose([
joint_transforms.RandomHorizontallyFlip(),
joint_transforms.Resize((args['scale'], args['scale']))
])
val_joint_transform = joint_transforms.Compose([
joint_transforms.Resize((args['scale'], args['scale']))
])
img_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
target_transform = transforms.ToTensor()
to_pil = transforms.ToPILImage()
train_set = ImageFolder(sbu_training_root, joint_transform, img_transform, target_transform)
train_loader = DataLoader(train_set, batch_size=args['train_batch_size'], num_workers=8, shuffle=True)
bce_logit = nn.BCEWithLogitsLoss().cuda()
log_path = os.path.join(ckpt_path, exp_name, str(datetime.datetime.now()) + '.txt')
def main():
net = BDRAR().cuda().train()
optimizer = optim.SGD([
{'params': [param for name, param in net.named_parameters() if name[-4:] == 'bias'],
'lr': 2 * args['lr']},
{'params': [param for name, param in net.named_parameters() if name[-4:] != 'bias'],
'lr': args['lr'], 'weight_decay': args['weight_decay']}
], momentum=args['momentum'])
if len(args['snapshot']) > 0:
print('training resumes from \'%s\'' % args['snapshot'])
net.load_state_dict(torch.load(os.path.join(ckpt_path, exp_name, args['snapshot'] + '.pth')))
optimizer.load_state_dict(torch.load(os.path.join(ckpt_path, exp_name, args['snapshot'] + '_optim.pth')))
optimizer.param_groups[0]['lr'] = 2 * args['lr']
optimizer.param_groups[1]['lr'] = args['lr']
check_mkdir(ckpt_path)
check_mkdir(os.path.join(ckpt_path, exp_name))
open(log_path, 'w').write(str(args) + '\n\n')
train(net, optimizer)
def train(net, optimizer):
curr_iter = args['last_iter']
while True:
train_loss_record, loss_fuse_record, loss1_h2l_record = AvgMeter(), AvgMeter(), AvgMeter()
loss2_h2l_record, loss3_h2l_record, loss4_h2l_record = AvgMeter(), AvgMeter(), AvgMeter()
loss1_l2h_record, loss2_l2h_record, loss3_l2h_record = AvgMeter(), AvgMeter(), AvgMeter()
loss4_l2h_record = AvgMeter()
for i, data in enumerate(train_loader):
optimizer.param_groups[0]['lr'] = 2 * args['lr'] * (1 - float(curr_iter) / args['iter_num']
) ** args['lr_decay']
optimizer.param_groups[1]['lr'] = args['lr'] * (1 - float(curr_iter) / args['iter_num']
) ** args['lr_decay']
inputs, labels = data
batch_size = inputs.size(0)
inputs = Variable(inputs).cuda()
labels = Variable(labels).cuda()
optimizer.zero_grad()
fuse_predict, predict1_h2l, predict2_h2l, predict3_h2l, predict4_h2l, \
predict1_l2h, predict2_l2h, predict3_l2h, predict4_l2h = net(inputs)
loss_fuse = bce_logit(fuse_predict, labels)
loss1_h2l = bce_logit(predict1_h2l, labels)
loss2_h2l = bce_logit(predict2_h2l, labels)
loss3_h2l = bce_logit(predict3_h2l, labels)
loss4_h2l = bce_logit(predict4_h2l, labels)
loss1_l2h = bce_logit(predict1_l2h, labels)
loss2_l2h = bce_logit(predict2_l2h, labels)
loss3_l2h = bce_logit(predict3_l2h, labels)
loss4_l2h = bce_logit(predict4_l2h, labels)
loss = loss_fuse + loss1_h2l + loss2_h2l + loss3_h2l + loss4_h2l + loss1_l2h + \
loss2_l2h + loss3_l2h + loss4_l2h
loss.backward()
optimizer.step()
train_loss_record.update(loss.data, batch_size)
loss_fuse_record.update(loss_fuse.data, batch_size)
loss1_h2l_record.update(loss1_h2l.data, batch_size)
loss2_h2l_record.update(loss2_h2l.data, batch_size)
loss3_h2l_record.update(loss3_h2l.data, batch_size)
loss4_h2l_record.update(loss4_h2l.data, batch_size)
loss1_l2h_record.update(loss1_l2h.data, batch_size)
loss2_l2h_record.update(loss2_l2h.data, batch_size)
loss3_l2h_record.update(loss3_l2h.data, batch_size)
loss4_l2h_record.update(loss4_l2h.data, batch_size)
curr_iter += 1
log = '[iter %d], [train loss %.5f], [loss_fuse %.5f], [loss1_h2l %.5f], [loss2_h2l %.5f], ' \
'[loss3_h2l %.5f], [loss4_h2l %.5f], [loss1_l2h %.5f], [loss2_l2h %.5f], [loss3_l2h %.5f], ' \
'[loss4_l2h %.5f], [lr %.13f]' % \
(curr_iter, train_loss_record.avg, loss_fuse_record.avg, loss1_h2l_record.avg, loss2_h2l_record.avg,
loss3_h2l_record.avg, loss4_h2l_record.avg, loss1_l2h_record.avg, loss2_l2h_record.avg,
loss3_l2h_record.avg, loss4_l2h_record.avg, optimizer.param_groups[1]['lr'])
print(log)
open(log_path, 'a').write(log + '\n')
if curr_iter > args['iter_num']:
torch.save(net.state_dict(), os.path.join(ckpt_path, exp_name, '%d.pth' % curr_iter))
return
if __name__ == '__main__':
main()