# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="2QR-3dEjBmy9" colab_type="text"
# ## Drive Mounter

# + id="mQj80MyKBlcD" colab_type="code" outputId="0c5120ca-6337-43b1-87d5-cbf53df5c1cd" colab={"base_uri": "https://localhost:8080/", "height": 122}
from google.colab import drive
drive.mount('/content/drive')

# + [markdown] id="kpuTVZx4mM2z" colab_type="text"
# ## Set up tensorBoard

# + id="Jm6tjIXixazT" colab_type="code" outputId="71022ceb-dc5b-4899-b788-b2126cee6f7b" colab={"base_uri": "https://localhost:8080/", "height": 51}
# %load_ext tensorboard

# + id="YDTBWvJwyXXj" colab_type="code" outputId="c65366c7-ef32-4947-fe19-274f1bc0454c" colab={"base_uri": "https://localhost:8080/", "height": 821}
# %tensorboard --logdir logs

# + id="VvO5IFcczJxK" colab_type="code" outputId="c2318788-e96d-4e4f-add1-a4fed8d72120" colab={"base_uri": "https://localhost:8080/", "height": 51}
from tensorboard import notebook
notebook.list() 

# + id="2sVsUoYTzKjH" colab_type="code" outputId="9807e88b-d4f7-40ef-9a20-d91bd5b7f099" colab={"base_uri": "https://localhost:8080/", "height": 1000}
notebook.display(port=6006, height=1000)

# + [markdown] id="-NBU1GlMXXdL" colab_type="text"
# ## Modelling 

# + id="5Q9yZ9dwXWen" colab_type="code" outputId="e15cf403-6462-472e-c0e1-ab8000d99c6e" colab={"base_uri": "https://localhost:8080/", "height": 34}
import tensorflow as tf
import numpy as np

import keras
from keras.models import Model, load_model
from keras.layers import Input ,BatchNormalization , Activation 
from keras.layers.convolutional import Conv2D, UpSampling2D
from keras.layers.pooling import MaxPooling2D
from keras.layers.merge import concatenate


def Convolution(input_tensor,filters):
    
    x = Conv2D(filters=filters,kernel_size=(3, 3),padding = 'same',strides=(1, 1))(input_tensor)
    x = BatchNormalization()(x)
    x = Activation('relu')(x) 
    return x

def model(input_shape):
    
    inputs = Input((input_shape))
    
    conv_1 = Convolution(inputs,32)
    
    maxp_1 = MaxPooling2D(pool_size = (2, 2), strides = (2, 2), padding = 'same') (conv_1)
    
    conv_2 = Convolution(maxp_1,64)
    maxp_2 = MaxPooling2D(pool_size = (2, 2), strides = (2, 2), padding = 'same') (conv_2)
    
    conv_3 = Convolution(maxp_2,128)
    
    maxp_3 = MaxPooling2D(pool_size = (2, 2), strides = (2, 2), padding = 'same') (conv_3)
    
    conv_4 = Convolution(maxp_3,256)
    maxp_4 = MaxPooling2D(pool_size = (2, 2), strides = (2, 2), padding = 'same') (conv_4)
    
    conv_5 = Convolution(maxp_4,512)
   
    upsample_6 = UpSampling2D((2, 2)) (conv_5)

    upsample_6 = concatenate([upsample_6, conv_4])
    
    conv_6 = Convolution(upsample_6,256)
    upsample_7 = UpSampling2D((2, 2)) (conv_6)
    
    upsample_7 = concatenate([upsample_7, conv_3])
    
    conv_7 = Convolution(upsample_7,128)
    upsample_8 = UpSampling2D((2, 2)) (conv_7)
    
    upsample_8 = concatenate([upsample_8, conv_2])

    conv_8 = Convolution(upsample_8,64)
    upsample_9 = UpSampling2D((2, 2)) (conv_8)
    
    upsample_9 = concatenate([upsample_9, conv_1])
    
    conv_9 = Convolution(upsample_9,32)
    outputs = Conv2D(1, (1, 1), activation='sigmoid') (conv_9)
    
    model = Model(inputs=[inputs], outputs=[outputs]) 
    
    return model


# + [markdown] id="bz07lh2tBbeF" colab_type="text"
# ## Dataset loading

# + id="cmhOZtMZTiBp" colab_type="code" colab={}
path='drive/My Drive/MS_data_k_fold/'

# + id="-lDhPZE6UKj2" colab_type="code" colab={}
import os
ext="Prepared_data"
fldr=path+ext

# + id="ulq3RxsGVn6K" colab_type="code" colab={}
import os
import numpy as np
import pandas as pd

def return_sets(i,fldr):
  sub_fldr=fldr+'/'+str(i)
  k=os.listdir(sub_fldr)
  for fl in k:
    if 'test' in fl:
      test_X=sub_fldr+'/'+fl+'/X.npy'
      X_test=np.load(test_X)
      test_Y=sub_fldr+'/'+fl+'/Y.npy'
      Y_test=np.load(test_Y)
    if 'train' in fl:
      train_X=sub_fldr+'/'+fl+'/X.npy'
      X_train=np.load(train_X)
      train_Y=sub_fldr+'/'+fl+'/Y.npy'
      Y_train=np.load(train_Y)
    if 'validate' in fl:
      validate_X=sub_fldr+'/'+fl+'/X.npy'
      X_validate=np.load(validate_X)
      validate_Y=sub_fldr+'/'+fl+'/Y.npy'
      Y_validate=np.load(validate_Y)
  return X_test,Y_test,X_train,Y_train,X_validate,Y_validate


# + [markdown] id="yzWO21F2dQzY" colab_type="text"
# ## Performance Metrices

# + id="olNo4xImdLHQ" colab_type="code" colab={}
from keras import backend as K
import numpy as np
import tensorflow as tf

# Computing Dice_Coefficient
def dice_coef(y_true, y_pred, smooth=1.0):
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)

# Computing Precision 
def precision(y_true, y_pred):
        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
        predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
        precision = true_positives / (predicted_positives + K.epsilon())
        return precision

# Computing Sensitivity      
def sensitivity(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
    return true_positives / (possible_positives + K.epsilon())

# Computing Specificity
def specificity(y_true, y_pred):
    true_negatives = K.sum(K.round(K.clip((1-y_true) * (1-y_pred), 0, 1)))
    possible_negatives = K.sum(K.round(K.clip(1-y_true, 0, 1)))
    return true_negatives / (possible_negatives + K.epsilon())


# + [markdown] id="oc2iNvecegZ4" colab_type="text"
# ## Accuracy Plots

# + id="XD8LoTpnegEg" colab_type="code" colab={}
import numpy as np
import matplotlib.pyplot as plt

# Accuracy vs Epoch
def Accuracy_Graph(history,i,fldr):
    plt.plot(history.history['accuracy'])
    plt.plot(history.history['val_accuracy'])
    #plt.title('Model accuracy')
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Validation'], loc='upper left')
    plt.subplots_adjust(top=1.00, bottom=0.0, left=0.0, right=0.95, hspace=0.25,
                        wspace=0.35)
    file_1=fldr+'/'+str(i)+'/Accuracy.png'
    plt.savefig(file_1)
    plt.show()
    
# Dice Similarity Coefficient vs Epoch
def Dice_coefficient_Graph(history,i,fldr):

    plt.plot(history.history['dice_coef'])
    plt.plot(history.history['val_dice_coef'])
    #plt.title('Dice_Coefficient')
    plt.ylabel('Dice_Coefficient')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Validation'], loc='upper left')
    plt.subplots_adjust(top=1.00, bottom=0.0, left=0.0, right=0.95, hspace=0.25,
                        wspace=0.35)
    file_1=fldr+'/'+str(i)+'/Dice_coef.png'
    plt.savefig(file_1)
    plt.show()
# Loss vs Epoch
def Loss_Graph(history,i,fldr):

    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    #plt.title('Model loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Validation'], loc='upper left')
    plt.subplots_adjust(top=1.00, bottom=0.0, left=0.0, right=0.95, hspace=0.25,
                        wspace=0.35)
    file_1=fldr+'/'+str(i)+'/Loss.png'
    plt.savefig(file_1)
    plt.show()


# + [markdown] id="_7UfgkOdcDoR" colab_type="text"
# ## Model executer

# + id="oDs3xd0_cdIc" colab_type="code" colab={}
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from sklearn.model_selection import train_test_split
import json
#import keras
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.layers import Input ,BatchNormalization , Activation 
from tensorflow.keras.layers import Conv2D, UpSampling2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import concatenate
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from tensorflow.keras.optimizers import Adam
import datetime



def executer(type_1,model,X_test,Y_test,X_train,Y_train,X_validate,Y_validate,fldr,i):
  logdir = os.path.join("logs", datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
  tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)
  #model = model(input_shape = (256,256,5))
  model.summary()
  file_1=fldr+'/'+str(i)+'/'+'Modified_UNet_'+type_1+'.h5'
  checkpointer = ModelCheckpoint(file_1, verbose=1)
  callback_list=[checkpointer,tensorboard_callback]
  record_file=fldr+'/'+str(i)+'/record.json'
  if os.path.exists(record_file):
    f=open(record_file,'r')
    data=json.load(f)
  else:
    data={}
  # Compiling the model
  k_adam=Adam(lr=0.001)
  model.compile(optimizer=k_adam, loss='binary_crossentropy', metrics=['accuracy',dice_coef,precision,sensitivity,specificity])
  # Fitting the model over the data
  history = model.fit(X_train,Y_train,validation_data=(X_validate,Y_validate),batch_size=32,epochs=60,validation_split=0.20,verbose=1,initial_epoch=0,callbacks=callback_list)

  # Saving the model
  model.save(file_1)
  history.history

  # Evaluating the model on the training and testing data 
  train_eval=model.evaluate(x=X_train, y=Y_train, batch_size=32 , verbose=1, sample_weight=None, steps=None)
  test_eval=model.evaluate(x=X_test, y=Y_test, batch_size=32, verbose=1, sample_weight=None, steps=None)
  data[str(i)]={}
  data[str(i)]["training"]=train_eval
  data[str(i)]["testing"]=test_eval

  with open(record_file, "w") as outfile:
			json.dump(data, outfile)
  # Plotting the Graphs of Accuracy, Dice_coefficient, Loss at each epoch on Training and Testing data
 

  return history


# + [markdown] id="aIIklmGAn1ey" colab_type="text"
# ## Caller

# + id="L3Ue8STwkbtr" colab_type="code" colab={}
def caller():
  path='drive/My Drive/MS_data_k_fold/'
  list_c=['Major']
  ext='Prepared_data'
  mdl=model(input_shape = (256,256,5))
  for i in list_c:
    j=3
    while j<=3:
      fldr=path+i+'/'+ext
      X_test,Y_test,X_train,Y_train,X_validate,Y_validate=return_sets(j,fldr)
      history=executer(i,mdl,X_test,Y_test,X_train,Y_train,X_validate,Y_validate,fldr,j)
      Accuracy_Graph(history,j,fldr)
      Dice_coefficient_Graph(history,j,fldr)
      Loss_Graph(history,j,fldr)
      j+=1


# + [markdown] id="qgZnQuDdp-Sc" colab_type="text"
# ## main

# + id="WHaf85knp8aK" colab_type="code" outputId="36331780-508c-4555-abd9-d788c27996d9" colab={"base_uri": "https://localhost:8080/", "height": 1000}
caller()

StarcoderdataJupyterScriptsDedupFiltered