{"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":"## About notebook\n\nSome code and techniques are from [RSNA-BCD: EfficientNet [TF][TPU-1VM][Infer]](https://www.kaggle.com/code/awsaf49/rsna-bcd-efficientnet-tf-tpu-1vm-infer) notebook.\n\n<h4>\n    <span style=\"color:orange\">\n        Training notebook: \n        <a href=\"https://www.kaggle.com/code/olegbaryshnikov/rsna-coat-tf-training\">[RSNA] CoaT [TF][Training]</a>\n    </span>\n</h4>","metadata":{}},{"cell_type":"markdown","source":"## Version History\n* **v6: [CoaTTiny baseline]** no TTA (LB:0.26)\n* **v9: [CoaTTiny baseline]** two random TTA + DALI (LB:0.27)\n* **v10: [CoaTTiny baseline]** none augs + flip model (LB:0.32)\n* **v11: [CoaTLiteSmall baseline]** none augs + flip model (LB:0.29)\n* **v13: [CoaTLiteSmall baseline]** none augs + flip model, new fold weights function (LB:0.32)","metadata":{}},{"cell_type":"markdown","source":"## DALI + ROI","metadata":{}},{"cell_type":"markdown","source":"**More about encapsulated DALI + ROI extraction: [Easy to Use DALI + ROI](https://www.kaggle.com/code/olegbaryshnikov/easy-to-use-dali-roi)**","metadata":{}},{"cell_type":"code","source":"%run ../input/easy-to-use-dali-roi-ipynb/easy-to-use-dali-roi.ipynb\n%reset -f\n\n#The images converted to .png are in the \"/tmp/imgs\" folder.","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-01-22T21:17:09.392317Z","iopub.execute_input":"2023-01-22T21:17:09.392969Z","iopub.status.idle":"2023-01-22T21:17:50.591781Z","shell.execute_reply.started":"2023-01-22T21:17:09.39284Z","shell.execute_reply":"2023-01-22T21:17:50.590703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Imports","metadata":{}},{"cell_type":"code","source":"!pip install -q --no-index --no-deps --find-links=/kaggle/input/keras-cv-attention-models keras-cv-attention-models","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:17:50.59588Z","iopub.execute_input":"2023-01-22T21:17:50.597903Z","iopub.status.idle":"2023-01-22T21:17:54.139273Z","shell.execute_reply.started":"2023-01-22T21:17:50.597875Z","shell.execute_reply":"2023-01-22T21:17:54.138083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport sys\nimport math\nimport glob\nimport string\nimport random\nimport shutil\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport numpy as np\nimport pandas as pd\nfrom IPython.display import display\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport tensorflow as tf\nimport tensorflow_addons as tfa\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import backend as K\n\nfrom tensorflow.keras.utils import plot_model\n\nfrom tqdm.notebook import tqdm\nimport gc\n\nimport keras_cv_attention_models as kecam\nfrom joblib import Parallel, delayed\n\nimport dicomsdl\n\nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-22T21:17:54.141516Z","iopub.execute_input":"2023-01-22T21:17:54.141947Z","iopub.status.idle":"2023-01-22T21:17:59.554863Z","shell.execute_reply.started":"2023-01-22T21:17:54.141905Z","shell.execute_reply":"2023-01-22T21:17:59.553668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.__version__","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:17:59.560869Z","iopub.execute_input":"2023-01-22T21:17:59.561235Z","iopub.status.idle":"2023-01-22T21:17:59.576147Z","shell.execute_reply.started":"2023-01-22T21:17:59.561201Z","shell.execute_reply":"2023-01-22T21:17:59.575124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Config","metadata":{}},{"cell_type":"code","source":"Config={\n    \"seed\": 1111,\n\n    \"dim_x\": 512,\n    \"dim_y\": 1024,\n\n    \"model_type\": \"coat\",\n    \"model_name\": \"CoaTLiteSmall\",\n    \"model_weights\": None,\n    \"include_preprocessing\": True,\n    \n    #\"hidden_layers_num\": 8,\n    \n    \"batch_size\": 4, \n    \"n_fold\": 4,\n    \n    \"quantization_aware_training\": True,\n    \"post_training_quantization\": False,\n    \"preserved_folder_structure\": False,\n    \n    #None: no augs, \"0\": normal augs, \"1\": low augs, \"random\": random augs, \"flip\": flip augs\n    \"aug_set\": [None,\"random\",\"random\",\"random\",\"random\"],\n}\n\nConfig[\"AUTO\"] = tf.data.experimental.AUTOTUNE","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:17:59.580511Z","iopub.execute_input":"2023-01-22T21:17:59.580891Z","iopub.status.idle":"2023-01-22T21:17:59.589438Z","shell.execute_reply.started":"2023-01-22T21:17:59.58086Z","shell.execute_reply":"2023-01-22T21:17:59.587765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed=42):\n    np.random.seed(seed)\n    random.seed(seed)\n    tf.random.set_seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    \nset_seed(Config[\"seed\"])","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:17:59.592046Z","iopub.execute_input":"2023-01-22T21:17:59.59346Z","iopub.status.idle":"2023-01-22T21:17:59.604143Z","shell.execute_reply.started":"2023-01-22T21:17:59.593425Z","shell.execute_reply":"2023-01-22T21:17:59.602735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data","metadata":{}},{"cell_type":"code","source":"test_csv_path = '/kaggle/input/rsna-breast-cancer-detection/test.csv'\n\ntest_images_temp_folder = '/tmp/imgs'\n\ntraining_notebook_path = '/kaggle/input/rsna-coat-tf-training'","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:17:59.611431Z","iopub.execute_input":"2023-01-22T21:17:59.614443Z","iopub.status.idle":"2023-01-22T21:17:59.619635Z","shell.execute_reply.started":"2023-01-22T21:17:59.614416Z","shell.execute_reply":"2023-01-22T21:17:59.618253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(test_csv_path)\ntest_df","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:17:59.621614Z","iopub.execute_input":"2023-01-22T21:17:59.622003Z","iopub.status.idle":"2023-01-22T21:17:59.649293Z","shell.execute_reply.started":"2023-01-22T21:17:59.621957Z","shell.execute_reply":"2023-01-22T21:17:59.64853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"saved_weights_paths = [os.path.join(training_notebook_path, f'model_weights/best_model_fold_{fold}.h5') for fold in range(0,Config[\"n_fold\"])]\nsaved_weights_paths","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:17:59.654127Z","iopub.execute_input":"2023-01-22T21:17:59.656688Z","iopub.status.idle":"2023-01-22T21:17:59.667098Z","shell.execute_reply.started":"2023-01-22T21:17:59.656639Z","shell.execute_reply":"2023-01-22T21:17:59.666096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = os.system(f'printf \"Number of files in temp directory: $(ls {test_images_temp_folder}| wc -l)\"')","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:17:59.67415Z","iopub.execute_input":"2023-01-22T21:17:59.676599Z","iopub.status.idle":"2023-01-22T21:17:59.689452Z","shell.execute_reply.started":"2023-01-22T21:17:59.676565Z","shell.execute_reply":"2023-01-22T21:17:59.688094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model","metadata":{}},{"cell_type":"code","source":"class TransformerModel(tf.keras.Model):\n    def __init__(self, pooling=False):\n        super().__init__()\n        \n        transformer_type = getattr(kecam, Config[\"model_type\"])\n        transformer_model = getattr(transformer_type,Config[\"model_name\"])(num_classes=0, input_shape=(Config['dim_y'],Config['dim_x'],3),\n                                                          pretrained=Config[\"model_weights\"])\n        self.base_model = transformer_model\n        \n        self.pooling = pooling\n        if(pooling):\n            self.pooler = layers.GlobalAveragePooling2D()\n            \n        \n        self.output_dropout_list = [layers.Dropout(0.1*i) for i in range(1,6)]\n        self.dense = layers.Dense(64)\n        \n        self.leaky_relu = layers.LeakyReLU(alpha=0.05)\n        \n        self.classifier = layers.Dense(1, activation = \"sigmoid\")\n    \n    def call(self, inputs, training=False):\n        image = inputs\n        \n        x = self.base_model(image)\n        \n        if(self.pooling):\n            x = self.pooler(x)\n        \n        #multi-sample dropout\n        x_classified_list = []\n        for i in range(0,5):\n            x_to_dense = x\n            if(training):\n                x_to_dense = self.output_dropout_list[i](x_to_dense)\n            \n            x_densed = self.dense(x_to_dense)\n            x_densed_act = self.leaky_relu(x_densed)\n            \n            x_classified = self.classifier(x_densed_act)\n            x_classified_list += [x_classified]\n        \n        output_stacked = tf.stack(x_classified_list, axis = 0)\n        output = tf.reduce_mean(output_stacked, axis = 0)\n        \n        return output","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:17:59.691731Z","iopub.execute_input":"2023-01-22T21:17:59.692052Z","iopub.status.idle":"2023-01-22T21:17:59.715712Z","shell.execute_reply.started":"2023-01-22T21:17:59.692022Z","shell.execute_reply":"2023-01-22T21:17:59.714785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Datasets","metadata":{}},{"cell_type":"code","source":"def random_float(shape=[], minval=0.0, maxval=1.0):\n    rnd = tf.random.uniform(\n        shape=shape, minval=minval, maxval=maxval, dtype=tf.float32)\n    return rnd\n\ndef dropout(image,DIM=[Config[\"dim_y\"],Config[\"dim_x\"]], CT = 12, SZ = 0.08, mode='fill', fill_with = 0):\n    # input image - is one image of size [dim,dim,3] not a batch of [b,dim,dim,3]\n    # output - image with CT squares of side size SZ*DIM removed\n\n    # DO DROPOUT WITH PROBABILITY DEFINED ABOVE\n    if (CT==0)|(SZ==0): \n        return image\n    CT = tf.random.uniform([],1,CT,tf.int32)\n    SZ = random_float(minval=0.7, maxval=1.0)*SZ\n    for k in range(CT):\n        # CHOOSE RANDOM LOCATION\n        x = tf.cast( tf.random.uniform([],0,DIM[1]),tf.int32)\n        y = tf.cast( tf.random.uniform([],0,DIM[0]),tf.int32)\n        # COMPUTE SQUARE \n        WIDTH = tf.cast( SZ*min(DIM),tf.int32)\n        ya = tf.math.maximum(0,y-WIDTH//2)\n        yb = tf.math.minimum(DIM[0],y+WIDTH//2)\n        xa = tf.math.maximum(0,x-WIDTH//2)\n        xb = tf.math.minimum(DIM[1],x+WIDTH//2)\n        # DROPOUT IMAGE\n        one = image[ya:yb,0:xa,:]\n        \n        two = tf.ones([yb-ya,xb-xa,3], dtype = image.dtype)\n        if(mode=='fill'):\n             two = two * fill_with\n        elif(mode=='mean'):\n            two = two * tf.reduce_mean(image[ya:yb,xa:xb,:])\n        two = tf.cast(two, image.dtype)\n            \n        three = image[ya:yb,xb:DIM[1],:]\n        middle = tf.concat([one,two,three],axis=1)\n        image = tf.concat([image[0:ya,:,:],middle,image[yb:DIM[0],:,:]],axis=0)\n        image = tf.reshape(image,[*DIM,3])\n\n    return image\n\ndef apply_augments_set_random(img):\n    rnd_bc = random_float()<0.5\n    rnd_crop = random_float()<0.2\n    rnd_drop = random_float()<0.1\n    rnd_flip = random_float()<0.3\n    \n    if (rnd_bc):\n        img = tf.image.random_brightness(img, 0.2)\n        img = tf.image.random_contrast(img, 0.85, 1.15)\n    if (rnd_crop):\n        crop_size = random_float(minval=0.75, maxval=0.95)\n        crop_size_x = tf.cast(Config[\"dim_x\"]*crop_size,tf.int32)\n        crop_size_y = tf.cast(Config[\"dim_y\"]*crop_size,tf.int32)\n        img = tf.image.random_crop(img, [crop_size_y,crop_size_x,3])\n        \n        img = tf.image.resize(img, [Config[\"dim_y\"],Config[\"dim_x\"]])\n        img = tf.cast(img,np.dtype('uint8'))\n    if (rnd_drop):\n        rnd = random_float()\n        if(rnd<0.7):\n            img = dropout(img,DIM=[Config[\"dim_y\"],Config[\"dim_x\"]], CT = 12, SZ = 0.08, mode='mean')\n        elif(rnd<0.85):\n            img = dropout(img,DIM=[Config[\"dim_y\"],Config[\"dim_x\"]], CT = 12, SZ = 0.08, mode='fill', fill_with = 0)\n        else:\n            img = dropout(img,DIM=[Config[\"dim_y\"],Config[\"dim_x\"]], CT = 12, SZ = 0.08, mode='fill', fill_with = 255)\n    if (rnd_flip):\n        img = tf.image.flip_left_right(img)\n    \n    return img\n\ndef apply_augments_set_0(img):\n    rnd_bc = random_float()<0.5\n    rnd_crop = random_float()<0.2\n    rnd_drop = random_float()<0.2\n    \n    if(not tf.math.reduce_any([rnd_bc,rnd_crop,rnd_drop])):\n        rnd_bc = True\n        \n    \n    if (rnd_bc):\n        img = tf.image.random_brightness(img, 0.2)\n        img = tf.image.random_contrast(img, 0.85, 1.15)\n    if (rnd_crop):\n        crop_size = random_float(minval=0.75, maxval=0.95)\n        crop_size_x = tf.cast(Config[\"dim_x\"]*crop_size,tf.int32)\n        crop_size_y = tf.cast(Config[\"dim_y\"]*crop_size,tf.int32)\n        img = tf.image.random_crop(img, [crop_size_y,crop_size_x,3])\n        \n        img = tf.image.resize(img, [Config[\"dim_y\"],Config[\"dim_x\"]])\n        img = tf.cast(img,np.dtype('uint8'))\n    if (rnd_drop):\n        rnd = random_float()\n        if(rnd<0.7):\n            img = dropout(img,DIM=[Config[\"dim_y\"],Config[\"dim_x\"]], CT = 12, SZ = 0.08, mode='mean')\n        elif(rnd<0.85):\n            img = dropout(img,DIM=[Config[\"dim_y\"],Config[\"dim_x\"]], CT = 12, SZ = 0.08, mode='fill', fill_with = 0)\n        else:\n            img = dropout(img,DIM=[Config[\"dim_y\"],Config[\"dim_x\"]], CT = 12, SZ = 0.08, mode='fill', fill_with = 255)\n    \n    return img\n\ndef apply_augments_set_1(img):\n    rnd = random_float()\n    \n    if (rnd < 0.8):\n        img = tf.image.random_brightness(img, 0.1)\n        img = tf.image.random_contrast(img, 0.95, 1.05)\n    if (rnd < 0.1 and rnd >= 0.8):\n        crop_size = random_float(minval=0.85, maxval=0.95)\n        crop_size_x = tf.cast(Config[\"dim_x\"]*crop_size,tf.int32)\n        crop_size_y = tf.cast(Config[\"dim_y\"]*crop_size,tf.int32)\n        img = tf.image.random_crop(img, [crop_size_y,crop_size_x,3])\n        \n        img = tf.image.resize(img, [Config[\"dim_y\"],Config[\"dim_x\"]])\n        img = tf.cast(img,np.dtype('uint8'))\n    \n    return img\n\ndef apply_augments_set_flip(img):\n    img = tf.image.flip_left_right(img)\n    \n    return img","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:17:59.720433Z","iopub.execute_input":"2023-01-22T21:17:59.721848Z","iopub.status.idle":"2023-01-22T21:17:59.760137Z","shell.execute_reply.started":"2023-01-22T21:17:59.721813Z","shell.execute_reply":"2023-01-22T21:17:59.759104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_img(path):\n    file_bytes = tf.io.read_file(path)\n    img = tf.image.decode_png(file_bytes, channels=3, dtype=np.dtype('uint8'))\n\n    img = tf.reshape(img, (Config[\"dim_y\"],Config[\"dim_x\"],3))\n\n    return img","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:17:59.764961Z","iopub.execute_input":"2023-01-22T21:17:59.767407Z","iopub.status.idle":"2023-01-22T21:17:59.774576Z","shell.execute_reply.started":"2023-01-22T21:17:59.767374Z","shell.execute_reply":"2023-01-22T21:17:59.773514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dataset(paths, augment ,normalize):\n    img_paths = tf.data.Dataset.from_tensor_slices(paths)\n    dataset = img_paths.map(decode_img, num_parallel_calls=Config[\"AUTO\"])\n    \n    if(augment == \"0\"):\n        #normal\n        dataset = dataset.map(apply_augments_set_0, num_parallel_calls=Config[\"AUTO\"])\n    elif(augment == \"1\"):\n        #low\n        dataset = dataset.map(apply_augments_set_1, num_parallel_calls=Config[\"AUTO\"])\n    elif(augment == \"random\"):\n        dataset = dataset.map(apply_augments_set_random, num_parallel_calls=Config[\"AUTO\"])\n    elif(augment == \"flip\"):\n        dataset = dataset.map(apply_augments_set_flip, num_parallel_calls=Config[\"AUTO\"])\n        \n    if(normalize and Config[\"include_preprocessing\"]):\n        dataset = dataset.map(lambda img: tf.cast(img,  tf.float32), num_parallel_calls=Config[\"AUTO\"])\n        dataset = dataset.map(lambda img: keras.applications.imagenet_utils.preprocess_input(img, mode='torch'),\n                              num_parallel_calls=Config[\"AUTO\"])\n        if(Config[\"quantization_aware_training\"] or Config[\"post_training_quantization\"]):\n            dataset = dataset.map(lambda img: tf.cast(img, tf.float16),\n                              num_parallel_calls=Config[\"AUTO\"])\n    return dataset\n\n\ndef get_dataset(paths, augment=0, normalize=True):\n    dataset = load_dataset(paths, augment, normalize)\n    dataset = dataset.batch(Config[\"batch_size\"])\n    dataset = dataset.prefetch(Config[\"AUTO\"])\n    return dataset","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:17:59.779252Z","iopub.execute_input":"2023-01-22T21:17:59.781727Z","iopub.status.idle":"2023-01-22T21:17:59.794814Z","shell.execute_reply.started":"2023-01-22T21:17:59.781693Z","shell.execute_reply":"2023-01-22T21:17:59.793777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if(Config[\"preserved_folder_structure\"]):\n    img_paths = test_df.apply(lambda x: os.path.join(test_images_temp_folder, str(x.patient_id), f'{str(x.image_id)}.png'), axis=1)\nelse:\n    img_paths = test_df.apply(lambda x: os.path.join(test_images_temp_folder, f'{str(x.patient_id)}_{str(x.image_id)}.png'), axis=1)\n\ndataset_dict=next(iter(get_dataset(img_paths, augment=None, normalize=False)))\n\nprint('image shape:',dataset_dict[0].shape)\n\nfig=plt.figure(figsize=(20, 10))\nfor i in range(0,Config[\"batch_size\"]):\n    fig.add_subplot(1,Config[\"batch_size\"],i+1)\n    plt.imshow(dataset_dict[i], cmap='bone')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:17:59.799453Z","iopub.execute_input":"2023-01-22T21:17:59.801978Z","iopub.status.idle":"2023-01-22T21:18:02.846551Z","shell.execute_reply.started":"2023-01-22T21:17:59.801945Z","shell.execute_reply":"2023-01-22T21:18:02.845538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Inference preparation","metadata":{}},{"cell_type":"code","source":"best_val_pF1_thrs = np.load(os.path.join(training_notebook_path,'best_val_pF1_thrs.npy'))\ndisplay(best_val_pF1_thrs)\n\nbest_val_thrs = np.load(os.path.join(training_notebook_path,'best_val_thrs.npy'))\ndisplay(best_val_thrs)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:18:02.847543Z","iopub.execute_input":"2023-01-22T21:18:02.848853Z","iopub.status.idle":"2023-01-22T21:18:02.879753Z","shell.execute_reply.started":"2023-01-22T21:18:02.848803Z","shell.execute_reply":"2023-01-22T21:18:02.879026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fold_weights = (best_val_pF1_thrs - np.min(best_val_pF1_thrs)*0.5)\nfold_weights = fold_weights/np.sum(fold_weights)\nfold_weights","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:22:19.19324Z","iopub.execute_input":"2023-01-22T21:22:19.193615Z","iopub.status.idle":"2023-01-22T21:22:19.201437Z","shell.execute_reply.started":"2023-01-22T21:22:19.193585Z","shell.execute_reply":"2023-01-22T21:22:19.200452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if(Config[\"post_training_quantization\"]):\n    temp_dir = \"/kaggle/temp\"\n    os.makedirs(temp_dir, exist_ok = True)\n    \n    for fold, path in enumerate(saved_weights_paths):\n        os.system(f\"cat {path} | sed s/float32/float16/g > {temp_dir}/casted_model_{fold}.h5\")\n        \n    saved_weights_paths = [f'{temp_dir}/casted_model_{fold}.h5' for fold in range(0,Config[\"n_fold\"])]\n    display(saved_weights_paths)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:18:02.892129Z","iopub.execute_input":"2023-01-22T21:18:02.892616Z","iopub.status.idle":"2023-01-22T21:18:02.899579Z","shell.execute_reply.started":"2023-01-22T21:18:02.89259Z","shell.execute_reply":"2023-01-22T21:18:02.898643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Inference","metadata":{}},{"cell_type":"code","source":"tf.keras.backend.clear_session()\n\nif(Config[\"preserved_folder_structure\"]):\n    img_paths = test_df.apply(lambda x: os.path.join(test_images_temp_folder, str(x.patient_id), f'{str(x.image_id)}.png'), axis=1)\nelse:\n    img_paths = test_df.apply(lambda x: os.path.join(test_images_temp_folder, f'{str(x.patient_id)}_{str(x.image_id)}.png'), axis=1)\n\ny_preds_fold_aug = []\n\nmodel = TransformerModel()\nmodel.build(input_shape = (None,Config[\"dim_y\"],Config[\"dim_x\"],3))\n\nfor fold in range(Config[\"n_fold\"]):\n    \n    saved_weights_path = saved_weights_paths[fold]\n    model.load_weights(saved_weights_path)\n    \n    y_preds_aug = []\n    #None: no augs, \"0\": normal augs, \"1\": low augs, \"random\": random augs\n    for aug in Config[\"aug_set\"]:\n        print(f\"TTA set {aug} fold {fold}\")\n\n        test_ds = get_dataset(img_paths,augment = aug)\n\n        y_pred = model.predict(test_ds)\n\n        y_pred = np.float32(y_pred)\n\n        y_preds_aug += [y_pred]\n\n    y_preds_fold_aug += [y_preds_aug]\n    display(y_preds_fold_aug[fold])","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:18:02.902753Z","iopub.execute_input":"2023-01-22T21:18:02.903016Z","iopub.status.idle":"2023-01-22T21:18:22.467582Z","shell.execute_reply.started":"2023-01-22T21:18:02.902993Z","shell.execute_reply":"2023-01-22T21:18:22.466689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TTA_results = np.max(y_preds_fold_aug, axis=1)\nTTA_results = TTA_results-best_val_thrs[:,None,None]\nTTA_results","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:18:22.469252Z","iopub.execute_input":"2023-01-22T21:18:22.469617Z","iopub.status.idle":"2023-01-22T21:18:22.479982Z","shell.execute_reply.started":"2023-01-22T21:18:22.469583Z","shell.execute_reply":"2023-01-22T21:18:22.47855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = np.average(TTA_results, axis=0, weights=fold_weights)\nprediction = np.int32(prediction > 0)\nprediction","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:18:22.481709Z","iopub.execute_input":"2023-01-22T21:18:22.481985Z","iopub.status.idle":"2023-01-22T21:18:22.490542Z","shell.execute_reply.started":"2023-01-22T21:18:22.481954Z","shell.execute_reply":"2023-01-22T21:18:22.489148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df = pd.DataFrame({'prediction_id':test_df.prediction_id,\n                        'cancer':prediction.reshape(-1)})\npred_df","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:18:22.491909Z","iopub.execute_input":"2023-01-22T21:18:22.493Z","iopub.status.idle":"2023-01-22T21:18:22.505564Z","shell.execute_reply.started":"2023-01-22T21:18:22.492966Z","shell.execute_reply":"2023-01-22T21:18:22.504584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.read_csv(f'/kaggle/input/rsna-breast-cancer-detection/sample_submission.csv')[['prediction_id']]\nsub_df = sub_df.merge(pred_df, on='prediction_id', how='left')\nsub_df = sub_df.groupby('prediction_id')['cancer'].max().reset_index()\nsub_df.to_csv('submission.csv',index=False)\n\nsub_df","metadata":{"execution":{"iopub.status.busy":"2023-01-22T21:18:22.506577Z","iopub.execute_input":"2023-01-22T21:18:22.508867Z","iopub.status.idle":"2023-01-22T21:18:22.539554Z","shell.execute_reply.started":"2023-01-22T21:18:22.50884Z","shell.execute_reply":"2023-01-22T21:18:22.53847Z"},"trusted":true},"execution_count":null,"outputs":[]}]}