{"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":"gpu","dataSources":[{"sourceId":8089,"databundleVersionId":44321,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-05-13T13:49:29.505151Z","iopub.execute_input":"2024-05-13T13:49:29.506418Z","iopub.status.idle":"2024-05-13T13:49:29.901163Z","shell.execute_reply.started":"2024-05-13T13:49:29.506375Z","shell.execute_reply":"2024-05-13T13:49:29.900213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nfrom tqdm import tqdm\nfrom skimage.io import imread, imshow\nfrom skimage.transform import resize\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport zipfile\nimport random\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-05-13T13:49:29.903291Z","iopub.execute_input":"2024-05-13T13:49:29.904098Z","iopub.status.idle":"2024-05-13T13:49:44.040983Z","shell.execute_reply.started":"2024-05-13T13:49:29.904057Z","shell.execute_reply":"2024-05-13T13:49:44.039932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data","metadata":{}},{"cell_type":"code","source":"SEED = 42\nnp.random.seed(SEED)\n\n# Path\nUNZIP_PATH = '../input/data-science-bowl-2018/'\nTRAIN_PATH = './train/'\nTEST_PATH = './test/'\n\n# Image parameters\nIMG_WIDTH = 128\nIMG_HEIGHT = 128\nIMG_CHANNELS = 3","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:49:44.042184Z","iopub.execute_input":"2024-05-13T13:49:44.042878Z","iopub.status.idle":"2024-05-13T13:49:44.048722Z","shell.execute_reply.started":"2024-05-13T13:49:44.042844Z","shell.execute_reply":"2024-05-13T13:49:44.047792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(UNZIP_PATH + 'stage1_train.zip', 'r') as zip_ref:\n    zip_ref.extractall('./train')\nwith zipfile.ZipFile(UNZIP_PATH + 'stage1_test.zip', 'r') as zip_ref:\n    zip_ref.extractall('./test')","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:49:44.050963Z","iopub.execute_input":"2024-05-13T13:49:44.051277Z","iopub.status.idle":"2024-05-13T13:49:49.136231Z","shell.execute_reply.started":"2024-05-13T13:49:44.051251Z","shell.execute_reply":"2024-05-13T13:49:49.135193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ids = next(os.walk(TRAIN_PATH))[1]\ntest_ids = next(os.walk(TEST_PATH))[1]","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:49:49.137615Z","iopub.execute_input":"2024-05-13T13:49:49.138037Z","iopub.status.idle":"2024-05-13T13:49:49.145754Z","shell.execute_reply.started":"2024-05-13T13:49:49.138000Z","shell.execute_reply":"2024-05-13T13:49:49.144618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_data():\n    train_ids = next(os.walk(TRAIN_PATH))[1]\n    X = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype = np.uint8)\n    y = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, 1), dtype=bool)\n\n    for n, id_ in enumerate(train_ids):\n        path = TRAIN_PATH + id_\n        img = imread(path + '/images/' + id_ + '.png')[:, :, :IMG_CHANNELS]\n\n        # Apply CLAHE to the image\n        img = cv2.cvtColor(img, cv2.COLOR_RGB2LAB)  # Convert to LAB color space\n        l, a, b = cv2.split(img)\n        clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))\n        l = clahe.apply(l)\n        img = cv2.merge((l, a, b))\n        img = cv2.cvtColor(img, cv2.COLOR_LAB2RGB)  # Convert back to RGB\n\n        img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n        X[n] = img\n\n        mask = np.zeros((IMG_HEIGHT, IMG_WIDTH, 1), dtype=bool)\n        for mask_file in next(os.walk(path + '/masks/'))[2]:\n            mask_ = imread(path + '/masks/' + mask_file)\n            mask_ = np.expand_dims(resize(mask_, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True), axis=-1)\n            mask = np.maximum(mask, mask_)\n        y[n] = mask\n\n    return X, y","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:49:49.147557Z","iopub.execute_input":"2024-05-13T13:49:49.148041Z","iopub.status.idle":"2024-05-13T13:49:49.228781Z","shell.execute_reply.started":"2024-05-13T13:49:49.147998Z","shell.execute_reply":"2024-05-13T13:49:49.227656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X, y = load_data()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:49:49.230360Z","iopub.execute_input":"2024-05-13T13:49:49.230796Z","iopub.status.idle":"2024-05-13T13:54:28.476815Z","shell.execute_reply.started":"2024-05-13T13:49:49.230759Z","shell.execute_reply":"2024-05-13T13:54:28.475828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:28.478465Z","iopub.execute_input":"2024-05-13T13:54:28.478906Z","iopub.status.idle":"2024-05-13T13:54:28.486460Z","shell.execute_reply.started":"2024-05-13T13:54:28.478850Z","shell.execute_reply":"2024-05-13T13:54:28.485463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:28.487791Z","iopub.execute_input":"2024-05-13T13:54:28.488073Z","iopub.status.idle":"2024-05-13T13:54:28.497681Z","shell.execute_reply.started":"2024-05-13T13:54:28.488049Z","shell.execute_reply":"2024-05-13T13:54:28.496818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot the first five images from X","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\nfor i in range(5):\n    plt.subplot(1, 5, i+1)\n    plt.imshow(X[i])\n    plt.axis('off')\n    plt.title(f'Image {i}')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:28.501803Z","iopub.execute_input":"2024-05-13T13:54:28.502089Z","iopub.status.idle":"2024-05-13T13:54:29.075302Z","shell.execute_reply.started":"2024-05-13T13:54:28.502056Z","shell.execute_reply":"2024-05-13T13:54:29.074300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot the first five masks from y","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\nfor i in range(5):\n    plt.subplot(1, 5, i+1)\n    plt.imshow(np.squeeze(y[i]), cmap='gray')\n    plt.axis('off')\n    plt.title(f'Mask {i}')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:29.076484Z","iopub.execute_input":"2024-05-13T13:54:29.076803Z","iopub.status.idle":"2024-05-13T13:54:29.604524Z","shell.execute_reply.started":"2024-05-13T13:54:29.076776Z","shell.execute_reply":"2024-05-13T13:54:29.603544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot the last five images from X","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\nfor i in range(-5, 0):\n    plt.subplot(1, 5, i+6)\n    plt.imshow(X[i])\n    plt.axis('off')\n    plt.title(f'Image {len(X) + i}')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:29.605840Z","iopub.execute_input":"2024-05-13T13:54:29.606222Z","iopub.status.idle":"2024-05-13T13:54:30.173623Z","shell.execute_reply.started":"2024-05-13T13:54:29.606185Z","shell.execute_reply":"2024-05-13T13:54:30.172578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot the last five masks from y","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\nfor i in range(-5, 0):\n    plt.subplot(1, 5, i+6)\n    plt.imshow(np.squeeze(y[i]), cmap='gray')\n    plt.axis('off')\n    plt.title(f'Mask {len(y) + i}')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:30.175096Z","iopub.execute_input":"2024-05-13T13:54:30.175873Z","iopub.status.idle":"2024-05-13T13:54:30.944225Z","shell.execute_reply.started":"2024-05-13T13:54:30.175832Z","shell.execute_reply":"2024-05-13T13:54:30.943211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Unet","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, Conv2DTranspose, concatenate, BatchNormalization, Dropout\nfrom tensorflow.keras.initializers import glorot_uniform\n\ndef unet_model(input_size=(IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS)):\n    inputs = Input(input_size)\n\n    # Contraction Path\n    conv1 = Conv2D(64, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(inputs)\n    conv1 = BatchNormalization()(conv1)\n    conv1 = Conv2D(64, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(conv1)\n    conv1 = BatchNormalization()(conv1)\n    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)\n\n    conv2 = Conv2D(128, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(pool1)\n    conv2 = BatchNormalization()(conv2)\n    conv2 = Conv2D(128, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(conv2)\n    conv2 = BatchNormalization()(conv2)\n    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)\n\n    conv3 = Conv2D(256, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(pool2)\n    conv3 = BatchNormalization()(conv3)\n    conv3 = Conv2D(256, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(conv3)\n    conv3 = BatchNormalization()(conv3)\n    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)\n\n    conv4 = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(pool3)\n    conv4 = BatchNormalization()(conv4)\n    conv4 = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(conv4)\n    conv4 = BatchNormalization()(conv4)\n    drop4 = Dropout(0.5)(conv4)\n    pool4 = MaxPooling2D(pool_size=(2, 2))(drop4)\n\n    # Bottleneck\n    conv5 = Conv2D(1024, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(pool4)\n    conv5 = BatchNormalization()(conv5)\n    conv5 = Conv2D(1024, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(conv5)\n    conv5 = BatchNormalization()(conv5)\n    drop5 = Dropout(0.5)(conv5)\n\n    # Expansion Path\n    up6 = Conv2DTranspose(512, (2, 2), strides=(2, 2), padding='same')(drop5)\n    concat6 = concatenate([up6, drop4])\n    conv6 = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(concat6)\n    conv6 = BatchNormalization()(conv6)\n    conv6 = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(conv6)\n    conv6 = BatchNormalization()(conv6)\n\n    up7 = Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv6)\n    concat7 = concatenate([up7, conv3])\n    conv7 = Conv2D(256, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(concat7)\n    conv7 = BatchNormalization()(conv7)\n    conv7 = Conv2D(256, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(conv7)\n    conv7 = BatchNormalization()(conv7)\n\n    up8 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv7)\n    concat8 = concatenate([up8, conv2])\n    conv8 = Conv2D(128, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(concat8)\n    conv8 = BatchNormalization()(conv8)\n    conv8 = Conv2D(128, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(conv8)\n    conv8 = BatchNormalization()(conv8)\n\n    up9 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv8)\n    concat9 = concatenate([up9, conv1])\n    conv9 = Conv2D(64, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(concat9)\n    conv9 = BatchNormalization()(conv9)\n    conv9 = Conv2D(64, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform')(conv9)\n    conv9 = BatchNormalization()(conv9)\n\n    # Output Layer\n    outputs = Conv2D(1, (1, 1), activation='sigmoid')(conv9)\n    model = Model(inputs=[inputs], outputs=[outputs])\n    return model\n","metadata":{"execution":{"iopub.status.busy":"2024-05-13T14:17:36.796708Z","iopub.execute_input":"2024-05-13T14:17:36.797149Z","iopub.status.idle":"2024-05-13T14:17:36.823358Z","shell.execute_reply.started":"2024-05-13T14:17:36.797116Z","shell.execute_reply":"2024-05-13T14:17:36.822333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define StratifiedKFold","metadata":{}},{"cell_type":"code","source":"# Flatten the image data\nX_flat = X.reshape(-1, X.shape[-1])\n# Flatten the mask data\ny_flat = y.reshape(-1, y.shape[-1])\n# Check the new shapes\nprint(\"Flattened X shape:\", X_flat.shape)\nprint(\"Flattened y shape:\", y_flat.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-13T14:02:28.877664Z","iopub.execute_input":"2024-05-13T14:02:28.878082Z","iopub.status.idle":"2024-05-13T14:02:28.884856Z","shell.execute_reply.started":"2024-05-13T14:02:28.878053Z","shell.execute_reply":"2024-05-13T14:02:28.883829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check unique values in y_flat\nunique_values = np.unique(y_flat)\nprint(\"Unique values in y_flat:\", unique_values)\n# If there are values other than 0 and 1, you might need to threshold the mask\ny_flat_thresholded = np.where(y_flat > 0, 1, 0)\n# Check the unique values after thresholding\nunique_values_thresholded = np.unique(y_flat_thresholded)\nprint(\"Unique values in y_flat_thresholded:\", unique_values_thresholded)","metadata":{"execution":{"iopub.status.busy":"2024-05-13T14:02:31.747847Z","iopub.execute_input":"2024-05-13T14:02:31.748564Z","iopub.status.idle":"2024-05-13T14:02:32.189386Z","shell.execute_reply.started":"2024-05-13T14:02:31.748535Z","shell.execute_reply":"2024-05-13T14:02:32.188335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import StratifiedKFold\nfrom tensorflow.keras.callbacks import EarlyStopping,ReduceLROnPlateau\nfrom keras.optimizers import Adam\nfrom tensorflow.keras.metrics import MeanIoU\nfrom tensorflow.keras.losses import BinaryCrossentropy\n\nn_splits = 5\nskf = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=42)\nval_loss_list = []\nval_accuracy_list = []\nval_mean_iou_list = []\n# Loop through the folds\nfor i, (train_index, test_index) in enumerate(skf.split(X_flat, y_flat_thresholded), 1):\n    print(f\"\\n\\n----------Mulai Fold {i}:----------\\n\")\n    print(f\"Train indices: {train_index}\")\n    print(f\"Test indices: {test_index}\")\n    print(\"----------------------\")\n    X_train, X_test = X_flat[train_index], X_flat[test_index]\n    y_train, y_test = y_flat_thresholded[train_index], y_flat_thresholded[test_index]\n    # Reshape input data to match the model's input shape\n    X_train = X_train.reshape(-1, IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS)\n    X_test = X_test.reshape(-1, IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS)\n    y_train = y_train.reshape(-1, IMG_WIDTH, IMG_HEIGHT, 1)\n    y_test = y_test.reshape(-1, IMG_WIDTH, IMG_HEIGHT, 1)\n    model = unet_model(input_size=(IMG_WIDTH,IMG_HEIGHT,IMG_CHANNELS))\n    # Inisialisasi fungsi loss\n    bce_loss = BinaryCrossentropy()\n    optimizer = Adam(learning_rate=0.0001)\n    model.compile(optimizer=optimizer, loss=bce_loss, metrics=['accuracy', MeanIoU(num_classes=2, name='mean_iou')])\n    early_stopping = EarlyStopping(monitor='val_loss', patience=5, \n                                     restore_best_weights=True)\n    reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, \n                                    patience=5, min_lr=0.00001)\n    # Fit the model\n    history = model.fit(\n                X_train,\n                y_train,\n                validation_data=(X_test, y_test),\n                batch_size=32,\n                epochs=50,\n                callbacks=[early_stopping,reduce_lr]\n                )\n    # Evaluate the model\n    eval_results = model.evaluate(X_test, y_test)\n    val_loss, val_accuracy, val_mean_iou = eval_results[0], eval_results[1], eval_results[2]\n    print(f\"\\nloss : {val_loss}\")\n    print(f\"accuracy : {val_accuracy}\")\n    print(f\"mean iou : {val_mean_iou}\")\n    val_loss_list.append(val_loss)\n    val_accuracy_list.append(val_accuracy)\n    val_mean_iou_list.append(val_mean_iou)\n    print(f\"\\n----------Akhir dari fold {i}----------\\n\\n\")","metadata":{"execution":{"iopub.status.busy":"2024-05-13T14:32:32.234565Z","iopub.execute_input":"2024-05-13T14:32:32.235001Z","iopub.status.idle":"2024-05-13T14:38:31.903034Z","shell.execute_reply.started":"2024-05-13T14:32:32.234969Z","shell.execute_reply":"2024-05-13T14:38:31.901902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, (loss, accuracy, iou) in enumerate(zip(val_loss_list, val_accuracy_list, val_mean_iou_list), 1):\n    print(f\"Fold {i}: \\n Loss={loss}\\n Accuracy={accuracy}\\n Mean IoU={iou}\\n\")","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-05-13T14:31:17.112268Z","iopub.execute_input":"2024-05-13T14:31:17.113510Z","iopub.status.idle":"2024-05-13T14:31:17.119776Z","shell.execute_reply.started":"2024-05-13T14:31:17.113464Z","shell.execute_reply":"2024-05-13T14:31:17.118636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculate average metrics\navg_val_loss = np.mean(val_loss_list)\navg_val_accuracy = np.mean(val_accuracy_list)\navg_val_mean_iou = np.mean(val_mean_iou_list)\n\nprint(\"Average Validation Loss:\", avg_val_loss)\nprint(\"Average Validation Accuracy:\", avg_val_accuracy)\nprint(\"Average Validation Mean IoU:\", avg_val_mean_iou)","metadata":{"execution":{"iopub.status.busy":"2024-05-13T14:44:37.613050Z","iopub.execute_input":"2024-05-13T14:44:37.613841Z","iopub.status.idle":"2024-05-13T14:44:37.620475Z","shell.execute_reply.started":"2024-05-13T14:44:37.613801Z","shell.execute_reply":"2024-05-13T14:44:37.619512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_train = model.predict(X_train, verbose=1)\npreds_val = model.predict(X_test, verbose=1)\n# Convert predictions to binary format\npreds_train_t = (preds_train > 0.5).astype(np.uint8)\npreds_val_t = (preds_val > 0.5).astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2024-05-13T14:12:50.905951Z","iopub.execute_input":"2024-05-13T14:12:50.906333Z","iopub.status.idle":"2024-05-13T14:12:52.872809Z","shell.execute_reply.started":"2024-05-13T14:12:50.906303Z","shell.execute_reply":"2024-05-13T14:12:52.871920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculate IoU, Dice Coefficient, and Pixel Accuracy on training set\ntrain_iou = MeanIoU(num_classes=2)\ntrain_iou.update_state(y_train, preds_train_t)\ntrain_dice = 2 * train_iou.result().numpy() / (train_iou.result().numpy() + 1)\ntrain_pixel_accuracy = accuracy_score(y_train.flatten(), preds_train_t.flatten())\n\nprint(f'Training IoU: {train_iou.result().numpy():.4f}')\nprint(f'Training Dice Coefficient: {train_dice:.4f}')\nprint(f'Training Pixel Accuracy: {train_pixel_accuracy*100:.2f}%')","metadata":{"execution":{"iopub.status.busy":"2024-05-13T14:12:56.390433Z","iopub.execute_input":"2024-05-13T14:12:56.391597Z","iopub.status.idle":"2024-05-13T14:12:57.303257Z","shell.execute_reply.started":"2024-05-13T14:12:56.391559Z","shell.execute_reply":"2024-05-13T14:12:57.302234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculate IoU, Dice Coefficient, and Pixel Accuracy on validation set\nval_iou = MeanIoU(num_classes=2)\nval_iou.update_state(y_test, preds_val_t)\nval_dice = 2 * val_iou.result().numpy() / (val_iou.result().numpy() + 1)\nval_pixel_accuracy = accuracy_score(y_test.flatten(), preds_val_t.flatten())\n\nprint(f'Validation IoU: {val_iou.result().numpy():.4f}')\nprint(f'Validation Dice Coefficient: {val_dice:.4f}')\nprint(f'Validation Pixel Accuracy: {val_pixel_accuracy*100:.2f}')","metadata":{"execution":{"iopub.status.busy":"2024-05-13T14:13:03.301529Z","iopub.execute_input":"2024-05-13T14:13:03.302470Z","iopub.status.idle":"2024-05-13T14:13:03.512415Z","shell.execute_reply.started":"2024-05-13T14:13:03.302434Z","shell.execute_reply":"2024-05-13T14:13:03.511403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loss Curve","metadata":{}},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Loss Curve')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T14:31:36.872074Z","iopub.execute_input":"2024-05-13T14:31:36.872478Z","iopub.status.idle":"2024-05-13T14:31:37.208604Z","shell.execute_reply.started":"2024-05-13T14:31:36.872446Z","shell.execute_reply":"2024-05-13T14:31:37.207584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Accuracy Curve","metadata":{}},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Accuracy Curve')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T14:31:42.163173Z","iopub.execute_input":"2024-05-13T14:31:42.163898Z","iopub.status.idle":"2024-05-13T14:31:42.503633Z","shell.execute_reply.started":"2024-05-13T14:31:42.163864Z","shell.execute_reply":"2024-05-13T14:31:42.502715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# IoU Curve","metadata":{}},{"cell_type":"code","source":"plt.plot(history.history['mean_iou'], label='Mean IoU (Training)')\nplt.plot(history.history['val_mean_iou'], label='Mean IoU (Validation)')\nplt.title('IoU Curve')\nplt.xlabel('Epochs')\nplt.ylabel('IoU')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T14:31:46.851623Z","iopub.execute_input":"2024-05-13T14:31:46.852544Z","iopub.status.idle":"2024-05-13T14:31:47.137195Z","shell.execute_reply.started":"2024-05-13T14:31:46.852508Z","shell.execute_reply":"2024-05-13T14:31:47.136000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Summary","metadata":{}},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:34.453012Z","iopub.status.idle":"2024-05-13T13:54:34.453359Z","shell.execute_reply.started":"2024-05-13T13:54:34.453186Z","shell.execute_reply":"2024-05-13T13:54:34.453203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Architecture","metadata":{}},{"cell_type":"code","source":"from keras.utils import plot_model\nfile_name = 'arsitektur_model.png'\nplot_model(model, to_file=file_name, show_shapes=True, show_layer_names=True)\nplt.figure(figsize=(70,70))\nimg = plt.imread(file_name)\nplt.imshow(img)\nplt.title('Arsitektur Model', fontsize=18) \nplt.axis('off') \nplt.savefig(file_name) \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:34.454454Z","iopub.status.idle":"2024-05-13T13:54:34.454821Z","shell.execute_reply.started":"2024-05-13T13:54:34.454613Z","shell.execute_reply":"2024-05-13T13:54:34.454628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Confusion Matrix","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport seaborn as sns\n\n# Convert predictions to binary format\npreds_val_t = (preds_val > 0.5).astype(np.uint8)\n\n# Flatten the true and predicted labels\ny_true_flat = y_test.flatten()\ny_pred_flat = preds_val_t.flatten()\n\n# Create confusion matrix\ncm = confusion_matrix(y_true_flat, y_pred_flat)\n\n# Plot confusion matrix\nplt.figure(figsize=(8, 6))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', cbar=False)\nplt.title('Confusion Matrix')\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:34.457206Z","iopub.status.idle":"2024-05-13T13:54:34.457542Z","shell.execute_reply.started":"2024-05-13T13:54:34.457383Z","shell.execute_reply":"2024-05-13T13:54:34.457398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_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)\n\npreds_train_t = (preds_train > 0.5).astype(np.uint8)\npreds_val_t = (preds_val > 0.5).astype(np.uint8)\n\n# Function to display images\ndef display_images(image):\n    plt.imshow(image)\n    plt.axis('off')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:34.458485Z","iopub.status.idle":"2024-05-13T13:54:34.458857Z","shell.execute_reply.started":"2024-05-13T13:54:34.458648Z","shell.execute_reply":"2024-05-13T13:54:34.458685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# A random index from training set","metadata":{}},{"cell_type":"code","source":"ix = random.randint(0, len(preds_train_t))\nprint(\"Input Image:\")\ndisplay_images(X_train[ix])\nprint(\"Ground Truth Mask:\")\ndisplay_images(np.squeeze(y_train[ix]))\nprint(\"Predicted Mask:\")\ndisplay_images(np.squeeze(preds_train_t[ix]))","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:34.460425Z","iopub.status.idle":"2024-05-13T13:54:34.460837Z","shell.execute_reply.started":"2024-05-13T13:54:34.460606Z","shell.execute_reply":"2024-05-13T13:54:34.460623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# A random index from validation set","metadata":{}},{"cell_type":"code","source":"ix = random.randint(0, len(preds_val_t))\nval_data_start = int(X_train.shape[0] * 0.9)\nprint(\"Validation Input Image:\")\ndisplay_images(X_train[val_data_start:][ix])\nprint(\"Validation Ground Truth Mask:\")\ndisplay_images(np.squeeze(y_train[val_data_start:][ix]))\nprint(\"Validation Predicted Mask:\")\ndisplay_images(np.squeeze(preds_val_t[ix]))","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:34.462848Z","iopub.status.idle":"2024-05-13T13:54:34.463337Z","shell.execute_reply.started":"2024-05-13T13:54:34.463076Z","shell.execute_reply":"2024-05-13T13:54:34.463100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# X_test","metadata":{}},{"cell_type":"code","source":"X_test = np.zeros((len(test_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\nsizes_test = []\nfor n, id_ in tqdm(enumerate(test_ids), total=len(test_ids)):\n    path = TEST_PATH + id_\n    img = imread(path + '/images/' + id_ + '.png')[:, :, :IMG_CHANNELS]\n    sizes_test.append([img.shape[0], img.shape[1]])\n\n    # Apply CLAHE to the image\n    img = cv2.cvtColor(img, cv2.COLOR_RGB2LAB)  # Convert to LAB color space\n    l, a, b = cv2.split(img)\n    clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))\n    l = clahe.apply(l)\n    img = cv2.merge((l, a, b))\n    img = cv2.cvtColor(img, cv2.COLOR_LAB2RGB)  # Convert back to RGB\n\n    img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n    X_test[n] = img","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:34.464372Z","iopub.status.idle":"2024-05-13T13:54:34.464885Z","shell.execute_reply.started":"2024-05-13T13:54:34.464622Z","shell.execute_reply":"2024-05-13T13:54:34.464645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:34.466849Z","iopub.status.idle":"2024-05-13T13:54:34.467181Z","shell.execute_reply.started":"2024-05-13T13:54:34.467018Z","shell.execute_reply":"2024-05-13T13:54:34.467033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot the first five images from X_test","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\nfor i in range(5):\n    plt.subplot(1, 5, i+1)\n    plt.imshow(X_test[i])\n    plt.axis('off')\n    plt.title(f'Image {i}')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:34.468306Z","iopub.status.idle":"2024-05-13T13:54:34.468705Z","shell.execute_reply.started":"2024-05-13T13:54:34.468493Z","shell.execute_reply":"2024-05-13T13:54:34.468510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot the last five images from X_test","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\nfor i in range(-5, 0):\n    plt.subplot(1, 5, i+6)\n    plt.imshow(X_test[i])\n    plt.axis('off')\n    plt.title(f'Image {len(X_test) + i}')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:34.470164Z","iopub.status.idle":"2024-05-13T13:54:34.470515Z","shell.execute_reply.started":"2024-05-13T13:54:34.470342Z","shell.execute_reply":"2024-05-13T13:54:34.470359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_test = model.predict(X_test, verbose=1)\npreds_test_t = (preds_test > 0.5).astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:34.471413Z","iopub.status.idle":"2024-05-13T13:54:34.471763Z","shell.execute_reply.started":"2024-05-13T13:54:34.471571Z","shell.execute_reply":"2024-05-13T13:54:34.471586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# A random index from X_test","metadata":{}},{"cell_type":"code","source":"ix = random.randint(0, len(preds_test_t))\nprint(\"Test Image:\")\ndisplay_images(X_test[ix])\nprint(\"Predicted Mask:\")\ndisplay_images(np.squeeze(preds_test_t[ix]))","metadata":{"execution":{"iopub.status.busy":"2024-05-13T13:54:34.472660Z","iopub.status.idle":"2024-05-13T13:54:34.472995Z","shell.execute_reply.started":"2024-05-13T13:54:34.472823Z","shell.execute_reply":"2024-05-13T13:54:34.472838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"mymodel.h5\")\nmodel.save('my_model.keras')","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-05-13T13:54:34.474458Z","iopub.status.idle":"2024-05-13T13:54:34.474809Z","shell.execute_reply.started":"2024-05-13T13:54:34.474626Z","shell.execute_reply":"2024-05-13T13:54:34.474641Z"},"trusted":true},"execution_count":null,"outputs":[]}]}