{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":8089,"databundleVersionId":44321,"sourceType":"competition"},{"sourceId":8410276,"sourceType":"datasetVersion","datasetId":5005426},{"sourceId":8558564,"sourceType":"datasetVersion","datasetId":5115268},{"sourceId":8590820,"sourceType":"datasetVersion","datasetId":5138584}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"## Imports\nimport os\nimport sys\nimport random\n\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2024-06-03T06:17:41.837396Z","iopub.execute_input":"2024-06-03T06:17:41.837789Z","iopub.status.idle":"2024-06-03T06:17:41.842906Z","shell.execute_reply.started":"2024-06-03T06:17:41.837749Z","shell.execute_reply":"2024-06-03T06:17:41.841944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Seeding\nseed = 42\nrandom.seed = seed\nnp.random.seed = seed\ntf.seed = seed","metadata":{"execution":{"iopub.status.busy":"2024-06-03T06:17:44.277069Z","iopub.execute_input":"2024-06-03T06:17:44.277428Z","iopub.status.idle":"2024-06-03T06:17:44.282035Z","shell.execute_reply.started":"2024-06-03T06:17:44.277401Z","shell.execute_reply":"2024-06-03T06:17:44.281021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DataGen(keras.utils.Sequence):\n    def __init__(self, ids, path, batch_size=8, image_size=128):\n        self.ids = ids\n        self.path = path\n        self.batch_size = batch_size\n        self.image_size = image_size\n        self.on_epoch_end()\n        \n    \n        \n    def __load__(self, id_name):\n        ## Path\n        image_path = os.path.join(self.path, id_name, \"images\", id_name) + \".png\"\n        mask_path = os.path.join(self.path, id_name, \"masks/\")\n        all_masks = os.listdir(mask_path)\n        \n        ## Reading Image\n        image = cv2.imread(image_path, 1)\n        image = cv2.resize(image, (self.image_size, self.image_size))\n        \n        mask = np.zeros((self.image_size, self.image_size, 1))\n        \n        ## Reading Masks\n        for name in all_masks:\n            _mask_path = mask_path + name\n            _mask_image = cv2.imread(_mask_path, -1)\n            _mask_image = cv2.resize(_mask_image, (self.image_size, self.image_size)) #128x128\n            _mask_image = np.expand_dims(_mask_image, axis=-1)\n            mask = np.maximum(mask, _mask_image)\n            \n        ## Normalizing \n        image = image/255.0\n        mask = mask/255.0\n        \n        return image, mask\n    \n    def __getitem__(self, index):\n        if(index+1)*self.batch_size > len(self.ids):\n            self.batch_size = len(self.ids) - index*self.batch_size\n        \n        files_batch = self.ids[index*self.batch_size : (index+1)*self.batch_size]\n        \n        image = []\n        mask  = []\n        \n        for id_name in files_batch:\n            _img, _mask = self.__load__(id_name)\n            image.append(_img)\n            mask.append(_mask)\n            \n        image = np.array(image)\n        mask  = np.array(mask)\n        \n        return image, mask\n    \n    def on_epoch_end(self):\n        # Shuffle data at the end of each epoch to reduce bias\n        random.shuffle(self.ids)\n    \n    def __len__(self):\n        return int(np.ceil(len(self.ids)/float(self.batch_size)))","metadata":{"execution":{"iopub.status.busy":"2024-06-03T06:17:47.814833Z","iopub.execute_input":"2024-06-03T06:17:47.815511Z","iopub.status.idle":"2024-06-03T06:17:47.829643Z","shell.execute_reply.started":"2024-06-03T06:17:47.815483Z","shell.execute_reply":"2024-06-03T06:17:47.828698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = 128\ntrain_path = \"../input/dataset-3236/stage2_train/\"\n# train_path = \"stage1_train/\"\nepochs = 10\nbatch_size = 8\n\n## Training Ids\ntrain_ids = next(os.walk(train_path))[1]\n\n## Validation Data Size\nval_data_size = 150\n\nvalid_ids = train_ids[:val_data_size]\ntrain_ids = train_ids[val_data_size:]","metadata":{"execution":{"iopub.status.busy":"2024-06-03T06:17:55.539675Z","iopub.execute_input":"2024-06-03T06:17:55.540624Z","iopub.status.idle":"2024-06-03T06:17:55.676532Z","shell.execute_reply.started":"2024-06-03T06:17:55.540586Z","shell.execute_reply":"2024-06-03T06:17:55.675803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gen = DataGen(train_ids, train_path, batch_size=batch_size, image_size=image_size)\nx, y = gen.__getitem__(0)\nprint(x.shape, y.shape)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T06:17:57.899430Z","iopub.execute_input":"2024-06-03T06:17:57.900228Z","iopub.status.idle":"2024-06-03T06:17:58.576616Z","shell.execute_reply.started":"2024-06-03T06:17:57.900195Z","shell.execute_reply":"2024-06-03T06:17:58.575575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r = random.randint(0, len(x)-1)\n\nfig = plt.figure()\nfig.subplots_adjust(hspace=0.4, wspace=0.4)\nax = fig.add_subplot(1, 2, 1)\nax.imshow(x[r])\nax = fig.add_subplot(1, 2, 2)\nax.imshow(np.reshape(y[r], (image_size, image_size)), cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2024-06-03T06:18:00.766426Z","iopub.execute_input":"2024-06-03T06:18:00.766869Z","iopub.status.idle":"2024-06-03T06:18:01.121255Z","shell.execute_reply.started":"2024-06-03T06:18:00.766836Z","shell.execute_reply":"2024-06-03T06:18:01.120384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def down_block(x, filters, kernel_size=(3, 3), padding=\"same\", strides=1):\n    c = keras.layers.Conv2D(filters, kernel_size, padding=padding, strides=strides, activation=\"relu\")(x)\n    c = keras.layers.Conv2D(filters, kernel_size, padding=padding, strides=strides, activation=\"relu\")(c)\n    p = keras.layers.MaxPool2D((2, 2), (2, 2))(c)\n    return c, p\n\ndef up_block(x, skip, filters, kernel_size=(3, 3), padding=\"same\", strides=1):\n    us = keras.layers.UpSampling2D((2, 2))(x)\n    concat = keras.layers.Concatenate()([us, skip])\n    c = keras.layers.Conv2D(filters, kernel_size, padding=padding, strides=strides, activation=\"relu\")(concat)\n    c = keras.layers.Conv2D(filters, kernel_size, padding=padding, strides=strides, activation=\"relu\")(c)\n    return c\n\ndef bottleneck(x, filters, kernel_size=(3, 3), padding=\"same\", strides=1):\n    c = keras.layers.Conv2D(filters, kernel_size, padding=padding, strides=strides, activation=\"relu\")(x)\n    c = keras.layers.Conv2D(filters, kernel_size, padding=padding, strides=strides, activation=\"relu\")(c)\n    return c","metadata":{"execution":{"iopub.status.busy":"2024-06-03T06:18:06.915261Z","iopub.execute_input":"2024-06-03T06:18:06.915599Z","iopub.status.idle":"2024-06-03T06:18:06.925662Z","shell.execute_reply.started":"2024-06-03T06:18:06.915574Z","shell.execute_reply":"2024-06-03T06:18:06.924702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def UNet():\n    f = [16, 32, 64, 128, 256]\n    inputs = keras.layers.Input((image_size, image_size, 3))\n    \n    p0 = inputs\n    c1, p1 = down_block(p0, f[0])  # 128 --> 64\n    c2, p2 = down_block(p1, f[1])  # 64  --> 32\n    c3, p3 = down_block(p2, f[2])  # 32  --> 16\n    c4, p4 = down_block(p3, f[3])  # 16  --> 8\n    \n    bn = bottleneck(p4, f[4])\n    \n    u1 = up_block(bn, c4, f[3])  # 8  --> 16\n    u2 = up_block(u1, c3, f[2])  # 16 --> 32\n    u3 = up_block(u2, c2, f[1])  # 32 --> 64\n    u4 = up_block(u3, c1, f[0])  # 64 --> 128\n    \n    outputs = keras.layers.Conv2D(1, (1, 1), padding=\"same\", activation=\"sigmoid\")(u4)\n    model = keras.models.Model(inputs, outputs)\n    return model\n  ","metadata":{"execution":{"iopub.status.busy":"2024-06-03T06:18:09.729943Z","iopub.execute_input":"2024-06-03T06:18:09.730792Z","iopub.status.idle":"2024-06-03T06:18:09.738401Z","shell.execute_reply.started":"2024-06-03T06:18:09.730744Z","shell.execute_reply":"2024-06-03T06:18:09.737443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = UNet()\nmodel.compile(optimizer=\"adam\", loss=\"binary_crossentropy\", metrics=[\"acc\"])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-06-03T06:18:11.980145Z","iopub.execute_input":"2024-06-03T06:18:11.980475Z","iopub.status.idle":"2024-06-03T06:18:12.943570Z","shell.execute_reply.started":"2024-06-03T06:18:11.980451Z","shell.execute_reply":"2024-06-03T06:18:12.942705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen = DataGen(train_ids, train_path, image_size=image_size, batch_size=batch_size)\nvalid_gen = DataGen(valid_ids, train_path, image_size=image_size, batch_size=batch_size)\n\ntrain_steps = len(train_ids)//batch_size\nvalid_steps = len(valid_ids)//batch_size\n\nmodel.fit(train_gen, validation_data=valid_gen, steps_per_epoch=train_steps,\n                    validation_steps=valid_steps, epochs=epochs)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T06:34:16.944819Z","iopub.execute_input":"2024-06-03T06:34:16.945452Z","iopub.status.idle":"2024-06-03T06:35:54.034652Z","shell.execute_reply.started":"2024-06-03T06:34:16.945419Z","shell.execute_reply":"2024-06-03T06:35:54.033626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = model.history.history['loss']\nval_loss = model.history.history['val_loss']\n\nplt.figure()\nplt.plot( val_loss, 'r', label='Validation loss')\nplt.plot( loss, 'bo', label='Training loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss Value')\nplt.ylim([0, 1])\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-03T06:36:20.825415Z","iopub.execute_input":"2024-06-03T06:36:20.826302Z","iopub.status.idle":"2024-06-03T06:36:21.092612Z","shell.execute_reply.started":"2024-06-03T06:36:20.826268Z","shell.execute_reply":"2024-06-03T06:36:21.091710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = model.history.history['acc']\nval_accuracy = model.history.history['val_acc']\n\nplt.figure()\nplt.plot( accuracy, 'bo', label='Training accuracy')\nplt.plot( val_accuracy, 'r', label='Validation accuracy')\nplt.title('Training and Validation accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Acc Value')\nplt.ylim([0, 1])\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-03T06:36:31.681538Z","iopub.execute_input":"2024-06-03T06:36:31.682394Z","iopub.status.idle":"2024-06-03T06:36:31.894152Z","shell.execute_reply.started":"2024-06-03T06:36:31.682361Z","shell.execute_reply":"2024-06-03T06:36:31.893274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = model.evaluate(valid_gen)\nprint(f'Test Accuracy: {test_acc}')\nprint(f'Test Loss: {test_loss}')","metadata":{"execution":{"iopub.status.busy":"2024-06-03T06:36:58.058654Z","iopub.execute_input":"2024-06-03T06:36:58.059403Z","iopub.status.idle":"2024-06-03T06:37:03.881903Z","shell.execute_reply.started":"2024-06-03T06:36:58.059367Z","shell.execute_reply":"2024-06-03T06:37:03.880987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Save the Weights\nmodel.save_weights(\"/kaggle/working/UNetW.weights.h5\")\n# model.load_weights(\"/kaggle/working/UNetW.weights.h5\")\n\n## Dataset for prediction\nx, y = valid_gen.__getitem__(2)\nresult = model.predict(x)\n\nresult = result > 0.5","metadata":{"execution":{"iopub.status.busy":"2024-06-03T06:37:05.741529Z","iopub.execute_input":"2024-06-03T06:37:05.742009Z","iopub.status.idle":"2024-06-03T06:37:06.148764Z","shell.execute_reply.started":"2024-06-03T06:37:05.741978Z","shell.execute_reply":"2024-06-03T06:37:06.148060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure()\nfig.subplots_adjust(hspace=0.4, wspace=0.4)\n\n# Plot original image\nax = fig.add_subplot(1, 3, 1)\nax.imshow(x[0])\nax.set_title(\"Original Image\")\n\n# Plot ground truth mask\nax = fig.add_subplot(1, 3, 2)\nax.imshow(np.reshape(y[0]*255, (image_size, image_size)), cmap=\"gray\")\nax.set_title(\"Ground Truth Mask\")\n\n# Plot predicted mask\nax = fig.add_subplot(1, 3, 3)\nax.imshow(np.reshape(result[0]*255, (image_size, image_size)), cmap=\"gray\")\nax.set_title(\"Predicted Mask\")\n\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2024-06-03T06:37:08.586764Z","iopub.execute_input":"2024-06-03T06:37:08.587583Z","iopub.status.idle":"2024-06-03T06:37:08.981716Z","shell.execute_reply.started":"2024-06-03T06:37:08.587555Z","shell.execute_reply":"2024-06-03T06:37:08.980800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure()\nfig.subplots_adjust(hspace=0.4, wspace=0.4)\n\n# Plot original image\nax = fig.add_subplot(1, 3, 1)\nax.imshow(x[1])\nax.set_title(\"Original Image\")\n\n# Plot ground truth mask\nax = fig.add_subplot(1, 3, 2)\nax.imshow(np.reshape(y[1]*255, (image_size, image_size)), cmap=\"gray\")\nax.set_title(\"Ground Truth Mask\")\n\n# Plot predicted mask\nax = fig.add_subplot(1, 3, 3)\nax.imshow(np.reshape(result[1]*255, (image_size, image_size)), cmap=\"gray\")\nax.set_title(\"Predicted Mask\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-03T06:37:11.987508Z","iopub.execute_input":"2024-06-03T06:37:11.987866Z","iopub.status.idle":"2024-06-03T06:37:12.374075Z","shell.execute_reply.started":"2024-06-03T06:37:11.987838Z","shell.execute_reply":"2024-06-03T06:37:12.373224Z"},"trusted":true},"execution_count":null,"outputs":[]}]}