{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":8089,"databundleVersionId":44321,"sourceType":"competition"}],"dockerImageVersionId":30615,"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 tensorflow as tf\nimport os\nfrom skimage.io import imread , imshow\nfrom skimage.transform import resize\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-16T13:16:51.432316Z","iopub.execute_input":"2023-12-16T13:16:51.432969Z","iopub.status.idle":"2023-12-16T13:17:04.960927Z","shell.execute_reply.started":"2023-12-16T13:16:51.432933Z","shell.execute_reply":"2023-12-16T13:17:04.960149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Credits to :\n# 1 Digital Sreeni \n# 2 https://www.kaggle.com/code/divyareddyrecharla/unet-augmentation-segmentation-models","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_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\")","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:17:04.962583Z","iopub.execute_input":"2023-12-16T13:17:04.963109Z","iopub.status.idle":"2023-12-16T13:17:09.524848Z","shell.execute_reply.started":"2023-12-16T13:17:04.963083Z","shell.execute_reply":"2023-12-16T13:17:09.524066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ids  = next(os.walk(\"stage1_train\"))[1]\ntest_ids = next(os.walk('stage1_test'))[1]","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:17:09.525897Z","iopub.execute_input":"2023-12-16T13:17:09.526171Z","iopub.status.idle":"2023-12-16T13:17:09.531851Z","shell.execute_reply.started":"2023-12-16T13:17:09.526147Z","shell.execute_reply":"2023-12-16T13:17:09.530981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"height , width = 128 , 128\nx_train = np.zeros((len(train_ids) , height , width , 3) , dtype = np.uint8 )\ny_train = np.zeros((len(train_ids) , height , width , 1) , dtype = bool)","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:17:09.534021Z","iopub.execute_input":"2023-12-16T13:17:09.534305Z","iopub.status.idle":"2023-12-16T13:17:09.540099Z","shell.execute_reply.started":"2023-12-16T13:17:09.534282Z","shell.execute_reply":"2023-12-16T13:17:09.539192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir =\"/kaggle/working/stage1_train/\"\ntest_dir = \"/kaggle/working/stage1_test/\"","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:17:09.541078Z","iopub.execute_input":"2023-12-16T13:17:09.541348Z","iopub.status.idle":"2023-12-16T13:17:09.549436Z","shell.execute_reply.started":"2023-12-16T13:17:09.541325Z","shell.execute_reply":"2023-12-16T13:17:09.548636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\n\nfor n , id_ in enumerate(train_ids )  :\n#     print(id_)\n    path = train_dir + id_\n#     print(path)\n    img = imread(path +  '/images/' + id_ + '.png')[: , : , : 3]\n    img = resize(img , (height , width ) , mode = 'constant' , preserve_range = True)\n    x_train[n] = img\n    mask = np.zeros((height , width , 1 ) , dtype = bool)\n    for mask_file in next(os.walk(path + '/masks/' ))[2] :\n        mask_ =  imread(path + '/masks/' + mask_file )\n        mask_ = resize(mask_ , (height , width ) ,mode = 'constant' , preserve_range = True)\n        mask_ = np.expand_dims(mask_ , axis = -1 )\n        mask = np.maximum(mask , mask_)\n    y_train[n] = mask\n    \n    \n    \n","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:17:09.550455Z","iopub.execute_input":"2023-12-16T13:17:09.550704Z","iopub.status.idle":"2023-12-16T13:21:08.560937Z","shell.execute_reply.started":"2023-12-16T13:17:09.550682Z","shell.execute_reply":"2023-12-16T13:21:08.560139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = \"/kaggle/working/stage1_test/\"\nx_test = np.zeros((len(test_ids) , height , width , 3) , dtype = np.uint8) \nsizes_x = []\nfor i , id_ in enumerate(test_ids) :\n    \n    path = test_dir + id_ \n    img = imread(path + '/images/' + id_ + '.png')[:,:,:3]\n    sizes_x.append([img.shape[0] , img.shape[1]])\n    img = resize( img , (height , width ) , mode ='constant' , preserve_range = True)\n    x_test[i] =  img\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:21:08.561995Z","iopub.execute_input":"2023-12-16T13:21:08.562268Z","iopub.status.idle":"2023-12-16T13:21:09.930288Z","shell.execute_reply.started":"2023-12-16T13:21:08.562243Z","shell.execute_reply":"2023-12-16T13:21:09.929472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# y_train  = np.where(y_train ==  True , 1 , 0 ).astype(np.float32)\nprint(x_train.shape)\nprint(y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:21:09.931400Z","iopub.execute_input":"2023-12-16T13:21:09.931693Z","iopub.status.idle":"2023-12-16T13:21:09.937002Z","shell.execute_reply.started":"2023-12-16T13:21:09.931667Z","shell.execute_reply":"2023-12-16T13:21:09.936078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\nidx = random.randint(0 , len(train_ids))\nimshow(x_train[idx] )\nplt.show()\nimshow(y_train[idx] )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:21:09.938316Z","iopub.execute_input":"2023-12-16T13:21:09.938799Z","iopub.status.idle":"2023-12-16T13:21:10.478242Z","shell.execute_reply.started":"2023-12-16T13:21:09.938767Z","shell.execute_reply":"2023-12-16T13:21:10.477282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrainx , testx , trainy , testy  = train_test_split(x_train , y_train , test_size = 0.1 , random_state = 8 )\nprint(trainx.shape)\nprint(testx.shape)\nprint(trainy.shape)\nprint(testy.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:21:10.481807Z","iopub.execute_input":"2023-12-16T13:21:10.482135Z","iopub.status.idle":"2023-12-16T13:21:10.651017Z","shell.execute_reply.started":"2023-12-16T13:21:10.482108Z","shell.execute_reply":"2023-12-16T13:21:10.650101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing import image\n\nBATCH_SIZE = 32\nseed = 8\nimage_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(trainx , augment=True,seed = seed)\nmask_datagen.fit(trainy ,augment=True,seed = seed)\n\nx = image_datagen.flow(trainx , batch_size=BATCH_SIZE,shuffle=True,seed=seed)\ny = mask_datagen.flow(trainy ,batch_size=BATCH_SIZE,shuffle=True,seed=seed)\n\nimage_datagen_val = image.ImageDataGenerator()\nmask_datagen_val = image.ImageDataGenerator()\n\nimage_datagen_val.fit(testx , augment=True,seed = seed)\nmask_datagen_val.fit(testy ,augment=True,seed = seed)\n\nx_val = image_datagen.flow(testx ,batch_size=BATCH_SIZE,shuffle=True,seed=seed)\ny_val = mask_datagen.flow(testy ,batch_size=BATCH_SIZE,shuffle=True,seed=seed)","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:21:10.652057Z","iopub.execute_input":"2023-12-16T13:21:10.652337Z","iopub.status.idle":"2023-12-16T13:21:13.830980Z","shell.execute_reply.started":"2023-12-16T13:21:10.652312Z","shell.execute_reply":"2023-12-16T13:21:13.830081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(x))\nprint(len(y))","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:21:13.832182Z","iopub.execute_input":"2023-12-16T13:21:13.832484Z","iopub.status.idle":"2023-12-16T13:21:13.837612Z","shell.execute_reply.started":"2023-12-16T13:21:13.832458Z","shell.execute_reply":"2023-12-16T13:21:13.836668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Input , Lambda , Conv2D , Conv2DTranspose , MaxPooling2D , Dropout , concatenate\nfrom tensorflow.keras.models import Model , load_model \n","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:21:13.838783Z","iopub.execute_input":"2023-12-16T13:21:13.839093Z","iopub.status.idle":"2023-12-16T13:21:13.860775Z","shell.execute_reply.started":"2023-12-16T13:21:13.839068Z","shell.execute_reply":"2023-12-16T13:21:13.860056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model building ###\n\n\ndef unet(input_shape = (height , width , 3)):\n    inp = Input(input_shape )\n    s = Lambda(lambda x : x/ 255)(inp)\n    c1 =  Conv2D(32 ,kernel_size = (3 , 3) , padding ='same' , activation = 'relu'  , kernel_initializer = 'he_normal')(s)\n    c1 = Dropout(0.1)(c1)\n    c1 = Conv2D(32 , kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal') (c1)\n    p1 = MaxPooling2D((2 ,2)) (c1)\n    \n    c2 = Conv2D(64 , kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal')(p1)\n    c2 = Dropout(0.1)(c2)\n    c2 = Conv2D(64 , kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal')(c2)\n    p2 = MaxPooling2D((2 ,2)) (c2)\n    \n    c3 = Conv2D(128 , kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal')(p2)\n    c3 = Dropout(0.1)(c3)\n    c3 = Conv2D(128 , kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal')(c3)\n    p3 = MaxPooling2D((2 ,2)) (c3)\n    \n    c4 = Conv2D(256 , kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal')(p3)\n    c4 = Dropout(0.1)(c4)\n    c4 = Conv2D(256 , kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal')(c4)\n    p4 = MaxPooling2D((2 ,2)) (c4)\n    \n    \n    ##Bridge Connection ##\n    \n    c5 = Conv2D(512, kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal')(p4)\n    c5 = Conv2D(512, kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal') (c5)\n    c5 =  Dropout(0.1) (c5)\n    \n    up6 =  Conv2DTranspose(256 , (2 , 2) ,strides = ( 2,2 ) , padding = 'same' ) (c5)\n    up6 = concatenate([up6 ,c4] , axis = 3)\n    c6 = Conv2D(256 , (3,3) , padding = 'same' , activation = 'relu' ,kernel_initializer = 'he_normal' )(up6)\n    c6 = Dropout(0.1)(c6)\n    c6 = Conv2D(256 , (3,3) , padding = 'same' , activation = 'relu' ,kernel_initializer = 'he_normal' ) (c6)\n    \n\n    \n    up7 = Conv2DTranspose(128 , ( 2 , 2 ) ,strides = ( 2,2 ) , padding = 'same' ) (c6)\n    up7 = concatenate([up7 , c3] , axis = 3)\n    c7 = Conv2D(128 , (3,3) , padding = 'same' , activation = 'relu' ,kernel_initializer = 'he_normal' )(up7)\n    c7 = Dropout(0.1)(c7)\n    c7 = Conv2D(128 , (3,3) , padding = 'same' , activation = 'relu' ,kernel_initializer = 'he_normal' ) (c7)\n    \n    up8 = Conv2DTranspose(64 , (2 , 2 ) ,strides = ( 2,2 ) , padding = 'same'  ) (c7)\n    up8 = concatenate([up8, c2] , axis = 3)\n    c8 = Conv2D(64 , (3,3) , padding = 'same' , activation = 'relu' ,kernel_initializer = 'he_normal' )(up8)\n    c8 = Dropout(0.1)(c8)\n    c8 = Conv2D(64 , (3,3) , padding = 'same' , activation = 'relu' ,kernel_initializer = 'he_normal' ) (c8)\n    \n    up9 = Conv2DTranspose(32 , ( 2 , 2 ) ,strides = ( 2,2 ) , padding = 'same'  ) (c8)\n    up9 = concatenate([up9, c1] , axis = 3)\n    c9 = Conv2D(32, (3,3) , padding = 'same' , activation = 'relu' ,kernel_initializer = 'he_normal' )(up9)\n    c9 = Dropout(0.1)(c9)\n    c9 = Conv2D(32 , (3,3) , padding = 'same' , activation = 'relu' ,kernel_initializer = 'he_normal' ) (c9)\n    \n    c10 = Conv2D(1 , (1 , 1) , activation = 'sigmoid' ) (c9)\n    \n    model = Model(inputs = [inp], outputs = [c10] )\n    \n    return model\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:21:13.862071Z","iopub.execute_input":"2023-12-16T13:21:13.862546Z","iopub.status.idle":"2023-12-16T13:21:13.883724Z","shell.execute_reply.started":"2023-12-16T13:21:13.862513Z","shell.execute_reply":"2023-12-16T13:21:13.882996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = unet()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:21:13.913781Z","iopub.execute_input":"2023-12-16T13:21:13.915057Z","iopub.status.idle":"2023-12-16T13:21:18.441205Z","shell.execute_reply.started":"2023-12-16T13:21:13.915031Z","shell.execute_reply":"2023-12-16T13:21:18.440325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tensorflow.keras.losses import binary_crossentropy\nfrom tensorflow.keras import backend as k\n\ndef dice_coef(y_true , y_pred):\n    smooth = 1\n    y_true  = k.flatten(y_true)\n    y_pred = k.flatten(y_pred)\n    intersection  =  k.sum(y_true * y_pred)\n    return (2. * intersection + smooth ) /( k.sum(y_true ) + k.sum(y_pred)  + smooth )\n\n\n# 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\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:21:18.442386Z","iopub.execute_input":"2023-12-16T13:21:18.442669Z","iopub.status.idle":"2023-12-16T13:21:18.450368Z","shell.execute_reply.started":"2023-12-16T13:21:18.442644Z","shell.execute_reply":"2023-12-16T13:21:18.449488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam\n\n# optimizer = Adam(lr=1e-3)\n\n\nmodel.compile(optimizer = 'adam' , metrics = [dice_coef] , loss = 'binary_crossentropy' )","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:21:18.451573Z","iopub.execute_input":"2023-12-16T13:21:18.451861Z","iopub.status.idle":"2023-12-16T13:21:18.477415Z","shell.execute_reply.started":"2023-12-16T13:21:18.451815Z","shell.execute_reply":"2023-12-16T13:21:18.476574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping , ReduceLROnPlateau , ModelCheckpoint , CSVLogger\n\nearly_stopping = EarlyStopping(patience = 8, verbose=2)\nreduce_lr  = ReduceLROnPlateau(factor=0.1, patience=5, min_lr=0.0001, verbose=2)\nmodel_checkpoint = ModelCheckpoint('best_model.h5', save_best_only=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:21:18.478461Z","iopub.execute_input":"2023-12-16T13:21:18.478715Z","iopub.status.idle":"2023-12-16T13:21:18.484186Z","shell.execute_reply.started":"2023-12-16T13:21:18.478692Z","shell.execute_reply":"2023-12-16T13:21:18.483230Z"},"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-12-16T13:21:18.498453Z","iopub.execute_input":"2023-12-16T13:21:18.498697Z","iopub.status.idle":"2023-12-16T13:21:18.507638Z","shell.execute_reply.started":"2023-12-16T13:21:18.498675Z","shell.execute_reply":"2023-12-16T13:21:18.506731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history  = model.fit_generator(train_generator , epochs = 1000 , validation_data =  val_generator , validation_steps= int(len(testy)), steps_per_epoch = int(len(trainy) / BATCH_SIZE) , callbacks = [ early_stopping , reduce_lr , model_checkpoint ] ,verbose = 2)","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:21:18.508685Z","iopub.execute_input":"2023-12-16T13:21:18.508973Z","iopub.status.idle":"2023-12-16T13:28:39.916026Z","shell.execute_reply.started":"2023-12-16T13:21:18.508950Z","shell.execute_reply":"2023-12-16T13:28:39.915033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import custom_object_scope\n\nwith custom_object_scope({'dice_coef': dice_coef}):\n    model = load_model(\"/kaggle/working/best_model.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:30:49.373609Z","iopub.execute_input":"2023-12-16T13:30:49.374482Z","iopub.status.idle":"2023-12-16T13:30:50.009627Z","shell.execute_reply.started":"2023-12-16T13:30:49.374447Z","shell.execute_reply":"2023-12-16T13:30:50.008601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:31:00.485657Z","iopub.execute_input":"2023-12-16T13:31:00.486032Z","iopub.status.idle":"2023-12-16T13:31:00.584140Z","shell.execute_reply.started":"2023-12-16T13:31:00.485996Z","shell.execute_reply":"2023-12-16T13:31:00.583238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# new_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:28:41.233067Z","iopub.status.idle":"2023-12-16T13:28:41.233488Z","shell.execute_reply.started":"2023-12-16T13:28:41.233283Z","shell.execute_reply":"2023-12-16T13:28:41.233303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.expand_dims(trainx[0] , axis = 0 ).shape","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:28:41.235525Z","iopub.status.idle":"2023-12-16T13:28:41.235964Z","shell.execute_reply.started":"2023-12-16T13:28:41.235734Z","shell.execute_reply":"2023-12-16T13:28:41.235754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\npreds  =model.predict(np.expand_dims(trainx[12], axis = 0 ) , verbose = 1)\npreds_t = (preds > 0.5).astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:31:07.043211Z","iopub.execute_input":"2023-12-16T13:31:07.044068Z","iopub.status.idle":"2023-12-16T13:31:08.040141Z","shell.execute_reply.started":"2023-12-16T13:31:07.044034Z","shell.execute_reply":"2023-12-16T13:31:08.039295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\npreds_train = model.predict(trainx,verbose=1)\npreds_val  = model.predict(testx,verbose=1)\n\n\npreds_train_t = (preds_train > 0.5).astype(np.uint8)\npreds_val_t = (preds_val > 0.5).astype(np.uint8)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:31:26.929778Z","iopub.execute_input":"2023-12-16T13:31:26.930609Z","iopub.status.idle":"2023-12-16T13:31:28.040602Z","shell.execute_reply.started":"2023-12-16T13:31:26.930577Z","shell.execute_reply":"2023-12-16T13:31:28.039874Z"},"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(trainx[ix])\nplt.show()\nimshow(np.squeeze(trainy[ix]))\nplt.show()\nimshow(np.squeeze(preds_train_t[ix]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:31:30.805657Z","iopub.execute_input":"2023-12-16T13:31:30.806029Z","iopub.status.idle":"2023-12-16T13:31:31.816253Z","shell.execute_reply.started":"2023-12-16T13:31:30.805996Z","shell.execute_reply":"2023-12-16T13:31:31.815291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ix = random.randint(0, len(preds_val_t))\nimshow(testx[ix])\nplt.show()\nimshow(np.squeeze(testy[ix]))\nplt.show()\nimshow(np.squeeze(preds_val_t[ix]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:31:31.817656Z","iopub.execute_input":"2023-12-16T13:31:31.817931Z","iopub.status.idle":"2023-12-16T13:31:33.154611Z","shell.execute_reply.started":"2023-12-16T13:31:31.817909Z","shell.execute_reply":"2023-12-16T13:31:33.153652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## for test ##\n\ntest_sub  = np.zeros((len(test_ids) , height , width , 3 ) , dtype = np.uint8 )\nsizes_test = []\n\nfor i ,  id_  in  enumerate(test_ids):\n    path = test_dir + id_\n    img  = imread(path + \"/images/\" + id_ + \".png\")[:,:,:3]\n    img  = resize(img , (height , width) , preserve_range = True , mode = 'constant' )\n    \n    test_sub[i] = img","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:51:25.378388Z","iopub.execute_input":"2023-12-16T13:51:25.378760Z","iopub.status.idle":"2023-12-16T13:51:26.708586Z","shell.execute_reply.started":"2023-12-16T13:51:25.378732Z","shell.execute_reply":"2023-12-16T13:51:26.707554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_test = model.predict(test_sub,verbose=2)","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:52:07.950085Z","iopub.execute_input":"2023-12-16T13:52:07.950418Z","iopub.status.idle":"2023-12-16T13:52:08.130149Z","shell.execute_reply.started":"2023-12-16T13:52:07.950394Z","shell.execute_reply":"2023-12-16T13:52:08.129330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ix = random.randint(0, len(preds_test))\nimshow(test_sub[ix])\nplt.show()\n\nimshow(np.squeeze(preds_test[ix]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-16T13:54:10.014253Z","iopub.execute_input":"2023-12-16T13:54:10.014646Z","iopub.status.idle":"2023-12-16T13:54:10.628925Z","shell.execute_reply.started":"2023-12-16T13:54:10.014615Z","shell.execute_reply":"2023-12-16T13:54:10.628093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}