{"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-09-15T12:59:00.631829Z","iopub.execute_input":"2023-09-15T12:59:00.632710Z","iopub.status.idle":"2023-09-15T12:59:07.529820Z","shell.execute_reply.started":"2023-09-15T12:59:00.632671Z","shell.execute_reply":"2023-09-15T12:59:07.528744Z"},"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\nfrom itertools import chain\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\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-09-15T12:59:21.358014Z","iopub.execute_input":"2023-09-15T12:59:21.358399Z","iopub.status.idle":"2023-09-15T12:59:29.654744Z","shell.execute_reply.started":"2023-09-15T12:59:21.358367Z","shell.execute_reply":"2023-09-15T12:59:29.653622Z"},"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-09-15T12:59:34.853560Z","iopub.execute_input":"2023-09-15T12:59:34.854316Z","iopub.status.idle":"2023-09-15T12:59:34.862434Z","shell.execute_reply.started":"2023-09-15T12:59:34.854279Z","shell.execute_reply":"2023-09-15T12:59:34.861268Z"},"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-09-15T12:59:36.015758Z","iopub.execute_input":"2023-09-15T12:59:36.016501Z","iopub.status.idle":"2023-09-15T13:04:14.658071Z","shell.execute_reply.started":"2023-09-15T12:59:36.016466Z","shell.execute_reply":"2023-09-15T13:04:14.657066Z"},"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-09-15T13:04:54.207662Z","iopub.execute_input":"2023-09-15T13:04:54.208393Z","iopub.status.idle":"2023-09-15T13:04:57.487316Z","shell.execute_reply.started":"2023-09-15T13:04:54.208358Z","shell.execute_reply":"2023-09-15T13:04:57.486280Z"},"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-09-15T13:05:00.181677Z","iopub.execute_input":"2023-09-15T13:05:00.182270Z","iopub.status.idle":"2023-09-15T13:05:02.203928Z","shell.execute_reply.started":"2023-09-15T13:05:00.182229Z","shell.execute_reply":"2023-09-15T13:05:02.202842Z"},"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-09-15T13:05:02.212846Z","iopub.execute_input":"2023-09-15T13:05:02.213834Z","iopub.status.idle":"2023-09-15T13:05:02.222308Z","shell.execute_reply.started":"2023-09-15T13:05:02.213764Z","shell.execute_reply":"2023-09-15T13:05:02.221187Z"},"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    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    \n    # Calculate the union (true positives + false positives + false negatives)\n    union = tf.reduce_sum(y_true) + tf.reduce_sum(y_pred) - intersection\n    \n    # Calculate the IoU for each class\n    iou = intersection / union\n    \n    return iou","metadata":{"execution":{"iopub.status.busy":"2023-09-15T13:05:07.385036Z","iopub.execute_input":"2023-09-15T13:05:07.385409Z","iopub.status.idle":"2023-09-15T13:05:07.392337Z","shell.execute_reply.started":"2023-09-15T13:05:07.385378Z","shell.execute_reply":"2023-09-15T13:05:07.391294Z"},"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)\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-09-15T13:05:11.373691Z","iopub.execute_input":"2023-09-15T13:05:11.374187Z","iopub.status.idle":"2023-09-15T13:05:16.083110Z","shell.execute_reply.started":"2023-09-15T13:05:11.374144Z","shell.execute_reply":"2023-09-15T13:05:16.072284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"earlyStopping = EarlyStopping(patience=3,verbose=1)\nmodelCheckpoint = ModelCheckpoint(\"model-unet2023-1.h5\",verbose=1,save_best_only=True)\nhistory = model.fit_generator(train_generator,validation_data=val_generator,validation_steps=10, steps_per_epoch=250,\n                             epochs=100,callbacks=[earlyStopping,modelCheckpoint])","metadata":{"execution":{"iopub.status.busy":"2023-09-14T11:55:51.226473Z","iopub.execute_input":"2023-09-14T11:55:51.227789Z","iopub.status.idle":"2023-09-14T12:38:18.987687Z","shell.execute_reply.started":"2023-09-14T11:55:51.227740Z","shell.execute_reply":"2023-09-14T12:38:18.986794Z"},"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-09-14T12:54:36.713453Z","iopub.execute_input":"2023-09-14T12:54:36.714481Z","iopub.status.idle":"2023-09-14T12:55:03.726535Z","shell.execute_reply.started":"2023-09-14T12:54:36.714435Z","shell.execute_reply":"2023-09-14T12:55:03.725094Z"},"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-09-14T12:58:04.148343Z","iopub.execute_input":"2023-09-14T12:58:04.149121Z","iopub.status.idle":"2023-09-14T12:58:05.110275Z","shell.execute_reply.started":"2023-09-14T12:58:04.149080Z","shell.execute_reply":"2023-09-14T12:58:05.109187Z"},"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-09-14T13:02:38.722303Z","iopub.execute_input":"2023-09-14T13:02:38.722806Z","iopub.status.idle":"2023-09-14T13:02:39.529268Z","shell.execute_reply.started":"2023-09-14T13:02:38.722763Z","shell.execute_reply":"2023-09-14T13:02:39.528366Z"},"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":"!pip install segmentation-models","metadata":{"execution":{"iopub.status.busy":"2023-09-15T13:05:26.081331Z","iopub.execute_input":"2023-09-15T13:05:26.081766Z","iopub.status.idle":"2023-09-15T13:05:41.033510Z","shell.execute_reply.started":"2023-09-15T13:05:26.081731Z","shell.execute_reply":"2023-09-15T13:05:41.032285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.environ[\"SM_FRAMEWORK\"] = \"tf.keras\"\n\nfrom tensorflow import keras\nimport segmentation_models as sm\n\nBACKBONE = 'vgg16'\n\n# define model\nmodel = sm.Unet(BACKBONE,input_shape=(IMG_HEIGHT, IMG_WIDTH, IMG_CHANNEL),classes=1,encoder_weights='imagenet')\nmodel.compile(\n    'Adam',\n    loss=sm.losses.binary_crossentropy,\n    metrics=[mean_iou],\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-15T13:05:45.277897Z","iopub.execute_input":"2023-09-15T13:05:45.278277Z","iopub.status.idle":"2023-09-15T13:05:46.866349Z","shell.execute_reply.started":"2023-09-15T13:05:45.278245Z","shell.execute_reply":"2023-09-15T13:05:46.865347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-09-15T13:05:51.808741Z","iopub.execute_input":"2023-09-15T13:05:51.809146Z","iopub.status.idle":"2023-09-15T13:05:51.952457Z","shell.execute_reply.started":"2023-09-15T13:05:51.809115Z","shell.execute_reply":"2023-09-15T13:05:51.951639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This code was used to check the mean_iou before training on a batch and check this function's output with \n# model's mean_iou output after each epoch. It turns out values were preety close but I did this with first 10 images \n# of train_generator. Doing the same thing with batch_size and steps_per_epoch and shuffling enabled makes \n# the values somewhat different but I am open to discussion about this topic and would encourage someone to do the same\n\nimport tensorflow as tf\nfrom tensorflow.keras.callbacks import Callback\nfrom tensorflow.python.ops.numpy_ops import np_config\nnp_config.enable_numpy_behavior()\nclass IoUCallback(Callback):\n    def on_epoch_end(self, epoch, logs=None):\n        print(f\"Epoch {epoch + 1}/{self.params['epochs']}\")\n\n    def on_epoch_begin(self, batch, logs=None):\n        # Get a batch of training data (x_batch) and corresponding labels (y_true_batch)\n        x_batch, y_true_batch = next(train_generator)\n        val = []\n        # Get the model's raw output for the batch\n        raw_output_batch = model.predict(x_batch)\n        # Calculate the IoU for each image in the batch\n        for i in range(len(x_batch)):\n            # Calculate the IoU for the current image\n            iou = round(mean_iou(y_true_batch[i], raw_output_batch[i]),4)\n            val.append(iou)\n#             Print the IoU for the current image\n#             print(f\"IoU for Image {i + 1} in Batch {1}: {iou}\\n\")\n        print(\"Begin Mean value is {}\\n\".format(np.mean(val),4))\n        \niou_callback = IoUCallback()","metadata":{"execution":{"iopub.status.busy":"2023-09-15T13:06:00.121612Z","iopub.execute_input":"2023-09-15T13:06:00.122029Z","iopub.status.idle":"2023-09-15T13:06:00.133167Z","shell.execute_reply.started":"2023-09-15T13:06:00.121995Z","shell.execute_reply":"2023-09-15T13:06:00.132056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"earlyStopping = EarlyStopping(patience=3,verbose=1)\nmodelCheckpoint = ModelCheckpoint(\"model-unet2023-2.h5\",verbose=1,save_best_only=True)\nhistory = model.fit(train_generator,validation_data=val_generator, validation_steps=10, steps_per_epoch=250,\n                             epochs=10,callbacks=[earlyStopping,modelCheckpoint])","metadata":{"execution":{"iopub.status.busy":"2023-09-15T13:06:01.033933Z","iopub.execute_input":"2023-09-15T13:06:01.034895Z","iopub.status.idle":"2023-09-15T13:09:51.374918Z","shell.execute_reply.started":"2023-09-15T13:06:01.034853Z","shell.execute_reply":"2023-09-15T13:09:51.373873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model(\"model-unet2023-2.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.9):],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-09-15T13:10:42.945858Z","iopub.execute_input":"2023-09-15T13:10:42.946217Z","iopub.status.idle":"2023-09-15T13:10:47.443056Z","shell.execute_reply.started":"2023-09-15T13:10:42.946190Z","shell.execute_reply":"2023-09-15T13:10:47.441627Z"},"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-09-15T13:11:07.004302Z","iopub.execute_input":"2023-09-15T13:11:07.004705Z","iopub.status.idle":"2023-09-15T13:11:08.053739Z","shell.execute_reply.started":"2023-09-15T13:11:07.004670Z","shell.execute_reply":"2023-09-15T13:11:08.052755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ix = 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-09-15T13:11:37.760940Z","iopub.execute_input":"2023-09-15T13:11:37.761361Z","iopub.status.idle":"2023-09-15T13:11:38.996121Z","shell.execute_reply.started":"2023-09-15T13:11:37.761325Z","shell.execute_reply":"2023-09-15T13:11:38.995037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}