{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":9988,"databundleVersionId":868324,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":6617956,"sourceType":"kernelVersion"},{"sourceId":299660,"sourceType":"modelInstanceVersion","isSourceIdPinned":false,"modelInstanceId":256055,"modelId":277371},{"sourceId":299664,"sourceType":"modelInstanceVersion","isSourceIdPinned":false,"modelInstanceId":256059,"modelId":277375}],"dockerImageVersionId":12836,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom skimage.io import imread\nimport matplotlib.pyplot as plt\nfrom skimage.segmentation import mark_boundaries\nfrom skimage.util.montage import montage2d as montage\nmontage_rgb = lambda x: np.stack([montage(x[:, :, :, i]) for i in range(x.shape[3])], -1)\nship_dir = '../input/airbus-ship-detection/'\ntrain_image_dir = os.path.join(ship_dir, 'train_v2')\ntest_image_dir = os.path.join(ship_dir, 'test_v2')\nimport gc; gc.enable() # memory is tight\n\nfrom skimage.morphology import label\ndef multi_rle_encode(img):\n    labels = label(img[:, :, 0])\n    return [rle_encode(labels==k) for k in np.unique(labels[labels>0])]\n\n# ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode\ndef rle_encode(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = img.T.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n\ndef rle_decode(mask_rle, shape=(768, 768)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height,width) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T  # Needed to align to RLE direction\n\ndef masks_as_image(in_mask_list):\n    # Take the individual ship masks and create a single mask array for all ships\n    all_masks = np.zeros((768, 768), dtype = np.int16)\n    #if isinstance(in_mask_list, list):\n    for mask in in_mask_list:\n        if isinstance(mask, str):\n            all_masks += rle_decode(mask)\n    return np.expand_dims(all_masks, -1)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-03-24T07:50:16.890539Z","iopub.execute_input":"2025-03-24T07:50:16.890835Z","iopub.status.idle":"2025-03-24T07:50:17.748173Z","shell.execute_reply.started":"2025-03-24T07:50:16.890783Z","shell.execute_reply":"2025-03-24T07:50:17.747599Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls ../input/baseline-u-net-model-part-1/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T07:53:56.507641Z","iopub.execute_input":"2025-03-24T07:53:56.507965Z","iopub.status.idle":"2025-03-24T07:53:57.579049Z","shell.execute_reply.started":"2025-03-24T07:53:56.507915Z","shell.execute_reply":"2025-03-24T07:53:57.578106Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras import models, layers\nfullres_model = models.load_model('../input/baseline-u-net-model-part-1/fullres_model.h5', compile=False)\nseg_in_shape = fullres_model.get_input_shape_at(0)[1:3]\nseg_out_shape = fullres_model.get_output_shape_at(0)[1:3]\nprint(seg_in_shape, '->', seg_out_shape)","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"execution":{"iopub.status.busy":"2025-03-24T08:02:55.840224Z","iopub.execute_input":"2025-03-24T08:02:55.840529Z","iopub.status.idle":"2025-03-24T08:02:56.963368Z","shell.execute_reply.started":"2025-03-24T08:02:55.840471Z","shell.execute_reply":"2025-03-24T08:02:56.962503Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load Test Data","metadata":{"_uuid":"d5dbb2d3ab3942262209c372309749734d3d2e2d"}},{"cell_type":"code","source":"test_paths = os.listdir(test_image_dir)\nprint(len(test_paths), 'test images found')","metadata":{"trusted":true,"_uuid":"56cbae027d512634ad9decef08a357131a9b04db","execution":{"iopub.status.busy":"2025-03-24T08:02:59.665898Z","iopub.execute_input":"2025-03-24T08:02:59.666163Z","iopub.status.idle":"2025-03-24T08:02:59.675617Z","shell.execute_reply.started":"2025-03-24T08:02:59.666123Z","shell.execute_reply":"2025-03-24T08:02:59.674803Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, m_axs = plt.subplots(8, 2, figsize = (10, 40))\nfor (ax1, ax2), c_img_name in zip(m_axs, test_paths):\n    c_path = os.path.join(test_image_dir, c_img_name)\n    c_img = imread(c_path)\n    first_img = np.expand_dims(c_img, 0)/255.0\n    first_seg = fullres_model.predict(first_img)\n    ax1.imshow(first_img[0])\n    ax1.set_title('Image')\n    ax2.imshow(first_seg[0, :, :, 0], vmin = 0, vmax = 1)\n    ax2.set_title('Prediction')\nfig.savefig('test_predictions.png')","metadata":{"trusted":true,"_uuid":"a46bf7473648a41796a12bfc4993cb9539a70c7f","_kg_hide-input":false},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Create Submission\nHere we run the analysis on all of the images and prepare the submission.","metadata":{"_uuid":"1194b8ef2e4098857c093ee1106a05a969769a2c"}},{"cell_type":"code","source":"from tqdm import tqdm_notebook\nfrom skimage.morphology import binary_opening, disk\nout_pred_rows = []\nfor c_img_name in tqdm_notebook(test_paths):\n    c_path = os.path.join(test_image_dir, c_img_name)\n    c_img = imread(c_path)\n    c_img = np.expand_dims(c_img, 0)/255.0\n    cur_seg = fullres_model.predict(c_img)[0]\n    cur_seg = binary_opening(cur_seg>0.5, np.expand_dims(disk(2), -1))\n    cur_rles = multi_rle_encode(cur_seg)\n    if len(cur_rles)>0:\n        for c_rle in cur_rles:\n            out_pred_rows += [{'ImageId': c_img_name, 'EncodedPixels': c_rle}]\n    else:\n        out_pred_rows += [{'ImageId': c_img_name, 'EncodedPixels': None}]\n    gc.collect()","metadata":{"trusted":true,"_uuid":"14578ebe1a7f1270d88c4782e308b2ce72f38597","execution":{"iopub.status.busy":"2025-03-24T08:03:12.707461Z","iopub.execute_input":"2025-03-24T08:03:12.707795Z","iopub.status.idle":"2025-03-24T08:52:46.251042Z","shell.execute_reply.started":"2025-03-24T08:03:12.707709Z","shell.execute_reply":"2025-03-24T08:52:46.250345Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df = pd.DataFrame(out_pred_rows)[['ImageId', 'EncodedPixels']]\nsubmission_df.to_csv('submission.csv', index=False)\nsubmission_df.sample(300)","metadata":{"trusted":true,"_uuid":"503c222c6a1c730436554b3f18e759cd0c025deb","execution":{"iopub.status.busy":"2025-03-24T08:54:13.790888Z","iopub.execute_input":"2025-03-24T08:54:13.791188Z","iopub.status.idle":"2025-03-24T08:54:13.960064Z","shell.execute_reply.started":"2025-03-24T08:54:13.791134Z","shell.execute_reply":"2025-03-24T08:54:13.959359Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# #testing for another model, use another kernel\n# import os\n# import tensorflow as tf\n# from tensorflow import keras\n# from tensorflow.keras.saving import register_keras_serializable\n# import cv2\n# import numpy as np\n# import matplotlib.pyplot as plt\n\n# test_image_dir = \"../input/airbus-ship-detection/test_v2\"\n# # model_dir = \"../input/model/keras/default/1/model.keras\"\n# model_dir = \"../input/unet_ship_segmentation/keras/default/1/unet_ship_segmentation.keras\"\n# images = os.listdir(test_image_dir)\n\n# # Define MeanIoU metric globally\n# mean_iou_metric = keras.metrics.MeanIoU(num_classes=2)\n\n# # Custom metric function\n# @register_keras_serializable(package='Custom', name='mean_iou')\n# def mean_iou(y_true, y_pred):\n#     y_pred = K.cast(y_pred > 0.5, 'float32')  # Threshold at 0.5\n#     mean_iou_metric.update_state(y_true, y_pred)\n#     return mean_iou_metric.result()\n\n# model = keras.models.load_model(model_dir)\n\n# #test\n# img = cv2.imread(test_image_dir + \"/\" + \"baebabc38.jpg\")\n# img = img / 255.0\n# img_input = np.expand_dims(img, axis=0)\n\n# prediction = model.predict(img_input).squeeze()\n# # binary_mask = (prediction >= 0.01).astype(np.uint8)\n\n# plt.figure(figsize=(15, 15))\n# plt.subplot(1, 2, 1)\n# plt.imshow(img)\n# plt.title(\"Original Image\")\n# plt.axis(\"off\")\n\n# plt.subplot(1, 2, 2)\n# plt.imshow(prediction, cmap=\"gray\")\n# plt.title(\"Predicted Mask\")\n# plt.axis(\"off\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T12:26:15.387975Z","iopub.execute_input":"2025-03-24T12:26:15.388333Z","iopub.status.idle":"2025-03-24T12:26:15.393305Z","shell.execute_reply.started":"2025-03-24T12:26:15.388271Z","shell.execute_reply":"2025-03-24T12:26:15.392396Z"}},"outputs":[],"execution_count":null}]}