-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
191 lines (145 loc) · 7.56 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
import numpy as np
np.set_printoptions(threshold=np.inf)
from tqdm import tqdm
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
from assess import hist_sum, compute_metrics
# from pytorchtools import EarlyStopping
# from gary2RGB import create_visual_anno
from index2one_hot import get_one_hot
from poly import adjust_learning_rate_poly
# ========================Methods=================================#
# from model.Bisenet.build_BiSeNet import BiSeNet
# from Model_Yin import MY_NET
# from model_others.BIT import define_G
# from model_others.SNUNet import SNUNet_ECAM
# from model_others.SNUNet import Siam_NestedUNet_Conc
# from model_others.AERNet import zh_net
# from model_others.ChangNet import ChangNet
# from model_others.FC_DIFF import FC_Siam_diff
# from model_others.FC_EF import FC_EF
from xiaorong_aux import MY_NET
# ========================Methods=================================#
# ========================Dataload================================#
# from TESTdataset import BuildingChangeDataset
# from BICDDdataset import BTCDDDataset
# from GZCDDdataset import GZCDDDataset
# from CDDdataset import CDDDataset
from SYSUCDdataset import SYSUCDDataset
#from LEVIRdataset import LEVIRDataset
import warnings
warnings.filterwarnings('ignore')
# =========================================================#
train_data = SYSUCDDataset(mode='train')
#train_data = GZCDDDataset(mode='train')
# train_data = GZCDDDataset(mode='train')
data_loader = DataLoader(train_data, batch_size=16, shuffle=True)
test_data = SYSUCDDataset(mode='test')
# test_data =GZCDDDataset(mode='test')
# test_data = GZCDDDataset(mode='test')
test_data_loader = DataLoader(test_data, batch_size=16, shuffle=False)
Epoch = 200
lr = 0.0001
n_class = 2
F1_max = 0.80
root = r'C:\PycharmProjects\project1\codes-zzs\results\sysucd'
# ==========================Net===============================#
# net = CrossNet(n_class,[4,8,16,32]).cuda()
# net = define_G(args = 'base_transformer_pos_s4_dd8').cuda()
net = MY_NET(2).cuda()
# ==========================Net===============================#
criterion = nn.BCEWithLogitsLoss().cuda()
# focal = FocalLoss(gamma=2, alpha=0.25).cuda()
optimizer = optim.Adam(net.parameters(), lr=lr)
# scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer,mode='min',factor=0.9,patience=5)
with open(root +'/train.txt', 'a') as f:
for epoch in range(Epoch):
# print('lr:', optimizer.state_dict()['param_groups'][0]['lr'])
torch.cuda.empty_cache()
new_lr = adjust_learning_rate_poly(optimizer, epoch, Epoch, lr, 0.9)
print('lr:', new_lr)
_train_loss = 0
_hist = np.zeros((n_class, n_class))
net.train()
for before, after, change in tqdm(data_loader, desc='epoch{}'.format(epoch), ncols=100):
before = before.cuda()
after = after.cuda()
# ed_change = change.cuda()
# ed_change = edge(ed_change)
# lbl = torch.where(ed_change > 0.1, 1, 0)
# plt.figure()
# plt.imshow(lbl.data.cpu().numpy()[0][0], cmap='gray')
# plt.show()
# lbl = lbl.squeeze(dim=1).long().cpu()
# lbl_one_hot = get_one_hot(lbl, 2).permute(0, 3, 1, 2).contiguous().cuda()
change = change.squeeze(dim=1).long()
change_one_hot = get_one_hot(change, 2).permute(0, 3, 1, 2).contiguous().cuda()
optimizer.zero_grad()
# pred = net(before, after)
# loss_pred = criterion(pred, change_one_hot)
# loss = loss_pred
pred,aux1,aux2,aux3 = net(before, after)
loss_pred = criterion(pred, change_one_hot)
loss_aux_1 = criterion(aux1, change_one_hot)
loss_aux_2 = criterion(aux2, change_one_hot)
loss_aux_3 = criterion(aux3, change_one_hot)
loss = loss_pred + loss_aux_1/(loss_aux_1/loss_pred).detach() + loss_aux_2/(loss_aux_2/loss_pred).detach() + loss_aux_3/(loss_aux_3/loss_pred).detach()
loss.backward()
optimizer.step()
_train_loss += loss.item()
label_pred = F.softmax(pred, dim=1).max(dim=1)[1].data.cpu().numpy()
label_true = change.data.cpu().numpy()
hist = hist_sum(label_true, label_pred, 2)
_hist += hist
# scheduler.step()
miou, oa, kappa, precision, recall, iou, F1 = compute_metrics(_hist)
trainloss = _train_loss / len(data_loader)
print('Epoch:', epoch, ' |train loss:', trainloss, ' |train oa:', oa, ' |train iou:', iou, ' |train F1:', F1)
f.write('Epoch:%d|train loss:%0.04f|train miou:%0.04f|train oa:%0.04f|train kappa:%0.04f|train precision:%0.04f|train recall:%0.04f|train iou:%0.04f|train F1:%0.04f' % (
epoch, trainloss, miou, oa, kappa, precision, recall, iou, F1))
f.write('\n')
f.flush()
with torch.no_grad():
with open(root + '/test.txt', 'a') as f1:
torch.cuda.empty_cache()
_test_loss = 0
_hist = np.zeros((n_class, n_class))
k = 0
net.eval()
for before, after, change in tqdm(test_data_loader, desc='epoch{}'.format(epoch), ncols=100):
before = before.cuda()
after = after.cuda()
change = change.squeeze(dim=1).long()
change_one_hot = get_one_hot(change, 2).permute(0, 3, 1, 2).contiguous().cuda()
pred,aux1,aux2,aux3 = net(before, after)
loss = criterion(pred, change_one_hot)
_test_loss += loss.item()
label_pred = F.softmax(pred, dim=1).max(dim=1)[1].data.cpu().numpy()
label_true = change.data.cpu().numpy()
hist = hist_sum(label_true, label_pred, 2)
_hist += hist
miou, oa, kappa, precision, recall, iou, F1 = compute_metrics(_hist)
testloss = _test_loss / len(test_data_loader)
print('Epoch:', epoch, ' |test loss:', testloss, ' |test oa:', oa, ' |test iou:', iou, ' |test F1:', F1)
f1.write('Epoch:%d|test loss:%0.04f|test miou:%0.04f|test oa:%0.04f|test kappa:%0.04f|test precision:%0.04f|test recall:%0.04f|test iou:%0.04f|test F1:%0.04f' % (
epoch, testloss, miou, oa, kappa, precision, recall, iou, F1))
f1.write('\n')
f1.flush()
# scheduler.step(testloss)
# torch.save(net, r'E:\SeniorCode\CDsl\summaryTEST\BisNet\epoch_{}.pth'.format(epoch))
# # 每隔args.checkpoint_step保存模型的参数字典
# if epoch % 5 == 0 and epoch != 0:
# torch.save(net, r'E:\SeniorCode\CDsl\summaryTEST\BisNet\epoch_{}.pth'.format(epoch))
# 每个epoch记录验证集miou
# if epoch % 1 == 0:
if F1 > F1_max:
# save_path = args.summary_path+args.dir_name+'/checkpoints/'+'miou_{:.6f}.pth'.format(miou)
# torch.save(model.state_dict(), save_path)
save_path = root + '/F1_{:.4f}_iou_{:.4f}_epoch_{}.pth'.format(F1, iou, epoch)
torch.save(net.state_dict(), save_path)
# torch.save(net, root + 'F1_{:.4f}_epoch_{}.pth'.format(F1, epoch))
F1_max = F1