{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom PIL import Image, ImageOps\nimport tensorflow as tf\nfrom random import seed\nimport matplotlib.pyplot as plt\nfrom skimage import color\nfrom tqdm.notebook import tqdm\n\nimport time\nimport os\nimport gc\nimport cv2\nfrom PIL import Image\ntqdm.pandas()\ngc.enable()\n\nt_size = 160 #Size of the image to rescale\ntime_limit = 7 * 3600 #time limit in seconds\nstart_time = time.time()\n\nrandom_seed = 1023\nseed(random_seed)\nnp.random.seed(random_seed)\ntf.random.set_seed(random_seed)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T15:50:46.289094Z","iopub.execute_input":"2022-12-09T15:50:46.289827Z","iopub.status.idle":"2022-12-09T15:50:51.985468Z","shell.execute_reply.started":"2022-12-09T15:50:46.289712Z","shell.execute_reply":"2022-12-09T15:50:51.984487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_masked_img(img, mask, title=''):\n    #rescalse mask to 0-1 range regardless of min and max value\n    mask = (mask - mask.min())/(mask.max()-mask.min())\n    mask = np.nan_to_num(mask) #Adress NaN incase of zerodiv error\n    #Round up the mask\n    mask = np.round(mask)\n    mask = mask.reshape(img.shape[:2])\n    \n    fig, ax = plt.subplots(1, 3, figsize=(9, 3))\n    fig.suptitle(title, fontsize=16)\n    \n    ax[0].imshow(mask); ax[0].set_title('Mask')\n    ax[1].imshow(img); ax[1].set_title('Image')\n    ax[2].imshow(color.label2rgb(mask, img,\n                               bg_label=0, bg_color=(1.,1.,1.), alpha=0.25))\n    ax[2].set_title('Masked Image')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-09T15:51:44.911944Z","iopub.execute_input":"2022-12-09T15:51:44.912893Z","iopub.status.idle":"2022-12-09T15:51:44.921173Z","shell.execute_reply.started":"2022-12-09T15:51:44.912854Z","shell.execute_reply":"2022-12-09T15:51:44.919856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##data.py\n##read the images\nimport os\nimport numpy as np\nimport cv2\nfrom glob import glob\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\n\nimsize=512\nnum_filters = [32, 64,128,256]\n\ndef load_data(path, split=0.1):\n    images = sorted(glob(os.path.join(path,\"rgb/*\")))\n    masks = sorted(glob(os.path.join(path,\"mask/*\")))\n\n    total_size = len(images)\n    test_size = int(split * total_size)\n\n    train_x = images\n    train_y = masks\n\n    train_x, test_x = train_test_split(train_x, test_size=test_size, random_state=42)\n    train_y, test_y = train_test_split(train_y, test_size=test_size, random_state=42)\n\n    return (train_x, train_y), (test_x, test_y)\n\ndef read_image(path):\n    path = path.decode()\n    x = cv2.imread(path, cv2.IMREAD_COLOR)\n    x = cv2.resize(x,(imsize,imsize))\n    x = x/255.0\n    return x\n\ndef read_mask(path):\n    path = path.decode()\n    x = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n    x = cv2.resize(x, (imsize,imsize))\n    x = x/255.0\n    x = np.expand_dims(x, axis=-1)\n    return x\n\ndef tf_parse(x, y):\n    def _parse(x, y):\n        x = read_image(x)\n        y = read_mask(y)\n        return x, y\n\n    x, y = tf.numpy_function(_parse, [x, y], [tf.float64, tf.float64])\n    x.set_shape([imsize, imsize, 3])\n    y.set_shape([ imsize,imsize, 1])\n    return x, y\n\ndef tf_dataset(x, y):\n    dataset = tf.data.Dataset.from_tensor_slices((x, y))\n    dataset = dataset.map(tf_parse)\n    #dataset = dataset.batch(batch)\n    dataset = dataset.repeat()\n    return dataset\n\n\nif __name__==\"__main__\":\n    print(\"\")\n    path =\"../input/hubmaphpa-512x512/sdp/\"\n    (train_x,train_y),(test_x,test_y)=load_data(path)\n    print(len(train_x),len(test_x))\n    ","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-12-09T15:51:47.781301Z","iopub.execute_input":"2022-12-09T15:51:47.781669Z","iopub.status.idle":"2022-12-09T15:51:48.365667Z","shell.execute_reply.started":"2022-12-09T15:51:47.781638Z","shell.execute_reply":"2022-12-09T15:51:48.364455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Model, Sequential\nfrom keras.layers import Activation, Dense, BatchNormalization, Dropout, Conv2D, Conv2DTranspose, MaxPooling2D, UpSampling2D, Input, Reshape\nfrom keras.callbacks import EarlyStopping\nfrom keras import backend as K\nfrom tensorflow.keras.optimizers import Adam, SGD\nimport tensorflow as tf\nimport numpy as np\nimport pandas as pd\nimport glob\nimport PIL\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport cv2\n%matplotlib inline\n\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nfrom warnings import filterwarnings\n\nfilterwarnings('ignore')\nplt.rcParams[\"axes.grid\"] = False\nnp.random.seed(101)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T15:51:51.977320Z","iopub.execute_input":"2022-12-09T15:51:51.977708Z","iopub.status.idle":"2022-12-09T15:51:52.802915Z","shell.execute_reply.started":"2022-12-09T15:51:51.977678Z","shell.execute_reply":"2022-12-09T15:51:52.801982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def iou(y_true, y_pred, smooth = 100):\n    intersection = K.sum(K.abs(y_true * y_pred), axis=-1)\n    sum_ = K.sum(K.square(y_true), axis = -1) + K.sum(K.square(y_pred), axis=-1)\n    jac = (intersection + smooth) / (sum_ - intersection + smooth)\n    return jac\n\ndef dice_coef(y_true, y_pred, smooth = 100):\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = K.sum(y_true_f * y_pred_f)\n    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n\ndef precision(y_true, y_pred):\n    '''Calculates the precision, a metric for multi-label classification of\n    how many selected items are relevant.\n    '''\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    precision = true_positives / (predicted_positives + K.epsilon())\n    return precision\n\ndef recall(y_true, y_pred):\n    '''Calculates the recall, a metric for multi-label classification of\n    how many relevant items are selected.\n    '''\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    recall = true_positives / (possible_positives + K.epsilon())\n    return recall\n\ndef accuracy(y_true, y_pred):\n    '''Calculates the mean accuracy rate across all predictions for binary\n    classification problems.\n    '''\n    return K.mean(K.equal(y_true, K.round(y_pred)))","metadata":{"execution":{"iopub.status.busy":"2022-12-09T15:51:55.551319Z","iopub.execute_input":"2022-12-09T15:51:55.551700Z","iopub.status.idle":"2022-12-09T15:51:55.563405Z","shell.execute_reply.started":"2022-12-09T15:51:55.551667Z","shell.execute_reply":"2022-12-09T15:51:55.562400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x1 = []\ntrain_y1 = []\ntest_x1 = []\ntest_y1 = []\n\nfor i in range(1,442):\n    img = Image.open(train_x[i])\n    img = img.resize((512,512))\n    img = np.array(img)/255\n    train_x1.append(img)\n    img = Image.open(train_y[i])\n    img = ImageOps.grayscale(img)\n    img = img.resize((512,512))\n    img = np.array(img)/255\n    train_y1.append(img)\n    if i < 49:\n        img = Image.open(test_x[i])\n        img = img.resize((512,512))\n        img = np.array(img)/255\n        test_x1.append(img)\n        img = Image.open(test_y[i])\n        img = ImageOps.grayscale(img)\n        img = img.resize((512,512))\n        img = np.array(img)/255\n        test_y1.append(img)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-12-09T15:52:04.962908Z","iopub.execute_input":"2022-12-09T15:52:04.963567Z","iopub.status.idle":"2022-12-09T15:52:07.427078Z","shell.execute_reply.started":"2022-12-09T15:52:04.963530Z","shell.execute_reply":"2022-12-09T15:52:07.426099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x1 = np.array(train_x1)\ntrain_y1 = np.array(train_y1)\ntest_x1 = np.array(test_x1)\ntest_y1 = np.array(test_y1)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T15:53:04.157276Z","iopub.execute_input":"2022-12-09T15:53:04.157886Z","iopub.status.idle":"2022-12-09T15:53:04.410595Z","shell.execute_reply.started":"2022-12-09T15:53:04.157846Z","shell.execute_reply":"2022-12-09T15:53:04.409340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(train_x1, train_y1, test_size = 0.10, random_state = 101)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T15:53:07.199529Z","iopub.execute_input":"2022-12-09T15:53:07.199895Z","iopub.status.idle":"2022-12-09T15:53:07.311801Z","shell.execute_reply.started":"2022-12-09T15:53:07.199861Z","shell.execute_reply":"2022-12-09T15:53:07.310762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Visualization of images with its corresponding mask","metadata":{}},{"cell_type":"code","source":"show_masked_img(x_train[30],y_train[30],\"Sample\")","metadata":{"execution":{"iopub.status.busy":"2022-12-09T15:54:56.013545Z","iopub.execute_input":"2022-12-09T15:54:56.014557Z","iopub.status.idle":"2022-12-09T15:54:56.657817Z","shell.execute_reply.started":"2022-12-09T15:54:56.014502Z","shell.execute_reply":"2022-12-09T15:54:56.656888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def segnet(epochs_num,savename):\n\n    # Encoding layer\n    img_input = Input(shape= (512, 512, 3))\n    x = Conv2D(64, (3, 3), padding='same', name='conv1',strides= (1,1))(img_input)\n    x = BatchNormalization(name='bn1')(x)\n    x = Activation('relu')(x)\n    x = Conv2D(64, (3, 3), padding='same', name='conv2')(x)\n    x = BatchNormalization(name='bn2')(x)\n    x = Activation('relu')(x)\n    x = MaxPooling2D()(x)\n    \n    x = Conv2D(128, (3, 3), padding='same', name='conv3')(x)\n    x = BatchNormalization(name='bn3')(x)\n    x = Activation('relu')(x)\n    x = Conv2D(128, (3, 3), padding='same', name='conv4')(x)\n    x = BatchNormalization(name='bn4')(x)\n    x = Activation('relu')(x)\n    x = MaxPooling2D()(x)\n\n    x = Conv2D(256, (3, 3), padding='same', name='conv5')(x)\n    x = BatchNormalization(name='bn5')(x)\n    x = Activation('relu')(x)\n    x = Conv2D(256, (3, 3), padding='same', name='conv6')(x)\n    x = BatchNormalization(name='bn6')(x)\n    x = Activation('relu')(x)\n    x = Conv2D(256, (3, 3), padding='same', name='conv7')(x)\n    x = BatchNormalization(name='bn7')(x)\n    x = Activation('relu')(x)\n    x = MaxPooling2D()(x)\n\n    x = Conv2D(512, (3, 3), padding='same', name='conv8')(x)\n    x = BatchNormalization(name='bn8')(x)\n    x = Activation('relu')(x)\n    x = Conv2D(512, (3, 3), padding='same', name='conv9')(x)\n    x = BatchNormalization(name='bn9')(x)\n    x = Activation('relu')(x)\n    x = Conv2D(512, (3, 3), padding='same', name='conv10')(x)\n    x = BatchNormalization(name='bn10')(x)\n    x = Activation('relu')(x)\n    x = MaxPooling2D()(x)\n    \n    x = Conv2D(512, (3, 3), padding='same', name='conv11')(x)\n    x = BatchNormalization(name='bn11')(x)\n    x = Activation('relu')(x)\n    x = Conv2D(512, (3, 3), padding='same', name='conv12')(x)\n    x = BatchNormalization(name='bn12')(x)\n    x = Activation('relu')(x)\n    x = Conv2D(512, (3, 3), padding='same', name='conv13')(x)\n    x = BatchNormalization(name='bn13')(x)\n    x = Activation('relu')(x)\n    x = MaxPooling2D()(x)\n\n    x = Dense(1024, activation = 'relu', name='fc1')(x)\n    x = Dense(1024, activation = 'relu', name='fc2')(x)\n    # Decoding Layer \n    x = UpSampling2D()(x)\n    x = Conv2DTranspose(512, (3, 3), padding='same', name='deconv1')(x)\n    x = BatchNormalization(name='bn14')(x)\n    x = Activation('relu')(x)\n    x = Conv2DTranspose(512, (3, 3), padding='same', name='deconv2')(x)\n    x = BatchNormalization(name='bn15')(x)\n    x = Activation('relu')(x)\n    x = Conv2DTranspose(512, (3, 3), padding='same', name='deconv3')(x)\n    x = BatchNormalization(name='bn16')(x)\n    x = Activation('relu')(x)\n    \n    x = UpSampling2D()(x)\n    x = Conv2DTranspose(512, (3, 3), padding='same', name='deconv4')(x)\n    x = BatchNormalization(name='bn17')(x)\n    x = Activation('relu')(x)\n    x = Conv2DTranspose(512, (3, 3), padding='same', name='deconv5')(x)\n    x = BatchNormalization(name='bn18')(x)\n    x = Activation('relu')(x)\n    x = Conv2DTranspose(256, (3, 3), padding='same', name='deconv6')(x)\n    x = BatchNormalization(name='bn19')(x)\n    x = Activation('relu')(x)\n\n    x = UpSampling2D()(x)\n    x = Conv2DTranspose(256, (3, 3), padding='same', name='deconv7')(x)\n    x = BatchNormalization(name='bn20')(x)\n    x = Activation('relu')(x)\n    x = Conv2DTranspose(256, (3, 3), padding='same', name='deconv8')(x)\n    x = BatchNormalization(name='bn21')(x)\n    x = Activation('relu')(x)\n    x = Conv2DTranspose(128, (3, 3), padding='same', name='deconv9')(x)\n    x = BatchNormalization(name='bn22')(x)\n    x = Activation('relu')(x)\n\n    x = UpSampling2D()(x)\n    x = Conv2DTranspose(128, (3, 3), padding='same', name='deconv10')(x)\n    x = BatchNormalization(name='bn23')(x)\n    x = Activation('relu')(x)\n    x = Conv2DTranspose(64, (3, 3), padding='same', name='deconv11')(x)\n    x = BatchNormalization(name='bn24')(x)\n    x = Activation('relu')(x)\n    \n    x = UpSampling2D()(x)\n    x = Conv2DTranspose(64, (3, 3), padding='same', name='deconv12')(x)\n    x = BatchNormalization(name='bn25')(x)\n    x = Activation('relu')(x)\n    x = Conv2DTranspose(1, (3, 3), padding='same', name='deconv13')(x)\n    x = BatchNormalization(name='bn26')(x)\n    x = Activation('sigmoid')(x)\n    pred = Reshape((512,512))(x)\n    \n    model = Model(inputs=img_input, outputs=pred)\n    \n    model.compile(optimizer= SGD(lr=0.0001, momentum=0.9, decay=0.0005, nesterov=False), loss= [\"binary_crossentropy\"]\n                  , metrics=[iou, dice_coef, precision, recall, accuracy])\n    model.summary()\n    hist = model.fit(x_train, y_train, epochs= epochs_num, batch_size= 8, validation_data= (x_val, y_val), verbose=1)\n    \n    model.save(savename)\n    return model,hist","metadata":{"execution":{"iopub.status.busy":"2022-11-04T18:43:44.628081Z","iopub.execute_input":"2022-11-04T18:43:44.628529Z","iopub.status.idle":"2022-11-04T18:43:44.662643Z","shell.execute_reply.started":"2022-11-04T18:43:44.628488Z","shell.execute_reply":"2022-11-04T18:43:44.661019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model, hist = segnet(1, 'segnet_epoch100_0.000_hpa+hubmap.h5')","metadata":{"execution":{"iopub.status.busy":"2022-11-04T18:43:49.423390Z","iopub.execute_input":"2022-11-04T18:43:49.423870Z","iopub.status.idle":"2022-11-04T18:44:17.263472Z","shell.execute_reply.started":"2022-11-04T18:43:49.423827Z","shell.execute_reply":"2022-11-04T18:44:17.261966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights('./segnet_epoch100_0.0001_hpa+hubmap.h5')","metadata":{"execution":{"iopub.status.busy":"2022-11-04T18:44:39.151390Z","iopub.execute_input":"2022-11-04T18:44:39.152619Z","iopub.status.idle":"2022-11-04T18:44:39.390084Z","shell.execute_reply.started":"2022-11-04T18:44:39.152560Z","shell.execute_reply":"2022-11-04T18:44:39.389038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('\\n~~~~~~~~~~~~~~~Stats after 1 epoch~~~~~~~~~~~~~~~~~~~')\nprint('\\n-------------On Train Set--------------------------\\n')\nres = model.evaluate(train_x1, train_y1, batch_size= 8)\nprint('________________________')\nprint('IOU:       |   {:.2f}  |'.format(res[1]*100))\nprint('Dice Coef: |   {:.2f}  |'.format(res[2]*100))\nprint('Precision: |   {:.2f}  |'.format(res[3]*100))\nprint('Recall:    |   {:.2f}  |'.format(res[4]*100))\nprint('Accuracy:  |   {:.2f}  |'.format(res[5]*100))\nprint(\"Loss:      |   {:.2f}  |\".format(res[0]*100))\nprint('________________________')\nprint('\\n-------------On Test  Set--------------------------\\n')\nres = model.evaluate(test_x1, test_y1, batch_size= 8)\nprint('________________________')\nprint('IOU:       |   {:.2f}  |'.format(res[1]*100))\nprint('Dice Coef: |   {:.2f}  |'.format(res[2]*100))\nprint('Precision: |   {:.2f}  |'.format(res[3]*100))\nprint('Recall:    |   {:.2f}  |'.format(res[4]*100))\nprint('Accuracy:  |   {:.2f}  |'.format(res[5]*100))\nprint(\"Loss:      |   {:.2f}  |\".format(res[0]*100))\nprint('________________________')\nprint('\\n-------------On validation Set---------------------\\n')\nres = model.evaluate(x_val, y_val, batch_size= 8)\nprint('________________________')\nprint('IOU:       |   {:.2f}  |'.format(res[1]*100))\nprint('Dice Coef: |   {:.2f}  |'.format(res[2]*100))\nprint('Precision: |   {:.2f}  |'.format(res[3]*100))\nprint('Recall:    |   {:.2f}  |'.format(res[4]*100))\nprint('Accuracy:  |   {:.2f}  |'.format(res[5]*100))\nprint(\"Loss:      |   {:.2f}  |\".format(res[0]*100))\nprint('________________________')","metadata":{"execution":{"iopub.status.busy":"2022-11-04T18:44:42.212374Z","iopub.execute_input":"2022-11-04T18:44:42.212779Z","iopub.status.idle":"2022-11-04T18:44:49.347069Z","shell.execute_reply.started":"2022-11-04T18:44:42.212745Z","shell.execute_reply":"2022-11-04T18:44:49.345928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_num = 7\nimg_pred = model.predict(test_x1[img_num].reshape(1,512,512,3))\nplt.figure(figsize=(16,16))\nplt.subplot(1,3,1)\nplt.imshow(test_x1[img_num])\nplt.title('Original Image')\nplt.subplot(1,3,2)\nplt.imshow(test_y1[img_num], plt.cm.binary_r)\nplt.title('Ground Truth')\nplt.subplot(1,3,3)\nplt.imshow(img_pred.reshape(512, 512), plt.cm.binary_r)\nplt.title('Predicted Output')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T18:54:42.324617Z","iopub.execute_input":"2022-11-04T18:54:42.325031Z","iopub.status.idle":"2022-11-04T18:54:42.969479Z","shell.execute_reply.started":"2022-11-04T18:54:42.324995Z","shell.execute_reply":"2022-11-04T18:54:42.966684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}