{"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":"markdown","source":"**This Notebook is a copy of C0CONUTS \"Unet + ImageDataGenerator LB 0.336\". Also trained vgg16 based unet using segmentation models.** ","metadata":{}},{"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\nbase_dir = \"../input/data-science-bowl-2018\"\ntrain_dir = os.path.join(base_dir, \"stage1_train.zip\")\ntest_dir = os.path.join(base_dir, \"stage1_test.zip\")\n\nimport zipfile\nwith zipfile.ZipFile(train_dir,\"r\") as z:\n    z.extractall(\"./stage1_train\")\n\nwith zipfile.ZipFile(test_dir,\"r\") as z:\n    z.extractall(\"./stage1_test\")\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","execution":{"iopub.status.busy":"2023-10-11T04:27:46.705801Z","iopub.execute_input":"2023-10-11T04:27:46.706630Z","iopub.status.idle":"2023-10-11T04:27:53.805622Z","shell.execute_reply.started":"2023-10-11T04:27:46.706596Z","shell.execute_reply":"2023-10-11T04:27:53.804529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport sys\nimport random\nimport warnings\n\nimport pandas as pd\nimport numpy as np\n\nimport matplotlib.pyplot as plt\n\nfrom tqdm import tqdm\n#tqdm provides a fast and extensible progress bar for loops and other iterable operations\n#tqdm in arabic \"taqaddum\" which means progress\nfrom itertools import chain\n#Python module that provides various functions for working with iterators and iterable objects efficiently\nfrom skimage.io import imread,imshow,imread_collection,concatenate_images\nfrom skimage.transform import resize\n# from skimage.morphology import label\nfrom skimage import img_as_ubyte\n#to convert an image from a floating-point representation to an unsigned byte representation \n\nfrom keras.models import Model,load_model\nfrom keras.layers import Input,Dropout,Lambda,Conv2D,Conv2DTranspose,MaxPooling2D,concatenate\nfrom keras.callbacks import EarlyStopping,ModelCheckpoint\nfrom keras.preprocessing import image\nfrom keras import backend as K\n\nimport tensorflow as tf\n\nBATCH_SIZE = 10\nIMG_WIDTH = 128\nIMG_HEIGHT = 128\nIMG_CHANNEL = 3\nTRAIN_PATH = \"./stage1_train/\"\nTEST_PATH = \"./stage1_test/\"\n\nwarnings.filterwarnings(\"ignore\")\nseed = 42","metadata":{"execution":{"iopub.status.busy":"2023-10-11T04:27:53.808190Z","iopub.execute_input":"2023-10-11T04:27:53.809052Z","iopub.status.idle":"2023-10-11T04:28:07.898080Z","shell.execute_reply.started":"2023-10-11T04:27:53.809014Z","shell.execute_reply":"2023-10-11T04:28:07.897158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# os.walk(path)\nTo iterate through the path and find all files and directories","metadata":{}},{"cell_type":"code","source":"train=next(os.walk(TRAIN_PATH))\nprint(train[0])","metadata":{"execution":{"iopub.status.busy":"2023-10-11T04:28:07.899206Z","iopub.execute_input":"2023-10-11T04:28:07.899847Z","iopub.status.idle":"2023-10-11T04:28:07.906376Z","shell.execute_reply.started":"2023-10-11T04:28:07.899812Z","shell.execute_reply":"2023-10-11T04:28:07.905276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train[1][50])\n","metadata":{"execution":{"iopub.status.busy":"2023-10-11T04:28:07.907876Z","iopub.execute_input":"2023-10-11T04:28:07.908546Z","iopub.status.idle":"2023-10-11T04:28:07.916409Z","shell.execute_reply.started":"2023-10-11T04:28:07.908517Z","shell.execute_reply":"2023-10-11T04:28:07.915347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train[2])","metadata":{"execution":{"iopub.status.busy":"2023-10-11T04:28:07.919301Z","iopub.execute_input":"2023-10-11T04:28:07.919603Z","iopub.status.idle":"2023-10-11T04:28:07.928344Z","shell.execute_reply.started":"2023-10-11T04:28:07.919583Z","shell.execute_reply":"2023-10-11T04:28:07.927450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path='./stage1_train/6d327ab4f0e3604fa6e9b8041c7e6db86ab809890d886c691f6e59c9168b7fbe/'","metadata":{"execution":{"iopub.status.busy":"2023-10-11T04:28:07.929661Z","iopub.execute_input":"2023-10-11T04:28:07.930286Z","iopub.status.idle":"2023-10-11T04:28:07.940293Z","shell.execute_reply.started":"2023-10-11T04:28:07.930258Z","shell.execute_reply":"2023-10-11T04:28:07.939311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t=next(os.walk(path))","metadata":{"execution":{"iopub.status.busy":"2023-10-11T04:28:07.941503Z","iopub.execute_input":"2023-10-11T04:28:07.942324Z","iopub.status.idle":"2023-10-11T04:28:07.952294Z","shell.execute_reply.started":"2023-10-11T04:28:07.942296Z","shell.execute_reply":"2023-10-11T04:28:07.951438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(t[1])","metadata":{"execution":{"iopub.status.busy":"2023-10-11T04:28:07.953583Z","iopub.execute_input":"2023-10-11T04:28:07.954391Z","iopub.status.idle":"2023-10-11T04:28:07.963791Z","shell.execute_reply.started":"2023-10-11T04:28:07.954361Z","shell.execute_reply":"2023-10-11T04:28:07.962780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(t[2])","metadata":{"execution":{"iopub.status.busy":"2023-10-11T04:28:07.965128Z","iopub.execute_input":"2023-10-11T04:28:07.966285Z","iopub.status.idle":"2023-10-11T04:28:07.974824Z","shell.execute_reply.started":"2023-10-11T04:28:07.966256Z","shell.execute_reply":"2023-10-11T04:28:07.973763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\ndef print_files_in_subfolders(path):\n    # Get the list of subdirectories in the current directory\n    subdirectories = [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))]\n\n    # Iterate through the subdirectories\n    for subdir in subdirectories:\n        subdir_path = os.path.join(path, subdir)\n\n        # Get the list of files in the subdirectory\n        files = [f for f in os.listdir(subdir_path) if os.path.isfile(os.path.join(subdir_path, f))]\n\n        # Print the files in the subdirectory\n        print(len(files))\n        for file in files:\n            print(os.path.join(subdir, file))\n\n# Specify the path to the directory containing the two subfolders\nstart_path = path\n\n# Call the function to print all files in the subfolders\nprint_files_in_subfolders(start_path)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-11T04:28:07.976165Z","iopub.execute_input":"2023-10-11T04:28:07.976937Z","iopub.status.idle":"2023-10-11T04:28:07.988611Z","shell.execute_reply.started":"2023-10-11T04:28:07.976909Z","shell.execute_reply":"2023-10-11T04:28:07.987693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport matplotlib.pyplot as plt\nimport cv2  # OpenCV for image processing\n\n# Define the path to the folder containing the images and masks\nfolder_path = path\n\n# Define paths to the 'images' and 'masks' folders\nimages_folder = os.path.join(folder_path, 'images')\nmasks_folder = os.path.join(folder_path, 'masks')\n\n# List the image and mask files\nimage_files = os.listdir(images_folder)\nmask_files = os.listdir(masks_folder)\n\n# Calculate the number of rows and columns based on the number of files and a maximum of 4 columns\nmax_columns = 4\nnum_cols = min(len(image_files) + len(mask_files), max_columns)\nnum_rows = (len(image_files) + len(mask_files) + num_cols - 1) // num_cols\n\n# Calculate the figure size based on the number of columns\nfig_width = 4 * num_cols\nfig_height = 3 * num_rows\nfig, axes = plt.subplots(num_rows, num_cols, figsize=(fig_width, fig_height))\n\n# Display the images and masks in the subplots\nfor i, image_file in enumerate(image_files):\n    image_path = os.path.join(images_folder, image_file)\n    image = cv2.imread(image_path)\n    \n    row = i // num_cols\n    col = i % num_cols\n    \n    axes[row, col].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n    axes[row, col].set_title(f'Image {i + 1}')\n    axes[row, col].axis('off')\n\nfor i, mask_file in enumerate(mask_files):\n    mask_path = os.path.join(masks_folder, mask_file)\n    mask = cv2.imread(mask_path)\n    \n    row = (i + len(image_files)) // num_cols\n    col = (i + len(image_files)) % num_cols\n    \n    axes[row, col].imshow(mask, cmap='gray')\n    axes[row, col].set_title(f'Mask {i + 1}')\n    axes[row, col].axis('off')\n\n# Hide any remaining empty subplots\nfor i in range(len(image_files) + len(mask_files), num_rows * num_cols):\n    row = i // num_cols\n    col = i % num_cols\n    axes[row, col].axis('off')\n\n# Adjust subplot spacing\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-11T04:28:07.989954Z","iopub.execute_input":"2023-10-11T04:28:07.990599Z","iopub.status.idle":"2023-10-11T04:28:10.634362Z","shell.execute_reply.started":"2023-10-11T04:28:07.990567Z","shell.execute_reply":"2023-10-11T04:28:10.633414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"from matplotlib import pyplot as plt\n%matplotlib inline\n\nimage_path = './stage1_train/6d327ab4f0e3604fa6e9b8041c7e6db86ab809890d886c691f6e59c9168b7fbe/images/6d327ab4f0e3604fa6e9b8041c7e6db86ab809890d886c691f6e59c9168b7fbe.png'\nimg =imread(image_path)\nplt.imshow(img)\nplt.axis('off')  # Optional: Turn off axis labels and ticks\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-11T04:28:10.635413Z","iopub.execute_input":"2023-10-11T04:28:10.635764Z","iopub.status.idle":"2023-10-11T04:28:10.919526Z","shell.execute_reply.started":"2023-10-11T04:28:10.635731Z","shell.execute_reply":"2023-10-11T04:28:10.918691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import pyplot as plt\n%matplotlib inline\n\nimage_path = './stage1_train/6d327ab4f0e3604fa6e9b8041c7e6db86ab809890d886c691f6e59c9168b7fbe/masks/e4fccb0761829d00ec4eaf88e7ad005b8b0c0c01e22d9f48d1cd6369cf3755e8.png'\nimg = imread(image_path)\nplt.imshow(img)\nplt.axis('off')  # Optional: Turn off axis labels and ticks\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T04:28:10.920606Z","iopub.execute_input":"2023-10-11T04:28:10.921470Z","iopub.status.idle":"2023-10-11T04:28:11.023652Z","shell.execute_reply.started":"2023-10-11T04:28:10.921437Z","shell.execute_reply":"2023-10-11T04:28:11.022255Z"},"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]\nnp.random.seed(10)","metadata":{"execution":{"iopub.status.busy":"2023-10-11T04:28:11.029192Z","iopub.execute_input":"2023-10-11T04:28:11.029625Z","iopub.status.idle":"2023-10-11T04:28:11.038505Z","shell.execute_reply.started":"2023-10-11T04:28:11.029585Z","shell.execute_reply":"2023-10-11T04:28:11.037402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.zeros((len(train_ids),IMG_HEIGHT,IMG_WIDTH,IMG_CHANNEL),dtype=np.uint8)\ny_train = np.zeros((len(train_ids),IMG_HEIGHT,IMG_WIDTH,1),dtype=np.bool)\n\nfor n,_id in tqdm(enumerate(train_ids)):\n    path = TRAIN_PATH + _id\n    img = imread(path + \"/images/\" + _id + \".png\")[:,:,:IMG_CHANNEL]\n    img = resize(img,(IMG_HEIGHT,IMG_WIDTH),mode=\"constant\",preserve_range=True)\n    X_train[n] = img\n    mask = np.zeros((IMG_HEIGHT,IMG_WIDTH,1),dtype=np.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)\n                              , axis=-1)\n        mask = np.maximum(mask,mask_)\n    y_train[n] = mask  \n    \nX_test = np.zeros((len(test_ids),IMG_HEIGHT,IMG_WIDTH,IMG_CHANNEL),dtype=np.uint8)\nsizes_test = []\nfor n ,_id in tqdm(enumerate(test_ids)):\n    path = TEST_PATH + _id\n    img = imread(path + \"/images/\" + _id + \".png\")[:,:,:IMG_CHANNEL]\n    sizes_test.append([img.shape[0],img.shape[1]])\n    img = resize(img,(IMG_HEIGHT,IMG_WIDTH),mode=\"constant\",preserve_range=True)\n    X_test[n] = img\n    \nprint(\"Done!!\")","metadata":{"execution":{"iopub.status.busy":"2023-10-11T04:28:11.043357Z","iopub.execute_input":"2023-10-11T04:28:11.044318Z","iopub.status.idle":"2023-10-11T04:32:24.892320Z","shell.execute_reply.started":"2023-10-11T04:28:11.044276Z","shell.execute_reply":"2023-10-11T04:32:24.891410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(y_train[99][:, :, 0], cmap='gray')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-11T04:32:24.893774Z","iopub.execute_input":"2023-10-11T04:32:24.894735Z","iopub.status.idle":"2023-10-11T04:32:25.159084Z","shell.execute_reply.started":"2023-10-11T04:32:24.894696Z","shell.execute_reply":"2023-10-11T04:32:25.158248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Model,load_model\nfrom keras.layers import Input,Dropout,Lambda,Conv2D,Conv2DTranspose,MaxPooling2D,concatenate\nfrom keras.callbacks import EarlyStopping,ModelCheckpoint\nfrom keras.preprocessing import image\nfrom keras import backend as K","metadata":{"execution":{"iopub.status.busy":"2023-10-11T05:02:24.789373Z","iopub.execute_input":"2023-10-11T05:02:24.789752Z","iopub.status.idle":"2023-10-11T05:02:24.794756Z","shell.execute_reply.started":"2023-10-11T05:02:24.789723Z","shell.execute_reply":"2023-10-11T05:02:24.793751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_datagen = image.ImageDataGenerator(shear_range=0.5,rotation_range=50,zoom_range=0.2\n                                         ,width_shift_range=0.2,height_shift_range=0.2,fill_mode=\"reflect\")\nmask_datagen = image.ImageDataGenerator(shear_range=0.5,rotation_range=50,zoom_range=0.2\n                                         ,width_shift_range=0.2,height_shift_range=0.2,fill_mode=\"reflect\")\n\nimage_datagen.fit(X_train[:int(X_train.shape[0]*0.9)],augment=True,seed = seed)\nmask_datagen.fit(y_train[:int(y_train.shape[0]*0.9)],augment=True,seed = seed)\n\nx = image_datagen.flow(X_train[:int(X_train.shape[0]*0.9)],batch_size=BATCH_SIZE,shuffle=True,seed=seed)\ny = mask_datagen.flow(y_train[:int(y_train.shape[0]*0.9)],batch_size=BATCH_SIZE,shuffle=True,seed=seed)\n\nimage_datagen_val = image.ImageDataGenerator()\nmask_datagen_val = image.ImageDataGenerator()\n\nimage_datagen_val.fit(X_train[int(X_train.shape[0]*0.9):],augment=True,seed = seed)\nmask_datagen_val.fit(y_train[int(y_train.shape[0]*0.9):],augment=True,seed = seed)\n\nx_val = image_datagen.flow(X_train[int(X_train.shape[0]*0.9):],batch_size=BATCH_SIZE,shuffle=True,seed=seed)\ny_val = mask_datagen.flow(y_train[int(y_train.shape[0]*0.9):],batch_size=BATCH_SIZE,shuffle=True,seed=seed)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-11T05:02:27.029240Z","iopub.execute_input":"2023-10-11T05:02:27.029949Z","iopub.status.idle":"2023-10-11T05:02:30.364762Z","shell.execute_reply.started":"2023-10-11T05:02:27.029920Z","shell.execute_reply":"2023-10-11T05:02:30.363555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imshow(x.next()[0].astype(np.uint8))\nplt.show()\nimshow(np.squeeze(y.next()[0].astype(np.uint8)))\nplt.show()\nimshow(x_val.next()[0].astype(np.uint8))\nplt.show()\nimshow(np.squeeze(y_val.next()[0].astype(np.uint8)))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T05:02:34.609155Z","iopub.execute_input":"2023-10-11T05:02:34.609827Z","iopub.status.idle":"2023-10-11T05:02:36.175442Z","shell.execute_reply.started":"2023-10-11T05:02:34.609796Z","shell.execute_reply":"2023-10-11T05:02:36.174508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = zip(x,y)\nval_generator = zip(x_val,y_val)","metadata":{"execution":{"iopub.status.busy":"2023-10-11T05:02:39.284124Z","iopub.execute_input":"2023-10-11T05:02:39.284527Z","iopub.status.idle":"2023-10-11T05:02:39.289349Z","shell.execute_reply.started":"2023-10-11T05:02:39.284495Z","shell.execute_reply":"2023-10-11T05:02:39.288329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(train_generator))","metadata":{"execution":{"iopub.status.busy":"2023-10-11T05:02:41.243703Z","iopub.execute_input":"2023-10-11T05:02:41.244192Z","iopub.status.idle":"2023-10-11T05:02:41.249364Z","shell.execute_reply.started":"2023-10-11T05:02:41.244154Z","shell.execute_reply":"2023-10-11T05:02:41.248405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mean_iou(y_true, y_pred):\n    y_true = tf.cast(y_true > 0.5, tf.int32) \n    # values to int >0.5 to 1 and <0.5 to 0\n    y_pred = tf.cast(y_pred > 0.5, tf.int32)\n    \n    # Calculate the intersection (true positives)\n    intersection = tf.reduce_sum(y_true * y_pred)\n    #calculating the total number of true positives (TP) (1's).\n    \n    # Calculate the union (true positives + false positives + false negatives)\n    union = tf.reduce_sum(y_true) + tf.reduce_sum(y_pred) - intersection\n    #(TP + FN) + (TP + FP) - (TP)\n    \n    # Calculate the IoU for each class\n    iou = intersection / union\n    #the proportion of overlap between the predicted and true binary masks.\n    # A higher IoU indicates better segmentation accuracy.\n    \n    \n    return iou","metadata":{"execution":{"iopub.status.busy":"2023-10-11T05:02:42.689227Z","iopub.execute_input":"2023-10-11T05:02:42.690502Z","iopub.status.idle":"2023-10-11T05:02:42.697530Z","shell.execute_reply.started":"2023-10-11T05:02:42.690456Z","shell.execute_reply":"2023-10-11T05:02:42.696476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Model,load_model\nfrom keras.layers import Input,Dropout,Lambda,Conv2D,Conv2DTranspose,MaxPooling2D,concatenate\nfrom keras.callbacks import EarlyStopping,ModelCheckpoint\nfrom keras.preprocessing import image\nfrom keras import backend as K\n\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2023-10-11T05:02:52.419136Z","iopub.execute_input":"2023-10-11T05:02:52.420308Z","iopub.status.idle":"2023-10-11T05:02:52.427048Z","shell.execute_reply.started":"2023-10-11T05:02:52.420265Z","shell.execute_reply":"2023-10-11T05:02:52.425693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Build U-Net model\ninputs = Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNEL))\ns = Lambda(lambda x: x / 255) (inputs)\n\nc1 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (s)\n#defines thhe initial values of weights\n#He_normal  initializes weights in a way that helps with training deep networks by reducing the risk of vanishing or exploding gradients.\nc1 = Dropout(0.1) (c1)\nc1 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (c1)\np1 = MaxPooling2D((2, 2)) (c1)\n\nc2 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (p1)\nc2 = Dropout(0.1) (c2)\nc2 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (c2)\np2 = MaxPooling2D((2, 2)) (c2)\n\nc3 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (p2)\nc3 = Dropout(0.2) (c3)\nc3 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (c3)\np3 = MaxPooling2D((2, 2)) (c3)\n\nc4 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (p3)\nc4 = Dropout(0.2) (c4)\nc4 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (c4)\np4 = MaxPooling2D(pool_size=(2, 2)) (c4)\n\nc5 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (p4)\nc5 = Dropout(0.3) (c5)\nc5 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (c5)\n\nu6 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same') (c5)\nu6 = concatenate([u6, c4])\nc6 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (u6)\nc6 = Dropout(0.2) (c6)\nc6 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (c6)\n\nu7 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same') (c6)\nu7 = concatenate([u7, c3])\nc7 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (u7)\nc7 = Dropout(0.2) (c7)\nc7 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (c7)\n\nu8 = Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same') (c7)\nu8 = concatenate([u8, c2])\nc8 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (u8)\nc8 = Dropout(0.1) (c8)\nc8 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (c8)\n\nu9 = Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same') (c8)\nu9 = concatenate([u9, c1], axis=3)\nc9 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (u9)\nc9 = Dropout(0.1) (c9)\nc9 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (c9)\n\noutputs = Conv2D(1, (1, 1), activation='sigmoid') (c9)\n\nmodel = Model(inputs=[inputs], outputs=[outputs])\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=[mean_iou])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T05:02:53.755455Z","iopub.execute_input":"2023-10-11T05:02:53.756347Z","iopub.status.idle":"2023-10-11T05:03:00.858247Z","shell.execute_reply.started":"2023-10-11T05:02:53.756295Z","shell.execute_reply":"2023-10-11T05:03:00.857490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"earlyStopping = EarlyStopping(patience=3,verbose=1)\n#EarlyStopping callback is used to monitor the training process and stop training early if a certain condition is met.\n#patience: The number of consecutive epochs with no improvement after which training will be stopped.\n#\n# verbose 1 means that training progress messages will be printed.\nmodelCheckpoint = ModelCheckpoint(\"model-unet2023-1.h5\",verbose=1,save_best_only=True)\n#ModelCheckpoint callback is used to save the model's weights during training.\n\nhistory = model.fit_generator(train_generator,validation_data=val_generator,validation_steps=10, steps_per_epoch=250,\n                             epochs=100,callbacks=[earlyStopping,modelCheckpoint])\n#model.fit_generator()function is used to train the neural network model using data generators (train_generator and val_generator)","metadata":{"execution":{"iopub.status.busy":"2023-10-11T05:08:45.996838Z","iopub.execute_input":"2023-10-11T05:08:45.998011Z","iopub.status.idle":"2023-10-11T05:09:57.620427Z","shell.execute_reply.started":"2023-10-11T05:08:45.997972Z","shell.execute_reply":"2023-10-11T05:09:57.619201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model(\"model-unet2023-1.h5\",custom_objects={\"mean_iou\":mean_iou})\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.1)],verbose=1)\npreds_test = model.predict(X_test,verbose=1)\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\npreds_test_upsampled = []\nfor i in range(len(preds_test)):\n    preds_test_upsampled.append( resize(np.squeeze(preds_test_t[i]),\n                                       (sizes_test[i][0],sizes_test[i][1]),\n                                       mode = \"constant\",preserve_range=True) )","metadata":{"execution":{"iopub.status.busy":"2023-10-11T05:31:22.425232Z","iopub.execute_input":"2023-10-11T05:31:22.425591Z","iopub.status.idle":"2023-10-11T05:31:24.723839Z","shell.execute_reply.started":"2023-10-11T05:31:22.425565Z","shell.execute_reply":"2023-10-11T05:31:24.722773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Perform a sanity check on some random training samples\nix = random.randint(0, len(preds_train_t))\nimshow(X_train[ix])\nplt.show()\nimshow(np.squeeze(y_train[ix]))\nplt.show()\nimshow(np.squeeze(preds_train_t[ix]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T05:31:27.689759Z","iopub.execute_input":"2023-10-11T05:31:27.690102Z","iopub.status.idle":"2023-10-11T05:31:28.739294Z","shell.execute_reply.started":"2023-10-11T05:31:27.690073Z","shell.execute_reply":"2023-10-11T05:31:28.738411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Perform a sanity check on some random validation samples\n# its not gonna perform that great as i made a mistake while creating validation set previously.\n# It's fixed now in this notebook\nix = random.randint(0, len(preds_val_t))\nimshow(X_train[int(X_train.shape[0]*0.9):][ix])\nplt.show()\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":{"execution":{"iopub.status.busy":"2023-10-11T05:31:41.975696Z","iopub.execute_input":"2023-10-11T05:31:41.976034Z","iopub.status.idle":"2023-10-11T05:31:42.958419Z","shell.execute_reply.started":"2023-10-11T05:31:41.976006Z","shell.execute_reply":"2023-10-11T05:31:42.957407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Now Let's create the whole thing but with a predefined backbone like alexnet,vgg,resnet using segmentation-models library**","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}