{"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 os\nimport zipfile\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import train_test_split\n\nimport cv2\n\nimport tensorflow as tf\nfrom keras import backend as K\n\nfrom tensorflow.keras.layers import Input, concatenate, Conv2D, MaxPooling2D, UpSampling2D, Dropout\nfrom tensorflow.keras import models\nfrom tensorflow.keras import optimizers\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:35:38.300621Z","iopub.execute_input":"2022-07-30T05:35:38.300954Z","iopub.status.idle":"2022-07-30T05:35:45.265507Z","shell.execute_reply.started":"2022-07-30T05:35:38.300849Z","shell.execute_reply":"2022-07-30T05:35:45.264746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(\"../input/data-science-bowl-2018/stage1_train.zip\",'r') as z:\n    z.extractall(\"stage1_train\")\n\nwith zipfile.ZipFile(\"../input/data-science-bowl-2018/stage2_test_final.zip\",'r') as z:\n    z.extractall(\"stage2_test_final\")","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:35:45.267453Z","iopub.execute_input":"2022-07-30T05:35:45.267721Z","iopub.status.idle":"2022-07-30T05:35:54.756432Z","shell.execute_reply.started":"2022-07-30T05:35:45.267684Z","shell.execute_reply":"2022-07-30T05:35:54.755687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/working/stage1_train/'\ntest_path = '/kaggle/working/stage2_test_final/'","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:35:54.758748Z","iopub.execute_input":"2022-07-30T05:35:54.759287Z","iopub.status.idle":"2022-07-30T05:35:54.763436Z","shell.execute_reply.started":"2022-07-30T05:35:54.759249Z","shell.execute_reply":"2022-07-30T05:35:54.762747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = os.listdir(train_path)\ntest_dir = os.listdir(test_path)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:35:54.764496Z","iopub.execute_input":"2022-07-30T05:35:54.764710Z","iopub.status.idle":"2022-07-30T05:35:54.789700Z","shell.execute_reply.started":"2022-07-30T05:35:54.764683Z","shell.execute_reply":"2022-07-30T05:35:54.789040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.zeros((len(train_dir), 256, 256, 3), dtype=np.uint8)\nY_train = np.zeros((len(train_dir), 256, 256, 1), dtype=bool)\n\nX_test = np.zeros((len(test_dir), 256, 256, 3), dtype=np.uint8)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:35:54.792463Z","iopub.execute_input":"2022-07-30T05:35:54.792776Z","iopub.status.idle":"2022-07-30T05:35:54.798588Z","shell.execute_reply.started":"2022-07-30T05:35:54.792736Z","shell.execute_reply":"2022-07-30T05:35:54.797780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,8))\nplt.subplot(121)\nplt.imshow(X_train[2])\nplt.title('Real image')\nplt.subplot(122)\nplt.imshow(Y_train[2])\nplt.title('Segmentation image');","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:36:32.884421Z","iopub.execute_input":"2022-07-30T05:36:32.884671Z","iopub.status.idle":"2022-07-30T05:36:33.304246Z","shell.execute_reply.started":"2022-07-30T05:36:32.884635Z","shell.execute_reply":"2022-07-30T05:36:33.302680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aug_gen_args = dict(shear_range = 0.2,\n                    zoom_range = 0.2,\n                    rotation_range=40,\n                    width_shift_range=0.2,\n                    height_shift_range=0.2,\n                    horizontal_flip=True,\n                    vertical_flip=True,\n                    fill_mode='reflect'\n                   )\n\nX_train_gen = ImageDataGenerator(**aug_gen_args)\ny_train_gen = ImageDataGenerator(**aug_gen_args)\nX_val_gen = ImageDataGenerator()\ny_val_gen = ImageDataGenerator()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:36:33.305206Z","iopub.execute_input":"2022-07-30T05:36:33.305419Z","iopub.status.idle":"2022-07-30T05:36:33.314197Z","shell.execute_reply.started":"2022-07-30T05:36:33.305391Z","shell.execute_reply":"2022-07-30T05:36:33.313514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aug_image_real = X_train[5].reshape((1,)+X_train[1].shape)\naug_image_seg = Y_train[5].reshape((1,)+Y_train[1].shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:36:33.315459Z","iopub.execute_input":"2022-07-30T05:36:33.315835Z","iopub.status.idle":"2022-07-30T05:36:33.322055Z","shell.execute_reply.started":"2022-07-30T05:36:33.315798Z","shell.execute_reply":"2022-07-30T05:36:33.320985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aug_image_real_check = X_train_gen.flow(aug_image_real, batch_size=1, seed=17, shuffle=False)\naug_image_seg_check = y_train_gen.flow(aug_image_seg, batch_size=1, seed=17, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:36:33.322966Z","iopub.execute_input":"2022-07-30T05:36:33.323150Z","iopub.status.idle":"2022-07-30T05:36:33.333533Z","shell.execute_reply.started":"2022-07-30T05:36:33.323127Z","shell.execute_reply":"2022-07-30T05:36:33.332746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,10))\nplt.subplot(141)\nplt.imshow(X_train[5])\nplt.title(\"original\")\ni=2\nfor batch in aug_image_real_check:\n    plt.subplot(14*10+i)\n    plt.imshow(image.array_to_img(batch[0]))\n    plt.title(\"augmented\")\n    i += 1\n    if i % 5 == 0:\n        break","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:36:33.336675Z","iopub.execute_input":"2022-07-30T05:36:33.336943Z","iopub.status.idle":"2022-07-30T05:36:34.074406Z","shell.execute_reply.started":"2022-07-30T05:36:33.336909Z","shell.execute_reply":"2022-07-30T05:36:34.073731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,10))\nplt.subplot(141)\nplt.imshow(Y_train[5])\nplt.title(\"original\")\ni=2\nfor batch in aug_image_seg_check:\n    plt.subplot(14*10+i)\n    plt.imshow(image.array_to_img(batch[0]))\n    plt.title(\"augmented\")\n    i += 1\n    if i % 5 == 0:\n        break","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:36:34.075645Z","iopub.execute_input":"2022-07-30T05:36:34.076201Z","iopub.status.idle":"2022-07-30T05:36:34.617384Z","shell.execute_reply.started":"2022-07-30T05:36:34.076156Z","shell.execute_reply":"2022-07-30T05:36:34.616738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train, val, y_train, y_val = train_test_split(X_train, Y_train, test_size=0.1, random_state=17)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:36:34.620588Z","iopub.execute_input":"2022-07-30T05:36:34.620926Z","iopub.status.idle":"2022-07-30T05:36:34.681220Z","shell.execute_reply.started":"2022-07-30T05:36:34.620898Z","shell.execute_reply":"2022-07-30T05:36:34.680440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_gen.fit(train, augment=True, seed=17)\ny_train_gen.fit(y_train, augment=True, seed=17)\nX_val_gen.fit(val, seed=17)\ny_val_gen.fit(y_val, seed=17)\n\nX_train_generator = X_train_gen.flow(train, batch_size=16, seed=17, shuffle=False)\ny_train_generator = y_train_gen.flow(y_train, batch_size=16, seed=17, shuffle=False)\nX_val_generator = X_val_gen.flow(val, batch_size=16, seed=17, shuffle=False)\ny_val_generator = y_val_gen.flow(y_val, batch_size=16, seed=17, shuffle=False)\n\ntrain_generator = zip(X_train_generator, y_train_generator)\nval_generator = zip(X_val_generator, y_val_generator)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:36:34.682541Z","iopub.execute_input":"2022-07-30T05:36:34.682842Z","iopub.status.idle":"2022-07-30T05:36:45.119473Z","shell.execute_reply.started":"2022-07-30T05:36:34.682803Z","shell.execute_reply":"2022-07-30T05:36:45.118743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def iou(y_true, y_pred):\n    intersection = K.sum(K.abs(y_true * y_pred), axis=[1,2,3])\n    union = K.sum(y_true, axis=[1,2,3]) + K.sum(y_pred, axis=[1,2,3]) - intersection\n    iou = K.mean((intersection + 1) / (union + 1), axis=0)\n    return iou","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:36:45.121659Z","iopub.execute_input":"2022-07-30T05:36:45.122007Z","iopub.status.idle":"2022-07-30T05:36:45.127995Z","shell.execute_reply.started":"2022-07-30T05:36:45.121969Z","shell.execute_reply":"2022-07-30T05:36:45.127209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mean_iou(y_true, y_pred):\n    results = []   \n    for t in np.arange(0.5, 1, 0.05):\n        t_y_pred = tf.cast((y_pred > t), tf.float32)\n        pred = iou(y_true, t_y_pred)\n        results.append(pred)\n        \n    return K.mean(K.stack(results), axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:36:45.129197Z","iopub.execute_input":"2022-07-30T05:36:45.129755Z","iopub.status.idle":"2022-07-30T05:36:45.137595Z","shell.execute_reply.started":"2022-07-30T05:36:45.129716Z","shell.execute_reply":"2022-07-30T05:36:45.136894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dice_loss(y_true, y_pred):\n    intersection = K.sum(K.abs(y_true * y_pred), axis=[1,2,3])\n    union = K.sum(y_true, axis=[1,2,3]) + K.sum(y_pred, axis=[1,2,3])\n    dice = K.mean((2. * intersection + 1) / (union + 1), axis=0)\n    return 1. - dice","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:36:45.138635Z","iopub.execute_input":"2022-07-30T05:36:45.139209Z","iopub.status.idle":"2022-07-30T05:36:45.147895Z","shell.execute_reply.started":"2022-07-30T05:36:45.139172Z","shell.execute_reply":"2022-07-30T05:36:45.147282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = Input((256, 256, 3))\ns = tf.keras.layers.Lambda(lambda x: x/255.0)(inputs)\n\nconv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)\nconv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)\npool1 = MaxPooling2D(pool_size=(2, 2))(conv1)\n\nconv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1)\nconv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)\npool2 = MaxPooling2D(pool_size=(2, 2))(conv2)\n\nconv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2)\nconv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)\npool3 = MaxPooling2D(pool_size=(2, 2))(conv3)\n\nconv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool3)\nconv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv4)\npool4 = MaxPooling2D(pool_size=(2, 2))(conv4)\n\nconv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool4)\nconv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv5)\nconv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv5)\n\nup6 = UpSampling2D(size=(2,2))(conv5)\nup6 = concatenate([up6, conv4])\nconv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(up6)\nconv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv6)\n\nup7 = UpSampling2D(size=(2,2))(conv6)\nup7 = concatenate([up7, conv3])\nconv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(up7)\nconv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv7)\n\nup8 = UpSampling2D(size=(2,2))(conv7)\nup8 = concatenate([up8, conv2])\nconv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(up8)\nconv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv8)\n\nup9 = UpSampling2D(size=(2,2))(conv8)\nup9 = concatenate([up9, conv1])\nconv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(up9)\nconv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv9)\n\nconv10 = Conv2D(1, (1, 1), activation='sigmoid')(conv9)\n\nmodel = models.Model(inputs=[inputs], outputs=[conv10])\n\nmodel.compile(optimizer=optimizers.Adam(learning_rate=2e-4), loss=dice_loss, metrics=mean_iou)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:36:45.150416Z","iopub.execute_input":"2022-07-30T05:36:45.150701Z","iopub.status.idle":"2022-07-30T05:36:48.088202Z","shell.execute_reply.started":"2022-07-30T05:36:45.150665Z","shell.execute_reply":"2022-07-30T05:36:48.087527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:36:48.089290Z","iopub.execute_input":"2022-07-30T05:36:48.089956Z","iopub.status.idle":"2022-07-30T05:36:48.109192Z","shell.execute_reply.started":"2022-07-30T05:36:48.089916Z","shell.execute_reply":"2022-07-30T05:36:48.108548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator,\n                    steps_per_epoch=len(train)/8,\n                    validation_data=val_generator,\n                    validation_steps=len(val)/8,\n                    epochs=25\n                   )","metadata":{"execution":{"iopub.status.busy":"2022-07-30T05:36:48.110276Z","iopub.execute_input":"2022-07-30T05:36:48.110589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history.history['mean_iou']\nval_loss = history.history['val_mean_iou']\n\nplt.figure(figsize=(15,10))\nplt.plot(loss, label='Train IOU')\nplt.plot(val_loss,'--', label='Val IOU')\nplt.title('Training and Validation mean IOU')\nplt.yticks(np.arange(0.5, 1, 0.05))\nplt.xticks(np.arange(0, 25))\nplt.grid()\nplt.legend();","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_pred = model.predict(train, verbose = 1)\nval_pred = model.predict(val, verbose=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,10))\nplt.subplot(131)\nplt.imshow(train[1])\nplt.title('Original image')\nplt.subplot(132)\nplt.imshow(np.squeeze(y_train[1]))\nplt.title('Segmented image')\nplt.subplot(133)\nplt.imshow(np.squeeze(train_pred[1]))\nplt.title('Predicted  image');","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,10))\nplt.subplot(131)\nplt.imshow(val[3])\nplt.title('Original image')\nplt.subplot(132)\nplt.imshow(np.squeeze(y_val[3]))\nplt.title('Segmented image')\nplt.subplot(133)\nplt.imshow(np.squeeze(val_pred[3]))\nplt.title('Predicted  image');","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = model.predict(X_test, verbose=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 32))\nfor i in range(421, 429):\n    plt.subplot(i)\n    if i % 2!=0:\n        plt.imshow(X_test[i])\n        plt.title('Original image')\n    else:\n        plt.imshow(np.squeeze(test_pred[i-1]))\n        plt.title('Predicted image')","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}