{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"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":"gpu","dataSources":[{"sourceId":9988,"databundleVersionId":868324,"sourceType":"competition"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install -U segmentation-models","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:17.542018Z","iopub.execute_input":"2024-07-02T16:36:17.542374Z","iopub.status.idle":"2024-07-02T16:36:31.560864Z","shell.execute_reply.started":"2024-07-02T16:36:17.542347Z","shell.execute_reply":"2024-07-02T16:36:31.559872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport tensorflow as tf \nimport os\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom os import listdir\nfrom time import process_time, time\nfrom math import ceil\nfrom PIL import Image\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.preprocessing import image\n\n\nos.environ[\"SM_FRAMEWORK\"] = \"tf.keras\"\n\nimport segmentation_models as sm","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:31.563157Z","iopub.execute_input":"2024-07-02T16:36:31.563963Z","iopub.status.idle":"2024-07-02T16:36:44.851912Z","shell.execute_reply.started":"2024-07-02T16:36:31.563919Z","shell.execute_reply":"2024-07-02T16:36:44.850981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed=1):\n    np.random.seed(seed)\n    tf.random.set_seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n\nset_seed()","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:44.857077Z","iopub.execute_input":"2024-07-02T16:36:44.857345Z","iopub.status.idle":"2024-07-02T16:36:44.862522Z","shell.execute_reply.started":"2024-07-02T16:36:44.857322Z","shell.execute_reply":"2024-07-02T16:36:44.861666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir_path = '/kaggle/input/airbus-ship-detection/'\n\ntest_dir = data_dir_path + 'test_v2/'\ntrain_dir = data_dir_path + 'train_v2/'\n\ntrain_segmentation = pd.read_csv(data_dir_path + 'train_ship_segmentations_v2.csv', index_col='ImageId')\n\nCOEF_SMALLER_IMG = 2\nIMG_H = 768 // COEF_SMALLER_IMG\nIMG_W = 768 // COEF_SMALLER_IMG\nBATCH_SIZE = 32","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:44.863690Z","iopub.execute_input":"2024-07-02T16:36:44.863945Z","iopub.status.idle":"2024-07-02T16:36:45.905050Z","shell.execute_reply.started":"2024-07-02T16:36:44.863923Z","shell.execute_reply":"2024-07-02T16:36:45.904254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# broken_imgs = []\n\n# start_time = time()\n# for i, img in enumerate(listdir(train_dir)):\n#     if not (img.endswith('.jpg')):\n#         print(img, 'not jpg')\n#         broken_imgs.append(img)\n#     elif os.path.getsize(train_dir + img) < 3000:\n#         print(img, 'size less 3000 bytes')\n#         broken_imgs.append(img)\n\n#     if i % 10000 == 0:\n#         temp = time()\n#         print(i, temp - start_time)\n#         start_time = temp\n        \n        \n# if len(broken_imgs) == 0:\n#     print('There is no broken imgs')\n# else:\n#     print('There is broken imgs', broken_imgs)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:45.906052Z","iopub.execute_input":"2024-07-02T16:36:45.906312Z","iopub.status.idle":"2024-07-02T16:36:45.910775Z","shell.execute_reply.started":"2024-07-02T16:36:45.906291Z","shell.execute_reply":"2024-07-02T16:36:45.909814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_segmentation['is_any_ships'] = train_segmentation['EncodedPixels'].isnull().map({True:0, False:1})\ntrain_segmentation['is_any_ships'] ","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:45.911963Z","iopub.execute_input":"2024-07-02T16:36:45.912307Z","iopub.status.idle":"2024-07-02T16:36:45.997359Z","shell.execute_reply.started":"2024-07-02T16:36:45.912275Z","shell.execute_reply":"2024-07-02T16:36:45.996440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count_imgs_with_ships = train_segmentation[train_segmentation['is_any_ships'] == 1].index.nunique()\n\ncount_imgs = len(list(train_segmentation.index.unique()))\n\ncount_ships = len(train_segmentation[train_segmentation['is_any_ships'] == 1].index)\n\ncount_imgs_no_ships = count_imgs - count_imgs_with_ships","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:45.998577Z","iopub.execute_input":"2024-07-02T16:36:45.998920Z","iopub.status.idle":"2024-07-02T16:36:46.104114Z","shell.execute_reply.started":"2024-07-02T16:36:45.998894Z","shell.execute_reply":"2024-07-02T16:36:46.103093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stats_cols = ['Images', 'have no ships', 'have ships', 'overall ships']\nstats_data = [[count_imgs, count_imgs_no_ships, count_imgs_with_ships, count_ships]]\n\nstats_df = pd.DataFrame(stats_data, columns=stats_cols)\nstats_df","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:46.105299Z","iopub.execute_input":"2024-07-02T16:36:46.105603Z","iopub.status.idle":"2024-07-02T16:36:46.118471Z","shell.execute_reply.started":"2024-07-02T16:36:46.105578Z","shell.execute_reply":"2024-07-02T16:36:46.117470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(stats_df)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:46.123274Z","iopub.execute_input":"2024-07-02T16:36:46.123548Z","iopub.status.idle":"2024-07-02T16:36:46.326560Z","shell.execute_reply.started":"2024-07-02T16:36:46.123525Z","shell.execute_reply":"2024-07-02T16:36:46.325590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def drop_values(li: list, cut_part: float):\n    for n in range(int(len(li) * cut_part)):\n        index = np.random.randint(0, len(li))\n        li.pop(index)\n\n        \ncut_no_ship_imgs = list(train_segmentation[train_segmentation['is_any_ships'] == 0].index)\ncut_part = 0.8\n\ndrop_values(cut_no_ship_imgs, cut_part)\n    \nlen(cut_no_ship_imgs)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:46.327635Z","iopub.execute_input":"2024-07-02T16:36:46.327910Z","iopub.status.idle":"2024-07-02T16:36:48.147442Z","shell.execute_reply.started":"2024-07-02T16:36:46.327886Z","shell.execute_reply":"2024-07-02T16:36:48.146471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"split_part = 0.25\ntrain_with_ship_imgs = list(train_segmentation[train_segmentation['is_any_ships'] == 1].index.unique())\nvalid_with_ship_imgs = list(train_with_ship_imgs)\n\ndrop_values(train_with_ship_imgs, split_part)\nvalid_with_ship_imgs = list(set(valid_with_ship_imgs) - set(train_with_ship_imgs))\n\nlen(train_with_ship_imgs), len(valid_with_ship_imgs)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:48.148503Z","iopub.execute_input":"2024-07-02T16:36:48.148764Z","iopub.status.idle":"2024-07-02T16:36:48.289411Z","shell.execute_reply.started":"2024-07-02T16:36:48.148741Z","shell.execute_reply":"2024-07-02T16:36:48.288401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_no_ship_imgs = list(cut_no_ship_imgs)\nvalid_no_ship_imgs = list(cut_no_ship_imgs)\n\ndrop_values(train_no_ship_imgs, split_part)\nvalid_no_ship_imgs = list(set(valid_no_ship_imgs) - set(train_no_ship_imgs))\n\nlen(train_no_ship_imgs), len(valid_no_ship_imgs)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:48.290676Z","iopub.execute_input":"2024-07-02T16:36:48.291056Z","iopub.status.idle":"2024-07-02T16:36:48.368910Z","shell.execute_reply.started":"2024-07-02T16:36:48.291005Z","shell.execute_reply":"2024-07-02T16:36:48.367875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs = train_with_ship_imgs + train_no_ship_imgs\nvalid_imgs = valid_with_ship_imgs + valid_no_ship_imgs\nnp.random.shuffle(train_imgs)\nnp.random.shuffle(valid_imgs)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:48.370275Z","iopub.execute_input":"2024-07-02T16:36:48.370677Z","iopub.status.idle":"2024-07-02T16:36:48.381367Z","shell.execute_reply.started":"2024-07-02T16:36:48.370645Z","shell.execute_reply":"2024-07-02T16:36:48.380371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_paint_pixels(rle, arr1d):\n    rl = rle.split()\n\n    for i in range(len(rl) // 2):\n        pixel = int(rl[i * 2])\n        length = int(rl[i * 2 + 1])\n        arr1d[pixel:pixel+length] = 1\n        \n    return arr1d\n\n\ndef rle_decode(rle_list, coef_smaller_img=COEF_SMALLER_IMG):    \n    if type(rle_list) is float:\n        return np.zeros((IMG_H, IMG_H, 1), dtype=np.float32)\n    \n    arr_for_img = np.zeros((768 * 768), dtype=np.float32)\n    \n    if type(rle_list) is str:\n        rle_paint_pixels(rle_list, arr_for_img)\n    else: \n        for rle in rle_list:\n            rle_paint_pixels(rle, arr_for_img)\n            \n    arr_for_img = arr_for_img.reshape((768, 768))\n    arr_for_img = arr_for_img.T\n    arr_for_img = arr_for_img.reshape((768, 768, 1))    \n    arr_for_img = arr_for_img[::coef_smaller_img, ::coef_smaller_img]\n    return arr_for_img\n\n\ndef rle_encode(mask_arr1d):\n    '''\n    Returns run-length encoding\n    with ALL ships in the mask\n    '''\n    \n    s = ''\n    \n    length = 1\n    one_pair = False\n    for i, pixel in enumerate(mask_arr1d):\n        if pixel == 1 and one_pair is False:\n            s += str(i) + ' '\n            one_pair = True\n        elif pixel == 1:\n            length += 1\n        elif pixel == 0 and one_pair is True:\n            s += str(length)  + ' '\n            one_pair = False\n            length = 1\n            \n    return s.removesuffix(' ')","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:48.382465Z","iopub.execute_input":"2024-07-02T16:36:48.382786Z","iopub.status.idle":"2024-07-02T16:36:48.396269Z","shell.execute_reply.started":"2024-07-02T16:36:48.382761Z","shell.execute_reply":"2024-07-02T16:36:48.395453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_test = Image.open(train_dir + '/e6fd0c12e.jpg')\ntest_arr = image.img_to_array(img_test)\n\nprint(test_arr.shape)\ntest_arr = test_arr[::COEF_SMALLER_IMG, ::COEF_SMALLER_IMG]\nprint(test_arr.shape)\nimage.array_to_img(test_arr)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:48.397223Z","iopub.execute_input":"2024-07-02T16:36:48.397460Z","iopub.status.idle":"2024-07-02T16:36:48.503108Z","shell.execute_reply.started":"2024-07-02T16:36:48.397440Z","shell.execute_reply":"2024-07-02T16:36:48.502228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_segmentation.loc['e6fd0c12e.jpg']['EncodedPixels']","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:48.504259Z","iopub.execute_input":"2024-07-02T16:36:48.504578Z","iopub.status.idle":"2024-07-02T16:36:48.513287Z","shell.execute_reply.started":"2024-07-02T16:36:48.504551Z","shell.execute_reply":"2024-07-02T16:36:48.512280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rle_encode works faster with float64 array than float32 one\ntest_arr = rle_decode(train_segmentation.loc['e6fd0c12e.jpg']['EncodedPixels'], coef_smaller_img=1).astype(np.float64)\n\nimage.array_to_img(test_arr)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:48.514310Z","iopub.execute_input":"2024-07-02T16:36:48.514608Z","iopub.status.idle":"2024-07-02T16:36:48.542896Z","shell.execute_reply.started":"2024-07-02T16:36:48.514583Z","shell.execute_reply":"2024-07-02T16:36:48.542017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# make 1d array for rle encoding\nprint(test_arr.shape)\ntest_arr = test_arr.reshape((768, 768)).T\nprint(test_arr.shape)\ntest_arr = test_arr.reshape((768 * 768))\nprint(test_arr.shape)\nprint(test_arr.dtype, type(test_arr))\n\nrle_encode(test_arr)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:48.544139Z","iopub.execute_input":"2024-07-02T16:36:48.544625Z","iopub.status.idle":"2024-07-02T16:36:48.833478Z","shell.execute_reply.started":"2024-07-02T16:36:48.544593Z","shell.execute_reply":"2024-07-02T16:36:48.832538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.array_to_img(rle_decode(rle_encode(test_arr), coef_smaller_img=1))","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:48.834600Z","iopub.execute_input":"2024-07-02T16:36:48.834891Z","iopub.status.idle":"2024-07-02T16:36:49.127917Z","shell.execute_reply.started":"2024-07-02T16:36:48.834866Z","shell.execute_reply":"2024-07-02T16:36:49.126936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_gen_train_dir_Xy(imgs_list):\n    n_batches = len(imgs_list) // BATCH_SIZE\n    \n    for batch_i in range(n_batches):\n        start_batch = batch_i * BATCH_SIZE\n        end_batch = batch_i * BATCH_SIZE + BATCH_SIZE\n        \n        batch_img = np.zeros((BATCH_SIZE, IMG_H, IMG_W, 3), dtype=np.float32)\n        batch_target = np.zeros((BATCH_SIZE, IMG_H, IMG_W, 1), dtype=np.float32)\n        \n        for i, path_img in enumerate(imgs_list[start_batch:end_batch]):\n            img = image.img_to_array(Image.open(train_dir + path_img), dtype=np.float32)\n            img = img[::COEF_SMALLER_IMG, ::COEF_SMALLER_IMG]\n            target = rle_decode(train_segmentation.loc[path_img]['EncodedPixels'])\n            \n            batch_img[i] = img\n            batch_target[i] = target\n            \n        yield batch_img, batch_target\n        \n\ndef gen_train():\n    return make_gen_train_dir_Xy(train_imgs)\n    \ndef gen_valid():\n    return make_gen_train_dir_Xy(valid_imgs)     ","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:49.129129Z","iopub.execute_input":"2024-07-02T16:36:49.129429Z","iopub.status.idle":"2024-07-02T16:36:49.137678Z","shell.execute_reply.started":"2024-07-02T16:36:49.129404Z","shell.execute_reply":"2024-07-02T16:36:49.136700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = tf.data.Dataset.from_generator(\n         gen_train,\n         output_signature=(\n             tf.TensorSpec(shape=(BATCH_SIZE, IMG_H, IMG_W, 3), dtype=tf.float32),\n             tf.TensorSpec(shape=(BATCH_SIZE, IMG_H, IMG_W, 1), dtype=tf.float32)\n         )\n).repeat().shuffle(2, reshuffle_each_iteration=None)\n\nvalid_data = tf.data.Dataset.from_generator(\n         gen_valid,\n         output_signature=(\n             tf.TensorSpec(shape=(BATCH_SIZE, IMG_H, IMG_W, 3), dtype=tf.float32),\n             tf.TensorSpec(shape=(BATCH_SIZE, IMG_H, IMG_W, 1), dtype=tf.float32)\n         )\n).repeat().shuffle(2, reshuffle_each_iteration=None)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:49.138853Z","iopub.execute_input":"2024-07-02T16:36:49.139248Z","iopub.status.idle":"2024-07-02T16:36:49.818747Z","shell.execute_reply.started":"2024-07-02T16:36:49.139217Z","shell.execute_reply":"2024-07-02T16:36:49.817699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = sm.Unet(\n    backbone_name='resnet50',\n    encoder_weights='imagenet',\n    activation='sigmoid',\n    input_shape=(IMG_H, IMG_W, 3),\n)\n\nmodel.compile(\n    tf.keras.optimizers.Adam(learning_rate=0.0003),\n    loss=sm.losses.bce_dice_loss,\n    metrics=[sm.metrics.iou_score],\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:38:23.106145Z","iopub.execute_input":"2024-07-02T16:38:23.106878Z","iopub.status.idle":"2024-07-02T16:38:25.779724Z","shell.execute_reply.started":"2024-07-02T16:38:23.106846Z","shell.execute_reply":"2024-07-02T16:38:25.778725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:49.829416Z","iopub.status.idle":"2024-07-02T16:36:49.829724Z","shell.execute_reply.started":"2024-07-02T16:36:49.829572Z","shell.execute_reply":"2024-07-02T16:36:49.829586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.load_weights('')\nearly_stopping = tf.keras.callbacks.EarlyStopping(\n    monitor='val_loss',\n    patience=3,\n    mode='min'\n)\n\nmodel_history = model.fit(\n    train_data,\n    validation_data=valid_data,\n    epochs=35,\n    steps_per_epoch=570,\n    validation_steps=190,\n    callbacks=[early_stopping]\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:38:40.035649Z","iopub.execute_input":"2024-07-02T16:38:40.036012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights('/kaggle/working/')\nmodel.save_weights('/kaggle/working/my_model.weights.h5')","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:49.832727Z","iopub.status.idle":"2024-07-02T16:36:49.833083Z","shell.execute_reply.started":"2024-07-02T16:36:49.832895Z","shell.execute_reply":"2024-07-02T16:36:49.832909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 4))\nplt.plot(model_history.history['loss'][0:], label='loss')\nplt.plot(model_history.history['val_loss'][0:], label='val_loss')\nplt.title('loss plot'); \nplt.xlabel('Epoch');\nplt.ylabel('bce dice loss');\nplt.legend()\nplt.show()\n\nplt.figure(figsize=(8, 4))\nplt.plot(model_history.history['iou_score'], label='iou_score')\nplt.plot(model_history.history['val_iou_score'], label='val_iou_score')\nplt.title('score plot'); \nplt.xlabel('Epoch');\nplt.ylabel('iou score');\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:49.834225Z","iopub.status.idle":"2024-07-02T16:36:49.834561Z","shell.execute_reply.started":"2024-07-02T16:36:49.834400Z","shell.execute_reply":"2024-07-02T16:36:49.834415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_imgs = listdir(test_dir)\nEMPTY_IMG = np.zeros((IMG_H, IMG_W, 1), dtype=np.float64)\n\n\ndef make_gen_test():\n    n_batches = ceil(len(test_imgs) / BATCH_SIZE)\n    \n    for batch_i in range(n_batches):\n        start_batch = batch_i * BATCH_SIZE\n        end_batch = batch_i * BATCH_SIZE + BATCH_SIZE\n        \n        batch_img = np.zeros((BATCH_SIZE, IMG_H, IMG_W, 3), dtype=np.float64)\n        \n        for i, path_img in enumerate(test_imgs[start_batch:end_batch]):\n            img = image.img_to_array(Image.open(test_dir + path_img), dtype=np.float64)\n            img = img[::COEF_SMALLER_IMG, ::COEF_SMALLER_IMG]\n            batch_img[i] = img\n\n        yield batch_img\n        \n\ntest_data = tf.data.Dataset.from_generator(\n         make_gen_test,\n         output_signature=(tf.TensorSpec(shape=(BATCH_SIZE, IMG_H, IMG_W, 3), dtype=tf.float64))\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:49.836494Z","iopub.status.idle":"2024-07-02T16:36:49.836824Z","shell.execute_reply.started":"2024-07-02T16:36:49.836648Z","shell.execute_reply":"2024-07-02T16:36:49.836666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_preds = pd.DataFrame(columns=['ImageId', 'EncodedPixels'])\n\n# rle_encode works faster with float64 array than float32 one\nfor i_batch, X in enumerate(test_data):\n    prediction_batch = model.predict_on_batch(X)\n    \n    for i_img, img_arr in enumerate(prediction_batch):  \n        img_arr = np.round(img_arr).astype(np.float64)\n        img_name = test_imgs[(i_batch-1)*BATCH_SIZE+i_img]\n        \n        if (EMPTY_IMG == img_arr).all():\n            df_preds.loc[len(df_preds)] = {'ImageId':img_name, 'EncodedPixels':np.nan}\n            continue\n        \n        img_arr = img_arr.repeat(2, axis=0).repeat(2, axis=1)\n        img_arr = img_arr.reshape((768, 768)).T.reshape((768*768))\n        rle = rle_encode(img_arr)\n        \n        df_preds.loc[len(df_preds)] = {'ImageId':img_name, 'EncodedPixels':rle}","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:49.837874Z","iopub.status.idle":"2024-07-02T16:36:49.838212Z","shell.execute_reply.started":"2024-07-02T16:36:49.838022Z","shell.execute_reply":"2024-07-02T16:36:49.838050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_preds","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:49.839532Z","iopub.status.idle":"2024-07-02T16:36:49.839880Z","shell.execute_reply.started":"2024-07-02T16:36:49.839704Z","shell.execute_reply":"2024-07-02T16:36:49.839719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if len(df_preds) > len(test_imgs): \n    to_drop = len(df_preds) - len(test_imgs)\n    df_preds.drop(df_preds.tail(to_drop).index, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:49.841321Z","iopub.status.idle":"2024-07-02T16:36:49.841674Z","shell.execute_reply.started":"2024-07-02T16:36:49.841506Z","shell.execute_reply":"2024-07-02T16:36:49.841521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_preds","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:49.843271Z","iopub.status.idle":"2024-07-02T16:36:49.843716Z","shell.execute_reply.started":"2024-07-02T16:36:49.843490Z","shell.execute_reply":"2024-07-02T16:36:49.843509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_preds.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T16:36:49.845349Z","iopub.status.idle":"2024-07-02T16:36:49.845795Z","shell.execute_reply.started":"2024-07-02T16:36:49.845567Z","shell.execute_reply":"2024-07-02T16:36:49.845585Z"},"trusted":true},"execution_count":null,"outputs":[]}]}