{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"```\nC-100-TRAIN-efficientnetb8-×640-e50-f0\nEpoch 00050: val_iou did not improve from 0.71561\n\nC-100-TRAIN-efficientnetb8-×640-e50-f1\nEpoch 00050: val_iou did not improve from 0.70014\n\nC-100-TRAIN-efficientnetb8-×640-e50-f2\nEpoch 00050: val_iou did not improve from 0.67578\n\nC-100-TRAIN-efficientnetb8-×640-e50-f3\nEpoch 00050: val_iou did not improve from 0.67825\n\nC-100-TRAIN-efficientnetb8-×640-e50-f4\nEpoch 00050: val_iou did not improve from 0.73172\n```\n\n---\n\n```\nC-102-TRAIN-efficientnetb8-×640-e50-DAExp5-f0\nEpoch 00050: val_iou did not improve from 0.72827\n\nC-102-TRAIN-efficientnetb8-×640-e50-DAExp5-f1\nEpoch 00050: val_iou did not improve from 0.69073\n\nC-102-TRAIN-efficientnetb8-×640-e50-DAExp5-f2\nEpoch 00050: val_iou did not improve from 0.68342\n\nC-102-TRAIN-efficientnetb8-×640-e50-DAExp5-f3\nEpoch 00050: val_iou did not improve from 0.67127\n\nC-102-TRAIN-efficientnetb8-×640-e50-DAExp5-f4\nEpoch 00050: val_iou did not improve from 0.71205\n```\n\n---\n\n```\nC-103-TRAIN-efficientnetb8-×640-e50-DAExp6-f0\nEpoch 00050: val_iou did not improve from 0.70097\n\nC-103-TRAIN-efficientnetb8-×640-e50-DAExp6-f1\nEpoch 00050: val_iou did not improve from 0.66923\n\nC-103-TRAIN-efficientnetb8-×640-e50-DAExp6-f2\nEpoch 00050: val_iou did not improve from 0.66570\n\nC-103-TRAIN-efficientnetb8-×640-e50-DAExp6-f3\nEpoch 00050: val_iou did not improve from 0.66451\n\nC-103-TRAIN-efficientnetb8-×640-e50-DAExp6-f4\nEpoch 00050: val_iou did not improve from 0.68982\n```\n\n---\n\n```\nC-301-TRAIN-efficientnetb7-x640-e50-DAExp5-f0\nEpoch 00050: val_iou did not improve from 0.71414\n\nC-301-TRAIN-efficientnetb7-x640-e50-DAExp5-f1\nEpoch 00050: val_iou did not improve from 0.68209\n\nC-301-TRAIN-efficientnetb7-x640-e50-DAExp5-f2\nEpoch 00050: val_iou did not improve from 0.67774\n\nC-301-TRAIN-efficientnetb7-x640-e50-DAExp5-f3\nEpoch 00050: val_iou did not improve from 0.67341\n\nC-301-TRAIN-efficientnetb7-x640-e50-DAExp5-f4\nEpoch 00050: val_iou did not improve from 0.71402\n```\n\n---\n\n```\nC-320-TRAIN-efficientnetb8-×640-e50-DAExp10-fAll\nEpoch 00050: val_iou did not improve from 0.72569\n\nC-320-TRAIN-efficientnetb8-×640-e50-DAExp10-f1\nEpoch 00050: val_iou did not improve from 0.69784\n\nC-320-TRAIN-efficientnetb8-×640-e50-DAExp10-f2\nEpoch 00050: val_iou did not improve from 0.67721\n\nC-320-TRAIN-efficientnetb8-×640-e50-DAExp10-f3\nEpoch 00050: val_iou did not improve from 0.67721\n\nC-320-TRAIN-efficientnetb8-×640-e50-DAExp10-f4\nEpoch 00050: val_iou did not improve from 0.71675\n```","metadata":{}},{"cell_type":"code","source":"# Import EfficientNet models with intermediate endpoints\nimport sys\nsys.path.append('../input/efficientnetv2-head-1x1-endpoint-v2/')\nsys.path.append('../input/efficientnetv2-head-1x1-endpoint-v2/efficientnetv2/')","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:46:40.838654Z","iopub.execute_input":"2022-09-19T07:46:40.839085Z","iopub.status.idle":"2022-09-19T07:46:40.939517Z","shell.execute_reply.started":"2022-09-19T07:46:40.838966Z","shell.execute_reply":"2022-09-19T07:46:40.938805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport tensorflow.keras.backend as K\nimport tensorflow_addons as tfa\nimport matplotlib.pyplot as plt\n\nfrom tensorflow.keras.mixed_precision import experimental as mixed_precision\nfrom kaggle_datasets import KaggleDatasets\nfrom tqdm.notebook import tqdm\nfrom multiprocessing import cpu_count\nfrom sklearn import metrics\nfrom sklearn.model_selection import KFold\n\nimport effnetv2_model\nimport tifffile\nimport re\nimport os\nimport io\nimport time\nimport pickle\nimport math\nimport random\nimport sys\nimport cv2\nimport gc\n\nprint(f'tensorflow version: {tf.__version__}')\nprint(f'tensorflow keras version: {tf.keras.__version__}')\nprint(f'python version: P{sys.version}')","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:46:40.942404Z","iopub.execute_input":"2022-09-19T07:46:40.942619Z","iopub.status.idle":"2022-09-19T07:46:46.723706Z","shell.execute_reply.started":"2022-09-19T07:46:40.942596Z","shell.execute_reply":"2022-09-19T07:46:46.722882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Seed all random number generators\ndef seed_everything(seed):\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    random.seed(seed)\n    np.random.seed(seed)\n    tf.random.set_seed(seed)\n    \nSEED = 42\nseed_everything(SEED)","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:46:46.725068Z","iopub.execute_input":"2022-09-19T07:46:46.725531Z","iopub.status.idle":"2022-09-19T07:46:46.730984Z","shell.execute_reply.started":"2022-09-19T07:46:46.725494Z","shell.execute_reply":"2022-09-19T07:46:46.730166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Threshold to classify a pixel as mask\nTHRESHOLD = 0.390\n\nDEBUG = False\nIS_TPU = True\n\n# Image dimensions\nIMG_SIZE = 640\nPATCH_SIZE = 640\nN_CHANNELS = 3\nN_PATCHES_PER_IMAGE = (IMG_SIZE // PATCH_SIZE) ** 2\n\nINPUT_SHAPE = (PATCH_SIZE, PATCH_SIZE, N_CHANNELS)\n\n# EfficientNet version, b0/b1/b2/b3/s/m/l/xl/xxl\nEFN_SIZE = 'b8'\nLR_MAX = 0.02\nEPOCHS = 30\nMOMENTUM = 0.00\n\n# Batch size\nBATCH_SIZE = 64\n\n# Dataset Mean and Standard Deviation\nMEAN = np.load('/kaggle/input/hubmap-patched-tfrecords-300x300/MEAN.npy')\nSTD = np.load('/kaggle/input/hubmap-patched-tfrecords-300x300/STD.npy')\n\nprint(f'MEAN: {MEAN}, STD: {STD}')","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:46:46.733316Z","iopub.execute_input":"2022-09-19T07:46:46.733592Z","iopub.status.idle":"2022-09-19T07:46:46.75951Z","shell.execute_reply.started":"2022-09-19T07:46:46.73356Z","shell.execute_reply":"2022-09-19T07:46:46.75881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Hardware Configuration","metadata":{}},{"cell_type":"code","source":"# Detect hardware, return appropriate distribution strategy\ntry:\n    TPU = tf.distribute.cluster_resolver.TPUClusterResolver()  # TPU detection. No parameters necessary if TPU_NAME environment variable is set. On Kaggle this is always the case.\n    print('Running on TPU ', TPU.master())\nexcept ValueError:\n    print('Running on GPU')\n    TPU = None\n\nif TPU:\n    tf.config.experimental_connect_to_cluster(TPU)\n    tf.tpu.experimental.initialize_tpu_system(TPU)\n    strategy = tf.distribute.experimental.TPUStrategy(TPU)\nelse:\n    strategy = tf.distribute.get_strategy() # default distribution strategy in Tensorflow. Works on CPU and single GPU.\n\nREPLICAS = strategy.num_replicas_in_sync\nprint(f'REPLICAS: {REPLICAS}')","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:46:46.760562Z","iopub.execute_input":"2022-09-19T07:46:46.761372Z","iopub.status.idle":"2022-09-19T07:46:46.774936Z","shell.execute_reply.started":"2022-09-19T07:46:46.76134Z","shell.execute_reply":"2022-09-19T07:46:46.774167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# FPN","metadata":{}},{"cell_type":"code","source":"def FPN(xs, output_channels, last_layer, debug=False):\n    def _conv(x):\n        x = tf.keras.layers.ZeroPadding2D(padding=1)(x)\n        x = tf.keras.layers.Conv2D(output_channels * 2, 3, padding='SAME', kernel_initializer='he_normal', activation='relu')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = tf.keras.layers.ZeroPadding2D(padding=1)(x)\n        x = tf.keras.layers.Conv2D(output_channels, 3, padding='SAME', kernel_initializer='he_normal')(x)\n        x = tf.image.resize(x, size=target_size, method=tf.image.ResizeMethod.BILINEAR)\n        x = tf.nn.relu(x)\n        return x\n\n    target_size = last_layer.shape[1:3]\n    xs = tf.keras.layers.Concatenate()([_conv(x) for x in xs])\n    x = tf.keras.layers.Concatenate()([xs, last_layer])\n\n    if debug:\n        return x, xs\n    else:\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:46:46.777295Z","iopub.execute_input":"2022-09-19T07:46:46.777668Z","iopub.status.idle":"2022-09-19T07:46:46.787979Z","shell.execute_reply.started":"2022-09-19T07:46:46.777618Z","shell.execute_reply":"2022-09-19T07:46:46.787257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ASPP","metadata":{}},{"cell_type":"code","source":"def ASPP(x, mid_c=320, dilations=[1, 2, 3, 4], out_c=640, debug=False):\n    def _aspp_module(x, filters, kernel_size, padding, dilation, groups=1):\n        x = tf.keras.layers.ZeroPadding2D(padding=padding)(x)\n        x = tf.keras.layers.Conv2D(\n                filters=filters,\n                kernel_size=kernel_size,\n                dilation_rate=dilation,\n                groups=1 if IS_TPU else groups,\n                kernel_initializer='he_uniform',\n            )(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = tf.nn.relu(x)\n        \n        return x\n    \n    x0 = tf.math.reduce_max(x, axis=(1,2), keepdims=True)\n    x0 = tf.keras.layers.Conv2D(filters=mid_c, kernel_size=1, strides=1, kernel_initializer='he_uniform', use_bias=False)(x0)\n    x0 = tf.keras.layers.BatchNormalization(gamma_initializer=tf.constant_initializer(value=0.25))(x0)\n    x0 = tf.nn.relu(x0)\n                                  \n                                  \n    xs = (\n        [_aspp_module(x, mid_c, 1, padding=0, dilation=1)] +\n        [_aspp_module(x, mid_c, 3, padding=d, dilation=d, groups=4) for d in dilations]\n    )\n    \n    x0= tf.image.resize(x0, size=xs[0].shape[1:3])\n    x = tf.keras.layers.Concatenate()([x0] + xs)\n    x = tf.keras.layers.Conv2D(filters=out_c, kernel_size=1, kernel_initializer='he_uniform', use_bias=False)(x)\n    x = tf.keras.layers.BatchNormalization()(x)\n    x = tf.nn.relu(x)\n                       \n    if debug:\n        return x, x0, xs\n    else:\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:46:46.790896Z","iopub.execute_input":"2022-09-19T07:46:46.791105Z","iopub.status.idle":"2022-09-19T07:46:46.802431Z","shell.execute_reply.started":"2022-09-19T07:46:46.791083Z","shell.execute_reply":"2022-09-19T07:46:46.801609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Upsample","metadata":{}},{"cell_type":"code","source":"def PixelShuffle(x, upscale_factor=2):\n    _, w, h, c = x.shape\n    n = -1\n\n    c_out = c // upscale_factor ** 2\n    w_out = w * upscale_factor\n    h_out = h * upscale_factor\n\n    x = tf.reshape(x, [-1, upscale_factor, upscale_factor, w, h, c_out])\n    x = tf.transpose(x, [0, 3, 1, 4, 2, 5])\n    x = tf.reshape(x, [-1, w_out, h_out, c_out])\n\n    return x","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:46:46.803656Z","iopub.execute_input":"2022-09-19T07:46:46.804128Z","iopub.status.idle":"2022-09-19T07:46:46.815016Z","shell.execute_reply.started":"2022-09-19T07:46:46.804094Z","shell.execute_reply":"2022-09-19T07:46:46.814134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Inspiration: https://www.tensorflow.org/tutorials/generative/pix2pix#build_an_input_pipeline_with_tfdata\ndef upsample(x, concat, target_filters, name, conv2dt_kernel_init_max, relu=True, dropout=0, debug=False):\n#     x = PixelShuffle(x)\n\n    filters = concat.shape[-1]\n    x_up = tf.keras.layers.Conv2DTranspose(\n            filters, # Number of Convolutional Filters\n            kernel_size=4, # Kernel Size\n            strides=2, # Kernel Steps\n            padding='SAME', # linear scaling\n            name=f'Conv2DTranspose_{name}', # Name of Layer\n            kernel_initializer='he_uniform',\n            use_bias=False,\n        )(x)\n    \n    concat = tf.keras.layers.BatchNormalization(\n        gamma_initializer=tf.constant_initializer(value=0.25),\n        name=f'BatchNormalization_{name}'\n    )(concat)\n    x = tf.keras.layers.Concatenate(name=f'Concatenate_{name}')([x_up, concat])\n    x = tf.nn.relu(x)\n    \n        \n    x = tf.keras.layers.Conv2D(target_filters, 3, padding='SAME', kernel_initializer='he_uniform', activation='relu', name=f'Conv2D_1_{name}')(x)\n    x = tf.keras.layers.Conv2D(target_filters, 3, padding='SAME', kernel_initializer='he_uniform', name=f'Conv2D_2_{name}')(x)\n    \n    if relu:\n        x = tf.nn.relu(x)\n    \n    x = tf.keras.layers.Dropout(dropout, name=f'Dropout_{name}')(x)\n\n    if debug:\n        return x, x_up, concat\n    else:\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:46:46.817113Z","iopub.execute_input":"2022-09-19T07:46:46.817788Z","iopub.status.idle":"2022-09-19T07:46:46.827538Z","shell.execute_reply.started":"2022-09-19T07:46:46.817748Z","shell.execute_reply":"2022-09-19T07:46:46.82675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"def get_model(dropout_decoder=0, dropout_cnn=0, file_path=None, lr=1e-3, eps=1e-7, clipnorm=5.0, wd_coef=1e-2, cnn_trainable=True):\n    with strategy.scope():\n        # EfficientNetV2 Backbone # \n        cnn = effnetv2_model.get_model(f'efficientnet-{EFN_SIZE}', include_top=False, weights=None, model_config={ 'conv_dropout': dropout_cnn })\n        cnn.trainable = cnn_trainable\n\n        # Inputs, note the names are equal to the dictionary keys in the dataset\n        image = tf.keras.layers.Input(INPUT_SHAPE, name='image', dtype=tf.float32)\n        image_norm = tf.cast(image, tf.float32) / 255\n        image_norm = tf.keras.layers.experimental.preprocessing.Normalization(mean=MEAN, variance=STD, dtype=tf.float32)(image_norm)\n\n        embedding, up6, up5, up4, up3, up2, up1 = cnn(image_norm, with_endpoints=True)\n        print(f'embedding shape: {embedding.shape} up1 shape: {up1.shape}, up2 shape: {up2.shape}')\n        print(f'up3 shape: {up3.shape}, up4 shape: {up4.shape}, up5 shape: {up5.shape}, up6 shape: {up6.shape}')\n        \n        dec0 = ASPP(up2)\n        dec0 = tf.keras.layers.Dropout(0.50)(dec0)\n\n        dec1 = upsample(dec0, up3, up4.shape[-1] * 4, 'upsample1', 0.02, dropout=dropout_decoder)\n        dec2 = upsample(dec1, up4, up5.shape[-1] * 2, 'upsample2', 0.02, dropout=dropout_decoder)\n        dec3 = upsample(dec2, up5, up6.shape[-1] * 2, 'upsample3', 0.02)\n        dec4 = upsample(dec3, up6, 64, 'upsample4', 0.02)\n        \n        print(f'dec0 shape: {dec0.shape}, dec1 shape: {dec1.shape}, dec2 shape: {dec2.shape}, dec3 shape: {dec3.shape}, dec4 shape: {dec4.shape}')\n        \n        dec_fpn = FPN([dec0, dec1, dec2, dec3], 32, dec4)\n        \n        print(f'dec_fpn shape: {dec_fpn.shape}')\n        \n        # Head\n        x = tf.keras.layers.Dropout(0.10)(dec_fpn)\n        x = tf.keras.layers.Conv2D(\n            filters=1,\n            kernel_size=1,\n            padding='SAME',\n            kernel_initializer=tf.random_normal_initializer(0.00, 0.05),\n            activation='sigmoid',\n            name='Conv2D_3_head'\n        )(x)\n        output = tf.image.resize(x, size=[IMG_SIZE, IMG_SIZE], method=tf.image.ResizeMethod.BILINEAR)\n        \n        model = tf.keras.models.Model(inputs=image, outputs=output)\n\n        if file_path:\n            print('Loading pretrained weights...')\n            model.load_weights(file_path)\n            \n        model.trainable = False\n\n        return model","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:46:46.830925Z","iopub.execute_input":"2022-09-19T07:46:46.831134Z","iopub.status.idle":"2022-09-19T07:46:46.845268Z","shell.execute_reply.started":"2022-09-19T07:46:46.831094Z","shell.execute_reply":"2022-09-19T07:46:46.844556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_v2l(dropout_decoder=0, dropout_cnn=0, file_path=None, lr=1e-3, eps=1e-7, clipnorm=5.0, wd_coef=1e-2, cnn_trainable=True):\n    with strategy.scope():\n        # EfficientNetV2 Backbone # \n        cnn = effnetv2_model.get_model('efficientnetv2-l', include_top=False, weights=None, model_config={ 'conv_dropout': dropout_cnn })\n        cnn.trainable = cnn_trainable\n\n        # Inputs, note the names are equal to the dictionary keys in the dataset\n        image = tf.keras.layers.Input(INPUT_SHAPE, name='image', dtype=tf.float32)\n        image_norm = tf.cast(image, tf.float32) / 255\n        image_norm = tf.keras.layers.experimental.preprocessing.Normalization(mean=MEAN, variance=STD, dtype=tf.float32)(image_norm)\n\n        embedding, up6, up5, up4, up3, up2, up1 = cnn(image_norm, with_endpoints=True)\n        print(f'embedding shape: {embedding.shape} up1 shape: {up1.shape}, up2 shape: {up2.shape}')\n        print(f'up3 shape: {up3.shape}, up4 shape: {up4.shape}, up5 shape: {up5.shape}, up6 shape: {up6.shape}')\n        \n        dec0 = ASPP(up2)\n        dec0 = tf.keras.layers.Dropout(0.50)(dec0)\n\n        dec1 = upsample(dec0, up3, up4.shape[-1] * 4, 'upsample1', 0.02, dropout=dropout_decoder)\n        dec2 = upsample(dec1, up4, up5.shape[-1] * 2, 'upsample2', 0.02, dropout=dropout_decoder)\n        dec3 = upsample(dec2, up5, up6.shape[-1] * 2, 'upsample3', 0.02)\n        dec4 = upsample(dec3, up6, 64, 'upsample4', 0.02)\n        \n        print(f'dec0 shape: {dec0.shape}, dec1 shape: {dec1.shape}, dec2 shape: {dec2.shape}, dec3 shape: {dec3.shape}, dec4 shape: {dec4.shape}')\n        \n        dec_fpn = FPN([dec0, dec1, dec2, dec3], 32, dec4)\n        \n        print(f'dec_fpn shape: {dec_fpn.shape}')\n        \n        # Head\n        x = tf.keras.layers.Dropout(0.10)(dec_fpn)\n        x = tf.keras.layers.Conv2D(\n            filters=1,\n            kernel_size=1,\n            padding='SAME',\n            kernel_initializer=tf.random_normal_initializer(0.00, 0.05),\n            activation='sigmoid',\n            name='Conv2D_3_head'\n        )(x)\n        output = tf.image.resize(x, size=[IMG_SIZE, IMG_SIZE], method=tf.image.ResizeMethod.BILINEAR)\n        \n        model = tf.keras.models.Model(inputs=image, outputs=output)\n\n        if file_path:\n            print('Loading pretrained weights...')\n            model.load_weights(file_path)\n            \n        model.trainable = False\n\n        return model","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:46:46.848157Z","iopub.execute_input":"2022-09-19T07:46:46.848342Z","iopub.status.idle":"2022-09-19T07:46:46.862037Z","shell.execute_reply.started":"2022-09-19T07:46:46.848319Z","shell.execute_reply":"2022-09-19T07:46:46.861276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_b7(dropout_decoder=0, dropout_cnn=0, file_path=None, lr=1e-3, eps=1e-7, clipnorm=5.0, wd_coef=1e-2, cnn_trainable=True):\n    with strategy.scope():\n        # EfficientNetV2 Backbone # \n        cnn = effnetv2_model.get_model('efficientnet-b7', include_top=False, weights=None, model_config={ 'conv_dropout': dropout_cnn })\n        cnn.trainable = cnn_trainable\n\n        # Inputs, note the names are equal to the dictionary keys in the dataset\n        image = tf.keras.layers.Input(INPUT_SHAPE, name='image', dtype=tf.float32)\n        image_norm = tf.cast(image, tf.float32) / 255\n        image_norm = tf.keras.layers.experimental.preprocessing.Normalization(mean=MEAN, variance=STD, dtype=tf.float32)(image_norm)\n\n        embedding, up6, up5, up4, up3, up2, up1 = cnn(image_norm, with_endpoints=True)\n        print(f'embedding shape: {embedding.shape} up1 shape: {up1.shape}, up2 shape: {up2.shape}')\n        print(f'up3 shape: {up3.shape}, up4 shape: {up4.shape}, up5 shape: {up5.shape}, up6 shape: {up6.shape}')\n        \n        dec0 = ASPP(up2)\n        dec0 = tf.keras.layers.Dropout(0.50)(dec0)\n\n        dec1 = upsample(dec0, up3, up4.shape[-1] * 4, 'upsample1', 0.02, dropout=dropout_decoder)\n        dec2 = upsample(dec1, up4, up5.shape[-1] * 2, 'upsample2', 0.02, dropout=dropout_decoder)\n        dec3 = upsample(dec2, up5, up6.shape[-1] * 2, 'upsample3', 0.02)\n        dec4 = upsample(dec3, up6, 64, 'upsample4', 0.02)\n        \n        print(f'dec0 shape: {dec0.shape}, dec1 shape: {dec1.shape}, dec2 shape: {dec2.shape}, dec3 shape: {dec3.shape}, dec4 shape: {dec4.shape}')\n        \n        dec_fpn = FPN([dec0, dec1, dec2, dec3], 32, dec4)\n        \n        print(f'dec_fpn shape: {dec_fpn.shape}')\n        \n        # Head\n        x = tf.keras.layers.Dropout(0.10)(dec_fpn)\n        x = tf.keras.layers.Conv2D(\n            filters=1,\n            kernel_size=1,\n            padding='SAME',\n            kernel_initializer=tf.random_normal_initializer(0.00, 0.05),\n            activation='sigmoid',\n            name='Conv2D_3_head'\n        )(x)\n        output = tf.image.resize(x, size=[IMG_SIZE, IMG_SIZE], method=tf.image.ResizeMethod.BILINEAR)\n        \n        model = tf.keras.models.Model(inputs=image, outputs=output)\n\n        if file_path:\n            print('Loading pretrained weights...')\n            model.load_weights(file_path)\n            \n        model.trainable = False\n\n        return model","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:46:46.863286Z","iopub.execute_input":"2022-09-19T07:46:46.863536Z","iopub.status.idle":"2022-09-19T07:46:46.877219Z","shell.execute_reply.started":"2022-09-19T07:46:46.863508Z","shell.execute_reply":"2022-09-19T07:46:46.876458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = get_model(file_path='../input/hubmap-train-c-102-20220913110359/C-102-TRAIN-efficientnetb8-640-e50-DAExp5-f0/model_best.h5')\nmodel_1 = get_model(file_path='../input/hubmap-train-c-102-20220913110359/C-102-TRAIN-efficientnetb8-640-e50-DAExp5-f1/model_best.h5')\nmodel_2 = get_model(file_path='../input/hubmap-train-c-102-20220913110359/C-102-TRAIN-efficientnetb8-640-e50-DAExp5-f2/model_best.h5')\nmodel_3 = get_model(file_path='../input/hubmap-train-c-102-20220913110359/C-102-TRAIN-efficientnetb8-640-e50-DAExp5-f3/model_best.h5')\nmodel_4 = get_model(file_path='../input/hubmap-train-c-102-20220913110359/C-102-TRAIN-efficientnetb8-640-e50-DAExp5-f4/model_best.h5')\n\nmodel_1_0 = get_model(file_path='../input/hubmap-train-c-100-20220912095119/C-100-TRAIN-efficientnetb8-640-e50-f0/model_best.h5')\nmodel_1_1 = get_model(file_path='../input/hubmap-train-c-100-20220912095119/C-100-TRAIN-efficientnetb8-640-e50-f1/model_best.h5')\nmodel_1_2 = get_model(file_path='../input/hubmap-train-c-100-20220912095119/C-100-TRAIN-efficientnetb8-640-e50-f2/model_best.h5')\nmodel_1_3 = get_model(file_path='../input/hubmap-train-c-100-20220912095119/C-100-TRAIN-efficientnetb8-640-e50-f3/model_best.h5')\nmodel_1_4 = get_model(file_path='../input/hubmap-train-c-100-20220912095119/C-100-TRAIN-efficientnetb8-640-e50-f4/model_best.h5')\n\nmodel_2_0 = get_model(file_path='../input/hubmap-pp-c-103-20220914075546/C-103-TRAIN-efficientnetb8-640-e50-DAExp6-f0/model_best.h5')\nmodel_2_1 = get_model(file_path='../input/hubmap-pp-c-103-20220914075546/C-103-TRAIN-efficientnetb8-640-e50-DAExp6-f1/model_best.h5')\nmodel_2_2 = get_model(file_path='../input/hubmap-pp-c-103-20220914075546/C-103-TRAIN-efficientnetb8-640-e50-DAExp6-f2/model_best.h5')\nmodel_2_3 = get_model(file_path='../input/hubmap-pp-c-103-20220914075546/C-103-TRAIN-efficientnetb8-640-e50-DAExp6-f3/model_best.h5')\nmodel_2_4 = get_model(file_path='../input/hubmap-pp-c-103-20220914075546/C-103-TRAIN-efficientnetb8-640-e50-DAExp6-f4/model_best.h5')\n\nmodel_3_0 = get_model_v2l(file_path='../input/hubmap-train-c-210-20220915071411/C-202-TRAIN-x640-e50-DAExp5-ModelExp1-f0/model_best.h5')\nmodel_3_1 = get_model_v2l(file_path='../input/hubmap-train-c-210-20220915071411/C-210-TRAIN-efficientnetv2l-x640-e50-DAExp5-ModelExp1-f1/model_best.h5')\nmodel_3_2 = get_model_v2l(file_path='../input/hubmap-train-c-210-20220915071411/C-210-TRAIN-efficientnetv2l-x640-e50-DAExp5-ModelExp1-f2/model_best.h5')\nmodel_3_3 = get_model_v2l(file_path='../input/hubmap-train-c-210-20220915071411/C-210-TRAIN-efficientnetv2l-x640-e50-DAExp5-ModelExp1-f3/model_best.h5')\nmodel_3_4 = get_model_v2l(file_path='../input/hubmap-train-c-210-20220915071411/C-210-TRAIN-efficientnetv2l-x640-e50-DAExp5-ModelExp1-f4/model_best.h5')\n\nmodel_4_0 = get_model_b7(file_path='../input/hubmap-train-c-301-20220918070304/C-301-TRAIN-efficientnetb7-x640-e50-DAExp5-f0/model_best.h5')\nmodel_4_1 = get_model_b7(file_path='../input/hubmap-train-c-301-20220918070304/C-301-TRAIN-efficientnetb7-x640-e50-DAExp5-f1/model_best.h5')\nmodel_4_2 = get_model_b7(file_path='../input/hubmap-train-c-301-20220918070304/C-301-TRAIN-efficientnetb7-x640-e50-DAExp5-f2/model_best.h5')\nmodel_4_3 = get_model_b7(file_path='../input/hubmap-train-c-301-20220918070304/C-301-TRAIN-efficientnetb7-x640-e50-DAExp5-f3/model_best.h5')\nmodel_4_4 = get_model_b7(file_path='../input/hubmap-train-c-301-20220918070304/C-301-TRAIN-efficientnetb7-x640-e50-DAExp5-f4/model_best.h5')\n\nmodel_5_0 = get_model(file_path='../input/hubmap-train-c-320-20220919071856/C-320-TRAIN-efficientnetb8-640-e50-DAExp10-f0/model_best.h5')\nmodel_5_1 = get_model(file_path='../input/hubmap-train-c-320-20220919071856/C-320-TRAIN-efficientnetb8-640-e50-DAExp10-f1/model_best.h5')\nmodel_5_2 = get_model(file_path='../input/hubmap-train-c-320-20220919071856/C-320-TRAIN-efficientnetb8-640-e50-DAExp10-f2/model_best.h5')\nmodel_5_3 = get_model(file_path='../input/hubmap-train-c-320-20220919071856/C-320-TRAIN-efficientnetb8-640-e50-DAExp10-f3/model_best.h5')\nmodel_5_4 = get_model(file_path='../input/hubmap-train-c-320-20220919071856/C-320-TRAIN-efficientnetb8-640-e50-DAExp10-f4/model_best.h5')","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:46:46.878771Z","iopub.execute_input":"2022-09-19T07:46:46.879264Z","iopub.status.idle":"2022-09-19T07:55:48.281015Z","shell.execute_reply.started":"2022-09-19T07:46:46.879234Z","shell.execute_reply":"2022-09-19T07:55:48.280262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot model summary\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:55:48.282161Z","iopub.execute_input":"2022-09-19T07:55:48.282397Z","iopub.status.idle":"2022-09-19T07:55:48.371196Z","shell.execute_reply.started":"2022-09-19T07:55:48.282367Z","shell.execute_reply":"2022-09-19T07:55:48.370508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tf.keras.utils.plot_model(model, show_shapes=True, show_dtype=True, show_layer_names=True, expand_nested=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:55:48.372307Z","iopub.execute_input":"2022-09-19T07:55:48.372572Z","iopub.status.idle":"2022-09-19T07:55:48.376626Z","shell.execute_reply.started":"2022-09-19T07:55:48.372539Z","shell.execute_reply":"2022-09-19T07:55:48.375701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Utility Funtions","metadata":{}},{"cell_type":"code","source":"# Resized a tensor to the specified size\ndef resize_tensor(tensor, size=IMG_SIZE, dtype=np.uint8):\n    return cv2.resize(tensor, [size, size], interpolation=cv2.INTER_CUBIC).astype(dtype)","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:55:48.377903Z","iopub.execute_input":"2022-09-19T07:55:48.378177Z","iopub.status.idle":"2022-09-19T07:55:48.386403Z","shell.execute_reply.started":"2022-09-19T07:55:48.378115Z","shell.execute_reply":"2022-09-19T07:55:48.38568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode\ndef get_mask(image_id):\n    row = train.loc[train['id'] == image_id].squeeze()\n    h, w = row[['img_height', 'img_width']]\n    mask = np.zeros(shape=[h * w], dtype=np.uint8)\n    s = row['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    for lo, hi in zip(starts, ends):\n        mask[lo : hi] = 1\n        \n    mask = mask.reshape([h, w]).T\n        \n    mask = resize_tensor(mask)\n    \n    mask = np.expand_dims(mask, axis=2)\n        \n    return mask","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:55:48.387613Z","iopub.execute_input":"2022-09-19T07:55:48.387874Z","iopub.status.idle":"2022-09-19T07:55:48.397566Z","shell.execute_reply.started":"2022-09-19T07:55:48.387841Z","shell.execute_reply":"2022-09-19T07:55:48.396704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reads an image and returns the image and original image size\ndef get_image(image_id, folder, negative=True):\n    image = tifffile.imread(f'/kaggle/input/hubmap-organ-segmentation/{folder}_images/{image_id}.tiff')\n    if len(image.shape) == 5:\n        image = image.squeeze().transpose(1, 2, 0)\n    \n    # Image Size\n    image_size, _, _ = image.shape\n    \n    # Reverse pixels to make tissue colored and background black\n    if negative:\n        image = image - image.min()\n        image = image / (image.max() - image.min())\n        image = image * 255\n        image = 255 - image.astype(np.uint8)\n        \n    # Resize\n    image = resize_tensor(image)\n    \n    return image, image_size","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:55:48.401081Z","iopub.execute_input":"2022-09-19T07:55:48.401656Z","iopub.status.idle":"2022-09-19T07:55:48.408068Z","shell.execute_reply.started":"2022-09-19T07:55:48.401623Z","shell.execute_reply":"2022-09-19T07:55:48.407465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# extract patches from an image\ndef extract_patches(image):\n    _, _, c = image.shape\n    image = tf.expand_dims(image, 0)\n    image_patches = tf.image.extract_patches(image, [1,PATCH_SIZE,PATCH_SIZE,1], [1, PATCH_SIZE, PATCH_SIZE, 1], [1, 1, 1, 1], padding='SAME')\n    image_patches = tf.reshape(image_patches, [N_PATCHES_PER_IMAGE, PATCH_SIZE, PATCH_SIZE, c])\n    image_patches = image_patches.numpy()\n\n    return image_patches","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:55:48.409311Z","iopub.execute_input":"2022-09-19T07:55:48.409786Z","iopub.status.idle":"2022-09-19T07:55:48.422618Z","shell.execute_reply.started":"2022-09-19T07:55:48.409753Z","shell.execute_reply":"2022-09-19T07:55:48.421886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://www.kaggle.com/bguberfain/memory-aware-rle-encoding\n#with transposed mask\ndef rle_encode_less_memory(img):\n    #the image should be transposed\n    pixels = img.T.flatten()\n    \n    # This simplified method requires first and last pixel to be zero\n    pixels[0] = 0\n    pixels[-1] = 0\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    runs[1::2] -= runs[::2]\n    \n    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:55:48.423481Z","iopub.execute_input":"2022-09-19T07:55:48.423837Z","iopub.status.idle":"2022-09-19T07:55:48.432648Z","shell.execute_reply.started":"2022-09-19T07:55:48.423807Z","shell.execute_reply":"2022-09-19T07:55:48.431944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference","metadata":{}},{"cell_type":"code","source":"# Training DataFrame\ntrain = pd.read_csv('/kaggle/input/hubmap-organ-segmentation/train.csv')\n\n#display(train.head())\n#display(train.info())","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:55:48.433679Z","iopub.execute_input":"2022-09-19T07:55:48.433914Z","iopub.status.idle":"2022-09-19T07:55:48.816171Z","shell.execute_reply.started":"2022-09-19T07:55:48.433885Z","shell.execute_reply":"2022-09-19T07:55:48.815445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test DataFrame\ntest = pd.read_csv('/kaggle/input/hubmap-organ-segmentation/test.csv')\n\n#display(test.head())\n#display(test.info())","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:55:48.819495Z","iopub.execute_input":"2022-09-19T07:55:48.819709Z","iopub.status.idle":"2022-09-19T07:55:48.830557Z","shell.execute_reply.started":"2022-09-19T07:55:48.819684Z","shell.execute_reply":"2022-09-19T07:55:48.829863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reconstruct the original image from patches\ndef merge_patches(patches):\n    image = np.zeros(shape=[IMG_SIZE, IMG_SIZE, patches.shape[-1]], dtype=patches.dtype)\n    s = int(N_PATCHES_PER_IMAGE ** 0.50)\n    for r in range(s):\n        for c in range(s):\n            start_x = r * PATCH_SIZE\n            end_x = (r + 1) * PATCH_SIZE\n            start_y = c * PATCH_SIZE\n            end_y = (c + 1) * PATCH_SIZE\n            image[start_x:end_x, start_y:end_y] = patches[r * s + c]\n            \n    return image","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:55:48.833188Z","iopub.execute_input":"2022-09-19T07:55:48.834544Z","iopub.status.idle":"2022-09-19T07:55:48.839983Z","shell.execute_reply.started":"2022-09-19T07:55:48.834507Z","shell.execute_reply":"2022-09-19T07:55:48.839251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sanity Check","metadata":{}},{"cell_type":"code","source":"# Plot 10 predictions to verify the trained weights are correctly loaded\ntest_rows = []\nN = 10\n\nfor row_idx, row in tqdm(train[:N].iterrows(), total=N):\n    # Preprocess Image\n    image, image_size = get_image(row['id'], 'train')\n    image_patches = extract_patches(image)\n    \n    # Make Prediction\n    mask_patches_pred = model.predict(image_patches)\n    mask_pred = merge_patches(mask_patches_pred)\n    mask_pred_resized = resize_tensor(mask_pred, size=image_size, dtype=np.float32)\n    \n    fig, axes = plt.subplots(1,2, figsize=(8,4))\n    axes[0].imshow(mask_pred_resized)\n    axes[1].imshow(image)\n    plt.show()\n        \n    # Resize and Binarize Mask\n    mask_binary = (mask_pred_resized > THRESHOLD).astype(np.int8)\n    # Append to Result\n    test_rows.append({\n        'id': row['id'],\n        'rle': rle_encode_less_memory(mask_binary)\n    })","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:55:48.842469Z","iopub.execute_input":"2022-09-19T07:55:48.842686Z","iopub.status.idle":"2022-09-19T07:56:23.946309Z","shell.execute_reply.started":"2022-09-19T07:55:48.842652Z","shell.execute_reply":"2022-09-19T07:56:23.945387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference Loop","metadata":{}},{"cell_type":"code","source":"# Predictions are stored as a list of dictionaries\ntest_rows = []\n\n# Iterate over all test images\nfor row_idx, row in tqdm(test.iterrows(), total=len(test)):\n    # Preprocess Image\n    image, image_size = get_image(row['id'], 'test')\n    image_patches = extract_patches(image)\n    \n    # ------------------------------------------------------------------------------- #\n    # Make Prediction\n    # ------------------------------------------------------------------------------- #\n    mask_patches_pred = model.predict(image_patches)\n    mask_patches_pred_1 = model_1.predict(image_patches)\n    mask_patches_pred_2 = model_2.predict(image_patches)\n    mask_patches_pred_3 = model_3.predict(image_patches)\n    mask_patches_pred_4 = model_4.predict(image_patches)\n    \n    mask_patches_pred_1_0 = model_1_0.predict(image_patches)\n    mask_patches_pred_1_1 = model_1_1.predict(image_patches)\n    mask_patches_pred_1_2 = model_1_2.predict(image_patches)\n    mask_patches_pred_1_3 = model_1_3.predict(image_patches)\n    mask_patches_pred_1_4 = model_1_4.predict(image_patches)\n    \n    mask_patches_pred_2_0 = model_2_0.predict(image_patches)\n    mask_patches_pred_2_1 = model_2_1.predict(image_patches)\n    mask_patches_pred_2_2 = model_2_2.predict(image_patches)\n    mask_patches_pred_2_3 = model_2_3.predict(image_patches)\n    mask_patches_pred_2_4 = model_2_4.predict(image_patches)\n    \n    mask_patches_pred_3_0 = model_3_0.predict(image_patches)\n    mask_patches_pred_3_1 = model_3_1.predict(image_patches)\n    mask_patches_pred_3_2 = model_3_2.predict(image_patches)\n    mask_patches_pred_3_3 = model_3_3.predict(image_patches)\n    mask_patches_pred_3_4 = model_3_4.predict(image_patches)\n    \n    mask_patches_pred_4_0 = model_4_0.predict(image_patches)\n    mask_patches_pred_4_1 = model_4_1.predict(image_patches)\n    mask_patches_pred_4_2 = model_4_2.predict(image_patches)\n    mask_patches_pred_4_3 = model_4_3.predict(image_patches)\n    mask_patches_pred_4_4 = model_4_4.predict(image_patches)\n    \n    mask_patches_pred_5_0 = model_5_0.predict(image_patches)\n    mask_patches_pred_5_1 = model_5_1.predict(image_patches)\n    mask_patches_pred_5_2 = model_5_2.predict(image_patches)\n    mask_patches_pred_5_3 = model_5_3.predict(image_patches)\n    mask_patches_pred_5_4 = model_5_4.predict(image_patches)\n    \n    # ------------------------------------------------------------------------------- #\n    # Merge patches\n    # ------------------------------------------------------------------------------- #\n    mask_pred = merge_patches(mask_patches_pred)\n    mask_pred_1 = merge_patches(mask_patches_pred_1)\n    mask_pred_2 = merge_patches(mask_patches_pred_2)\n    mask_pred_3 = merge_patches(mask_patches_pred_3)\n    mask_pred_4 = merge_patches(mask_patches_pred_4)\n    \n    mask_pred_1_0 = merge_patches(mask_patches_pred_1_0)\n    mask_pred_1_1 = merge_patches(mask_patches_pred_1_1)\n    mask_pred_1_2 = merge_patches(mask_patches_pred_1_2)\n    mask_pred_1_3 = merge_patches(mask_patches_pred_1_3)\n    mask_pred_1_4 = merge_patches(mask_patches_pred_1_4)\n    \n    mask_pred_2_0 = merge_patches(mask_patches_pred_2_0)\n    mask_pred_2_1 = merge_patches(mask_patches_pred_2_1)\n    mask_pred_2_2 = merge_patches(mask_patches_pred_2_2)\n    mask_pred_2_3 = merge_patches(mask_patches_pred_2_3)\n    mask_pred_2_4 = merge_patches(mask_patches_pred_2_4)\n    \n    mask_pred_3_0 = merge_patches(mask_patches_pred_3_0)\n    mask_pred_3_1 = merge_patches(mask_patches_pred_3_1)\n    mask_pred_3_2 = merge_patches(mask_patches_pred_3_2)\n    mask_pred_3_3 = merge_patches(mask_patches_pred_3_3)\n    mask_pred_3_4 = merge_patches(mask_patches_pred_3_4)\n    \n    mask_pred_4_0 = merge_patches(mask_patches_pred_4_0)\n    mask_pred_4_1 = merge_patches(mask_patches_pred_4_1)\n    mask_pred_4_2 = merge_patches(mask_patches_pred_4_2)\n    mask_pred_4_3 = merge_patches(mask_patches_pred_4_3)\n    mask_pred_4_4 = merge_patches(mask_patches_pred_4_4)\n    \n    mask_pred_5_0 = merge_patches(mask_patches_pred_5_0)\n    mask_pred_5_1 = merge_patches(mask_patches_pred_5_1)\n    mask_pred_5_2 = merge_patches(mask_patches_pred_5_2)\n    mask_pred_5_3 = merge_patches(mask_patches_pred_5_3)\n    mask_pred_5_4 = merge_patches(mask_patches_pred_5_4)\n    \n    # ------------------------------------------------------------------------------- #\n    # Concat Models Pred\n    # ------------------------------------------------------------------------------- #\n    mask_pred = np.stack([\n                          mask_pred,\n                          mask_pred_1,\n                          mask_pred_2,\n                          mask_pred_3,\n                          mask_pred_4,\n#                           mask_pred_1_0,\n#                           mask_pred_1_1,\n#                           mask_pred_1_2,\n#                           mask_pred_1_3,\n#                           mask_pred_1_4,\n                          mask_pred_2_0,\n                          mask_pred_2_1,\n                          mask_pred_2_2,\n                          mask_pred_2_3,\n                          mask_pred_2_4,\n                          mask_pred_3_0,\n                          mask_pred_3_1,\n                          mask_pred_3_2,\n                          mask_pred_3_3,\n                          mask_pred_3_4,\n                          mask_pred_4_0,\n                          mask_pred_4_1,\n                          mask_pred_4_2,\n                          mask_pred_4_3,\n                          mask_pred_4_4,\n                          mask_pred_5_0,\n                          mask_pred_5_1,\n                          mask_pred_5_2,\n                          mask_pred_5_3,\n                          mask_pred_5_4,\n                         ]).mean(axis=0)\n    \n    # ------------------------------------------------------------------------------- #\n    # Resize mask to original size\n    # ------------------------------------------------------------------------------- #\n    mask_pred_resized = resize_tensor(mask_pred, size=image_size, dtype=np.float32)\n        \n    # ------------------------------------------------------------------------------- #\n    # Hubmap Datasource\n    # ------------------------------------------------------------------------------- #\n    if row['data_source'] == 'Hubmap':\n        if row['organ'] == 'kidney':\n            mask_binary = (mask_pred_resized > 0.190).astype(np.int8)\n        elif row['organ'] == 'largeintestine':\n            mask_binary = (mask_pred_resized > 0.190).astype(np.int8)\n        elif row['organ'] == 'lung':\n            mask_binary = (mask_pred_resized > 0.050).astype(np.int8)\n        elif row['organ'] == 'prostate':\n            mask_binary = (mask_pred_resized > 0.190).astype(np.int8)\n        elif row['organ'] == 'spleen':\n            mask_binary = (mask_pred_resized > 0.190).astype(np.int8)\n        else:\n            mask_binary = (mask_pred_resized > 0.190).astype(np.int8)\n    # ------------------------------------------------------------------------------- #\n    # HPA Datasource\n    # ------------------------------------------------------------------------------- #\n    elif row['data_source'] == 'HPA':\n        if row['organ'] == 'kidney':\n            mask_binary = (mask_pred_resized > 0.410).astype(np.int8)\n        elif row['organ'] == 'largeintestine':\n            mask_binary = (mask_pred_resized > 0.410).astype(np.int8)\n        elif row['organ'] == 'lung':\n            mask_binary = (mask_pred_resized > 0.050).astype(np.int8)\n        elif row['organ'] == 'prostate':\n            mask_binary = (mask_pred_resized > 0.410).astype(np.int8)\n        elif row['organ'] == 'spleen':\n            mask_binary = (mask_pred_resized > 0.410).astype(np.int8)\n        else:\n            mask_binary = (mask_pred_resized > 0.410).astype(np.int8)\n    # ------------------------------------------------------------------------------- #\n    # Other Datasource\n    # ------------------------------------------------------------------------------- #\n    else:\n        mask_binary = (mask_pred_resized > 0.390).astype(np.int8)\n        \n    # ------------------------------------------------------------------------------- #\n    # Visualization\n    # ------------------------------------------------------------------------------- #\n    if row_idx == 0:\n        fig, axes = plt.subplots(1,2, figsize=(8,4))\n        axes[0].imshow(mask_pred_resized)\n        axes[1].imshow(image)\n        plt.show()\n        \n    # ------------------------------------------------------------------------------- #\n    # Visualization\n    # ------------------------------------------------------------------------------- #\n    if row_idx == 0:\n        fig, axes = plt.subplots(1,2, figsize=(8,4))\n        axes[0].imshow(mask_binary)\n        axes[1].imshow(image)\n        plt.show()\n    \n    \n    # Append to Result\n    test_rows.append({\n        'id': row['id'],\n        'rle': rle_encode_less_memory(mask_binary)\n    })","metadata":{"execution":{"iopub.status.busy":"2022-09-19T07:56:23.948364Z","iopub.execute_input":"2022-09-19T07:56:23.948669Z","iopub.status.idle":"2022-09-19T08:00:08.890897Z","shell.execute_reply.started":"2022-09-19T07:56:23.948632Z","shell.execute_reply":"2022-09-19T08:00:08.889929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Make Submission CSV","metadata":{}},{"cell_type":"code","source":"# Make submission DataFrame\ntest_df = pd.DataFrame(test_rows)\n\n#display(test_df.head())\n#display(test_df.info())","metadata":{"execution":{"iopub.status.busy":"2022-09-19T08:00:08.892456Z","iopub.execute_input":"2022-09-19T08:00:08.892803Z","iopub.status.idle":"2022-09-19T08:00:08.901091Z","shell.execute_reply.started":"2022-09-19T08:00:08.892766Z","shell.execute_reply":"2022-09-19T08:00:08.900164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Write Submission CSV\ntest_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-19T08:00:08.902443Z","iopub.execute_input":"2022-09-19T08:00:08.90278Z","iopub.status.idle":"2022-09-19T08:00:08.915564Z","shell.execute_reply.started":"2022-09-19T08:00:08.902737Z","shell.execute_reply":"2022-09-19T08:00:08.914831Z"},"trusted":true},"execution_count":null,"outputs":[]}]}