-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBT-Unet.py
653 lines (530 loc) · 21.5 KB
/
BT-Unet.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
import tensorflow as tf
from tensorflow.keras.metrics import *
from tensorflow.keras.layers import *
from tensorflow.keras import layers
from tensorflow.keras.models import Model
from tensorflow.keras.applications import *
from tensorflow.keras.optimizers import Adam, Nadam
from tensorflow.keras.callbacks import *
from tensorflow.keras import backend as K
from hausdorff import hausdorff_distance
from tensorflow.keras.regularizers import l2
from sklearn.model_selection import StratifiedKFold
import os
import random
import numpy as np
from tqdm import tqdm
from skimage.io import imread,imshow
from skimage.morphology import label
from skimage.transform import resize
import matplotlib.pyplot as plt
import datetime
################### FUNCTIONS TO DEVELOP U-NET MODEL ###################
def expend_as(tensor, rep):
return Lambda(lambda x, repnum: K.repeat_elements(x, repnum, axis=3),
arguments={'repnum': rep})(tensor)
def double_conv_layer(x, filter_size, size, dropout, batch_norm=False):
axis = 3
conv = SeparableConv2D(size, (filter_size, filter_size), padding='same')(x)
if batch_norm is True:
conv = BatchNormalization(axis=axis)(conv)
conv = Activation('relu')(conv)
conv = SeparableConv2D(size, (filter_size, filter_size), padding='same')(conv)
if batch_norm is True:
conv = BatchNormalization(axis=axis)(conv)
conv = Activation('relu')(conv)
if dropout > 0:
conv = Dropout(dropout)(conv)
shortcut = Conv2D(size, kernel_size=(1, 1), padding='same')(x)
if batch_norm is True:
shortcut = BatchNormalization(axis=axis)(shortcut)
res_path = add([shortcut, conv])
return res_path
def encoder(inputs):
num_filters = [16, 32, 64, 128]
skip_connections = []
x = inputs
for i, f in enumerate(num_filters):
a = double_conv_layer(x, 3, f, 0.1, True)
skip_connections.append(a)
x = MaxPooling2D(pool_size=(2, 2))(a)
return x, skip_connections
def bottleneck(inputs):
x = inputs
f = 256
x3 = double_conv_layer(x, 3, f, 0.1, True)
return x3
def decoder(inputs, skip_connections):
num_filters = [128, 64, 32, 16]
skip_connections.reverse()
x = inputs
batch_norm = True
for i, f in enumerate(num_filters):
x_up = UpSampling2D(size=(2, 2), data_format="channels_last")(x)
x_att = concatenate([x_up, skip_connections[i]], axis=-1)
x = double_conv_layer(x_att, 3, f, 0.1, True)
return x
def output(inputs):
x = Conv2D(1, kernel_size=(1,1))(inputs)
x = BatchNormalization()(x)
x = Activation('sigmoid')(x)
return x
################### LR SCHEDULER ###################
class WarmUpCosine(tf.keras.optimizers.schedules.LearningRateSchedule):
"""
Implements an LR scheduler that warms up the learning rate for some training steps
(usually at the beginning of the training) and then decays it
with CosineDecay (see https://arxiv.org/abs/1608.03983)
"""
def __init__(
self, learning_rate_base, total_steps, warmup_learning_rate, warmup_steps
):
super(WarmUpCosine, self).__init__()
self.learning_rate_base = learning_rate_base
self.total_steps = total_steps
self.warmup_learning_rate = warmup_learning_rate
self.warmup_steps = warmup_steps
self.pi = tf.constant(np.pi)
def __call__(self, step):
if self.total_steps < self.warmup_steps:
raise ValueError("Total_steps must be larger or equal to warmup_steps.")
learning_rate = (
0.5
* self.learning_rate_base
* (
1
+ tf.cos(
self.pi
* (tf.cast(step, tf.float32) - self.warmup_steps)
/ float(self.total_steps - self.warmup_steps)
)
)
)
if self.warmup_steps > 0:
if self.learning_rate_base < self.warmup_learning_rate:
raise ValueError(
"Learning_rate_base must be larger or equal to "
"warmup_learning_rate."
)
slope = (
self.learning_rate_base - self.warmup_learning_rate
) / self.warmup_steps
warmup_rate = slope * tf.cast(step, tf.float32) + self.warmup_learning_rate
learning_rate = tf.where(
step < self.warmup_steps, warmup_rate, learning_rate
)
return tf.where(
step > self.total_steps, 0.0, learning_rate, name="learning_rate"
)
################### FUNCTIONS TO CORRUPT IMAGES ###################
def random_resize_crop(image, scale=[0.75, 1.0], crop_size=128):
if crop_size == 32:
image_shape = 48
image = tf.image.resize(image, (image_shape, image_shape))
else:
image_shape = 96
image = tf.image.resize(image, (image_shape, image_shape))
size = tf.random.uniform(
shape=(1,),
minval=scale[0] * image_shape,
maxval=scale[1] * image_shape,
dtype=tf.float32,
)
size = tf.cast(size, tf.int32)[0]
crop = tf.image.random_crop(image, (size, size, 3))
crop_resize = tf.image.resize(crop, (crop_size, crop_size))
return crop_resize
def flip_random_crop(image):
image = tf.image.random_flip_left_right(image)
image = random_resize_crop(image, crop_size=CROP_TO)
return image
def float_parameter(level, maxval):
return tf.cast(level * maxval / 10.0, tf.float32)
def sample_level(n):
return tf.random.uniform(shape=[1], minval=0.1, maxval=n, dtype=tf.float32)
def rotation(image):
augmented_image = tf.image.rot90(image)
return augmented_image
def solarize(image, level=6):
threshold = float_parameter(sample_level(level), 1)
return tf.where(image < threshold, image, 255 - image)
def color_jitter(x, strength=0.5):
x = tf.image.random_brightness(x, max_delta=0.8 * strength)
x = tf.image.random_contrast(
x, lower=1 - 0.8 * strength, upper=1 + 0.8 * strength
)
x = tf.image.random_saturation(
x, lower=1 - 0.8 * strength, upper=1 + 0.8 * strength
)
x = tf.image.random_hue(x, max_delta=0.2 * strength)
x = tf.clip_by_value(x, 0, 255)
return x
def color_drop(x):
x = tf.image.rgb_to_grayscale(x)
x = tf.tile(x, [1, 1, 3])
return x
def random_apply(func, x, p):
if tf.random.uniform([], minval=0, maxval=1) < p:
return func(x)
else:
return x
def custom_augment(image):
image = tf.cast(image, tf.float32)
image = flip_random_crop(image)
image = random_apply(rotation, image, p=0.5)
#image = random_apply(color_jitter, image, p=0.9)
#image = random_apply(color_drop, image, p=0.3)
#image = random_apply(solarize, image, p=0.3)
return image
################### SETTING DATA ###################
IMG_HEIGHT = 256
IMG_WIDTH = 256
IMG_CHANNELS = 3
X_train = np.zeros((70, IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.float32) #input data samples
Y_train = np.zeros((70, IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.float32) # corrensponding binary mask
X_test = np.zeros((30, IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.float32) #test data samples
Y_test = np.zeros((30, IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.float32) # corrensponding binary mask
################### CORRUPTING DATA FOR PRE-TRAINING ###################
AUTO = tf.data.AUTOTUNE
CROP_TO = IMG_HEIGHT
SEED = 42
BATCH_SIZE = 8
ssl_ds_one = tf.data.Dataset.from_tensor_slices(X_train)
ssl_ds_one = (
ssl_ds_one.shuffle(1024, seed=SEED)
.map(custom_augment, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
.prefetch(AUTO)
)
ssl_ds_two = tf.data.Dataset.from_tensor_slices(X_train)
ssl_ds_two = (
ssl_ds_two.shuffle(1024, seed=SEED)
.map(custom_augment, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
.prefetch(AUTO)
)
ssl_ds = tf.data.Dataset.zip((ssl_ds_one, ssl_ds_two))
################### SETUP BARLOW TWINS ###################
def off_diagonal(x):
n = tf.shape(x)[0]
flattened = tf.reshape(x, [-1])[:-1]
off_diagonals = tf.reshape(flattened, (n-1, n+1))[:, 1:]
return tf.reshape(off_diagonals, [-1])
def normalize_repr(z):
z_norm = (z - tf.reduce_mean(z, axis=0)) / tf.math.reduce_std(z, axis=0)
return z_norm
def compute_loss(z_a, z_b, lambd):
# Get batch size and representation dimension.
batch_size = tf.cast(tf.shape(z_a)[0], z_a.dtype)
repr_dim = tf.shape(z_a)[1]
# Normalize the representations along the batch dimension.
z_a_norm = normalize_repr(z_a)
z_b_norm = normalize_repr(z_b)
# Cross-correlation matrix.
c = tf.matmul(z_a_norm, z_b_norm, transpose_a=True) / batch_size
# Loss.
on_diag = tf.linalg.diag_part(c) + (-1)
on_diag = tf.reduce_sum(tf.pow(on_diag, 2))
off_diag = off_diagonal(c)
off_diag = tf.reduce_sum(tf.pow(off_diag, 2))
loss = on_diag + (lambd * off_diag)
return loss
class BarlowTwins(tf.keras.Model):
def __init__(self, encoder, lambd=5e-3):
super(BarlowTwins, self).__init__()
self.encoder = encoder
self.lambd = lambd
self.loss_tracker = tf.keras.metrics.Mean(name="loss")
@property
def metrics(self):
return [self.loss_tracker]
def train_step(self, data):
# Unpack the data.
ds_one, ds_two = data
# Forward pass through the encoder and predictor.
with tf.GradientTape() as tape:
z_a, z_b = self.encoder(ds_one, training=True), self.encoder(ds_two, training=True)
loss = compute_loss(z_a, z_b, self.lambd)
# Compute gradients and update the parameters.
gradients = tape.gradient(loss, self.encoder.trainable_variables)
self.optimizer.apply_gradients(zip(gradients, self.encoder.trainable_variables))
# Monitor loss.
self.loss_tracker.update_state(loss)
return {"loss": self.loss_tracker.result()}
PROJECT_DIM = IMG_HEIGHT/2
BATCH_SIZE = 8
EPOCHS = 100
WEIGHT_DECAY = 5e-4
STEPS_PER_EPOCH = len(X_train) // BATCH_SIZE
TOTAL_STEPS = STEPS_PER_EPOCH * EPOCHS
WARMUP_EPOCHS = int(EPOCHS * 0.1)
WARMUP_STEPS = int(WARMUP_EPOCHS * STEPS_PER_EPOCH)
lr_decayed_fn = WarmUpCosine(
learning_rate_base=1e-3,
total_steps=EPOCHS * STEPS_PER_EPOCH,
warmup_learning_rate=0.0,
warmup_steps=WARMUP_STEPS
)
plt.plot(lr_decayed_fn(tf.range(EPOCHS*STEPS_PER_EPOCH, dtype=tf.float32)))
plt.ylabel("Learning Rate")
plt.xlabel("Train Step")
plt.show()
def projection_head(x, hidden_dim=128):
"""Constructs the projection head."""
for i in range(2):
x = Dense(
hidden_dim,
name=f"projection_layer_{i}",
kernel_regularizer=l2(WEIGHT_DECAY),
)(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
outputs = Dense(hidden_dim, name="projection_output")(x)
return outputs
def build_encoder(shape, hidden_dim=128):
inputs = Input(shape)
s = layers.experimental.preprocessing.Rescaling(1.0 / 255)(inputs)
#s = inputs
x, skip_1 = encoder(s)
x = bottleneck(x)
# Projections
trunk_output = GlobalAvgPool2D()(x)
projection_outputs = projection_head(trunk_output, hidden_dim=hidden_dim)
model = Model(inputs, projection_outputs)
return model
################### GET THE U-NET ENCODER MODEL ###################
unet_enc = build_encoder((IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS), hidden_dim=PROJECT_DIM)
unet_enc.summary()
optimizer = tf.keras.optimizers.SGD(learning_rate=lr_decayed_fn, momentum=0.9)
################### TRAINING BARLOW TWINS ###################
barlow_twins = BarlowTwins(unet_enc)
barlow_twins.compile(optimizer=optimizer)
barlow_twins.encoder.get_weights()[0]
history = barlow_twins.fit(ssl_ds, epochs=EPOCHS)
# Visualize the training progress of the model.
plt.plot(history.history["loss"])
plt.grid()
plt.title("Barlow Twin Loss")
plt.show()
# Save the model.
barlow_twins.encoder.save("barlow_twins_UNET")
barlow_twins.encoder.load_weights("barlow_twins_UNET")
################### METRICS ###################
def iou_metric(y_true_in, y_pred_in, print_table=False):
labels = label(y_true_in > 0.5)
y_pred = label(y_pred_in > 0.5)
true_objects = len(np.unique(labels))
pred_objects = len(np.unique(y_pred))
intersection = np.histogram2d(labels.flatten(), y_pred.flatten(), bins=(true_objects, pred_objects))[0]
# Compute areas (needed for finding the union between all objects)
area_true = np.histogram(labels, bins = true_objects)[0]
area_pred = np.histogram(y_pred, bins = pred_objects)[0]
area_true = np.expand_dims(area_true, -1)
area_pred = np.expand_dims(area_pred, 0)
# Compute union
union = area_true + area_pred - intersection
# Exclude background from the analysis
intersection = intersection[1:,1:]
union = union[1:,1:]
union[union == 0] = 1e-9
# Compute the intersection over union
iou = intersection / union
#print('IOU {}'.format(iou))
# Precision helper function
def precision_at(threshold, iou):
matches = iou > threshold
true_positives = np.sum(matches, axis=1) == 1 # Correct objects
false_positives = np.sum(matches, axis=0) == 0 # Missed objects
false_negatives = np.sum(matches, axis=1) == 0 # Extra objects
tp, fp, fn = np.sum(true_positives), np.sum(false_positives), np.sum(false_negatives)
return tp, fp, fn
# Loop over IoU thresholds
prec = []
if print_table:
print("Thresh\tTP\tFP\tFN\tPrec.")
for t in np.arange(0.5, 1.0, 0.05):
tp, fp, fn = precision_at(t, iou)
if (tp + fp + fn) > 0:
p = tp / (tp + fp + fn)
else:
p = 0
if print_table:
print("{:1.3f}\t{}\t{}\t{}\t{:1.3f}".format(t, tp, fp, fn, p))
prec.append(p)
if print_table:
print("AP\t-\t-\t-\t{:1.3f}".format(np.mean(prec)))
return np.mean(prec)
def iou_metric_batch(y_true_in, y_pred_in):
batch_size = y_true_in.shape[0]
value = 0.
for batch in range(batch_size):
value = value + iou_metric(y_true_in[batch], y_pred_in[batch])
return value/batch_size
def my_iou_metric(label, pred):
metric_value = tf.py_function(iou_metric_batch, [label, pred], tf.float32)
return metric_value
def my_iou_metric_loss(label, pred):
loss = 1-tf.py_function(iou_metric_batch, [label, pred], tf.float32)
#loss = -tf.map_fn(my_iou_metric_loss(label, pred), tf.range(tf.shape(pred)[0]))
loss.set_shape((None,))
return loss
def dice_coeff(y_true, y_pred):
smooth = 1.
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
score = (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
return score
def haud_dist(y_true, y_pred):
y_true = np.squeeze(y_true)
y_pred = np.squeeze(y_pred)
return hausdorff_distance(y_true,y_pred)
def haud_dist_batch(y_true, y_pred):
if len(y_true.shape)==2:
return haud_dist(y_true, y_pred)
else:
batch_size = y_true.shape[0]
hd = 0.
for batch in range(batch_size):
hd = hd + haud_dist(y_true[batch], y_pred[batch])
return hd/batch_size
def my_haud_dist(label, pred):
metric_value = tf.py_function(haud_dist_batch, [label, pred], tf.float32)
return metric_value
def evalResult(gt,pred,target_size=(256,256),flag_multi_class = False,num_class = 2):
gt = np.squeeze(gt)
pred = np.squeeze(pred)
acc = Accuracy()
acc.update_state(np.squeeze(gt), np.squeeze(pred))
r_acc = acc.result().numpy()
pr = Precision()
pr.update_state(np.squeeze(gt), np.squeeze(pred))
r_pr = pr.result().numpy()
rc = Recall()
rc.update_state(np.squeeze(gt), np.squeeze(pred))
r_rc = rc.result().numpy()
mi = MeanIoU(num_class)
mi.update_state(np.squeeze(gt), np.squeeze(pred))
r_mi = mi.result().numpy()
dc = 0.
for img in range(gt.shape[0]):
dc = dc + dice_coeff(gt[img], pred[img]).numpy()
dc = dc / gt.shape[0]
hd = haud_dist_batch(gt,pred)
miou = iou_metric_batch(gt,pred)
mae = MeanAbsoluteError()
r_mae = mae(np.squeeze(gt), np.squeeze(pred)).numpy()
print("Accuracy=",r_acc, "Precision=",r_pr, "Recall=",r_rc, "MeanIoU=",r_mi, "DiceCoefficient=",dc, "HD=",hd, "MyIoU=",miou, "MAE=",r_mae)
################### LOSS FUNCTIONS ###################
def focal_loss(target_tensor, prediction_tensor, weights=None, alpha=0.25, gamma=2):
sigmoid_p = tf.nn.sigmoid(prediction_tensor)
zeros = array_ops.zeros_like(sigmoid_p, dtype=sigmoid_p.dtype)
# For poitive prediction, only need consider front part loss, back part is 0;
# target_tensor > zeros <=> z=1, so poitive coefficient = z - p.
pos_p_sub = array_ops.where(target_tensor > zeros, target_tensor - sigmoid_p, zeros)
# For negative prediction, only need consider back part loss, front part is 0;
# target_tensor > zeros <=> z=1, so negative coefficient = 0.
neg_p_sub = array_ops.where(target_tensor > zeros, zeros, sigmoid_p)
per_entry_cross_ent = - alpha * (pos_p_sub ** gamma) * tf.math.log(tf.clip_by_value(sigmoid_p, 1e-8, 1.0)) - (1 - alpha) * (neg_p_sub ** gamma) * tf.math.log(tf.clip_by_value(1.0 - sigmoid_p, 1e-8, 1.0))
return tf.reduce_sum(per_entry_cross_ent)
def mean_iou_loss(y_true, y_pred):
prec = []
for t in np.arange(0.5, 1.0, 0.05):
y_pred_ = tf.compat.v1.to_int32(y_pred > t)
score, up_opt = tf.compat.v1.metrics.mean_iou(y_true, y_pred_, 2)
K.get_session().run(tf.local_variables_initializer())
with tf.control_dependencies([up_opt]):
score = tf.identity(score)
prec.append(score)
return -tf.math.log(K.mean(K.stack(prec), axis=0))
def dice_loss(y_true, y_pred):
loss = 1 - dice_coeff(y_true, y_pred)
return loss
def bce_dice_loss(y_true, y_pred):
loss = 0.4*categorical_crossentropy(y_true, y_pred) + 0.6*dice_loss(y_true, y_pred)
return loss
def bce_dice_loss2(y_true, y_pred):
fl = focal_loss(y_true, y_pred, gamma=5)
loss = 0.2*categorical_crossentropy(y_true, y_pred) + 0.3*dice_loss(y_true, y_pred) + 0.5*fl
return loss
################### CALLBACKS ###################
log_path = "logs/"
keyname = "BT-Unet"
cur_date = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tb_log_dir = log_path + "fit/" + keyname + '_' + cur_date
tensorboard_callback = TensorBoard(log_dir=tb_log_dir, histogram_freq=0)
model_checkpoint = ModelCheckpoint('model_'+keyname+'.hdf5', monitor='loss',verbose=1, save_best_only=True)
early_stopping = EarlyStopping(monitor='loss', verbose=1, patience=20)
csv_logger = CSVLogger(log_path + keyname + '_' + cur_date + '.log', separator=',', append=False)
reduce_lr = ReduceLROnPlateau(monitor='loss', factor=0.1, patience=10)
################### TRAINING ###################
n_folds = 10
skf = StratifiedKFold(n_splits=n_folds)
for train_index, test_index in skf.split(X_train[:,0,0,0], Y_train[:,0,0,0]):
new_x_train = np.zeros((len(test_index), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.float32) # Limited training data
new_y_train = np.zeros((len(test_index), IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.float32)
new_x_test = np.zeros((len(train_index), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.float32)
new_y_test = np.zeros((len(train_index), IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.float32)
idx=0
for i in test_index:
new_x_train[idx] = X_train[i]
new_y_train[idx] = Y_train[i]
idx = idx + 1
idx=0
for i in train_index:
new_x_test[idx] = X_train[i]
new_y_test[idx] = Y_train[i]
idx = idx + 1
model = None # Clearing the NN.
backbone = tf.keras.Model(
barlow_twins.encoder.input, barlow_twins.encoder.layers[-9].output
)
new_skip_connections = [backbone.get_layer(index=11).output, # This will vary depending on the encoder structure
backbone.get_layer(index=22).output,
backbone.get_layer(index=33).output,
backbone.get_layer(index=44).output]
backbone.trainable=True
x = backbone.output
x = decoder(x, new_skip_connections)
outputs = output(x)
model = Model(barlow_twins.encoder.input, outputs)
model.compile(
loss=bce_dice_loss,
optimizer=Adam(),
metrics=['accuracy', Precision(), MeanIoU(num_classes=2), Recall(), dice_coeff, MeanAbsoluteError(), my_haud_dist, my_iou_metric]
)
callbacks = [
model_checkpoint,
reduce_lr,
csv_logger,
tensorboard_callback,
early_stopping
]
results = model.fit(new_x_train, new_y_train, batch_size=8, epochs=1, validation_data=(new_x_test, new_y_test), callbacks=callbacks)
################### EVALUATION ###################
#model.load_weights('model_'+keyname+'.hdf5')
preds = model.predict(X_test)
gt_test = Y_test.astype(np.float32)
preds_t = (preds > 0.5).astype(np.float32)
evalResult(gt_test, preds_t)
x=0
xx = [0,1,2]
key_code=datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
for i in range(3):
plt.figure(figsize=(10,10))
#ix = random.randint(0, len(preds_train))
ix = xx[i]
#print(ix)
#ix = 58
plt.subplot(3,3,x+1)
imshow(X_test[ix].astype('uint8'))
#plt.title('Image')
plt.subplot(3,3,x+2)
imshow(Y_test[ix])
#plt.title('Mask')
plt.subplot(3,3,x+3)
imshow(preds_t[ix])
#plt.title('Predicted Mask')
#plt.show()
plt.savefig('out/Results'+key_code+'.png')
x = x+3