-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
324 lines (249 loc) · 11.5 KB
/
models.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import tensorflow as tf
from tensorflow.keras.utils import plot_model
from tensorflow.keras.layers import Activation, Concatenate, Conv2D, Conv2DTranspose, MaxPool2D, Input, BatchNormalization
from tensorflow.keras.models import Model
from tensorflow.keras.losses import BinaryCrossentropy as BCE
from tensorflow.keras.callbacks import EarlyStopping, CSVLogger, TensorBoard, ModelCheckpoint, LearningRateScheduler
from tensorflow.keras.optimizers import Adam
import tensorflow.keras.backend as K
from metrics_losses import metrics, dice_xent, iou
import h5py
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from skimage import img_as_ubyte
import cv2 as cv
from preprocess import single_overlay
from skimage.measure import regionprops, label
from skimage.morphology import remove_small_objects
class modelBuilder:
def __init__(self, input_shape = (256, 256, 3)) -> None:
self.input_shape = input_shape
self.input_layer = Input (input_shape)
def __conv_unit(self, inputs, nfilters):
x = Conv2D(nfilters, kernel_size= 3, padding= 'same')(inputs)
x = BatchNormalization()(x)
x = Activation('elu')(x)
x = Conv2D(nfilters, 3, padding= 'same')(inputs)
x = BatchNormalization()(x)
x = Activation('elu')(x)
return x
def _encoder_unit(self, inputs, nfilters):
x = self.__conv_unit(inputs, nfilters)
p = MaxPool2D()(x)
return x, p
def _decoder_unit(self, inputs, skpdfeatures, nfilters):
x = Conv2DTranspose(nfilters, kernel_size= 2, strides = 2, padding = 'same')(inputs)
x = Concatenate()([x, skpdfeatures])
x = self.__conv_unit(x, nfilters)
return x
def _out_unit (self, out_list, branch_name):
out = self.__conv_unit(out_list, 64)
out = self.__conv_unit(out, 32)
out = Conv2D(1, 1, padding= 'same', activation= 'sigmoid', name = branch_name)(out)
return out
def simple_unet (self, name = 'U_SIMPLE'):
# ENCODE #
x1, p1 = self._encoder_unit(self.input_layer, 32)
x2, p2 = self._encoder_unit(p1, 64)
x3, p3 = self._encoder_unit(p2, 128)
x4, p4 = self._encoder_unit(p3, 256)
# BRIDGE #
b = self.__conv_unit(p4, 512)
# DECODE #
y1 = self._decoder_unit (b, x4, 256 )
y2 = self._decoder_unit (y1, x3, 128)
y3 = self._decoder_unit (y2, x2, 64)
y4 = self._decoder_unit (y3, x1, 32)
# OUTPUT #
output = Conv2D(1, 1, padding= 'same', activation= 'sigmoid')(y4)
model = Model(inputs = self.input_layer, outputs = output, name = name)
return model
def compound_unet(self):
UNET_masks = self.simple_unet(name = 'U_MASKS')
UNET_borders = self.simple_unet( name = 'U_BORDERS')
output_list = Concatenate()([UNET_masks.output, UNET_borders.output])
# Encode Outs
out1 = self._out_unit(output_list, 'U_MASKS')
out2 = self._out_unit(output_list, 'U_BORDERS')
model = Model(inputs = self.input_layer, outputs = [out1, out2], name = 'UNET_ARCH')
return model
class fitEval(modelBuilder):
def __init__(self, simple = False, log_path = '', checkpnt_path = '', lr = 0.001, input_shape = (256, 256, 3)) -> None:
super().__init__(input_shape)
if simple:
self.model = self.simple_unet()
self.loss = dice_xent
self.loss_weights = None
self.monitor = 'iou'
else:
self.model = self.compound_unet()
self.loss = { "U_MASKS": dice_xent, "U_BORDERS": dice_xent }
self.loss_weights = {"U_MASKS": 1.0, "U_BORDERS": 1.0}
self.monitor = 'U_MASKS_iou'
self.lr = lr
self.lr_downstep = 20
self.decay = 0.75
self.optimizer = Adam(learning_rate= self.lr)
self.summary = self.model.summary
self.plot = plot_model(self.model, show_shapes=True)
self.log_path = log_path
self.checkpnt_path = checkpnt_path
metrcs = metrics()
self.model.compile(
optimizer= self.optimizer,
loss= self.loss,
loss_weights = self.loss_weights,
metrics= [iou, metrcs.mAP, metrcs.precsn, metrcs.recall])
def schedule(self, epoch):
return self.lr * (self.decay ** np.floor(epoch/self.lr_downstep))
def fit(self, train_gen, val_gen, round = 0, length = (3342, 836, 1045 ), user_dct = None):
len_train, len_val, len_test = length
self.len_test = len_test
self.round = round
self.epochs = 100
self.batchsize = 12
self.trainsteps = len_train // self.batchsize
self.valsteps = len_val // self.batchsize
self.teststeps = len_test
if user_dct:
for k,v in user_dct.items():
if k in self.__dict__:
setattr(self, k, v)
else:
raise KeyError(k)
self.earlyStop = EarlyStopping(monitor= self.monitor, min_delta= 0.001, patience= 10, verbose= 1, restore_best_weights= True)
self.tensorboard = TensorBoard(log_dir= self.log_path)
self.csvlogger = CSVLogger(self.log_path + f'training_{self.round}.log')
# self.checkpointer = ModelCheckpoint(self.checkpnt_path + f'{self.round}_' +"mchp_{epoch:04d}.hdf5", verbose= 1, save_weights_only= True )
self.checkpointer = ModelCheckpoint(self.checkpnt_path + f'{self.round}_' + "best_model.hdf5", monitor = self.monitor, mode = 'max', verbose= 1, save_best_only= True, save_weights_only= True )
self.lr_sched = LearningRateScheduler (self.schedule, verbose= 1)
callbacks = [self.lr_sched, self.csvlogger, self.checkpointer, self.tensorboard]
results = self.model.fit( x = train_gen, validation_data = val_gen,
verbose= 1,
steps_per_epoch= self.trainsteps,
validation_steps= self.valsteps,
batch_size= self.batchsize,
callbacks = callbacks ,
epochs = self.epochs)
return results
pix_sup= np.vectorize(lambda x: 0 if x < 0.5 else 1)
def evaluate(self, test_gen, len_test = 1045, batch_size = 12):
self.len_test = len_test
self.batchsize = batch_size
test_scores = self.model.evaluate(test_gen, batch_size= self.batchsize , steps= self.len_test // self.batchsize , return_dict= True)
return test_scores
def load_weights(self, weightpath):
model = self.model
model.load_weights(weightpath)
self.model = model
def prec_recall(self, test_gen, len_xtest = 1045, neg_data = False):
iouthresh_steps =[i for i in np.arange (0.1, 1,.05)]
p_curve = []
r_curve = []
for step in iouthresh_steps:
p_scores = []
r_scores = []
for i in range (len_xtest):
xt , [ymask, yborder] = next(test_gen)
yprd_msk = self.model.predict(xt)
met = metrics(iou_threshold = step)
pr = met.precsn(ymask, yprd_msk).numpy()
rc = met.recall(ymask, yprd_msk).numpy() if neg_data else 0
p_scores.append (pr)
r_scores.append (rc)
p_curve.append(np.mean(p_scores))
r_curve.append(np.mean(r_scores))
self.plot_presRecall(iouthresh_steps, p_curve, r_curve)
return p_curve, r_curve
@staticmethod
def plot_presRecall(steps, p_curve, r_curve):
x = [np.round(i, 2) for i in np.arange (0.1, 1, .1)]
width = 0.05
plt.figure(figsize = (20,8))
plt.subplot(121)
plt.bar(x, width= width, height= p_curve[::2])
plt.xticks(x, x)
plt.yticks(np.arange(0, 1.05, 0.05))
plt.xlabel('IoU Threshold', fontsize = 14)
plt.ylabel ('Average Precision', fontsize = 14 )
plt.suptitle('Evaluation', y = 1.02, fontsize = 18)
plt.subplot(122)
plt.plot(steps, p_curve)
if sum(r_curve) > 0 : plt.plot(steps, r_curve)
plt.xlabel('IoU Threshold', fontsize = 14)
plt.xticks(steps, rotation = 20)
plt.yticks(np.arange(0,1.05, 0.05))
plt.ylabel('Precision | Recall', fontsize = 14)
plt.legend(['Precision', 'Recall'], loc='best')
plt.tight_layout()
plt.show()
@staticmethod
def label_overlay(ximg, mask, border, test = False):
pixel_clip = np.vectorize(lambda pixel: 0 if pixel < 0.5 else 1)
mask_clr, brdr_clr = (255, 0, 0) , (0, 255, 0)
if test:
mask_clr, brdr_clr = (0, 0, 255), (255, 255, 255)
mask, border = (pixel_clip(label).squeeze() for label in (mask, border))
image = np.copy(ximg)
image /= np.max(image)
image *= 255
image = image.astype(dtype = np.uint8).squeeze()
mask *= 255
border *= 255
mask = mask.astype(dtype = np.uint8).squeeze()
border = border.astype(dtype = np.uint8).squeeze()
blue_canvas = np.full(image.shape, mask_clr, image.dtype)
white_canvas = np.full(image.shape, brdr_clr, image.dtype)
blueMask = cv.bitwise_and(blue_canvas, blue_canvas, mask=mask)
whiteborder = cv.bitwise_and(white_canvas, white_canvas, mask=border)
out = cv.addWeighted(blueMask, .5, image, 1, 0, image)
out = cv.addWeighted(whiteborder, .5, out, 1, 0, out)
return out
def make_pred(self, xt):
p_label = self.model.predict(xt)
if len(p_label) == 2:
p_mask, p_border = p_label
else:
p_mask, p_border = p_label, np.zeros_like(p_label)
return p_mask, p_border
@staticmethod
def process_pred(p_mask, min_size = 1000):
pixel_clip = np.vectorize(lambda pixel: 0 if pixel < 0.5 else 1)
p_mask = pixel_clip(p_mask.squeeze())
labels = label(p_mask, background = 0)
labels = remove_small_objects(labels, min_size = min_size)
props = [p for p in regionprops(labels)]
areas = [p.area for p in props]
bboxs = []
if len(areas) > 0:
sorted_inds = sorted (np.argsort(areas), reverse = 1)
for ind in sorted_inds:
max_area = np.argmax(areas)
candidate = props[ind]
bbox_coords = candidate.bbox
bboxs.append(bbox_coords)
return bboxs, labels
def inspect_preds(self, testgen, length):
ovs = []
for i in range(length):
X ,_ = next(testgen)
p_mask, p_border = self.make_pred(X)
bboxs, labels = self.process_pred(p_mask, min_size = 1000)
_ , border_labels = self.process_pred(p_border, min_size = 200)
ov = self.label_overlay(X, labels, border_labels, test = True)
if len(bboxs) > 0:
for bbx in bboxs:
minr, minc, maxr, maxc = bbx
start, end = (minc, minr) , (maxc, maxr)
ov = cv.rectangle(ov, start, end, color = (36,255,12 ), thickness = 2)
ovs.append(ov)
return ovs
class loadModel(fitEval):
def __init__(self, simple=True, lr= 0.00001, input_shape=(256, 256, 3)) -> None:
super().__init__(simple,lr, input_shape)
self.weightpath = ''
self.lr = lr
def load_model(self, weightpath):
self.weightpath = weightpath
return self.load_weights(self.weightpath)