{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":9988,"databundleVersionId":868324,"sourceType":"competition"},{"sourceId":8950965,"sourceType":"datasetVersion","datasetId":5386692}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install -q tensorflow==2.16.1 patchify ","metadata":{"execution":{"iopub.status.busy":"2024-07-17T15:26:07.089122Z","iopub.execute_input":"2024-07-17T15:26:07.089564Z","iopub.status.idle":"2024-07-17T15:27:18.107324Z","shell.execute_reply.started":"2024-07-17T15:26:07.089526Z","shell.execute_reply":"2024-07-17T15:27:18.106240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport pandas as pd\nimport tensorflow.keras.layers as tkl\nimport tensorflow.keras.backend as K\n\nimport os\nimport shutil\nfrom patchify import patchify, unpatchify\n\nimport imageio.v2 as imageio\nimport matplotlib.pyplot as plt\nimport skimage.draw\nfrom PIL import Image\nimport pickle\n\n%matplotlib inline","metadata":{"_uuid":"77c72b9f-d244-4e6e-81fa-051598ed0394","_cell_guid":"e707e628-2781-4d16-8fae-77b4ddd1a0ee","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T15:27:26.844828Z","iopub.execute_input":"2024-07-17T15:27:26.845225Z","iopub.status.idle":"2024-07-17T15:27:26.935837Z","shell.execute_reply.started":"2024-07-17T15:27:26.845189Z","shell.execute_reply":"2024-07-17T15:27:26.935095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os","metadata":{"execution":{"iopub.status.busy":"2024-07-17T18:37:44.915453Z","iopub.execute_input":"2024-07-17T18:37:44.915970Z","iopub.status.idle":"2024-07-17T18:37:44.920874Z","shell.execute_reply.started":"2024-07-17T18:37:44.915940Z","shell.execute_reply":"2024-07-17T18:37:44.919681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.listdir('/kaggle/working/checkpoints')","metadata":{"execution":{"iopub.status.busy":"2024-07-17T18:37:47.868821Z","iopub.execute_input":"2024-07-17T18:37:47.869212Z","iopub.status.idle":"2024-07-17T18:37:47.878740Z","shell.execute_reply.started":"2024-07-17T18:37:47.869183Z","shell.execute_reply":"2024-07-17T18:37:47.877635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a href=\"/kaggle/working/checkpoints/unet_dice_patches_e-5.keras\"> Download File </a>","metadata":{}},{"cell_type":"code","source":"# !pip show tensorflow","metadata":{"_uuid":"e5cb97a7-ab5e-4c48-823c-7c7420771b09","_cell_guid":"7424f601-3868-4b69-b9a6-145a6bbeaf89","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T15:07:32.240704Z","iopub.execute_input":"2024-07-17T15:07:32.240996Z","iopub.status.idle":"2024-07-17T15:07:45.369640Z","shell.execute_reply.started":"2024-07-17T15:07:32.240970Z","shell.execute_reply":"2024-07-17T15:07:45.368377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 768\nDATASET_IMG_SIZE = 128\n\nmask_folder = '/kaggle/working/airbus-ship-detection/train_masks/'\ntrain_folder = '/kaggle/input/airbus-ship-detection/train_v2/'\n\nmask_patches = '/kaggle/input/airbus-ship-patches/mask_patches/mask_patches'\nimg_patches = '/kaggle/input/airbus-ship-patches/clear_img_patches/clear_img_patches'","metadata":{"_uuid":"852ed849-2b8f-405a-b666-5af1a6927a5f","_cell_guid":"73a0591e-d006-4435-a2d0-11bb742b46eb","execution":{"iopub.status.busy":"2024-07-17T15:30:24.196165Z","iopub.execute_input":"2024-07-17T15:30:24.196834Z","iopub.status.idle":"2024-07-17T15:30:24.201179Z","shell.execute_reply.started":"2024-07-17T15:30:24.196802Z","shell.execute_reply":"2024-07-17T15:30:24.200273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data into variables","metadata":{}},{"cell_type":"code","source":"# X = []\n# y = []\n\n# if os.path.exists(mask_patches):\n#     for f in os.listdir(mask_patches):\n#         X.append(os.path.join(img_patches, f.replace('.png', '.jpg')))\n#         y.append(os.path.join(mask_patches, f))\n# else:\n#     for f in os.listdir(mask_folder):\n#         X.append(os.path.join(train_folder, f.replace('.png', '.jpg')))\n#         y.append(os.path.join(mask_folder, f))","metadata":{"_uuid":"43a0a5e8-9080-4f7b-80ca-ffef8505a7ff","_cell_guid":"33f6797f-b5e4-46c5-8a52-00783a8fba61","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T15:31:03.404062Z","iopub.execute_input":"2024-07-17T15:31:03.404751Z","iopub.status.idle":"2024-07-17T15:31:05.084479Z","shell.execute_reply.started":"2024-07-17T15:31:03.404719Z","shell.execute_reply":"2024-07-17T15:31:05.083657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(len(X), len(y))\n# print(X[0], y[0])","metadata":{"_uuid":"58dba0f3-0c5b-44f7-b3f2-2d2066ae0d24","_cell_guid":"4625825c-7c92-44de-badf-1c9c7f4590db","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T15:31:08.774354Z","iopub.execute_input":"2024-07-17T15:31:08.775177Z","iopub.status.idle":"2024-07-17T15:31:08.780318Z","shell.execute_reply.started":"2024-07-17T15:31:08.775145Z","shell.execute_reply":"2024-07-17T15:31:08.779179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare Dataset","metadata":{"_uuid":"4093d66c-e44c-4251-94c9-4f86dd0c713b","_cell_guid":"b137576d-9a96-4300-9b02-9380a1fc2d32","trusted":true}},{"cell_type":"markdown","source":"### read image -> decode -> resize","metadata":{}},{"cell_type":"code","source":"def process_path(image_path, mask_path):\n    \n    img = tf.io.read_file(image_path)\n    img = tf.io.decode_jpeg(img, channels=3)\n    mask = tf.io.read_file(mask_path)\n    mask = tf.io.decode_png(mask, channels=1)\n  \n    return img, mask\n\ndef preprocess(image, mask):\n    \n    input_image = tf.image.resize(image, (DATASET_IMG_SIZE, DATASET_IMG_SIZE), method='nearest')\n    input_mask = tf.image.resize(mask, (DATASET_IMG_SIZE, DATASET_IMG_SIZE), method='nearest')\n    \n    #print(input_image.dtype, input_mask.dtype)\n    #input_mask = tf.cast(input_mask / 255, tf.dtypes.uint8)\n\n    return input_image, input_mask","metadata":{"_uuid":"56802f6b-53d7-410c-8999-8aaca2b04f16","_cell_guid":"8dd150ef-d09e-40ee-88ba-5f203a5622f5","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T15:31:24.323759Z","iopub.execute_input":"2024-07-17T15:31:24.324540Z","iopub.status.idle":"2024-07-17T15:31:24.330913Z","shell.execute_reply.started":"2024-07-17T15:31:24.324508Z","shell.execute_reply":"2024-07-17T15:31:24.329889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.model_selection import train_test_split","metadata":{"_uuid":"bf4ff3e6-7d48-451d-b655-5c74b45ace37","_cell_guid":"e0614ba4-c840-4ce3-a275-81744919f0e8","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T15:31:27.100117Z","iopub.execute_input":"2024-07-17T15:31:27.100927Z","iopub.status.idle":"2024-07-17T15:31:27.524015Z","shell.execute_reply.started":"2024-07-17T15:31:27.100891Z","shell.execute_reply":"2024-07-17T15:31:27.523195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.05, random_state=42)","metadata":{"_uuid":"50ad5c4b-9faf-4c8a-82c5-4b13fb125ef7","_cell_guid":"7437c3c0-7c3d-4974-810c-bc2fa59cb80a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T15:31:29.705501Z","iopub.execute_input":"2024-07-17T15:31:29.705861Z","iopub.status.idle":"2024-07-17T15:31:29.785455Z","shell.execute_reply.started":"2024-07-17T15:31:29.705832Z","shell.execute_reply":"2024-07-17T15:31:29.784549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))\n# image_ds = train_dataset.map(process_path)\n# processed_image_ds = image_ds.map(preprocess)\n\n# valid_dataset = tf.data.Dataset.from_tensor_slices((X_valid, y_valid))\n# valid_image_ds = valid_dataset.map(process_path)\n# valid_processed_image_ds = valid_image_ds.map(preprocess)","metadata":{"_uuid":"844f1009-f505-4a56-9b22-9918b4c30be0","_cell_guid":"4347eeda-83e5-49d6-ae6b-96d00fd5a596","execution":{"iopub.status.busy":"2024-07-17T15:31:32.972244Z","iopub.execute_input":"2024-07-17T15:31:32.973206Z","iopub.status.idle":"2024-07-17T15:31:34.711708Z","shell.execute_reply.started":"2024-07-17T15:31:32.973162Z","shell.execute_reply":"2024-07-17T15:31:34.710751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display(display_list):\n    plt.figure(figsize=(10, 10))\n\n    title = ['Input Image', 'True Mask', 'Predicted Mask']\n\n    for i in range(len(display_list)):\n        plt.subplot(1, len(display_list), i+1)\n        plt.title(title[i])\n        plt.imshow(tf.keras.preprocessing.image.array_to_img(display_list[i]))\n        plt.axis('off')\n    plt.show()","metadata":{"_uuid":"9950afde-b3ce-4cef-9398-f3803e876068","_cell_guid":"f9d1b08c-fc35-4fd0-99a0-4d9bf26d5d77","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T15:31:36.678496Z","iopub.execute_input":"2024-07-17T15:31:36.678857Z","iopub.status.idle":"2024-07-17T15:31:36.684909Z","shell.execute_reply.started":"2024-07-17T15:31:36.678826Z","shell.execute_reply":"2024-07-17T15:31:36.683966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{"_uuid":"afee8e13-bbfe-44cc-879d-1a84270eb7b4","_cell_guid":"257e851b-6d93-4e73-857f-bfc271e237c9","trusted":true}},{"cell_type":"code","source":"def conv_block(inputs=None, n_filters=32, dropout_prob=0, max_pooling=True):\n    \"\"\"\n    Convolutional downsampling block\n    \"\"\"\n    conv = tkl.Conv2D(n_filters, # Number of filters\n                  3,   # Kernel size   \n                  activation='relu',\n                  padding='same',\n                  kernel_initializer='he_normal')(inputs)\n    conv = tkl.Conv2D(n_filters, # Number of filters\n                  3,   # Kernel size\n                  activation='relu',\n                  padding='same',\n                  kernel_initializer='he_normal')(conv)\n    if dropout_prob > 0:\n        conv = tkl.Dropout(dropout_prob)(conv)    \n    if max_pooling:\n        next_layer = tkl.MaxPooling2D(2)(conv)        \n    else:\n        next_layer = conv\n    skip_connection = conv\n    \n    return next_layer, skip_connection","metadata":{"_uuid":"10d976c0-0bd0-41cd-a00a-356e6de1bd5f","_cell_guid":"e18d5b43-3e44-4362-b54e-5912db76d44a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T15:31:44.385366Z","iopub.execute_input":"2024-07-17T15:31:44.385741Z","iopub.status.idle":"2024-07-17T15:31:44.393062Z","shell.execute_reply.started":"2024-07-17T15:31:44.385709Z","shell.execute_reply":"2024-07-17T15:31:44.392118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def upsampling_block(expansive_input, contractive_input, n_filters=32):\n    \"\"\"\n    Convolutional upsampling block\n    \"\"\"\n    up = tkl.Conv2DTranspose(\n                 n_filters,\n                 3,\n                 strides=2,\n                 padding='same')(expansive_input)\n    merge = tkl.concatenate([up, contractive_input], axis=3)\n    conv = tkl.Conv2D(n_filters,\n                 3,\n                 activation='relu',\n                 padding='same',\n                 kernel_initializer='he_normal')(merge)\n    conv = tkl.Conv2D(n_filters,\n                 3,\n                 activation='relu',\n                 padding='same',\n                 kernel_initializer='he_normal')(conv)\n\n    return conv","metadata":{"_uuid":"6af298d8-e0be-4d7a-bb3c-f9f028bd115f","_cell_guid":"84a31f77-16a3-4c07-bc40-e12c3b98a87f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T15:31:47.534696Z","iopub.execute_input":"2024-07-17T15:31:47.535410Z","iopub.status.idle":"2024-07-17T15:31:47.541410Z","shell.execute_reply.started":"2024-07-17T15:31:47.535377Z","shell.execute_reply":"2024-07-17T15:31:47.540490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def unet_model(input_size=(768, 768, 3), n_filters=32, n_classes=1):\n    inputs = tkl.Input(input_size)\n    lam = tkl.Lambda(lambda x: x/255.)(inputs)\n    \n    cblock1 = conv_block(lam, n_filters)\n    cblock2 = conv_block(cblock1[0], n_filters*2)\n    cblock3 = conv_block(cblock2[0], n_filters*4)\n    cblock4 = conv_block(cblock3[0], n_filters*8, dropout_prob=0.3)\n    cblock5 = conv_block(cblock4[0], n_filters*16, dropout_prob=0.3, max_pooling=False) \n    \n    ublock6 = upsampling_block(cblock5[0], cblock4[1],  n_filters*8)\n    ublock7 = upsampling_block(ublock6, cblock3[1],  n_filters*4)\n    ublock8 = upsampling_block(ublock7, cblock2[1],  n_filters*2)\n    ublock9 = upsampling_block(ublock8, cblock1[1],  n_filters)\n\n    conv9 = tkl.Conv2D(n_filters,\n                 3,\n                 activation='relu',\n                 padding='same',\n                 kernel_initializer='he_normal')(ublock9)\n    conv10 = tkl.Conv2D(n_classes, 1, padding='same', activation='sigmoid')(conv9)\n    \n    model = tf.keras.Model(inputs=inputs, outputs=conv10)\n\n    return model","metadata":{"_uuid":"ff542701-bad7-430b-8985-6d96c9834a3a","_cell_guid":"70da80be-9942-47a5-ade0-ac3041f69b6a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T15:31:50.106846Z","iopub.execute_input":"2024-07-17T15:31:50.107206Z","iopub.status.idle":"2024-07-17T15:31:50.116788Z","shell.execute_reply.started":"2024-07-17T15:31:50.107178Z","shell.execute_reply":"2024-07-17T15:31:50.115892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{"_uuid":"fd893d09-c2d6-40ce-95f8-7905967c6361","_cell_guid":"a810e82f-8ad2-41bf-a2a2-3ff451ffb781","trusted":true}},{"cell_type":"code","source":"def dice_loss(y_true, y_pred, smooth=1):\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = K.sum(y_true_f * y_pred_f)\n    dice = (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n    dice = 1 - dice\n    \n    return dice","metadata":{"_uuid":"4e146957-ab69-4010-8b9e-586288a234d8","_cell_guid":"841b4c66-a7b0-46d4-b4b6-20eda16f647a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T15:31:54.492409Z","iopub.execute_input":"2024-07-17T15:31:54.492823Z","iopub.status.idle":"2024-07-17T15:31:54.498448Z","shell.execute_reply.started":"2024-07-17T15:31:54.492794Z","shell.execute_reply":"2024-07-17T15:31:54.497445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checkpoint_folder = '/checkpoints'\n\n# if not os.path.exists(checkpoint_folder):\n#     os.makedirs(checkpoint_folder)","metadata":{"execution":{"iopub.status.busy":"2024-07-17T15:37:27.488674Z","iopub.execute_input":"2024-07-17T15:37:27.489375Z","iopub.status.idle":"2024-07-17T15:37:27.493735Z","shell.execute_reply.started":"2024-07-17T15:37:27.489345Z","shell.execute_reply":"2024-07-17T15:37:27.492809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.listdir('/kaggle/working/checkpoints')","metadata":{"execution":{"iopub.status.busy":"2024-07-17T15:58:40.133768Z","iopub.execute_input":"2024-07-17T15:58:40.134696Z","iopub.status.idle":"2024-07-17T15:58:40.140416Z","shell.execute_reply.started":"2024-07-17T15:58:40.134660Z","shell.execute_reply":"2024-07-17T15:58:40.139580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.getcwd()","metadata":{"execution":{"iopub.status.busy":"2024-07-17T15:57:51.789318Z","iopub.execute_input":"2024-07-17T15:57:51.789694Z","iopub.status.idle":"2024-07-17T15:57:51.795825Z","shell.execute_reply.started":"2024-07-17T15:57:51.789665Z","shell.execute_reply":"2024-07-17T15:57:51.794926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import shutil\n# shutil.move('/kaggle/working/checkpoints/unet_dice_patches_e-4.keras', 'unet_dice_patches_e-4.keras')","metadata":{"execution":{"iopub.status.busy":"2024-07-17T15:59:26.218965Z","iopub.execute_input":"2024-07-17T15:59:26.219857Z","iopub.status.idle":"2024-07-17T15:59:26.225706Z","shell.execute_reply.started":"2024-07-17T15:59:26.219822Z","shell.execute_reply":"2024-07-17T15:59:26.224720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.path.join(checkpoint_folder, 'unet_dice_patches_e-4.keras')","metadata":{"execution":{"iopub.status.busy":"2024-07-17T15:29:03.332001Z","iopub.execute_input":"2024-07-17T15:29:03.332364Z","iopub.status.idle":"2024-07-17T15:29:03.338857Z","shell.execute_reply.started":"2024-07-17T15:29:03.332336Z","shell.execute_reply":"2024-07-17T15:29:03.337833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.listdir('/kaggle/working/checkpoints/checkpoints')","metadata":{"execution":{"iopub.status.busy":"2024-07-16T14:27:04.914297Z","iopub.execute_input":"2024-07-16T14:27:04.915070Z","iopub.status.idle":"2024-07-16T14:27:04.921290Z","shell.execute_reply.started":"2024-07-16T14:27:04.915040Z","shell.execute_reply":"2024-07-16T14:27:04.920304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# unet_dice: e-5, smooth 100, epoch 100, batch 64, val_acc 0.9969, val_loss 0.329\n# unet_dice_e-4: e-4, smooth 1, epoch 5, batch 128, lr_plato\n\ncheckpoint_path = os.path.join('patches_e-4.keras')\n\nearlystop = callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', mode='min', patience=2)\ncheckpoint = tf.keras.callbacks.ModelCheckpoint(\n    filepath=checkpoint_path,\n    monitor='val_accuracy',\n    mode='max',\n    save_best_only=True)\n\nlr_plato = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', patience=1, factor=0.1, mode='min')","metadata":{"_uuid":"61ce7fa4-f0f0-4a28-ae60-2990cd347dd5","_cell_guid":"8f163b7c-8e5d-4529-b675-d5351a3fc9d5","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T16:01:26.466376Z","iopub.execute_input":"2024-07-17T16:01:26.466729Z","iopub.status.idle":"2024-07-17T16:01:26.472727Z","shell.execute_reply.started":"2024-07-17T16:01:26.466703Z","shell.execute_reply":"2024-07-17T16:01:26.471851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_height = DATASET_IMG_SIZE\nimg_width = DATASET_IMG_SIZE\nnum_channels = 3\nBATCH_SIZE = 128\nBUFFER_SIZE = 4*BATCH_SIZE\nLR = 1e-4\n\n# train_dataset = processed_image_ds.cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE)\n# valid_dataset = valid_processed_image_ds.cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE)\n\nunet = unet_model((img_height, img_width, num_channels))\n\n# print(os.path.join('kaggle/working', 'unet_dice_patches_e-4.keras'))\nprev_checkpoint = 'patches_e-4_15epochs.keras'\n\nunet.load_weights(prev_checkpoint)\n\nunet.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=LR),\n              loss=dice_loss,\n              metrics=['accuracy'])","metadata":{"_uuid":"1077584a-d086-4742-9fd8-752cebd5c913","_cell_guid":"3f3a2ecb-349e-4d0b-9bf9-97e9e1f32d9a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T16:07:33.833553Z","iopub.execute_input":"2024-07-17T16:07:33.834191Z","iopub.status.idle":"2024-07-17T16:07:34.087462Z","shell.execute_reply.started":"2024-07-17T16:07:33.834162Z","shell.execute_reply":"2024-07-17T16:07:34.086321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_testdata(img_path):\n    \"\"\"\n    read and decode image file\n    :param img_path: image filename, string\n    :return: image tensor of dtype=uint8\n    \"\"\"\n    img = tf.io.read_file(img_path)\n    img = tf.io.decode_jpeg(img, channels=3)\n\n    return img","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def encode_for_submission(image):\n    \"\"\"\n    encode pixels from (row,column) format to [num_pixel, run_length]\n    for submission on airbus challenge\n    :param image: image array, array\n    :return: encoded string, str\n    \"\"\"\n    pairs = []\n\n    for col in range(image.shape[1]):\n        start = None\n        end = None\n        for row in range(image.shape[0]):\n            if image[row, col] == 1:\n                pixel_num = col*image.shape[1]+row+1\n                if not start:\n                    start = pixel_num\n                    pairs.append(start)\n                end = pixel_num\n            elif end:\n                pairs.append(end-pairs[-1]+1)\n                start = None\n                end = None\n\n    encoded = ' '.join(map(str, pairs))\n\n    return encoded","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_full_images(image, model):\n    \"\"\"\n    make prediction on full size image, for example (768,768)\n    or another square size height=width % 128 == 0\n    1.make patches from image\n    2.get predictions on these patches\n    3.reconstruct full size prediction from patches\n    :param image: image tensor\n    :param model: model to use for prediction\n    :return: prediction\n    \"\"\"\n    image = image.numpy()\n    patches = patchify(image, (DATASET_IMG_SIZE, DATASET_IMG_SIZE, 3), step=DATASET_IMG_SIZE)\n    patches = patches.reshape(36, DATASET_IMG_SIZE, DATASET_IMG_SIZE, 3)\n\n    preds = binarize(model.predict(patches))\n\n    n_cols = n_rows = int(IMG_SIZE / DATASET_IMG_SIZE)\n\n    preds = preds.reshape(n_rows, n_cols, DATASET_IMG_SIZE, DATASET_IMG_SIZE, 1)\n    preds = np.squeeze(preds)\n\n    reconstructed_pred = unpatchify(preds, (IMG_SIZE, IMG_SIZE))\n    reconstructed_pred = reconstructed_pred[..., np.newaxis]\n\n    return reconstructed_pred","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_folder = '/kaggle/input/airbus-ship-detection/test_v2'\n    \nx_test = []\nfor f in os.listdir(test_folder):\n    x_test.append(f)\n\ntest_dataset = tf.data.Dataset.from_tensor_slices(x_test)\ntest_dataset = test_dataset.map(preprocess_testdata)\n\nencoded_list = []\nfor i, test_img in enumerate(test_dataset):\n    pred = predict_full_images(test_img, unet)\n    encoded_list.append(encode_for_submission(pred))\n\ndf = pd.DataFrame({'ImageId':x_test, 'EncodedPixels':encoded_list})\ndf.to_csv('submission.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(processed_image_ds.element_spec)","metadata":{"_uuid":"6a37773e-ef80-4308-9cd3-3a17a26fa3b2","_cell_guid":"9f73aef6-6abb-4675-a6b4-08945f64547e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T15:33:15.966402Z","iopub.execute_input":"2024-07-17T15:33:15.967286Z","iopub.status.idle":"2024-07-17T15:33:15.971842Z","shell.execute_reply.started":"2024-07-17T15:33:15.967252Z","shell.execute_reply":"2024-07-17T15:33:15.970990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### load previous history if available","metadata":{"_uuid":"43e75881-98dd-4db5-9fad-908c9d56cd3b","_cell_guid":"8ac7f8b1-7ef9-4ff5-a573-bd0701443916","trusted":true}},{"cell_type":"code","source":"# with open(os.path.join(checkpoint_folder, 'unet_dice_patches_history.pkl'), \"rb\") as file_pi:\n#     history0 = pickle.load(file_pi)","metadata":{"_uuid":"c50c483d-ae84-4e47-8455-75e571af1497","_cell_guid":"c0efa7af-1bcb-4617-a22c-f0c29e0c58c8","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-14T18:48:50.211740Z","iopub.execute_input":"2024-07-14T18:48:50.212026Z","iopub.status.idle":"2024-07-14T18:48:50.221431Z","shell.execute_reply.started":"2024-07-14T18:48:50.212003Z","shell.execute_reply":"2024-07-14T18:48:50.220594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# EPOCHS = 15","metadata":{"_uuid":"641a857a-4c8e-41d1-ad99-1bb1649e1d62","_cell_guid":"735a209e-302d-493e-bfb0-c1587ef1491e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T15:33:22.509935Z","iopub.execute_input":"2024-07-17T15:33:22.510297Z","iopub.status.idle":"2024-07-17T15:33:22.514445Z","shell.execute_reply.started":"2024-07-17T15:33:22.510267Z","shell.execute_reply":"2024-07-17T15:33:22.513451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history = unet.fit(train_dataset, epochs=EPOCHS, validation_data=valid_dataset, callbacks=[checkpoint, earlystop, lr_plato])","metadata":{"_uuid":"583c9707-8919-4a55-a7fc-796e1231fba1","_cell_guid":"c442f80f-19a5-4e3b-8f27-99e4c9997ed1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T06:55:24.691309Z","iopub.execute_input":"2024-07-17T06:55:24.691696Z","iopub.status.idle":"2024-07-17T07:32:08.857675Z","shell.execute_reply.started":"2024-07-17T06:55:24.691668Z","shell.execute_reply":"2024-07-17T07:32:08.856627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with open(os.path.join(checkpoint_folder, 'history_e4_15epochs.pkl'), 'wb') as file_pi:\n#     pickle.dump(history.history, file_pi)","metadata":{"_uuid":"8ffaecf5-0708-402d-b6c3-2dfce9d9af39","_cell_guid":"35739067-3f18-40f4-8b8d-60a69338bba4","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T07:42:11.954431Z","iopub.execute_input":"2024-07-17T07:42:11.955089Z","iopub.status.idle":"2024-07-17T07:42:11.961521Z","shell.execute_reply.started":"2024-07-17T07:42:11.955053Z","shell.execute_reply":"2024-07-17T07:42:11.960529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from IPython.display import FileLink, FileLinks","metadata":{"execution":{"iopub.status.busy":"2024-07-17T18:38:21.450699Z","iopub.execute_input":"2024-07-17T18:38:21.451102Z","iopub.status.idle":"2024-07-17T18:38:21.455823Z","shell.execute_reply.started":"2024-07-17T18:38:21.451074Z","shell.execute_reply":"2024-07-17T18:38:21.454719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cd /kaggle/working","metadata":{"execution":{"iopub.status.busy":"2024-07-17T18:39:23.946330Z","iopub.execute_input":"2024-07-17T18:39:23.947379Z","iopub.status.idle":"2024-07-17T18:39:23.953858Z","shell.execute_reply.started":"2024-07-17T18:39:23.947336Z","shell.execute_reply":"2024-07-17T18:39:23.952821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# name = 'history_e4_15epochs.pkl'\n# path = '.'","metadata":{"execution":{"iopub.status.busy":"2024-07-17T18:43:09.295155Z","iopub.execute_input":"2024-07-17T18:43:09.296043Z","iopub.status.idle":"2024-07-17T18:43:09.300222Z","shell.execute_reply.started":"2024-07-17T18:43:09.296009Z","shell.execute_reply":"2024-07-17T18:43:09.299208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def find(name, path):\n#     for root, dirs, files in os.walk(path):\n#         if name in files:\n            return os.path.join(root, name)","metadata":{"execution":{"iopub.status.busy":"2024-07-17T18:43:11.334016Z","iopub.execute_input":"2024-07-17T18:43:11.334377Z","iopub.status.idle":"2024-07-17T18:43:11.340556Z","shell.execute_reply.started":"2024-07-17T18:43:11.334351Z","shell.execute_reply":"2024-07-17T18:43:11.339382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# FileLink(r'checkpoints/history_e4_15epochs.pkl')","metadata":{"execution":{"iopub.status.busy":"2024-07-17T18:39:33.262419Z","iopub.execute_input":"2024-07-17T18:39:33.263060Z","iopub.status.idle":"2024-07-17T18:39:33.269655Z","shell.execute_reply.started":"2024-07-17T18:39:33.263026Z","shell.execute_reply":"2024-07-17T18:39:33.268577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# FileLink(r'unet_dice_patches_history_e4.pkl')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plot results","metadata":{"_uuid":"d788cef8-ea3f-4977-84f2-27f3417f14cc","_cell_guid":"fcf8220e-5530-46ba-a7f3-b157e71cdc2c","trusted":true}},{"cell_type":"code","source":"# plt.figure(figsize=(8, 4))\n\n# plt.plot(history.history[\"accuracy\"], label='accuracy')\n# plt.plot(history.history[\"val_accuracy\"], label='val_accuracy')\n# plt.title('unet with dice accuracy')\n# plt.xlabel('epochs')\n# plt.ylabel('accuracy')\n# plt.legend()\n# plt.show()","metadata":{"_uuid":"7a13d1f9-49c2-4a0d-846a-927cd47461d3","_cell_guid":"d1ea3996-64e0-40ff-ba49-a62e45667a79","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predictions","metadata":{"_uuid":"fe8c0d75-9263-4daa-a97d-b397409b5be0","_cell_guid":"bb88ff72-4d4f-4d74-b11c-caf3d05ab64a","trusted":true}},{"cell_type":"code","source":"def predict_full_image(image):\n    \n    image = image.numpy()\n    \n    patches = patchify(image, (DATASET_IMG_SIZE, DATASET_IMG_SIZE, 3), step=DATASET_IMG_SIZE)\n    patches = patches.reshape(36, DATASET_IMG_SIZE, DATASET_IMG_SIZE, 3)\n    \n    preds = binarize(unet.predict(patches, batch_size=36))\n    \n    n_cols = n_rows = int(IMG_SIZE / DATASET_IMG_SIZE)\n\n    preds = preds.reshape(n_rows, n_rows, DATASET_IMG_SIZE, DATASET_IMG_SIZE, 1)\n    preds = np.squeeze(preds)\n\n    reconstructed_pred = unpatchify(preds, (IMG_SIZE, IMG_SIZE))\n    reconstructed_pred = reconstructed_pred[..., np.newaxis]\n    \n    return  reconstructed_pred","metadata":{"execution":{"iopub.status.busy":"2024-07-17T07:43:18.490684Z","iopub.execute_input":"2024-07-17T07:43:18.491085Z","iopub.status.idle":"2024-07-17T07:43:18.498882Z","shell.execute_reply.started":"2024-07-17T07:43:18.491052Z","shell.execute_reply":"2024-07-17T07:43:18.497752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def binarize(preds):\n    return preds > 0.5","metadata":{"execution":{"iopub.status.busy":"2024-07-17T07:43:21.820277Z","iopub.execute_input":"2024-07-17T07:43:21.820695Z","iopub.status.idle":"2024-07-17T07:43:21.825778Z","shell.execute_reply.started":"2024-07-17T07:43:21.820660Z","shell.execute_reply":"2024-07-17T07:43:21.824636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_predictions(dataset=None, num=1):\n\n    if dataset:\n        for image_batch, mask_batch in dataset.take(num):\n            pred_masks_for_batch = binarize(unet.predict(image_batch))\n#             pred_masks_for_batch = list(map(predict_from_patches, image_batch))\n            pred = pred_masks_for_batch[0]\n            #for img, mask, pred in zip(image_batch, mask_batch, pred_masks_for_batch):\n        \n            display([image_batch[0], mask_batch[0], pred])\n                #display([img,mask,pred])\n    else:\n        display([sample_image, sample_mask,\n             create_mask(model.predict(sample_image[tf.newaxis, ...]))])","metadata":{"_uuid":"341a6d2d-f5dc-4b6b-8457-66cd833b3ffa","_cell_guid":"b0574306-f529-416c-a1e6-1900bfed9ef7","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T07:43:23.955279Z","iopub.execute_input":"2024-07-17T07:43:23.955654Z","iopub.status.idle":"2024-07-17T07:43:23.963196Z","shell.execute_reply.started":"2024-07-17T07:43:23.955625Z","shell.execute_reply":"2024-07-17T07:43:23.961870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show_predictions(train_dataset, 10)","metadata":{"_uuid":"0fbd2b8e-3ac5-4554-ba82-a17a09033ae6","_cell_guid":"2c76fb95-02e1-4833-ac72-8e179380a75c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T07:43:27.545363Z","iopub.execute_input":"2024-07-17T07:43:27.546148Z","iopub.status.idle":"2024-07-17T07:43:47.157143Z","shell.execute_reply.started":"2024-07-17T07:43:27.546103Z","shell.execute_reply":"2024-07-17T07:43:47.156148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test model","metadata":{}},{"cell_type":"code","source":"# test_folder = '/kaggle/input/airbus-ship-detection/test_v2'","metadata":{"execution":{"iopub.status.busy":"2024-07-17T07:44:21.235947Z","iopub.execute_input":"2024-07-17T07:44:21.236767Z","iopub.status.idle":"2024-07-17T07:44:21.241480Z","shell.execute_reply.started":"2024-07-17T07:44:21.236724Z","shell.execute_reply":"2024-07-17T07:44:21.240340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_test = []\n\n# for f in os.listdir(test_folder):\n#     X_test.append(os.path.join(test_folder, f))","metadata":{"_uuid":"61dc413c-fe98-4c46-a552-8d16b2fa1d8a","_cell_guid":"e8d025c8-c736-47a8-a959-ea6f8c98ef65","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-07-17T07:44:25.885137Z","iopub.execute_input":"2024-07-17T07:44:25.886101Z","iopub.status.idle":"2024-07-17T07:44:26.816222Z","shell.execute_reply.started":"2024-07-17T07:44:25.886066Z","shell.execute_reply":"2024-07-17T07:44:26.815138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# len(X_test), X_test[0]","metadata":{"execution":{"iopub.status.busy":"2024-07-17T07:44:28.588059Z","iopub.execute_input":"2024-07-17T07:44:28.588462Z","iopub.status.idle":"2024-07-17T07:44:28.595318Z","shell.execute_reply.started":"2024-07-17T07:44:28.588431Z","shell.execute_reply":"2024-07-17T07:44:28.594317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def prepare_testdata(img_path):\n#     img = tf.io.read_file(img_path)\n#     img = tf.io.decode_jpeg(img, channels=3)\n#     return img","metadata":{"execution":{"iopub.status.busy":"2024-07-17T07:44:33.140092Z","iopub.execute_input":"2024-07-17T07:44:33.141045Z","iopub.status.idle":"2024-07-17T07:44:33.146946Z","shell.execute_reply.started":"2024-07-17T07:44:33.141000Z","shell.execute_reply":"2024-07-17T07:44:33.145756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_dataset = tf.data.Dataset.from_tensor_slices(X_test[:10])\n# test_dataset = test_dataset.map(prepare_testdata)\n# preds = list(map(predict_full_image, test_dataset))","metadata":{"execution":{"iopub.status.busy":"2024-07-17T07:44:35.831006Z","iopub.execute_input":"2024-07-17T07:44:35.831433Z","iopub.status.idle":"2024-07-17T07:44:52.478173Z","shell.execute_reply.started":"2024-07-17T07:44:35.831399Z","shell.execute_reply":"2024-07-17T07:44:52.477146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for img, pred in zip(test_dataset, preds):\n#     display([img, pred])","metadata":{"execution":{"iopub.status.busy":"2024-07-17T07:44:54.469312Z","iopub.execute_input":"2024-07-17T07:44:54.470003Z","iopub.status.idle":"2024-07-17T07:44:58.995004Z","shell.execute_reply.started":"2024-07-17T07:44:54.469967Z","shell.execute_reply":"2024-07-17T07:44:58.994089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}