{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30887,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:26:37.469614Z","iopub.execute_input":"2025-05-16T05:26:37.469999Z","iopub.status.idle":"2025-05-16T05:26:37.477996Z","shell.execute_reply.started":"2025-05-16T05:26:37.469957Z","shell.execute_reply":"2025-05-16T05:26:37.477014Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nprint(\"GPUs available:\", tf.config.list_physical_devices('GPU'))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:26:37.479038Z","iopub.execute_input":"2025-05-16T05:26:37.479282Z","iopub.status.idle":"2025-05-16T05:26:37.503867Z","shell.execute_reply.started":"2025-05-16T05:26:37.479261Z","shell.execute_reply":"2025-05-16T05:26:37.503206Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport sys\nimport random\n\nimport warnings\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n\nfrom tqdm import tqdm\nfrom itertools import chain\nfrom skimage.io import imread, imshow, imread_collection, concatenate_images\nfrom skimage.transform import resize\nfrom skimage.morphology import label\n\nfrom keras.models import Model, load_mode\nfrom keras.layers import Input,Dropout, Lambda, Conv2D, Conv2DTranspose, MaxPooling2D,concatenate\n\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom keras import backend as K\n\nimport tensorflow as tf\n\n# Set some parameters\nIMG_WIDTH = 128\nIMG_HEIGHT = 128\nIMG_CHANNELS = 3\n\n\nwarnings.filterwarnings('ignore', category=UserWarning, module='skimage')\nnp.random.seed(42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:26:37.505317Z","iopub.execute_input":"2025-05-16T05:26:37.505660Z","iopub.status.idle":"2025-05-16T05:26:37.520566Z","shell.execute_reply.started":"2025-05-16T05:26:37.505625Z","shell.execute_reply":"2025-05-16T05:26:37.519846Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\n# List all datasets in the Kaggle input directory\nprint(\"Available datasets in /kaggle/input/:\")\nprint(os.listdir(\"/kaggle/input/data-science-bowl-2018\"))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:26:37.521741Z","iopub.execute_input":"2025-05-16T05:26:37.522036Z","iopub.status.idle":"2025-05-16T05:26:37.538339Z","shell.execute_reply.started":"2025-05-16T05:26:37.522015Z","shell.execute_reply":"2025-05-16T05:26:37.537427Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from zipfile import ZipFile\nimport os\n\n# Paths\nzip_path = \"/kaggle/input/data-science-bowl-2018/stage1_train.zip\"  # Path to the zip file\nextract_path = \"./stage1_train\"  # Path where files will be extracted\n\n# Extract the zip file\nwith ZipFile(zip_path, \"r\") as zip_ref:\n    zip_ref.extractall(extract_path)\n\nprint(\"Extraction completed!\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:26:37.539204Z","iopub.execute_input":"2025-05-16T05:26:37.539421Z","iopub.status.idle":"2025-05-16T05:26:42.311672Z","shell.execute_reply.started":"2025-05-16T05:26:37.539402Z","shell.execute_reply":"2025-05-16T05:26:42.310573Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from zipfile import ZipFile\nimport os\n\n# Paths\nzip_path = \"/kaggle/input/data-science-bowl-2018/stage1_test.zip\"  # Path to the zip file\nextract_path = \"./stage1_test\"  # Path where files will be extracted\n\n# Extract the zip file\nwith ZipFile(zip_path, \"r\") as zip_ref:\n    zip_ref.extractall(extract_path)\n\nprint(\"Extraction completed!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:26:42.313342Z","iopub.execute_input":"2025-05-16T05:26:42.313567Z","iopub.status.idle":"2025-05-16T05:26:42.383473Z","shell.execute_reply.started":"2025-05-16T05:26:42.313547Z","shell.execute_reply":"2025-05-16T05:26:42.382482Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TRAIN_PATH = \"./stage1_train/\"\nTEST_PATH=\"./stage1_test/\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:26:42.384418Z","iopub.execute_input":"2025-05-16T05:26:42.384762Z","iopub.status.idle":"2025-05-16T05:26:42.388591Z","shell.execute_reply.started":"2025-05-16T05:26:42.384729Z","shell.execute_reply":"2025-05-16T05:26:42.387714Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nfrom skimage.io import imread\nfrom skimage.transform import resize\nfrom tqdm import tqdm\nimport numpy as np\n\n# Constants\nIMG_HEIGHT = 128\nIMG_WIDTH = 128\nIMG_CHANNELS = 3\n\n# Updated TRAIN_PATH\nTRAIN_PATH = \"./stage1_train/\"\ntrain_ids = next(os.walk(TRAIN_PATH))[1]\n\n# Initialize arrays\nX_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\nY_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, 1), dtype=bool)\n\nprint('Getting and resizing train images and masks ... ')\nfor n, id_ in tqdm(enumerate(train_ids), total=len(train_ids)):\n    # Construct paths\n    path = os.path.join(TRAIN_PATH, id_)\n    image_path = os.path.join(path, 'images', f\"{id_}.png\")\n    \n    try:\n        # Load and resize the image\n        img = imread(image_path)[:, :, :IMG_CHANNELS]\n        img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n        X_train[n] = img\n\n        # Process masks\n        mask = np.zeros((IMG_HEIGHT, IMG_WIDTH, 1), dtype=bool)\n        mask_dir = os.path.join(path, 'masks')\n        for mask_file in next(os.walk(mask_dir))[2]:\n            mask_ = imread(os.path.join(mask_dir, mask_file))\n            mask_ = np.expand_dims(resize(mask_, (IMG_HEIGHT, IMG_WIDTH), mode='constant',\n                                          preserve_range=True), axis=-1)\n            mask = np.maximum(mask, mask_)\n        Y_train[n] = mask\n\n    except FileNotFoundError:\n        print(f\"File not found: {image_path}\")\n    except NotADirectoryError:\n        print(f\"Error: {path} is not a directory. Did you extract the zip file?\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:26:42.390891Z","iopub.execute_input":"2025-05-16T05:26:42.391176Z","iopub.status.idle":"2025-05-16T05:30:42.035500Z","shell.execute_reply.started":"2025-05-16T05:26:42.391154Z","shell.execute_reply":"2025-05-16T05:30:42.034538Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape,Y_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:30:42.036648Z","iopub.execute_input":"2025-05-16T05:30:42.036969Z","iopub.status.idle":"2025-05-16T05:30:42.042105Z","shell.execute_reply.started":"2025-05-16T05:30:42.036912Z","shell.execute_reply":"2025-05-16T05:30:42.041176Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print('Getting and resizing test images ... ')\ntest_ids = next(os.walk(TEST_PATH))[1]\nsizes_test = []\nX_test = np.zeros((len(test_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\nfor n, id_ in tqdm(enumerate(test_ids), total=len(test_ids)):\n    # Construct paths\n    path = os.path.join(TEST_PATH, id_)\n    image_path = os.path.join(path, 'images', f\"{id_}.png\")\n    \n    try:\n        # Load the original image and store its size\n        img = imread(image_path)[:, :, :IMG_CHANNELS]\n        sizes_test.append([img.shape[0], img.shape[1]])\n\n        # Resize the image\n        img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n        X_test[n] = img\n    except FileNotFoundError:\n        print(f\"Test image not found: {image_path}\")\n    except NotADirectoryError:\n        print(f\"Error: {path} is not a directory. Did you extract the zip file?\")\n\nprint('Done!')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:30:42.043015Z","iopub.execute_input":"2025-05-16T05:30:42.043313Z","iopub.status.idle":"2025-05-16T05:30:43.384157Z","shell.execute_reply.started":"2025-05-16T05:30:42.043285Z","shell.execute_reply":"2025-05-16T05:30:43.383212Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:30:43.385025Z","iopub.execute_input":"2025-05-16T05:30:43.385356Z","iopub.status.idle":"2025-05-16T05:30:43.390070Z","shell.execute_reply.started":"2025-05-16T05:30:43.385321Z","shell.execute_reply":"2025-05-16T05:30:43.389211Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nimage_datagen = ImageDataGenerator(\n    rotation_range=20,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    shear_range=0.1,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest')\n\nmask_datagen = ImageDataGenerator(\n    rotation_range=20,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    shear_range=0.1,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:30:43.390821Z","iopub.execute_input":"2025-05-16T05:30:43.391048Z","iopub.status.idle":"2025-05-16T05:30:43.405402Z","shell.execute_reply.started":"2025-05-16T05:30:43.391030Z","shell.execute_reply":"2025-05-16T05:30:43.404700Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"gen_seed=49\nseed = gen_seed\nimage_generator = image_datagen.flow(X_train, batch_size=16, seed=gen_seed)\nmask_generator = mask_datagen.flow(Y_train, batch_size=16, seed=gen_seed)\ntrain_dataset = tf.data.Dataset.from_generator(\n    lambda: zip(image_generator, mask_generator),\n    output_signature=(\n        tf.TensorSpec(shape=(None, IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=tf.float32),\n        tf.TensorSpec(shape=(None, IMG_HEIGHT, IMG_WIDTH, 1), dtype=tf.float32)\n    )\n).prefetch(tf.data.AUTOTUNE)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:30:43.406140Z","iopub.execute_input":"2025-05-16T05:30:43.406427Z","iopub.status.idle":"2025-05-16T05:30:43.485901Z","shell.execute_reply.started":"2025-05-16T05:30:43.406399Z","shell.execute_reply":"2025-05-16T05:30:43.485251Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ix = random.randint(0, len(train_ids))\nimshow(X_train[ix])\nplt.show()\nimshow(np.squeeze(Y_train[ix]))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:30:43.486645Z","iopub.execute_input":"2025-05-16T05:30:43.486848Z","iopub.status.idle":"2025-05-16T05:30:43.864840Z","shell.execute_reply.started":"2025-05-16T05:30:43.486829Z","shell.execute_reply":"2025-05-16T05:30:43.863755Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Attention block\ndef attention_block(input_x, gating_x, inter_filters):\n    theta_x = layers.Conv2D(inter_filters, (1, 1), strides=(1, 1), padding='same')(input_x)\n    phi_g = layers.Conv2D(inter_filters, (1, 1), strides=(1, 1), padding='same')(gating_x)\n    add_xg = layers.Add()([theta_x, phi_g])\n    relu_xg = layers.Activation('relu')(add_xg)\n    psi = layers.Conv2D(1, (1, 1), strides=(1, 1), padding='same')(relu_xg)\n    sigmoid_xg = layers.Activation('sigmoid')(psi)\n    upsample_psi = layers.Lambda(lambda inputs: tf.image.resize(inputs[0], tf.shape(inputs[1])[1:3]))([sigmoid_xg, input_x])\n\n    multiply_xg = layers.Multiply()([input_x, upsample_psi])\n    return multiply_xg\n\n# Model blocks\ndef residual_block(x, filters, kernel_size=(3, 3)):\n    shortcut = layers.Conv2D(filters, (1, 1), padding=\"same\")(x)\n    x = layers.Conv2D(filters, kernel_size, padding=\"same\")(x)\n    x = layers.BatchNormalization()(x)\n    x = layers.ReLU()(x)\n    x = layers.Conv2D(filters, kernel_size, padding=\"same\")(x)\n    x = layers.BatchNormalization()(x)\n    x = layers.Add()([shortcut, x])\n    x = layers.ReLU()(x)\n    return x\n\ndef encoder_block(x, filters, kernel_size):\n    x = residual_block(x, filters, kernel_size)\n    skip = x\n    x = layers.MaxPooling2D((2, 2))(x)\n    return x, skip\n\ndef decoder_block(x, skip, filters, kernel_size):\n    x = layers.Conv2DTranspose(filters, (2, 2), strides=(2, 2), padding=\"same\")(x)\n    attn = attention_block(skip, x, filters // 2)\n    x = layers.Concatenate()([x, attn])\n    x = residual_block(x, filters, kernel_size)\n    return x\n\ndef CellSegUNet(input_shape, kernel_size):\n    inputs = layers.Input(input_shape)\n    s = Lambda(lambda x: x / 255)(inputs)\n    x1, skip1 = encoder_block(s, 32, kernel_size)\n    x2, skip2 = encoder_block(x1, 64, kernel_size)\n    x3, skip3 = encoder_block(x2, 128, kernel_size)\n    x4, skip4 = encoder_block(x3, 256, kernel_size)\n    bottleneck = residual_block(x4, 512, kernel_size)\n    x = decoder_block(bottleneck, skip4, 256, kernel_size)\n    x = decoder_block(x, skip3, 128, kernel_size)\n    x = decoder_block(x, skip2, 64, kernel_size)\n    x = decoder_block(x, skip1, 32, kernel_size)\n    outputs = layers.Conv2D(1, (1, 1), activation='sigmoid')(x)\n    return Model(inputs, outputs)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:30:43.865917Z","iopub.execute_input":"2025-05-16T05:30:43.866296Z","iopub.status.idle":"2025-05-16T05:30:43.878041Z","shell.execute_reply.started":"2025-05-16T05:30:43.866260Z","shell.execute_reply":"2025-05-16T05:30:43.877003Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"FILTERS = [32, 64, 128, 256]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:30:43.878870Z","iopub.execute_input":"2025-05-16T05:30:43.879149Z","iopub.status.idle":"2025-05-16T05:30:43.897049Z","shell.execute_reply.started":"2025-05-16T05:30:43.879128Z","shell.execute_reply":"2025-05-16T05:30:43.896218Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def dice_coefficient(y_true, y_pred):\n    y_true = tf.cast(y_true, tf.float32)\n    y_pred = tf.cast(y_pred, tf.float32)\n    numerator = 2 * tf.reduce_sum(y_true * y_pred)\n    denominator = tf.reduce_sum(y_true + y_pred)\n    return numerator / (denominator + tf.keras.backend.epsilon())\n\ndef iou(y_true, y_pred):\n    y_true = tf.cast(y_true, tf.float32)\n    y_pred = tf.cast(y_pred, tf.float32)\n    intersection = tf.reduce_sum(y_true * y_pred)\n    union = tf.reduce_sum(y_true + y_pred - y_true * y_pred)\n    return intersection / (union + tf.keras.backend.epsilon())\n\ndef bce_dice_loss(y_true, y_pred):\n    bce = tf.keras.losses.BinaryCrossentropy()(y_true, y_pred)\n    dice = 1 - dice_coefficient(y_true, y_pred)\n    return bce + dice\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:30:43.897792Z","iopub.execute_input":"2025-05-16T05:30:43.898056Z","iopub.status.idle":"2025-05-16T05:30:43.911383Z","shell.execute_reply.started":"2025-05-16T05:30:43.898036Z","shell.execute_reply":"2025-05-16T05:30:43.910718Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(pd.DataFrame(results))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras import layers, Model\nfrom tensorflow.keras.layers import Lambda\n\ncombinations = [((3, 3), 'adam'), ((5, 5), 'adam'), ((7, 7), 'adam'), ((7, 7), 'sgd'), ((3, 3), 'sgd'), ((5, 5), 'sgd'),((3, 3), 'rmsprop'), ((5, 5), 'rmsprop'), ((7, 7), 'rmsprop'), ((3, 3), 'adagrad'), ((5, 5), 'adagrad'), ((7, 7), 'adagrad')]\nresults = []\n\ninput_shape = (IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS)\n\nfor kernel_size, optimizer_name in combinations:\n    print(f\"\\nTraining with kernel_size={kernel_size}, optimizer={optimizer_name}\")\n    model = CellSegUNet(input_shape, kernel_size)\n    model.summary()\n\n    if optimizer_name == 'adam':\n        optimizer = tf.keras.optimizers.Adam(learning_rate=1e-4)\n    elif optimizer_name == 'sgd':\n        optimizer = tf.keras.optimizers.SGD(learning_rate=1e-2, momentum=0.9)\n    elif optimizer_name == 'rmsprop':\n        optimizer = tf.keras.optimizers.RMSprop(learning_rate=1e-4)\n    elif optimizer_name == 'adagrad':\n        optimizer = tf.keras.optimizers.Adagrad(learning_rate=1e-2)\n    model.compile(optimizer=optimizer, loss=bce_dice_loss, metrics=[dice_coefficient, iou])\n    history = model.fit(train_dataset, steps_per_epoch=len(X_train) // 16, epochs=15, verbose=1)\n    results.append({\n            \"kernel_size\": kernel_size,\n            \"optimizer\": optimizer_name,\n            \"dice\": history.history['dice_coefficient'][-1],\n            \"iou\": history.history['iou'][-1]\n        })\nprint(pd.DataFrame(results))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-16T05:33:35.643546Z","iopub.execute_input":"2025-05-16T05:33:35.644070Z"}},"outputs":[],"execution_count":null}]}