-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_edge_point.py
294 lines (259 loc) · 9.2 KB
/
train_edge_point.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# standard library
import os
import sys
sys.path.append("./")
sys.path.insert(0, os.getcwd())
# 3rd part packages
from keras.models import load_model
from keras.optimizers import RMSprop
from keras import metrics
from keras.callbacks import LearningRateScheduler
from local_callbacks import ModelCheckpoint
from argparse import ArgumentParser
# local source
from args_edge_point import get_arguments
from models.linknet import LinkNet
from models.keras_fc_densenet import build_FC_DenseNet
from models.conv2d_transpose import Conv2DTranspose
# from data import generator_edge_point as data_generator
from dataset import data_generator
from metrics import loss
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
parser = ArgumentParser()
args = parser.parse_args()
def main(in_dataset_name, in_dataset_fold,
in_dataset_itr, in_model_ext,
in_resume_checkpoint_path, save_checkpoint_path):
# Get command line arguments
args.mode = 'train'
args.dataset_name = in_dataset_name
args.dataset_fold = in_dataset_fold
args.dataset_itr = in_dataset_itr
model_ext = in_model_ext
resume_checkpoint_path = in_resume_checkpoint_path
args.patch_size = 512
args.loss_weight_1 = {
'out_1': 1.0,
'out_2': 0.01,
'out_3': 0.1,
'out_4': 0.1,
'out_5': 0.0
}
args.loss_weight_2 = {
'out_1': 0.01,
'out_2': 0.01,
'out_3': 0.01,
'out_4': 0.01,
'out_5': 1.0
}
args.loss_weight_3 = {
'out_1': 1.0,
'out_2': 0.01,
'out_3': 1.0,
'out_4': 0.01,
'out_5': 1.0
}
if args.dataset_itr == 0:
args.train_sub_dir = 'train_r0'
args.val_sub_dir = 'val'
args.loss_weight = args.loss_weight_1
args.resume = False
args.epochs = 200
elif args.dataset_itr == 1:
args.train_sub_dir = 'train_r1'
args.val_sub_dir = 'val'
args.loss_weight = args.loss_weight_1
args.resume = True
args.epochs = 200
elif args.dataset_itr == 2:
args.train_sub_dir = 'train_r2'
args.val_sub_dir = 'val'
args.loss_weight = args.loss_weight_1
args.resume = True
args.epochs = 200
elif args.dataset_itr == 3:
args.train_sub_dir = 'train_r3'
args.val_sub_dir = 'val'
args.loss_weight = args.loss_weight_2
args.resume = True
args.epochs = 50
elif args.dataset_itr == 4:
args.train_sub_dir = 'train_r4'
args.val_sub_dir = 'val'
args.loss_weight = args.loss_weight_3
args.resume = False
args.epochs = 200
else:
raise Exception('dataset_itr not in list')
args.dataset_dir = os.path.join(
'./data/', args.dataset_name, args.dataset_fold)
args.checkpoint_dir = os.path.join(
save_checkpoint_path,
args.dataset_fold + '_model')
args.initial_epoch = 0
args.pretrained_encoder = True
args.weights_path = './checkpoints/linknet_encoder_weights.h5'
args.workers = 32
args.verbose = 1
args.learning_rate = 5e-4
args.lr_decay = 0.1
args.lr_decay_epochs = 200
args.batch_size = 8
args.outputchannels = 3
args.scale_range = (0.9, 1.1)
args.brightrange = (0.7, 1.2)
num_classes = 1
input_shape = (args.patch_size, args.patch_size, 3)
if not os.path.exists(args.checkpoint_dir):
os.makedirs(args.checkpoint_dir)
args.name = 'LinkNet.nuclei.%s.%s.h5' % (
os.path.basename(os.path.normpath(args.dataset_dir))[:10],
str(args.patch_size)
)
train_generator = data_generator.DataGenerator(
datapath=args.dataset_dir,
mode=args.train_sub_dir,
patchsize=(args.patch_size, args.patch_size),
batch_size=args.batch_size,
outputchannels=args.outputchannels,
expandtimes=10,
shuffle=True,
flag_rotate=True,
flag_scale=True,
flag_flip=True,
brightrange=args.brightrange,
scale_range=args.scale_range,
flag_bright=True,
flag_color=True
)
val_generator = data_generator.DataGenerator(
datapath=args.dataset_dir,
mode=args.val_sub_dir,
patchsize=(args.patch_size, args.patch_size),
batch_size=2,
outputchannels=args.outputchannels,
expandtimes=1,
shuffle=False,
flag_rotate=False,
flag_scale=True,
flag_flip=False,
brightrange=args.brightrange,
scale_range=args.scale_range,
flag_bright=False,
flag_color=False,
flag_random=False
)
loss_weight = args.loss_weight
resume_str = '_resume' if args.resume else ''
checkpoint_path = os.path.join(
args.checkpoint_dir,
args.name[:-3] +
'_loss_%s_%s_%s_%s_%s_%s_r%s%s_%s.h5' % (
str(loss_weight['out_1']),
str(loss_weight['out_2']),
str(loss_weight['out_3']),
str(loss_weight['out_4']),
str(loss_weight['out_5']),
args.dataset_fold,
str(args.dataset_itr),
resume_str,
model_ext)
)
print("--> Checkpoint path: {}".format(checkpoint_path))
last_ckpt_path = checkpoint_path[:-3] + '.last.h5'
best_ckpt_path = checkpoint_path[:-3] + '.{epoch:02d}.h5'
model = None
if args.mode.lower() in ('train', 'full'):
if args.resume:
print("--> Resuming model: {}".format(resume_checkpoint_path))
model = LinkNet(num_classes, input_shape=input_shape)
model = model.get_model(
pretrained_encoder=False
)
model.load_weights(resume_checkpoint_path)
print('load %s weights success!' % resume_checkpoint_path)
# print("--> Resuming model: {}".format(resume_checkpoint_path))
# model = build_FC_DenseNet(
# model_version='fcdn56', nb_classes=num_classes,
# final_softmax=False,
# input_shape=input_shape, dropout_rate=0.2,
# data_format='channels_last')
#
# model.load_weights(resume_checkpoint_path)
# print('load %s weights success!' % resume_checkpoint_path)
if model is None:
model = LinkNet(num_classes, input_shape=input_shape)
model = model.get_model(
pretrained_encoder=args.pretrained_encoder,
weights_path=args.weights_path
)
# model = build_FC_DenseNet(
# model_version='fcdn56', nb_classes=num_classes,
# final_softmax=False,
# input_shape=input_shape, dropout_rate=0.2,
# data_format='channels_last')
print(model.summary())
# Optimizer: RMSprop
optim = RMSprop(args.learning_rate)
for output in model.outputs:
print(output.name)
# Compile the model
# Loss: Categorical crossentropy loss
model.compile(
optimizer=optim,
loss={
'out_1': loss.point_loss,
'out_2': loss.point_dis_loss,
'out_3': loss.edge_dis_loss,
'out_4': loss.fake_loss,
# 'out_5': loss.edge_supplement_online_loss,
'out_5': loss.edge_supplement_loss,
},
loss_weights=loss_weight,
metrics=[]
)
# Set up learining rate scheduler
def _lr_decay(epoch):
return args.lr_decay ** (epoch // args.lr_decay_epochs) *\
args.learning_rate
lr_scheduler = LearningRateScheduler(_lr_decay)
# Checkpoint callback - save the best model
checkpoint = ModelCheckpoint(
best_ckpt_path,
last_ckpt_path,
monitor='val_loss',
save_best=True,
save_last=True,
mode='min',
val_dir=args.dataset_dir + 'val/'
)
callbacks = [lr_scheduler, checkpoint]
# Train the model
model.fit_generator(
train_generator,
epochs=args.epochs,
max_queue_size=24,
initial_epoch=args.initial_epoch,
callbacks=callbacks,
workers=0,
verbose=args.verbose,
use_multiprocessing=False,
validation_data=val_generator
)
return last_ckpt_path
if __name__ == '__main__':
os.environ['CUDA_VISIBLE_DEVICES'] = '4'
in_dataset_name = 'monuseg'
in_dataset_fold = 'train_val'
in_dataset_itr = 3
in_model_ext = 'point_neg_fake_sobel'
in_resume_checkpoint_path = \
'checkpoints/monuseg_ln/train_val_model/' \
'LinkNet.nuclei.train_val.512_loss_1.0_' \
'0.01_0.1_0.1_0.0_train_val_r2_resume_point_edge_fake.last.h5'
save_checkpoint_path = './checkpoints/monuseg_ln'
main(in_dataset_name, in_dataset_fold,
in_dataset_itr, in_model_ext, in_resume_checkpoint_path,
save_checkpoint_path)