{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":4832625,"sourceType":"datasetVersion","datasetId":2800272},{"sourceId":4832642,"sourceType":"datasetVersion","datasetId":2800278},{"sourceId":4832728,"sourceType":"datasetVersion","datasetId":2800337},{"sourceId":4832842,"sourceType":"datasetVersion","datasetId":2800409},{"sourceId":4832882,"sourceType":"datasetVersion","datasetId":2800437},{"sourceId":4832998,"sourceType":"datasetVersion","datasetId":2800514}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport random\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nplt.style.use(\"ggplot\")\n%matplotlib inline\n\nfrom tqdm import tqdm_notebook, tnrange\nfrom itertools import chain\nfrom skimage.io import imread, imshow, concatenate_images\nfrom skimage.transform import resize\nfrom skimage.morphology import label\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom keras import layers\nfrom keras.models import Model, load_model\nfrom keras.layers import Input, BatchNormalization, Activation, Dense, Dropout\nfrom keras.layers.core import Lambda, RepeatVector, Reshape\nfrom keras.layers.convolutional import Conv2D, Conv2DTranspose\nfrom keras.layers.pooling import MaxPooling2D, GlobalMaxPool2D\nfrom keras.layers import concatenate, add, Add\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:45.707530Z","iopub.execute_input":"2023-11-27T08:05:45.708333Z","iopub.status.idle":"2023-11-27T08:05:47.968275Z","shell.execute_reply.started":"2023-11-27T08:05:45.708302Z","shell.execute_reply":"2023-11-27T08:05:47.963067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set some parameters\nim_width = 128\nim_height = 128\nborder = 5","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:47.968933Z","iopub.status.idle":"2023-11-27T08:05:47.969250Z","shell.execute_reply.started":"2023-11-27T08:05:47.969094Z","shell.execute_reply":"2023-11-27T08:05:47.969108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = next(os.walk(\"/kaggle/input/duts-salient-object-detection-dataset/DUTS-TR/DUTS-TR-Image\"))[2] # list of names all images in the given path\n# ids = next(os.walk(\"/kaggle/input/duts-salient-object-detection-dataset/DUTS-TE/DUTS-TE-Image\"))[2] # list of names all images in the given path\nprint(\"No. of images = \", len(ids))","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:47.970486Z","iopub.status.idle":"2023-11-27T08:05:47.970827Z","shell.execute_reply.started":"2023-11-27T08:05:47.970640Z","shell.execute_reply":"2023-11-27T08:05:47.970654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.zeros((len(ids), im_height, im_width, 1), dtype=np.float32)\ny = np.zeros((len(ids), im_height, im_width, 1), dtype=np.float32)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:47.972009Z","iopub.status.idle":"2023-11-27T08:05:47.972436Z","shell.execute_reply.started":"2023-11-27T08:05:47.972212Z","shell.execute_reply":"2023-11-27T08:05:47.972232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Load the images and masks into arrays","metadata":{}},{"cell_type":"code","source":"from tqdm.notebook import tqdm\n\n# tqdm is used to display the progress bar\nfor n, id_ in tqdm(enumerate(ids), total=len(ids)):\n # Load images\n# img = load_img(\"/kaggle/input/duts-salient-object-detection-dataset/DUTS-TR/DUTS-TR-Image/\"+id_, grayscale=True) \n img = load_img(\"/kaggle/input/duts-salient-object-detection-dataset/DUTS-TR/DUTS-TR-Image/\"+id_, color_mode = \"grayscale\")\n# img = load_img(\"/kaggle/input/duts-salient-object-detection-dataset/DUTS-TE/DUTS-TE-Image\"+id_, color_mode = \"grayscale\")\n\n x_img = img_to_array(img)\n x_img = resize(x_img, (im_width, im_width, 1), mode = 'constant', preserve_range = True)\n # Load masks\n mask = img_to_array(load_img(\"/kaggle/input/duts-salient-object-detection-dataset/DUTS-TR/DUTS-TR-Mask/\"+id_.split('.')[0]+'.png', grayscale=True))\n# mask = img_to_array(load_img(\"/kaggle/input/duts-salient-object-detection-dataset/DUTS-TE/DUTS-TE-Mask\"+id_.split('.')[0]+'.png', grayscale=True))\n\n mask = resize(mask, (im_width, im_width, 1), mode = 'constant', preserve_range = True)\n # Save imagesdvdvd\n X[n] = x_img/255.0\n y[n] = mask/255.0","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:47.973522Z","iopub.status.idle":"2023-11-27T08:05:47.973978Z","shell.execute_reply.started":"2023-11-27T08:05:47.973725Z","shell.execute_reply":"2023-11-27T08:05:47.973762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.load('/kaggle/input/dutomron/DUT-OMRON-img.npy')\ny = np.load('/kaggle/input/dutomron/DUT-OMRON-mask.npy')","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:47.975798Z","iopub.status.idle":"2023-11-27T08:05:47.976119Z","shell.execute_reply.started":"2023-11-27T08:05:47.975964Z","shell.execute_reply":"2023-11-27T08:05:47.975979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split train and valid\nX_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:47.977446Z","iopub.status.idle":"2023-11-27T08:05:47.977922Z","shell.execute_reply.started":"2023-11-27T08:05:47.977678Z","shell.execute_reply":"2023-11-27T08:05:47.977698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Train Images: ',len(X_train), ' Train Labels: ',len(y_train))\nprint('Test Images: ',len(X_valid), ' Test Labels: ',len(y_valid))","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:47.979717Z","iopub.status.idle":"2023-11-27T08:05:47.980169Z","shell.execute_reply.started":"2023-11-27T08:05:47.979951Z","shell.execute_reply":"2023-11-27T08:05:47.979972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_train[0])","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:47.981365Z","iopub.status.idle":"2023-11-27T08:05:47.981816Z","shell.execute_reply.started":"2023-11-27T08:05:47.981572Z","shell.execute_reply":"2023-11-27T08:05:47.981592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Below code can be used to visualize the images and corresponding masks","metadata":{}},{"cell_type":"code","source":"import random\n# Visualize any randome image along with the mask\nix = random.randint(0, len(X_train))\nhas_mask = y_train[ix].max() > 0 # salt indicator\n\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize = (20, 15))\n\nax1.imshow(X_train[ix, ..., 0], cmap = 'gray')\nif has_mask: # if Groundtruth\n # draw a boundary(contour) in the original image separating salt and non-salt areas\n ax1.contour(y_train[ix].squeeze(), colors = 'k', linewidths = 5, levels = [0.5])\nax1.set_title('Image')\n\nax2.imshow(y_train[ix].squeeze(), cmap = 'gray')\nax2.set_title('Ground truth')","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:47.983051Z","iopub.status.idle":"2023-11-27T08:05:47.983371Z","shell.execute_reply.started":"2023-11-27T08:05:47.983214Z","shell.execute_reply":"2023-11-27T08:05:47.983229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers \nfrom tensorflow.keras.layers import Input , Conv2D , MaxPooling2D , Dropout , concatenate , UpSampling2D, BatchNormalization, Activation, Add, Dense, LSTM, add\nfrom tensorflow.keras import models\nfrom tensorflow.keras import losses\nfrom tensorflow.keras import optimizers\nimport numpy as np\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras.metrics import Recall, Precision","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:47.984620Z","iopub.status.idle":"2023-11-27T08:05:47.984958Z","shell.execute_reply.started":"2023-11-27T08:05:47.984797Z","shell.execute_reply":"2023-11-27T08:05:47.984813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"################Residual Block ##############################\ndef BatchActivate(x):\n x = BatchNormalization()(x)\n x = Activation('relu')(x)\n return x\n\ndef convolution_block(x, filters, size, strides=(1,1), padding='same', activation=True):\n x = Conv2D(filters, size, strides=strides, padding=padding)(x)\n if activation == True:\n x = BatchActivate(x)\n return x\n\ndef residual_block(blockInput, num_filters, batch_activate = False):\n x = BatchActivate(blockInput)\n x = convolution_block(x, num_filters, (3,3) )\n x = convolution_block(x, num_filters, (3,3), activation=False)\n x = Add()([x, blockInput])\n if batch_activate:\n x = BatchActivate(x)\n return x\n#################End Residual Block##############################","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:47.986905Z","iopub.status.idle":"2023-11-27T08:05:47.987225Z","shell.execute_reply.started":"2023-11-27T08:05:47.987069Z","shell.execute_reply":"2023-11-27T08:05:47.987084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#################ASPP Module#####################################\ndef convolution_blockspp(\n block_input,\n num_filters=256,\n kernel_size=3,\n dilation_rate=1,\n padding=\"same\",\n use_bias=False,\n):\n x = layers.Conv2D(\n num_filters,\n kernel_size=kernel_size,\n dilation_rate=dilation_rate,\n padding=\"same\",\n use_bias=use_bias,\n kernel_initializer=keras.initializers.HeNormal(),\n )(block_input)\n x = layers.BatchNormalization()(x)\n return tf.nn.relu(x)\n\n\ndef DilatedSpatialPyramidPooling(dspp_input):\n dims = dspp_input.shape\n x = layers.AveragePooling2D(pool_size=(dims[-3], dims[-2]))(dspp_input)\n x = convolution_blockspp(x, kernel_size=1, use_bias=True)\n out_pool = layers.UpSampling2D(\n size=(dims[-3] // x.shape[1], dims[-2] // x.shape[2]), interpolation=\"bilinear\",\n )(x)\n\n out_1 = convolution_blockspp(dspp_input, kernel_size=1, dilation_rate=1)\n out_6 = convolution_blockspp(dspp_input, kernel_size=3, dilation_rate=6)\n out_12 = convolution_blockspp(dspp_input, kernel_size=3, dilation_rate=12)\n out_18 = convolution_blockspp(dspp_input, kernel_size=3, dilation_rate=18)\n\n x = layers.Concatenate(axis=-1)([out_pool, out_1, out_6, out_12, out_18])\n output = convolution_blockspp(x, kernel_size=1)\n return output\n#################End ASPP Block#############################","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:47.988412Z","iopub.status.idle":"2023-11-27T08:05:47.988872Z","shell.execute_reply.started":"2023-11-27T08:05:47.988617Z","shell.execute_reply":"2023-11-27T08:05:47.988637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\n# def conv2d_block(inputs, filters, kernel_size=3, strides=1, use_bn=True):\n# x = tf.keras.layers.Conv2D(filters, kernel_size, strides=strides, padding='same')(inputs)\n# if use_bn:\n# x = tf.keras.layers.BatchNormalization()(x)\n# x = tf.keras.layers.ReLU()(x)\n# return x\ndef conv2d_block(input_tensor, n_filters, kernel_size = 3, batchnorm = True):\n \"\"\"Function to add 2 convolutional layers with the parameters passed to it\"\"\"\n # first layer\n x = Conv2D(filters=n_filters, kernel_size=(kernel_size, kernel_size), kernel_initializer=\"he_normal\",\n padding=\"same\")(input_tensor)\n if batchnorm:\n x = BatchNormalization()(x)\n x = Activation(\"relu\")(x)\n\n # second layer\n x = Conv2D(filters=n_filters, kernel_size=(kernel_size, kernel_size), kernel_initializer=\"he_normal\",\n padding=\"same\")(x)\n if batchnorm:\n x = BatchNormalization()(x)\n x = Activation(\"relu\")(x)\n\n return x\n\ndef get_unet(input_img, n_filters = 16, dropout = 0.1, batchnorm = True):\n \"\"\"Function to define the UNET Model\"\"\"\n # Contracting Path\n c1 = conv2d_block(input_img, n_filters * 1, kernel_size = 3, batchnorm = batchnorm)\n r1 = residual_block(c1, n_filters * 1)\n p1 = MaxPooling2D((2, 2))(r1)\n p1 = Dropout(dropout)(p1)\n \n c2 = conv2d_block(p1, n_filters * 2, kernel_size = 3, batchnorm = batchnorm)\n r2 = residual_block(c2, n_filters * 2)\n p2 = MaxPooling2D((2, 2))(c2)\n p2 = Dropout(dropout)(p2)\n \n c3 = conv2d_block(p2, n_filters * 4, kernel_size = 3, batchnorm = batchnorm)\n r3 = residual_block(c3, n_filters * 4)\n p3 = MaxPooling2D((2, 2))(c3)\n p3 = Dropout(dropout)(p3)\n \n c4 = conv2d_block(p3, n_filters * 8, kernel_size = 3, batchnorm = batchnorm)\n r4 = residual_block(c4, n_filters * 8)\n p4 = MaxPooling2D((2, 2))(c4)\n p4 = Dropout(dropout)(p4)\n \n c5 = conv2d_block(p4, n_filters = n_filters * 16, kernel_size = 3, batchnorm = batchnorm)\n \n # Expansive Path\n u6 = Conv2DTranspose(n_filters * 8, (3, 3), strides = (2, 2), padding = 'same')(c5)\n u6 = concatenate([u6, c4])\n u6 = Dropout(dropout)(u6)\n c6 = conv2d_block(u6, n_filters * 8, kernel_size = 3, batchnorm = batchnorm)\n \n u7 = Conv2DTranspose(n_filters * 4, (3, 3), strides = (2, 2), padding = 'same')(c6)\n u7 = concatenate([u7, c3])\n u7 = Dropout(dropout)(u7)\n c7 = conv2d_block(u7, n_filters * 4, kernel_size = 3, batchnorm = batchnorm)\n \n u8 = Conv2DTranspose(n_filters * 2, (3, 3), strides = (2, 2), padding = 'same')(c7)\n u8 = concatenate([u8, c2])\n u8 = Dropout(dropout)(u8)\n c8 = conv2d_block(u8, n_filters * 2, kernel_size = 3, batchnorm = batchnorm)\n \n u9 = Conv2DTranspose(n_filters * 1, (3, 3), strides = (2, 2), padding = 'same')(c8)\n u9 = concatenate([u9, c1])\n u9 = Dropout(dropout)(u9)\n c9 = conv2d_block(u9, n_filters * 1, kernel_size = 3, batchnorm = batchnorm)\n \n outputs = Conv2D(1, (1, 1), activation='sigmoid')(c9)\n model = Model(inputs=[input_img], outputs=[outputs])\n return model","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:47.990603Z","iopub.status.idle":"2023-11-27T08:05:47.991050Z","shell.execute_reply.started":"2023-11-27T08:05:47.990825Z","shell.execute_reply":"2023-11-27T08:05:47.990846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def jaccard(y_true, y_pred, smooth=1):\n intersection = K.sum(K.abs(y_true * y_pred), axis=[1,2,3])\n union = K.sum(y_true,[1,2,3])+K.sum(y_pred,[1,2,3])-intersection\n iou = K.mean((intersection + smooth) / (union + smooth), axis=0)\n return iou","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:47.992055Z","iopub.status.idle":"2023-11-27T08:05:47.992480Z","shell.execute_reply.started":"2023-11-27T08:05:47.992254Z","shell.execute_reply":"2023-11-27T08:05:47.992274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_img = Input((im_height, im_width, 1), name='img')\nmodel = get_unet(input_img, n_filters=16, dropout=0.05, batchnorm=True)\nmodel.compile(optimizer=Adam(), loss=\"binary_crossentropy\", metrics=[\"accuracy\",\"mae\",jaccard,keras.metrics.Precision(),keras.metrics.Recall()])","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:47.996207Z","iopub.status.idle":"2023-11-27T08:05:47.996924Z","shell.execute_reply.started":"2023-11-27T08:05:47.996660Z","shell.execute_reply":"2023-11-27T08:05:47.996681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:47.998027Z","iopub.status.idle":"2023-11-27T08:05:47.998451Z","shell.execute_reply.started":"2023-11-27T08:05:47.998233Z","shell.execute_reply":"2023-11-27T08:05:47.998253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [\n EarlyStopping(patience=500, verbose=1),\n ReduceLROnPlateau(factor=0.1, patience=5, min_lr=0.0001, verbose=1),\n ModelCheckpoint('/kaggle/working/saliency.h5', verbose=1, save_best_only=True, save_weights_only=True)\n]","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.000217Z","iopub.status.idle":"2023-11-27T08:05:48.000638Z","shell.execute_reply.started":"2023-11-27T08:05:48.000418Z","shell.execute_reply":"2023-11-27T08:05:48.000439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# results = model.fit(X_train, y_train, batch_size=16, epochs=100, callbacks=callbacks,\\\n# validation_data=(X_valid, y_valid))\nresults = model.fit(X_train, y_train, batch_size=16, epochs=10, callbacks=callbacks,\\\n validation_data=(X_valid, y_valid))","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.001928Z","iopub.status.idle":"2023-11-27T08:05:48.002257Z","shell.execute_reply.started":"2023-11-27T08:05:48.002096Z","shell.execute_reply":"2023-11-27T08:05:48.002112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\nplt.title(\"Learning curve\")\nplt.plot(results.history[\"loss\"], label=\"loss\")\nplt.plot(results.history[\"val_loss\"], label=\"val_loss\")\nplt.plot( np.argmin(results.history[\"val_loss\"]), np.min(results.history[\"val_loss\"]), marker=\"x\", color=\"r\", label=\"best model\")\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"log_loss\")\nplt.legend();","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.004028Z","iopub.status.idle":"2023-11-27T08:05:48.004341Z","shell.execute_reply.started":"2023-11-27T08:05:48.004183Z","shell.execute_reply":"2023-11-27T08:05:48.004198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Inference","metadata":{}},{"cell_type":"code","source":"# load the best model\nmodel.load_weights('/kaggle/working/saliency.h5')","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.005743Z","iopub.status.idle":"2023-11-27T08:05:48.006196Z","shell.execute_reply.started":"2023-11-27T08:05:48.005969Z","shell.execute_reply":"2023-11-27T08:05:48.005990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate on validation set (this must be equals to the best log_loss)\nmodel.evaluate(X_valid, y_valid, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.008189Z","iopub.status.idle":"2023-11-27T08:05:48.008623Z","shell.execute_reply.started":"2023-11-27T08:05:48.008404Z","shell.execute_reply":"2023-11-27T08:05:48.008425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict on train, val and test\npreds_train = model.predict(X_train, verbose=1)\npreds_val = model.predict(X_valid, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.011155Z","iopub.status.idle":"2023-11-27T08:05:48.011594Z","shell.execute_reply.started":"2023-11-27T08:05:48.011372Z","shell.execute_reply":"2023-11-27T08:05:48.011393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Threshold predictions\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":"2023-11-27T08:05:48.013288Z","iopub.status.idle":"2023-11-27T08:05:48.014192Z","shell.execute_reply.started":"2023-11-27T08:05:48.013948Z","shell.execute_reply":"2023-11-27T08:05:48.013972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_sample(X, y, preds, binary_preds, ix=None):\n \"\"\"Function to plot the results\"\"\"\n if ix is None:\n ix = random.randint(0, len(X))\n\n has_mask = y[ix].max() > 0\n\n fig, ax = plt.subplots(1, 4, figsize=(20, 10))\n ax[0].imshow(X[ix, ..., 0], cmap='gray')\n if has_mask:\n ax[0].contour(y[ix].squeeze(), colors='k', levels=[0.5])\n ax[0].set_title('Object')\n\n ax[1].imshow(y[ix].squeeze())\n ax[1].set_title('Actual Object')\n\n ax[2].imshow(preds[ix].squeeze(), vmin=0, vmax=1)\n if has_mask:\n ax[2].contour(y[ix].squeeze(), colors='k', levels=[0.5])\n ax[2].set_title('Predicted Object')\n \n ax[3].imshow(binary_preds[ix].squeeze(), vmin=0, vmax=1)\n if has_mask:\n ax[3].contour(y[ix].squeeze(), colors='k', levels=[0.5])\n ax[3].set_title('Object Predicted binary');","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.015498Z","iopub.status.idle":"2023-11-27T08:05:48.015946Z","shell.execute_reply.started":"2023-11-27T08:05:48.015708Z","shell.execute_reply":"2023-11-27T08:05:48.015729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Predictions on training set","metadata":{}},{"cell_type":"code","source":"# Check if training data looks all right\nplot_sample(X_train, y_train, preds_train, preds_train_t, ix=14)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.017254Z","iopub.status.idle":"2023-11-27T08:05:48.017667Z","shell.execute_reply.started":"2023-11-27T08:05:48.017460Z","shell.execute_reply":"2023-11-27T08:05:48.017480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_train, y_train, preds_train, preds_train_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.019457Z","iopub.status.idle":"2023-11-27T08:05:48.020390Z","shell.execute_reply.started":"2023-11-27T08:05:48.020141Z","shell.execute_reply":"2023-11-27T08:05:48.020164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_train, y_train, preds_train, preds_train_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.021479Z","iopub.status.idle":"2023-11-27T08:05:48.022489Z","shell.execute_reply.started":"2023-11-27T08:05:48.022227Z","shell.execute_reply":"2023-11-27T08:05:48.022251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_train, y_train, preds_train, preds_train_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.023392Z","iopub.status.idle":"2023-11-27T08:05:48.023823Z","shell.execute_reply.started":"2023-11-27T08:05:48.023591Z","shell.execute_reply":"2023-11-27T08:05:48.023611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_train, y_train, preds_train, preds_train_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.025626Z","iopub.status.idle":"2023-11-27T08:05:48.026112Z","shell.execute_reply.started":"2023-11-27T08:05:48.025869Z","shell.execute_reply":"2023-11-27T08:05:48.025892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_train, y_train, preds_train, preds_train_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.028320Z","iopub.status.idle":"2023-11-27T08:05:48.028781Z","shell.execute_reply.started":"2023-11-27T08:05:48.028533Z","shell.execute_reply":"2023-11-27T08:05:48.028555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_train, y_train, preds_train, preds_train_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.030698Z","iopub.status.idle":"2023-11-27T08:05:48.031140Z","shell.execute_reply.started":"2023-11-27T08:05:48.030922Z","shell.execute_reply":"2023-11-27T08:05:48.030943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_train, y_train, preds_train, preds_train_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.032891Z","iopub.status.idle":"2023-11-27T08:05:48.033739Z","shell.execute_reply.started":"2023-11-27T08:05:48.033509Z","shell.execute_reply":"2023-11-27T08:05:48.033532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_train, y_train, preds_train, preds_train_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.035094Z","iopub.status.idle":"2023-11-27T08:05:48.035937Z","shell.execute_reply.started":"2023-11-27T08:05:48.035691Z","shell.execute_reply":"2023-11-27T08:05:48.035713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_train, y_train, preds_train, preds_train_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.037302Z","iopub.status.idle":"2023-11-27T08:05:48.038156Z","shell.execute_reply.started":"2023-11-27T08:05:48.037928Z","shell.execute_reply":"2023-11-27T08:05:48.037951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_train, y_train, preds_train, preds_train_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.039465Z","iopub.status.idle":"2023-11-27T08:05:48.040334Z","shell.execute_reply.started":"2023-11-27T08:05:48.040089Z","shell.execute_reply":"2023-11-27T08:05:48.040112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_train, y_train, preds_train, preds_train_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.041488Z","iopub.status.idle":"2023-11-27T08:05:48.042462Z","shell.execute_reply.started":"2023-11-27T08:05:48.042193Z","shell.execute_reply":"2023-11-27T08:05:48.042217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Predictions on test set","metadata":{}},{"cell_type":"code","source":"# Check if valid data looks all right\nplot_sample(X_valid, y_valid, preds_val, preds_val_t, ix=19)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.043583Z","iopub.status.idle":"2023-11-27T08:05:48.044261Z","shell.execute_reply.started":"2023-11-27T08:05:48.044040Z","shell.execute_reply":"2023-11-27T08:05:48.044061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_valid, y_valid, preds_val, preds_val_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.045699Z","iopub.status.idle":"2023-11-27T08:05:48.046685Z","shell.execute_reply.started":"2023-11-27T08:05:48.046448Z","shell.execute_reply":"2023-11-27T08:05:48.046471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_valid, y_valid, preds_val, preds_val_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.047966Z","iopub.status.idle":"2023-11-27T08:05:48.048396Z","shell.execute_reply.started":"2023-11-27T08:05:48.048174Z","shell.execute_reply":"2023-11-27T08:05:48.048194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_valid, y_valid, preds_val, preds_val_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.049704Z","iopub.status.idle":"2023-11-27T08:05:48.050151Z","shell.execute_reply.started":"2023-11-27T08:05:48.049924Z","shell.execute_reply":"2023-11-27T08:05:48.049945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_valid, y_valid, preds_val, preds_val_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.051481Z","iopub.status.idle":"2023-11-27T08:05:48.051941Z","shell.execute_reply.started":"2023-11-27T08:05:48.051691Z","shell.execute_reply":"2023-11-27T08:05:48.051714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_valid, y_valid, preds_val, preds_val_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.053322Z","iopub.status.idle":"2023-11-27T08:05:48.053786Z","shell.execute_reply.started":"2023-11-27T08:05:48.053532Z","shell.execute_reply":"2023-11-27T08:05:48.053555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_valid, y_valid, preds_val, preds_val_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.056175Z","iopub.status.idle":"2023-11-27T08:05:48.056639Z","shell.execute_reply.started":"2023-11-27T08:05:48.056403Z","shell.execute_reply":"2023-11-27T08:05:48.056424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_valid, y_valid, preds_val, preds_val_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.059052Z","iopub.status.idle":"2023-11-27T08:05:48.059509Z","shell.execute_reply.started":"2023-11-27T08:05:48.059273Z","shell.execute_reply":"2023-11-27T08:05:48.059296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_valid, y_valid, preds_val, preds_val_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.061569Z","iopub.status.idle":"2023-11-27T08:05:48.062046Z","shell.execute_reply.started":"2023-11-27T08:05:48.061804Z","shell.execute_reply":"2023-11-27T08:05:48.061834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_valid, y_valid, preds_val, preds_val_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.064005Z","iopub.status.idle":"2023-11-27T08:05:48.064440Z","shell.execute_reply.started":"2023-11-27T08:05:48.064221Z","shell.execute_reply":"2023-11-27T08:05:48.064243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_valid, y_valid, preds_val, preds_val_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.066569Z","iopub.status.idle":"2023-11-27T08:05:48.067047Z","shell.execute_reply.started":"2023-11-27T08:05:48.066805Z","shell.execute_reply":"2023-11-27T08:05:48.066835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_valid, y_valid, preds_val, preds_val_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.068112Z","iopub.status.idle":"2023-11-27T08:05:48.068579Z","shell.execute_reply.started":"2023-11-27T08:05:48.068327Z","shell.execute_reply":"2023-11-27T08:05:48.068364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_valid, y_valid, preds_val, preds_val_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.070708Z","iopub.status.idle":"2023-11-27T08:05:48.071197Z","shell.execute_reply.started":"2023-11-27T08:05:48.070961Z","shell.execute_reply":"2023-11-27T08:05:48.070983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_valid, y_valid, preds_val, preds_val_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.073124Z","iopub.status.idle":"2023-11-27T08:05:48.073578Z","shell.execute_reply.started":"2023-11-27T08:05:48.073343Z","shell.execute_reply":"2023-11-27T08:05:48.073367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_valid, y_valid, preds_val, preds_val_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.074879Z","iopub.status.idle":"2023-11-27T08:05:48.075316Z","shell.execute_reply.started":"2023-11-27T08:05:48.075090Z","shell.execute_reply":"2023-11-27T08:05:48.075111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_valid, y_valid, preds_val, preds_val_t)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T08:05:48.077463Z","iopub.status.idle":"2023-11-27T08:05:48.077949Z","shell.execute_reply.started":"2023-11-27T08:05:48.077680Z","shell.execute_reply":"2023-11-27T08:05:48.077702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}