-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
242 lines (232 loc) · 8.08 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
import torch.utils.data as data
from tqdm import tqdm
from dataset import RoadExtractionDataset
from extractor import Extractor
# # DLinkNet
# from network import DLinkNet34
# from loss import DiceBCELoss
#
# root_dir = '../datasets/deepglobe/train_crops/'
# weight_dir = '../weights/essentialfeatures/dlinknet/'
#
# dataset = RoadExtractionDataset(root_dir)
# dataloader = data.DataLoader(dataset, batch_size=4, shuffle=True)
#
# model = Extractor(DLinkNet34, DiceBCELoss)
#
# total_epoch = 300
# train_epoch_best_loss = 100.
# no_optim = 0
# for epoch in range(0, total_epoch + 1):
# dataloader_iter = iter(dataloader)
# train_epoch_loss = 0
# for image, mask, _ in tqdm(dataloader_iter):
# train_loss = model.optimize()
# train_epoch_loss += train_loss
# train_epoch_loss /= len(dataloader)
# print('epoch:', epoch, ' train_epoch_loss:', train_epoch_loss, ' lr:', model.old_lr)
# if train_epoch_loss >= train_epoch_best_loss:
# no_optim += 1
# else:
# no_optim = 0
# model.save(weight_dir + str(epoch) + '.th')
# model.save(weight_dir + 'best.th')
# train_epoch_best_loss = train_epoch_loss
# if no_optim > 6:
# print('early stop at %d epoch' % epoch)
# break
# if no_optim > 3:
# if model.old_lr < 5e-7:
# break
# model.load(weight_dir + 'best.th')
# model.update_lr()
# # DLinkNet + HT
# from network import HTDLinkNet34
# from loss import DiceBCELoss
#
# root_dir = '../datasets/deepglobe/train_crops/'
# weight_dir = '../weights/essentialfeatures/htdlinknet/'
#
# dataset = RoadExtractionDataset(root_dir)
# dataloader = data.DataLoader(dataset, batch_size=4, shuffle=True)
#
# model = Extractor(HTDLinkNet34, DiceBCELoss)
#
# total_epoch = 300
# train_epoch_best_loss = 100.
# no_optim = 0
# for epoch in range(0, total_epoch + 1):
# dataloader_iter = iter(dataloader)
# train_epoch_loss = 0
# for image, mask, _ in tqdm(dataloader_iter):
# train_loss = model.optimize()
# train_epoch_loss += train_loss
# train_epoch_loss /= len(dataloader)
# print('epoch:', epoch, ' train_epoch_loss:', train_epoch_loss, ' lr:', model.old_lr)
# if train_epoch_loss >= train_epoch_best_loss:
# no_optim += 1
# else:
# no_optim = 0
# model.save(weight_dir + str(epoch) + '.th')
# model.save(weight_dir + 'best.th')
# train_epoch_best_loss = train_epoch_loss
# if no_optim > 6:
# print('early stop at %d epoch' % epoch)
# break
# if no_optim > 3:
# if model.old_lr < 5e-7:
# break
# model.load(weight_dir + 'best.th')
# model.update_lr()
# # DLinkNet + NR
# from network import MDLinkNet34
# from loss import SmoothDiceBCELoss
#
# root_dir = '../datasets/deepglobe/train_crops/'
# weight_dir = '../weights/essentialfeatures/mdlinknet/'
#
# dataset = RoadExtractionDataset(root_dir)
# dataloader = data.DataLoader(dataset, batch_size=4, shuffle=True)
#
# model = Extractor(MDLinkNet34, SmoothDiceBCELoss)
#
# total_epoch = 300
# train_epoch_best_loss = 100.
# no_optim = 0
# for epoch in range(0, total_epoch + 1):
# dataloader_iter = iter(dataloader)
# train_epoch_loss = 0
# for image, mask, nr_map in tqdm(dataloader_iter):
# model.set_input(image, mask, nr_map)
# train_loss = model.optimize()
# train_epoch_loss += train_loss
# train_epoch_loss /= len(dataloader)
# print('epoch:', epoch, ' train_epoch_loss:', train_epoch_loss, ' lr:', model.old_lr)
# if train_epoch_loss >= train_epoch_best_loss:
# no_optim += 1
# else:
# no_optim = 0
# model.save(weight_dir + str(epoch) + '.th')
# model.save(weight_dir + 'best.th')
# train_epoch_best_loss = train_epoch_loss
# if no_optim > 6:
# print('early stop at %d epoch' % epoch)
# break
# if no_optim > 3:
# if model.old_lr < 5e-7:
# break
# model.load(weight_dir + 'best.th')
# model.update_lr()
# DLinkNet + HT + NR
from network import MHTDLinkNet34
from loss import SmoothDiceBCELoss
root_dir = '../datasets/deepglobe/train_crops/'
weight_dir = '../weights/essentialfeatures/mhtdlinknet/'
dataset = RoadExtractionDataset(root_dir)
dataloader = data.DataLoader(dataset, batch_size=12, shuffle=True)
model = Extractor(MHTDLinkNet34, SmoothDiceBCELoss)
model.load(weight_dir + 'best.th')
total_epoch = 300
train_epoch_best_loss = 100.
no_optim = 0
for epoch in range(59, total_epoch + 1):
dataloader_iter = iter(dataloader)
train_epoch_loss = 0
for image, mask, nr_map in tqdm(dataloader_iter):
model.set_input(image, mask, nr_map)
train_loss = model.optimize()
train_epoch_loss += train_loss
train_epoch_loss /= len(dataloader)
print('epoch:', epoch, ' train_epoch_loss:', train_epoch_loss, ' lr:', model.old_lr)
if train_epoch_loss >= train_epoch_best_loss:
no_optim += 1
else:
no_optim = 0
model.save(weight_dir + str(epoch) + '.th')
model.save(weight_dir + 'best.th')
train_epoch_best_loss = train_epoch_loss
if no_optim > 6:
print('early stop at %d epoch' % epoch)
break
if no_optim > 3:
if model.old_lr < 5e-7:
break
model.load(weight_dir + 'best.th')
model.update_lr()
# # ResUNet
# from network import ResUNet
# from loss import DiceBCELoss
#
# root_dir = '../datasets/deepglobe/train_crops/'
# weight_dir = '../weights/essentialfeatures/resunet/'
#
# dataset = RoadExtractionDataset(root_dir, nr_head=False)
# dataloader = data.DataLoader(dataset, batch_size=4, shuffle=True)
#
# model = Extractor(ResUNet, DiceBCELoss)
#
# total_epoch = 300
# train_epoch_best_loss = 100.
# no_optim = 0
# for epoch in range(1, total_epoch + 1):
# dataloader_iter = iter(dataloader)
# train_epoch_loss = 0
# for image, mask in tqdm(dataloader_iter):
# model.set_input(image, mask)
# train_loss = model.optimize()
# train_epoch_loss += train_loss
# train_epoch_loss /= len(dataloader)
# print('epoch:', epoch, ' train_epoch_loss:', train_epoch_loss, ' lr:', model.old_lr)
# if train_epoch_loss >= train_epoch_best_loss:
# no_optim += 1
# else:
# no_optim = 0
# model.save(weight_dir + str(epoch) + '.th')
# model.save(weight_dir + 'best.th')
# train_epoch_best_loss = train_epoch_loss
# if no_optim > 6:
# print('early stop at %d epoch' % epoch)
# break
# if no_optim > 3:
# if model.old_lr < 5e-7:
# break
# model.load(weight_dir + 'best.th')
# model.update_lr()
# # UNet
# from network import UNet
# from loss import DiceBCELoss
#
# root_dir = '../datasets/deepglobe/train_crops/'
# weight_dir = '../weights/essentialfeatures/unet/'
#
# dataset = RoadExtractionDataset(root_dir, nr_head=False)
# dataloader = data.DataLoader(dataset, batch_size=4, shuffle=True)
#
# model = Extractor(UNet, DiceBCELoss)
#
# total_epoch = 300
# train_epoch_best_loss = 100.
# no_optim = 0
# for epoch in range(1, total_epoch + 1):
# dataloader_iter = iter(dataloader)
# train_epoch_loss = 0
# for image, mask, _ in tqdm(dataloader_iter):
# train_loss = model.optimize()
# train_epoch_loss += train_loss
# train_epoch_loss /= len(dataloader)
# print('epoch:', epoch, ' train_epoch_loss:', train_epoch_loss, ' lr:', model.old_lr)
# if train_epoch_loss >= train_epoch_best_loss:
# no_optim += 1
# else:
# no_optim = 0
# model.save(weight_dir + str(epoch) + '.th')
# model.save(weight_dir + 'best.th')
# train_epoch_best_loss = train_epoch_loss
# if no_optim > 6:
# print('early stop at %d epoch' % epoch)
# break
# if no_optim > 3:
# if model.old_lr < 5e-7:
# break
# model.load(weight_dir + 'best.th')
# model.update_lr()