image_output_4.png image_output_3.png image_output_2.png image_output_1.png
from keras.preprocessing import image

from keras.preprocessing import image

from keras.preprocessing import image

from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau

from tensorflow.keras.metrics import Precision, Recall, MeanIoU

from tensorflow.keras.metrics import Precision, Recall, MeanIoU

from tensorflow.keras.models import Model

from tensorflow.keras.optimizers import Adam, Nadam, SGD

from tensorflow.keras.optimizers import Adam, Nadam, SGD

import csv

import numpy as np

import numpy as np

import numpy as np

import numpy as np

import tensorflow as tf

import tensorflow as tf

import tensorflow as tf

import numpy as np
import tensorflow as tf
def seed_everything(SEED):
    np.random.seed(SEED)
    tf.random.set_seed(SEED)
seed = 42
seed_everything(seed)
'\nResUNet++ architecture in Keras TensorFlow\n'
import os
import numpy as np
import cv2
import tensorflow as tf
from tensorflow.keras.layers import *
from tensorflow.keras.models import Model
def squeeze_excite_block(inputs, ratio=8):
    init = inputs
    channel_axis = -1
    filters = init.shape[channel_axis]
    se_shape = (1, 1, filters)
    se = GlobalAveragePooling2D()(init)
    se = Reshape(se_shape)(se)
    se = Dense(filters // ratio, activation='relu', kernel_initializer='he_normal', use_bias=False)(se)
    se = Dense(filters, activation='sigmoid', kernel_initializer='he_normal', use_bias=False)(se)
    x = Multiply()([init, se])
    return x
def stem_block(x, n_filter, strides):
    x_init = x
    x = Conv2D(n_filter, (3, 3), padding='same', strides=strides)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv2D(n_filter, (3, 3), padding='same')(x)
    s = Conv2D(n_filter, (1, 1), padding='same', strides=strides)(x_init)
    s = BatchNormalization()(s)
    x = Add()([x, s])
    x = squeeze_excite_block(x)
    return x
def resnet_block(x, n_filter, strides=1):
    x_init = x
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv2D(n_filter, (3, 3), padding='same', strides=strides)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv2D(n_filter, (3, 3), padding='same', strides=1)(x)
    s = Conv2D(n_filter, (1, 1), padding='same', strides=strides)(x_init)
    s = BatchNormalization()(s)
    x = Add()([x, s])
    x = squeeze_excite_block(x)
    return x
def aspp_block(x, num_filters, rate_scale=1):
    x1 = Conv2D(num_filters, (3, 3), dilation_rate=(6 * rate_scale, 6 * rate_scale), padding='same')(x)
    x1 = BatchNormalization()(x1)
    x2 = Conv2D(num_filters, (3, 3), dilation_rate=(12 * rate_scale, 12 * rate_scale), padding='same')(x)
    x2 = BatchNormalization()(x2)
    x3 = Conv2D(num_filters, (3, 3), dilation_rate=(18 * rate_scale, 18 * rate_scale), padding='same')(x)
    x3 = BatchNormalization()(x3)
    x4 = Conv2D(num_filters, (3, 3), padding='same')(x)
    x4 = BatchNormalization()(x4)
    y = Add()([x1, x2, x3, x4])
    y = Conv2D(num_filters, (1, 1), padding='same')(y)
    return y
def attetion_block(g, x):
    """
        g: Output of Parallel Encoder block
        x: Output of Previous Decoder block
    """
    filters = x.shape[-1]
    g_conv = BatchNormalization()(g)
    g_conv = Activation('relu')(g_conv)
    g_conv = Conv2D(filters, (3, 3), padding='same')(g_conv)
    g_pool = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(g_conv)
    x_conv = BatchNormalization()(x)
    x_conv = Activation('relu')(x_conv)
    x_conv = Conv2D(filters, (3, 3), padding='same')(x_conv)
    gc_sum = Add()([g_pool, x_conv])
    gc_conv = BatchNormalization()(gc_sum)
    gc_conv = Activation('relu')(gc_conv)
    gc_conv = Conv2D(filters, (3, 3), padding='same')(gc_conv)
    gc_mul = Multiply()([gc_conv, x])
    return gc_mul
def build_model(input_size=512):
    n_filters = [16, 32, 64, 128, 256]
    inputs = Input((input_size, input_size, 3))
    c0 = inputs
    c1 = stem_block(c0, n_filters[0], strides=1)
    c2 = resnet_block(c1, n_filters[1], strides=2)
    c3 = resnet_block(c2, n_filters[2], strides=2)
    c4 = resnet_block(c3, n_filters[3], strides=2)
    b1 = aspp_block(c4, n_filters[4])
    d1 = attetion_block(c3, b1)
    d1 = UpSampling2D((2, 2))(d1)
    d1 = Concatenate()([d1, c3])
    d1 = resnet_block(d1, n_filters[3])
    d2 = attetion_block(c2, d1)
    d2 = UpSampling2D((2, 2))(d2)
    d2 = Concatenate()([d2, c2])
    d2 = resnet_block(d2, n_filters[2])
    d3 = attetion_block(c1, d2)
    d3 = UpSampling2D((2, 2))(d3)
    d3 = Concatenate()([d3, c1])
    d3 = resnet_block(d3, n_filters[1])
    outputs = aspp_block(d3, n_filters[0])
    outputs = Conv2D(1, (1, 1), padding='same')(outputs)
    outputs = Activation('sigmoid')(outputs)
    model = Model(inputs, outputs)
    return model

unet = build_model()
unet.summary()

from tensorflow.keras.metrics import Precision, Recall, MeanIoU
from tensorflow.keras.optimizers import Adam, Nadam, SGD
import tensorflow as tf
smooth = 1.0
def dice_coef(y_true, y_pred):
    y_true_f = tf.keras.layers.Flatten()(y_true)
    y_pred_f = tf.keras.layers.Flatten()(y_pred)
    intersection = tf.reduce_sum(y_true_f * y_pred_f)
    return (2.0 * intersection + smooth) / (tf.reduce_sum(y_true_f) + tf.reduce_sum(y_pred_f) + smooth)
def dice_loss(y_true, y_pred):
    y_true_f = tf.keras.layers.Flatten()(y_true)
    y_pred_f = tf.keras.layers.Flatten()(y_pred)
    intersection = tf.reduce_sum(y_true_f * y_pred_f)
    return 1.0 - (2.0 * intersection + smooth) / (tf.reduce_sum(y_true_f) + tf.reduce_sum(y_pred_f) + smooth)
def unet_loss(y_true, y_pred):
    bce = tf.keras.losses.BinaryCrossentropy()(y_true, y_pred)
    dice = dice_loss(y_true, y_pred)
    loss = bce + dice
    return loss

import numpy as np
trainImages = np.load('/kaggle/input/isib2016-allnpdata-noclahe-noaug/train_images.npy')
trainMasks = np.load('/kaggle/input/isib2016-allnpdata-noclahe-noaug/train_masks.npy')
valImages = np.load('/kaggle/input/isib2016-allnpdata-noclahe-noaug/val_images.npy')
valMasks = np.load('/kaggle/input/isib2016-allnpdata-noclahe-noaug/val_masks.npy')

from keras.preprocessing import image
BATCH_SIZE = 8
image_datagen_train = image.ImageDataGenerator()
mask_datagen_train = image.ImageDataGenerator()
image_datagen_train.fit(trainImages, augment=False, seed=seed)
mask_datagen_train.fit(trainMasks, augment=False, seed=seed)
x_train = image_datagen_train.flow(trainImages, batch_size=BATCH_SIZE, shuffle=True, seed=seed)
y_train = mask_datagen_train.flow(trainMasks, batch_size=BATCH_SIZE, shuffle=True, seed=seed)

from keras.preprocessing import image
BATCH_SIZE = 8
image_datagen_val = image.ImageDataGenerator()
mask_datagen_val = image.ImageDataGenerator()
image_datagen_val.fit(valImages, augment=False, seed=seed)
mask_datagen_val.fit(valMasks, augment=False, seed=seed)
x_val = image_datagen_val.flow(valImages, batch_size=BATCH_SIZE, shuffle=True, seed=seed)
y_val = mask_datagen_val.flow(valMasks, batch_size=BATCH_SIZE, shuffle=True, seed=seed)

train_generator = zip(x_train, y_train)
val_generator = zip(x_val, y_val)

from tensorflow.keras.metrics import Precision, Recall, MeanIoU
from tensorflow.keras.optimizers import Adam, Nadam, SGD
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
lr = 0.0001
optimizer = Nadam(lr)
metrics = [Recall(), Precision(), dice_coef, MeanIoU(num_classes=2)]
unet.compile(loss=dice_loss, optimizer=optimizer, metrics=metrics)
checkpoint1 = ModelCheckpoint('/kaggle/working/MDLChkP_Everything/unet.h5', verbose=1, monitor='val_loss', mode='min', save_best_only=True, save_weights_only=False)
checkpoint2 = ModelCheckpoint('/kaggle/working/MDLChkP_WeightsOnly/', verbose=1, monitor='val_loss', mode='min', save_best_only=True, save_weights_only=True)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, min_lr=1e-06, verbose=1)
early_stopping = EarlyStopping(monitor='val_loss', patience=20, restore_best_weights=True)
callbacks = [checkpoint1, checkpoint2, reduce_lr, early_stopping]

train_steps = len(x_train)
val_steps = len(x_val)
history = unet.fit_generator(train_generator, validation_data=val_generator, validation_steps=val_steps, steps_per_epoch=train_steps, epochs=120, callbacks=callbacks)

import numpy as np
testImages = np.load('/kaggle/input/isib2016-allnpdata-noclahe-noaug/test_images.npy')
testMasks = np.load('/kaggle/input/isib2016-allnpdata-noclahe-noaug/test_masks.npy')

from keras.preprocessing import image
BATCH_SIZE = 8
image_datagen_test = image.ImageDataGenerator()
mask_datagen_test = image.ImageDataGenerator()
image_datagen_test.fit(testImages, augment=False, seed=seed)
mask_datagen_test.fit(testMasks, augment=False, seed=seed)
x_test = image_datagen_test.flow(testImages, batch_size=BATCH_SIZE, shuffle=True, seed=seed)
y_test = mask_datagen_test.flow(testMasks, batch_size=1, shuffle=True, seed=seed)

preds_test = unet.predict(x_test, verbose=1)
y_pred = (preds_test > 0.5).astype(np.float32)
dice_scores = []
for i in range(len(y_pred)):
    k = y_test.next()
    arr = np.squeeze(k, axis=0)
    dice = dice_coef(arr, y_pred[i])
    dice_scores.append(dice)
    print('\n Dice score: \t \n', dice)
average_dice = np.mean(dice_scores)
print('Average dice coefficient: ', average_dice)
import csv
my_values = [tensor.numpy() for tensor in dice_scores]
with open('dice_scores.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(my_values)