-
Notifications
You must be signed in to change notification settings - Fork 9
/
SwinNet_train.py
224 lines (193 loc) · 8.51 KB
/
SwinNet_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
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
# -*- coding: utf-8 -*-
"""
@author: caigentan@AnHui University
@software: PyCharm
@file: SwinNet.py
@time: 2021/5/6 16:12
"""
import os
import torch
import torch.nn.functional as F
import sys
sys.path.append('./models')
import numpy as np
from datetime import datetime
from models.Swin_Transformer import SwinTransformer,SwinNet
from torchvision.utils import make_grid
from data import get_loader, test_dataset
from utils import clip_gradient, adjust_lr
from tensorboardX import SummaryWriter
import logging
import torch.backends.cudnn as cudnn
from options import opt
import yaml
# set the device for training
if opt.gpu_id == '0':
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
print('USE GPU 0')
elif opt.gpu_id == '1':
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
print('USE GPU 1')
cudnn.benchmark = True
image_root = opt.rgb_root
gt_root = opt.gt_root
depth_root = opt.depth_root
edge_root = opt.edge_root
test_image_root = opt.test_rgb_root
test_gt_root = opt.test_gt_root
test_depth_root = opt.test_depth_root
save_path = opt.save_path
logging.basicConfig(filename=save_path + 'RGBDSwinTransNet.log',
format='[%(asctime)s-%(filename)s-%(levelname)s:%(message)s]', level=logging.INFO, filemode='a',
datefmt='%Y-%m-%d %I:%M:%S %p')
logging.info("SwinTransNet-Train_4_pairs")
# set yaml config path
# yaml_path = './config/module.yaml'
# config = yaml.load(open(yaml_path, 'r'), yaml.SafeLoader)
# build the model
# model = SwinTransformer(embed_dim=128, depths=[2,2,18,2], num_heads=[4,8,16,32])
model = SwinNet()
num_parms = 0
if (opt.load is not None):
model.load_pre(opt.load)
# model.load_state_dict(torch.load(opt.load)['model'], strict=False)
# model.load_state_dict(torch.load(opt.load))
# model.load_state_dict(torch.load(opt.load_pre))
print('load model from ', opt.load)
model.cuda()
for p in model.parameters():
num_parms += p.numel()
logging.info("Total Parameters (For Reference): {}".format(num_parms))
print("Total Parameters (For Reference): {}".format(num_parms))
params = model.parameters()
optimizer = torch.optim.Adam(params, opt.lr)
# set the path
if not os.path.exists(save_path):
os.makedirs(save_path)
# load data
print('load data...')
train_loader = get_loader(image_root, gt_root,depth_root, edge_root, batchsize=opt.batchsize, trainsize=opt.trainsize)
test_loader = test_dataset(test_image_root, test_gt_root,test_depth_root, opt.trainsize)
total_step = len(train_loader)
logging.info("Config")
logging.info(
'epoch:{};lr:{};batchsize:{};trainsize:{};clip:{};decay_rate:{};load:{};save_path:{};decay_epoch:{}'.format(
opt.epoch, opt.lr, opt.batchsize, opt.trainsize, opt.clip, opt.decay_rate, opt.load, save_path,
opt.decay_epoch))
# set loss function
CE = torch.nn.BCEWithLogitsLoss()
ECE = torch.nn.BCELoss()
step = 0
writer = SummaryWriter(save_path + 'summary')
best_mae = 1
best_epoch = 0
# train function
def train(train_loader, model, optimizer, epoch, save_path):
global step
model.train()
sal_loss_all = 0
edge_loss_all = 0
loss_all = 0
epoch_step = 0
try:
for i, (images, gts, depth,edge) in enumerate(train_loader, start=1):
optimizer.zero_grad()
images = images.cuda()
gts = gts.cuda()
depth = depth.repeat(1,3,1,1).cuda()
edge = edge.cuda()
s, e = model(images,depth)
sal_loss = CE(s, gts)
# edge_loss = bce2d_new(e, edge, reduction='sum')
edge_loss = CE(e, edge)
# edge_loss = edge_loss / opt.batchsize
loss = sal_loss + edge_loss
loss.backward()
clip_gradient(optimizer, opt.clip)
optimizer.step()
step += 1
epoch_step += 1
# sal_loss_all += sal_loss.data
loss_all += loss.data
memory_used = torch.cuda.max_memory_allocated() / (1024.0 * 1024.0)
if i % 100 == 0 or i == total_step or i == 1:
print('{} Epoch [{:03d}/{:03d}], Step [{:04d}/{:04d}], LR:{:.7f}||sal_loss:{:4f} ||edge_loss:{:4f}'.
format(datetime.now(), epoch, opt.epoch, i, total_step,
optimizer.state_dict()['param_groups'][0]['lr'], sal_loss.data, edge_loss.data))
logging.info(
'#TRAIN#:Epoch [{:03d}/{:03d}], Step [{:04d}/{:04d}], LR:{:.7f}, sal_loss:{:4f} ||edge_loss:{:4f}, mem_use:{:.0f}MB'.
format(epoch, opt.epoch, i, total_step, optimizer.state_dict()['param_groups'][0]['lr'], sal_loss.data,edge_loss.data,memory_used))
writer.add_scalar('Loss', loss.data, global_step=step)
grid_image = make_grid(images[0].clone().cpu().data, 1, normalize=True)
writer.add_image('RGB', grid_image, step)
grid_image = make_grid(gts[0].clone().cpu().data, 1, normalize=True)
writer.add_image('Ground_truth', grid_image, step)
res = s[0].clone()
res = res.sigmoid().data.cpu().numpy().squeeze()
res = (res - res.min()) / (res.max() - res.min() + 1e-8)
writer.add_image('res', torch.tensor(res), step, dataformats='HW')
# sal_loss_all /= epoch_step
loss_all /= epoch_step
logging.info('#TRAIN#:Epoch [{:03d}/{:03d}],Loss_AVG: {:.4f}'.format(epoch, opt.epoch, loss_all))
writer.add_scalar('Loss-epoch', loss_all, global_step=epoch)
if (epoch) % 5 == 0:
torch.save(model.state_dict(), save_path + 'SwinTransNet_epoch_{}.pth'.format(epoch))
except KeyboardInterrupt:
print('Keyboard Interrupt: save model and exit.')
if not os.path.exists(save_path):
os.makedirs(save_path)
torch.save(model.state_dict(), save_path + 'SwinTransNet_epoch_{}.pth'.format(epoch + 1))
print('save checkpoints successfully!')
raise
def bce2d_new(input, target, reduction=None):
assert (input.size() == target.size())
pos = torch.eq(target, 1).float()
neg = torch.eq(target, 0).float()
# ing = ((torch.gt(target, 0) & torch.lt(target, 1))).float()
num_pos = torch.sum(pos)
num_neg = torch.sum(neg)
num_total = num_pos + num_neg
alpha = num_neg / num_total
beta = 1.1 * num_pos / num_total
# target pixel = 1 -> weight beta
# target pixel = 0 -> weight 1-beta
weights = alpha * pos + beta * neg
return F.binary_cross_entropy_with_logits(input, target, weights, reduction=reduction)
# test function
def test(test_loader, model, epoch, save_path):
global best_mae, best_epoch
model.eval()
with torch.no_grad():
mae_sum = 0
for i in range(test_loader.size):
image, gt, depth, name, img_for_post = test_loader.load_data()
gt = np.asarray(gt, np.float32)
gt /= (gt.max() + 1e-8)
image = image.cuda()
depth = depth.repeat(1,3,1,1).cuda()
res,e = model(image,depth)
res = F.upsample(res, size=gt.shape, mode='bilinear', align_corners=False)
res = res.sigmoid().data.cpu().numpy().squeeze()
res = (res - res.min()) / (res.max() - res.min() + 1e-8)
mae_sum += np.sum(np.abs(res - gt)) * 1.0 / (gt.shape[0] * gt.shape[1])
mae = mae_sum / test_loader.size
writer.add_scalar('MAE', torch.tensor(mae), global_step=epoch)
print('Epoch: {} MAE: {} #### bestMAE: {} bestEpoch: {}'.format(epoch, mae, best_mae, best_epoch))
if epoch == 1:
best_mae = mae
else:
if mae < best_mae:
best_mae = mae
best_epoch = epoch
torch.save(model.state_dict(), save_path + 'SwinTransNet_epoch_best.pth')
print('best epoch:{}'.format(epoch))
logging.info('#TEST#:Epoch:{} MAE:{} bestEpoch:{} bestMAE:{}'.format(epoch, mae, best_epoch, best_mae))
if __name__ == '__main__':
print("Start train...")
# 初次衰减循环增大10个epoch即110后才进行第一次衰减
for epoch in range(1, opt.epoch):
# if (epoch % 50 ==0 and epoch < 60):
cur_lr = adjust_lr(optimizer, opt.lr, epoch, opt.decay_rate, opt.decay_epoch)
writer.add_scalar('learning_rate', cur_lr, global_step=epoch)
train(train_loader, model, optimizer, epoch, save_path)
test(test_loader, model, epoch, save_path)