-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
308 lines (254 loc) · 13.3 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
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
import datetime
import numpy
import os
import copy
import time
import json
import argparse
import torch
import cv2
import torch.nn as nn
import torchvision
import torch.nn.functional as F
from torch.utils.data import DataLoader
# from sklearn.metrics import precision_recall_fscore_support as prfs
# from utils.data import get_loader
from utils.func import AvgMeter, clip_gradient
from utils.lr_scheduler import get_scheduler
from utils.dataset import MyDataset
from utils.helper import set_metrics, get_mean_metrics, initialize_metrics
from utils.losses import get_criterion
from model.metric_tool import ConfuseMatrixMeter, AverageMeter, get_confuse_matrix
# from model.RD3D_ame import Net_arch
from model.Netmodel import Netmodel
from utils.loss_f import BCEDICE_loss
import xlsxwriter
import warnings
from thop import profile
warnings.filterwarnings("ignore")
def parse_option():
parser = argparse.ArgumentParser()
# data set
parser.add_argument('--batchsize', type=int, default=8)
parser.add_argument('--trainsize', type=int, default=256)
parser.add_argument('--hflip', action='store_true', help='hflip data')
parser.add_argument('--vflip', action='store_true', help='vflip data')
parser.add_argument('--data_dir', type=str, default='E:\\AllData\\LEVERCD\\ABLabel')
# parser.add_argument('--data_dir', type=str, default='E:\\AllData\\WHU\\ABLabel')
# parser.add_argument('--data_dir', type=str, default='E:\\AllData\\SYSU-CD\\ABLable')
# training
parser.add_argument('--model', type=str, default='RD3D+', help='RD3D or RD3D+')
parser.add_argument('--epochs', type=int, default=100, help='epoch number')
parser.add_argument('--optim', type=str, default='adamW', help='optimizer')
parser.add_argument('--lr', type=float, default=0.0001, help='learning rate') # ori_lr: 0.0001
parser.add_argument('--lr_scheduler', type=str, default='cosine', choices=['step', 'cosine'])
parser.add_argument('--warmup_epoch', type=int, default=-1, help='warmup epoch')
parser.add_argument('--warmup_multiplier', type=int, default=100, help='warmup multiplier')
parser.add_argument('--lr_decay_epochs', type=int, default=[120, 160, 200], nargs='+',
help='for step scheduler. where to decay lr, can be a list')
parser.add_argument('--lr_decay_steps', type=int, default=20,
help='for step scheduler. step size to decay lr')
parser.add_argument('--lr_decay_rate', type=float, default=0.1,
help='for step scheduler. decay rate for learning rate')
parser.add_argument('--weight_decay', type=float, default=0.0001, help='weight decay')
parser.add_argument('--momentum', type=float, default=0.9, help='momentum for SGD')
parser.add_argument('--clip', type=float, default=0.5, help='gradient clipping margin')
# io
parser.add_argument('--output_dir', type=str, default='./output', help='output director')
opt, unparsed = parser.parse_known_args()
opt.output_dir = os.path.join(opt.output_dir, str(int(time.time())))
# opt.output_dir = os.path.join(opt.output_dir, f'/{datetime.datetime.now().strftime("%Y%m%d-%H%M%S")}/')
return opt
def build_loader(opt):
train_data = MyDataset(opt.data_dir, "train")
train_loader = DataLoader(train_data, batch_size=opt.batchsize, shuffle=True, num_workers=2, pin_memory=True)
val_data = MyDataset(opt.data_dir, "val")
val_loader = DataLoader(val_data, batch_size=opt.batchsize, shuffle=False, num_workers=2, pin_memory=True)
return train_loader, val_loader
def build_model(opt):
resnet = torchvision.models.resnet18(pretrained=True)
# model = CD3D_Net(32, copy.deepcopy(resnet))
model = Netmodel(32, copy.deepcopy(resnet))
model = model.cuda()
return model
def main(opt):
train_loader, val_loader = build_loader(opt)
n_data = len(train_loader.dataset)
print(f"length of training dataset: {n_data}\n")
print(f"length of val dataset: {len(val_loader.dataset)}\n")
# build model
model = build_model(opt)
# print number of parameters
parameters_tot = 0
for nom, param in model.named_parameters():
parameters_tot += torch.prod(torch.tensor(param.data.shape))
print("Number of model parameters {}".format(parameters_tot))
# 定义写出行列号
train_hook = xlsxwriter.Workbook('WHU_0-001_train.xlsx')
train_record = train_hook.add_worksheet()
train_record.write('A1', 'epoch')
train_record.write('B1', 'Pre')
train_record.write('C1', 'Recall')
train_record.write('D1', 'F1')
train_record.write('E1', 'IoU')
train_record.write('F1', 'acc')
train_record.write('G1', 'loss')
val_hook = xlsxwriter.Workbook('WHU_0-001_val.xlsx')
val_record = val_hook.add_worksheet()
val_record.write('A1', 'epoch')
val_record.write('B1', 'Pre')
val_record.write('C1', 'Recall')
val_record.write('D1', 'F1')
val_record.write('E1', 'IoU')
val_record.write('F1', 'acc')
val_record.write('G1', 'loss')
row = 1
col = 0
CE = BCEDICE_loss
# build optimizer
if opt.optim == 'adam':
optimizer = torch.optim.Adam(model.parameters(), opt.lr, weight_decay=opt.weight_decay)
elif opt.optim == 'adamW':
optimizer = torch.optim.AdamW(model.parameters(), opt.lr, weight_decay=opt.weight_decay)
elif opt.optim == 'sdg':
optimizer = torch.optim.SGD(model.parameters(), opt.lr / 10.0 * opt.batchsize, momentum=opt.momentum,
weight_decay=opt.weight_decay)
else:
raise NotImplementedError
scheduler = get_scheduler(optimizer, len(train_loader), opt)
# routine
for epoch in range(1, opt.epochs + 1):
tic = time.time()
tool4metric = ConfuseMatrixMeter(n_class=2)
train(train_loader, model, optimizer, CE, scheduler, epoch, tool4metric, train_record, row, col)
print('epoch {}, total time {:.2f}, learning_rate {}'.format(epoch, (time.time() - tic),
optimizer.param_groups[0]['lr']))
print('begin val')
val(val_loader, model, CE, epoch, tool4metric, val_record, row, col)
print('epoch {}, total time {:.2f}'.format(epoch, (time.time() - tic)))
if epoch >= 30:
torch.save(model.state_dict(), os.path.join(opt.output_dir, f"RD3D_{epoch}_ckpt.pth"))
print("model saved {}!".format(os.path.join(opt.output_dir, f"RD3D_{epoch}_ckpt.pth")))
row = row + 1
torch.save(model.state_dict(), os.path.join(opt.output_dir, f"RD3D_last_ckpt.pth"))
print("model saved {}!".format(os.path.join(opt.output_dir, f"RD3D_ckpt.pth")))
train_hook.close()
val_hook.close()
os.path.join(opt.output_dir, f"RD3D_last_ckpt.pth")
def train(train_loader, model, optimizer, criterion, scheduler, epoch, tool4metric, train_record, row, col):
tool4metric.clear()
model.train()
# train_metrics = initialize_metrics()
loss_record = AvgMeter()
for i, pack in enumerate(train_loader, start=1):
optimizer.zero_grad()
imageA, imageB, gts = pack
imageA = imageA.cuda().float()
imageB = imageB.cuda().float()
gts = gts.cuda().float()
imageA = imageA.unsqueeze(2)
imageB = imageB.unsqueeze(2)
images = torch.cat([imageA, imageB], 2)
# flops, params = profile(model, (imageA, imageB,))
# print('flops:', flops, 'params:', params)
# print('flops: %.2f M, params: %.2f M' % (flops / 1000000000.0, params / 1000000.0))
# forward
pred_s = model(images)
# pred_s = pred_s.squeeze(1)
gts = torch.unsqueeze(gts, dim=1)
loss = criterion(pred_s, gts)
loss.backward()
clip_gradient(optimizer, opt.clip)
optimizer.step()
scheduler.step()
loss_record.update(loss.data, opt.batchsize)
bin_preds_mask = (pred_s.to('cpu') > 0.5).detach().numpy().astype(int)
mask = gts.to('cpu').numpy().astype(int)
# out_png = bin_preds_mask.squeeze(0)
# out_mask = mask.squeeze(0)
# cv2.imwrite('./pred.png', out_png)
# cv2.imwrite('./mask.png', out_mask)
tool4metric.update_cm(pr=bin_preds_mask, gt=mask)
# gts_temp = gts.data.cpu().numpy().flatten()
# prs_temp = bin_preds_mask.reshape(bin_preds_mask.shape[1] * bin_preds_mask.shape[2] * bin_preds_mask.shape[0])
# cd_train_report = prfs(prs_temp, gts_temp, average='binary', pos_label=1)
# train_metrics = set_metrics(train_metrics, cd_train_report,)
# mean_train_metrics = get_mean_metrics(train_metrics)
if i % 100 == 0 or i == len(train_loader):
print('Epoch [{:03d}/{:03d}], Step [{:04d}/{:04d}],'
'Loss: {:.4f}'.format(epoch, opt.epochs, i, len(train_loader), loss_record.show()))
scores_dictionary = tool4metric.get_scores()
# print("IoU for epoch {} is {}".format(epoch, scores_dictionary["iou"]))
# print("F1 for epoch {} is {}".format(epoch, scores_dictionary["F1"]))
# print("acc for epoch {} is {}".format(epoch, scores_dictionary["acc"]))
# print("precision for epoch {} is {}".format(epoch, scores_dictionary["precision"]))
# print("recall for epoch {} is {}".format(epoch, scores_dictionary["recall"]))
print('---------------------------------------------')
# print('pre of prfs is {}'.format(mean_train_metrics['cd_precisions']))
# print('recall of prfs is {}'.format(mean_train_metrics['cd_recalls']))
# print('F1 of prfs is {}'.format(mean_train_metrics['cd_f1scores']))
# print("EPOCH {} TRAIN METRICS".format(epoch) + str(mean_train_metrics))
train_record.write(row, col, epoch)
train_record.write(row, col + 1, scores_dictionary['precision'])
train_record.write(row, col + 2, scores_dictionary['recall'])
train_record.write(row, col + 3, scores_dictionary['F1'])
train_record.write(row, col + 4, scores_dictionary['iou'])
train_record.write(row, col + 5, scores_dictionary['acc'])
train_record.write(row, col + 6, loss_record.show())
def val(val_loader, model, criterion, epoch, tool4metric, val_record, row, col):
model.eval()
tool4metric.clear()
loss_record = AvgMeter()
# val_metrics = initialize_metrics()
with torch.no_grad():
for i, pack in enumerate(val_loader):
imageA, imageB, gts = pack
imageA = imageA.cuda().float()
imageB = imageB.cuda().float()
gts = gts.cuda().float()
imageA = imageA.unsqueeze(2)
imageB = imageB.unsqueeze(2)
images = torch.cat([imageA, imageB], 2)
pred_s = model(images)
# pred_s = pred_s.squeeze(1)
gts = torch.unsqueeze(gts, dim=1)
loss1 = criterion(pred_s, gts)
loss = loss1
bin_preds_mask = (pred_s.to('cpu') > 0.5).detach().numpy().astype(int)
mask = gts.to('cpu').numpy().astype(int)
tool4metric.update_cm(pr=bin_preds_mask, gt=mask)
loss_record.update(loss.data, opt.batchsize)
# gts_temp = gts.data.cpu().numpy().flatten()
# prs_temp = bin_preds_mask.reshape(
# bin_preds_mask.shape[1] * bin_preds_mask.shape[2] * bin_preds_mask.shape[0])
# cd_train_report = prfs(prs_temp, gts_temp, average='binary', pos_label=1)
# val_metrics = set_metrics(val_metrics, cd_train_report, )
# mean_val_metrics = get_mean_metrics(val_metrics)
if i % 100 == 0 or i == len(val_loader):
print('Epoch [{:03d}/{:03d}], Step [{:04d}/{:04d}],'
'Loss: {:.4f}'.format(epoch, opt.epochs, i, len(val_loader), loss_record.show()))
scores_dictionary = tool4metric.get_scores()
print("IoU for epoch {} is {}".format(epoch, scores_dictionary["iou"]))
print("F1 for epoch {} is {}".format(epoch, scores_dictionary["F1"]))
print("acc for epoch {} is {}".format(epoch, scores_dictionary["acc"]))
print("precision for epoch {} is {}".format(epoch, scores_dictionary["precision"]))
print("recall for epoch {} is {}".format(epoch, scores_dictionary["recall"]))
print('---------------------------------------------')
# print('pre of prfs is {}'.format(mean_val_metrics['cd_precisions']))
# print('recall of prfs is {}'.format(mean_val_metrics['cd_recalls']))
# print('F1 of prfs is {}'.format(mean_val_metrics['cd_f1scores']))
val_record.write(row, col, epoch)
val_record.write(row, col + 1, scores_dictionary['precision'])
val_record.write(row, col + 2, scores_dictionary['recall'])
val_record.write(row, col + 3, scores_dictionary['F1'])
val_record.write(row, col + 4, scores_dictionary['iou'])
val_record.write(row, col + 5, scores_dictionary['acc'])
val_record.write(row, col + 6, loss_record.show())
if __name__ == '__main__':
opt = parse_option()
os.makedirs(opt.output_dir, exist_ok=True)
path = os.path.join(opt.output_dir, 'config.json')
with open(path, 'w') as f:
json.dump(vars(opt), f, indent=2)
print("\n full config save to {}".format(path))
main(opt)