{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.14","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"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import tensorflow as tf\n\nfrom tensorflow.keras.losses import BinaryCrossentropy\n\nimport numpy as np\n\nimport os\n\nimport random\n\nfrom tqdm import tqdm\n\nfrom skimage.io import imread, imshow\n\nfrom skimage.transform import resize\n\nimport matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-07T02:15:17.535719Z","iopub.execute_input":"2024-11-07T02:15:17.536037Z","iopub.status.idle":"2024-11-07T02:15:31.478267Z","shell.execute_reply.started":"2024-11-07T02:15:17.536002Z","shell.execute_reply":"2024-11-07T02:15:31.477355Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Setting up the data","metadata":{}},{"cell_type":"code","source":"seed = 42\n\nnp.random.seed = seed","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-07T02:15:31.479885Z","iopub.execute_input":"2024-11-07T02:15:31.480428Z","iopub.status.idle":"2024-11-07T02:15:31.484614Z","shell.execute_reply.started":"2024-11-07T02:15:31.480391Z","shell.execute_reply":"2024-11-07T02:15:31.483674Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMG_WIDTH = 128\n\nIMG_HEIGHT = 128\n\nIMG_CHANNELS = 3","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-07T02:15:31.485997Z","iopub.execute_input":"2024-11-07T02:15:31.486673Z","iopub.status.idle":"2024-11-07T02:15:31.520661Z","shell.execute_reply.started":"2024-11-07T02:15:31.486615Z","shell.execute_reply":"2024-11-07T02:15:31.519797Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TRAIN_PATH = '/kaggle/input/data-science-bowl-2018/stage1_train.zip'\n\nTEST_PATH = '/kaggle/input/data-science-bowl-2018/stage1_test.zip'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-07T02:16:06.68124Z","iopub.execute_input":"2024-11-07T02:16:06.681891Z","iopub.status.idle":"2024-11-07T02:16:06.686174Z","shell.execute_reply.started":"2024-11-07T02:16:06.681852Z","shell.execute_reply":"2024-11-07T02:16:06.685147Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import zipfile\n\nextract_paths = {\n    TRAIN_PATH: '/kaggle/working/train_data',\n    TEST_PATH: '/kaggle/working/test_data'\n}\n\n# Loop over each path and extract\nfor zip_path, extract_to in extract_paths.items():\n    os.makedirs(extract_to, exist_ok=True)\n\n    with zipfile.ZipFile(zip_path, 'r') as zip_ref:\n        for member in zip_ref.namelist():\n            zip_ref.extract(member, extract_to)\n\n    print(f\"Contents of {extract_to}:\")\n    print(os.listdir(extract_to))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-07T02:27:11.004522Z","iopub.execute_input":"2024-11-07T02:27:11.004907Z","iopub.status.idle":"2024-11-07T02:27:14.939641Z","shell.execute_reply.started":"2024-11-07T02:27:11.004873Z","shell.execute_reply":"2024-11-07T02:27:14.937816Z"},"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TRAIN_PATH_U = '/kaggle/working/train_data'\nTEST_PATH_U = '/kaggle/working/test_data'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-07T02:29:17.83104Z","iopub.execute_input":"2024-11-07T02:29:17.831418Z","iopub.status.idle":"2024-11-07T02:29:17.835747Z","shell.execute_reply.started":"2024-11-07T02:29:17.831385Z","shell.execute_reply":"2024-11-07T02:29:17.834851Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ids = next(os.walk(TRAIN_PATH_U))[1]\n\ntest_ids = next(os.walk(TEST_PATH_U))[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-07T02:29:18.28315Z","iopub.execute_input":"2024-11-07T02:29:18.284219Z","iopub.status.idle":"2024-11-07T02:29:18.289961Z","shell.execute_reply.started":"2024-11-07T02:29:18.28416Z","shell.execute_reply":"2024-11-07T02:29:18.289018Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\n\nY_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-07T02:29:22.937416Z","iopub.execute_input":"2024-11-07T02:29:22.93777Z","iopub.status.idle":"2024-11-07T02:29:22.942948Z","shell.execute_reply.started":"2024-11-07T02:29:22.937737Z","shell.execute_reply":"2024-11-07T02:29:22.941935Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print('Resizing training images and masks')\n\nfor n, id_ in tqdm(enumerate(train_ids), total=len(train_ids)):\n\n    path = TRAIN_PATH_U + '/' + id_\n\n    img = imread(path + '/images/' + id_ + '.png')[:,:,:IMG_CHANNELS]\n\n    img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n\n    X_train[n] = img\n\n    mask = np.zeros((IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool_)\n\n    for mask_file in next(os.walk(path + '/masks/'))[2]:\n\n        mask_ = imread(path + '/masks/' + mask_file)\n\n        mask_ = np.expand_dims(resize(mask_, (IMG_HEIGHT, IMG_WIDTH), mode='constant', \n\n                                      preserve_range=True), axis=-1)\n\n        mask = np.maximum(mask, mask_)\n\n    Y_train[n] = mask\n\n\n\n#test images\n\nX_test = np.zeros((len(test_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\n\nsizes_test = []\n\nprint('Resizing test images')\n\nfor n, id_ in tqdm(enumerate(test_ids), total=len(test_ids)):\n\n    path = TEST_PATH_U +'/'+ id_\n\n    img = imread(path + '/images/' + id_ + '.png')[:,:,:IMG_CHANNELS]\n\n    sizes_test.append([img.shape[0], img.shape[1]])\n\n    img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n\n    X_test[n] = img\n\nprint('Done!')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-07T02:55:11.025324Z","iopub.execute_input":"2024-11-07T02:55:11.025797Z","iopub.status.idle":"2024-11-07T02:59:13.902775Z","shell.execute_reply.started":"2024-11-07T02:55:11.025752Z","shell.execute_reply":"2024-11-07T02:59:13.901839Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check if training data looks all right\n\nix = random.randint(0, len(train_ids))\n\nimshow(X_train[ix])\n\nplt.show()\n\nimshow(np.squeeze(Y_train[ix]))\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-07T02:59:50.111042Z","iopub.execute_input":"2024-11-07T02:59:50.111771Z","iopub.status.idle":"2024-11-07T02:59:50.758601Z","shell.execute_reply.started":"2024-11-07T02:59:50.111734Z","shell.execute_reply":"2024-11-07T02:59:50.757698Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# U-Net Model Architecture","metadata":{}},{"cell_type":"markdown","source":"![Image](https://iitgnacin-my.sharepoint.com/:i:/g/personal/23110065_iitgn_ac_in/ET2eBA4rFVNOi_ujhCMUKiEBtBaJTIF7ScpQelqCo95gjQ)\n","metadata":{}},{"cell_type":"code","source":"# Define the model\n\ninputs = tf.keras.layers.Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))\n\ns = tf.keras.layers.Lambda(lambda x: x / 255)(inputs) # Rescale the image values to [0, 1]\n\n\n\n# Contraction path\n\nc1 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(s) # 16 refers to the number of filters, (3, 3) is the size of the filter, activation='relu' is the activation function, kernel_initializer='he_normal' is the initialization method, and padding='same' is the padding method. \n\nc1 = tf.keras.layers.Dropout(0.1)(c1)\n\nc1 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c1)\n\np1 = tf.keras.layers.MaxPooling2D((2, 2))(c1) #Where is stride? In Keras, the default stride for a MaxPooling2D layer (and other pooling layers) is equal to the pool size.\n\n\n\nc2 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p1)\n\nc2 = tf.keras.layers.Dropout(0.1)(c2)\n\nc2 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c2)\n\np2 = tf.keras.layers.MaxPooling2D((2, 2))(c2)\n\n\n\nc3 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p2)\n\nc3 = tf.keras.layers.Dropout(0.2)(c3)\n\nc3 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c3)\n\np3 = tf.keras.layers.MaxPooling2D((2, 2))(c3)\n\n\n\nc4 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p3)\n\nc4 = tf.keras.layers.Dropout(0.2)(c4)\n\nc4 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c4)\n\np4 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(c4)\n\n\n\nc5 = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p4)\n\nc5 = tf.keras.layers.Dropout(0.3)(c5)\n\nc5 = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c5)\n\n\n\n# Expansive path\n\nu6 = tf.keras.layers.Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(c5) # Transposed convolutional layer with 128 filters and a kernel size of (2, 2) and a stride of (2, 2) to upsample the image.\n\nu6 = tf.keras.layers.concatenate([u6, c4]) # Concatenate the upsampled image with the corresponding image from the contraction path.\n\nc6 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u6)\n\nc6 = tf.keras.layers.Dropout(0.2)(c6)\n\nc6 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c6)\n\n\n\nu7 = tf.keras.layers.Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(c6)\n\nu7 = tf.keras.layers.concatenate([u7, c3])\n\nc7 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u7)\n\nc7 = tf.keras.layers.Dropout(0.2)(c7)\n\nc7 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c7)\n\n\n\nu8 = tf.keras.layers.Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(c7)\n\nu8 = tf.keras.layers.concatenate([u8, c2])\n\nc8 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u8)\n\nc8 = tf.keras.layers.Dropout(0.1)(c8)\n\nc8 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c8)\n\n\n\nu9 = tf.keras.layers.Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same')(c8)\n\nu9 = tf.keras.layers.concatenate([u9, c1], axis=3)\n\nc9 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u9)\n\nc9 = tf.keras.layers.Dropout(0.1)(c9)\n\nc9 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c9)\n\n\n\noutputs = tf.keras.layers.Conv2D(1, (1, 1), activation=None)(c9) # The 1x1 convolutional layer with a sigmoid activation function is used to map each 16-component feature vector to a single value between 0 and 1. This value represents the probability that the corresponding pixel belongs to the foreground class.\n\n\n\nmodel = tf.keras.Model(inputs=[inputs], outputs=[outputs])\n\nmodel.compile(optimizer='adam', loss=BinaryCrossentropy(from_logits=True), metrics=['accuracy'])\n\nmodel.summary()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-07T03:06:06.761831Z","iopub.execute_input":"2024-11-07T03:06:06.762472Z","iopub.status.idle":"2024-11-07T03:06:07.991487Z","shell.execute_reply.started":"2024-11-07T03:06:06.762432Z","shell.execute_reply":"2024-11-07T03:06:07.990491Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Model checkpoint\ncheckpoint_path = '/kaggle/working/model_for_nuclei.keras'\ncheckpointer = tf.keras.callbacks.ModelCheckpoint(checkpoint_path, verbose=1, save_best_only=True)\n\ncallbacks = [\n\n    tf.keras.callbacks.EarlyStopping(patience=2, monitor='val_loss'),\n\n    tf.keras.callbacks.TensorBoard(log_dir='logs')]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-07T03:06:12.389783Z","iopub.execute_input":"2024-11-07T03:06:12.390812Z","iopub.status.idle":"2024-11-07T03:06:12.395884Z","shell.execute_reply.started":"2024-11-07T03:06:12.39077Z","shell.execute_reply":"2024-11-07T03:06:12.394849Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = model.fit(X_train, Y_train, validation_split=0.1, batch_size=16, epochs=25, callbacks=callbacks)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-07T03:06:15.500658Z","iopub.execute_input":"2024-11-07T03:06:15.501055Z","iopub.status.idle":"2024-11-07T03:07:19.903769Z","shell.execute_reply.started":"2024-11-07T03:06:15.501015Z","shell.execute_reply":"2024-11-07T03:07:19.902826Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Making Prediction","metadata":{}},{"cell_type":"code","source":"idx = random.randint(0, len(X_train))\n\npreds_train = model.predict(X_train[:int(X_train.shape[0]*0.9)], verbose=1)\npreds_val = model.predict(X_train[int(X_train.shape[0]*0.9):], verbose=1)\npreds_test = model.predict(X_test, verbose=1)\n \n\npreds_train_t = (preds_train > 0.5).astype(np.uint8)\npreds_val_t = (preds_val > 0.5).astype(np.uint8)\npreds_test_t = (preds_test > 0.5).astype(np.uint8)\n\n\n# Visualize the results on the training set\n\nix = random.randint(0, len(preds_train_t))\nimshow(X_train[ix])\nplt.show()\n\nimshow(np.squeeze(Y_train[ix]))\nplt.show()\nimshow(np.squeeze(preds_train_t[ix]))\nplt.show()\n\n\n\n# Visualize the results on the validation set\n\nix = random.randint(0, len(preds_val_t))\nimshow(X_train[int(X_train.shape[0]*0.9):][ix])\nplt.show()\n\nimshow(np.squeeze(Y_train[int(Y_train.shape[0]*0.9):][ix]))\nplt.show()\nimshow(np.squeeze(preds_val_t[ix]))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-07T03:08:16.591931Z","iopub.execute_input":"2024-11-07T03:08:16.592359Z","iopub.status.idle":"2024-11-07T03:08:25.524832Z","shell.execute_reply.started":"2024-11-07T03:08:16.592319Z","shell.execute_reply":"2024-11-07T03:08:25.523877Z"}},"outputs":[],"execution_count":null}]}