{"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":"<div class=\"alert alert-block alert-success\" style=\"font-size:25px\">\n 1. Imports, constants and dependencies \n</div>","metadata":{"papermill":{"duration":0.008671,"end_time":"2022-12-05T00:21:16.552675","exception":false,"start_time":"2022-12-05T00:21:16.544004","status":"completed"},"tags":[]}},{"cell_type":"code","source":"\ntry:\n    import pylibjpeg\nexcept:\n    !pip install -q /kaggle/input/rsna-2022-whl/{pydicom-2.3.0-py3-none-any.whl,pylibjpeg-1.4.0-py3-none-any.whl,python_gdcm-3.0.15-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl}\n    !pip install -q /kaggle/input/rsna-bcd-whl-ds/python_gdcm-3.0.20-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl\n    # !pip install -q /kaggle/input/rsna-bcd-whl-ds/pylibjpeg-1.4.0-py3-none-any.whl\n    !pip install -q /kaggle/input/rsna-bcd-whl-ds/dicomsdl-0.109.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl","metadata":{"papermill":{"duration":112.187954,"end_time":"2022-12-05T00:23:08.74552","exception":false,"start_time":"2022-12-05T00:21:16.557566","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-02-18T14:40:14.895051Z","iopub.execute_input":"2023-02-18T14:40:14.895864Z","iopub.status.idle":"2023-02-18T14:41:55.74033Z","shell.execute_reply.started":"2023-02-18T14:40:14.895768Z","shell.execute_reply":"2023-02-18T14:41:55.739139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport torch\nimport sys\nsys.path.append('/kaggle/input/timm-pytorch-image-models/pytorch-image-models-master')\nfrom timm import create_model\nfrom tqdm.notebook import tqdm\nfrom joblib import Parallel, delayed\nimport cv2\n# import dicomsdl\n\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nfrom pprint import pprint\npd.set_option('display.max_rows', 1000)\npd.set_option('display.max_columns', 1000)\nplt.rcParams['figure.figsize'] = (20, 5)\n\n\n\nRSNA_2022_PATH = '/kaggle/input/rsna-breast-cancer-detection'\nPNG_TEST_IMAGES_PATH = f'test'\nMODELS_PATH = '/kaggle/input/wandb-models/models'\nDCM_TEST_IMAGES_PATH = f'/kaggle/input/rsna-breast-cancer-detection/test_images'\n\n# AUX_TARGET_NCLASSES = [2, 2, 6, 2, 2, 2, 4, 5, 2, 10, 10]\n\n\n# try:\n#     from kaggle_secrets import UserSecretsClient\n#     IS_KAGGLE = True\n# except:\n#     IS_KAGGLE = False\n\nDEBUG = True\n\n# if not IS_KAGGLE:\n#     print('Running locally')\n#     RSNA_2022_PATH = 'data'\n#     DCM_TEST_IMAGES_PATH = f'data/test_images'\n#     MODELS_PATH = 'models_roi_1024_v2'\n\n\nDEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'\nif DEVICE == 'cuda':\n    BATCH_SIZE = 16\nelse:\n    BATCH_SIZE = 2\n    \nclass CFG:\n    resize_dim = 1024\n    aspect_ratio = True\n    img_size = [512, 256]\nimg_size = [512, 256]","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:41:55.744077Z","iopub.execute_input":"2023-02-18T14:41:55.744411Z","iopub.status.idle":"2023-02-18T14:42:00.763804Z","shell.execute_reply.started":"2023-02-18T14:41:55.744377Z","shell.execute_reply":"2023-02-18T14:42:00.762691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\" style=\"font-size:25px\">\n    2. Loading test dataframe\n</div>\n ","metadata":{"papermill":{"duration":0.005183,"end_time":"2022-12-05T00:23:12.335995","exception":false,"start_time":"2022-12-05T00:23:12.330812","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def load_df_test():\n    df_test = pd.read_csv(f'{RSNA_2022_PATH}/test.csv')\n    return df_test\n\ndf_test = load_df_test()\n\ndf_test","metadata":{"papermill":{"duration":0.047641,"end_time":"2022-12-05T00:23:12.38885","exception":false,"start_time":"2022-12-05T00:23:12.341209","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-02-18T14:42:00.765802Z","iopub.execute_input":"2023-02-18T14:42:00.76645Z","iopub.status.idle":"2023-02-18T14:42:00.802587Z","shell.execute_reply.started":"2023-02-18T14:42:00.766405Z","shell.execute_reply":"2023-02-18T14:42:00.801567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\" style=\"font-size:25px\">\n    3. Dataset class\n</div>\n\n`BreastCancerDataset` class returns individual images. It uses a dataframe parameter `df` as a source of metadata to locate and load images from `path` folder. ","metadata":{"papermill":{"duration":0.005225,"end_time":"2022-12-05T00:23:12.399738","exception":false,"start_time":"2022-12-05T00:23:12.394513","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"### Preprocess images ROI","metadata":{}},{"cell_type":"code","source":"!rm -rf test","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:42:00.805112Z","iopub.execute_input":"2023-02-18T14:42:00.805567Z","iopub.status.idle":"2023-02-18T14:42:01.75727Z","shell.execute_reply.started":"2023-02-18T14:42:00.805532Z","shell.execute_reply":"2023-02-18T14:42:01.755988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor\nimport re\nimport pydicom\n\ndef fit_image(fname, size=1024):\n    # 1. Read, resize\n    \n    \n    patient = fname.split('/')[-2]\n    image = fname.split('/')[-1][:-4]\n    dicom = pydicom.dcmread(fname)\n    img = dicom.pixel_array\n    img = (img - img.min()) / (img.max() - img.min())\n    if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        img = 1 - img\n    img = cv2.resize(img, (img_size[0], img_size[1]))\n    \n    # 2. Crop\n    X = img\n    # Some images have narrow exterior \"frames\" that complicate selection of the main data. Cutting off the frame\n    X = X[5:-5, 5:-5]\n    \n    \n    # regions of non-empty pixels\n    output= cv2.connectedComponentsWithStats((X > 0.05).astype(np.uint8)[:, :], 8, cv2.CV_32S)\n\n    # stats.shape == (N, 5), where N is the number of regions, 5 dimensions correspond to:\n    # left, top, width, height, area_size\n    stats = output[2]\n    \n    # finding max area which always corresponds to the breast data. \n    idx = stats[1:, 4].argmax() + 1\n    x1, y1, w, h = stats[idx][:4]\n    x2 = x1 + w\n    y2 = y1 + h\n    \n    # cutting out the breast data\n    X_fit = X[y1: y2, x1: x2]\n    \n    patient_id, im_id = os.path.basename(os.path.dirname(fname)), os.path.basename(fname)[:-4]\n    os.makedirs(f'{PNG_TEST_IMAGES_PATH}/test_images/{patient_id}', exist_ok=True)\n    cv2.imwrite(f'{PNG_TEST_IMAGES_PATH}/test_images/{patient_id}/{im_id}.png', (X_fit[:, :] * 255).astype(np.uint8))\n\ndef fit_all_images(all_images):\n    with ThreadPoolExecutor(2) as p:\n        for i in tqdm(p.map(fit_image, all_images), total=len(all_images)):\n            pass\n\nall_images = glob.glob('/kaggle/input/rsna-breast-cancer-detection/test_images/*/*') \n# all_images = glob.glob('/kaggle/input/rsna-breast-cancer-detection/train_images/10006/*')\nfit_all_images(all_images)","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:42:01.759145Z","iopub.execute_input":"2023-02-18T14:42:01.759563Z","iopub.status.idle":"2023-02-18T14:42:04.261808Z","shell.execute_reply.started":"2023-02-18T14:42:01.759521Z","shell.execute_reply":"2023-02-18T14:42:04.260744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!find test | head","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:42:04.263496Z","iopub.execute_input":"2023-02-18T14:42:04.264218Z","iopub.status.idle":"2023-02-18T14:42:05.226537Z","shell.execute_reply.started":"2023-02-18T14:42:04.26418Z","shell.execute_reply":"2023-02-18T14:42:05.225325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision\nfrom PIL import Image\n\ndef get_transforms(data):\n    def transforms(img):\n        if data=='train':\n            tfm = [\n                torchvision.transforms.RandomHorizontalFlip(0.5),\n                torchvision.transforms.RandomRotation(degrees=(-5, 5)), \n#                 torchvision.transforms.RandomResizedCrop((512, 256), scale=(0.8, 1), ratio=(0.45, 0.55)) \n                                torchvision.transforms.Resize((512, 256))\n\n            ]\n        elif data=='valid':\n            tfm = [\n#                 torchvision.transforms.RandomHorizontalFlip(0.5),\n                torchvision.transforms.Resize((512, 256))\n            ]\n        img = torchvision.transforms.Compose(tfm + [            \n            torchvision.transforms.ToTensor(),\n            torchvision.transforms.Normalize(mean=0.2179, std=0.0529),\n        ])(img)\n        return img\n\n    return lambda img: transforms(img)\nDEBUG=True\nif DEBUG:\n    tfm = get_transforms(data='train')\n    img = Image.open(f\"{PNG_TEST_IMAGES_PATH}/test_images/10008/68070693.png\")\n    plt.imshow(np.array(img), cmap='gray')\n    plt.show()\n\n    plt.figure(figsize=(20, 20))\n    for i in range(8):\n        v = tfm(img).permute(1, 2, 0)\n#         v -= v.min()\n#         v /= v.max()\n        # plt.imshow(v)\n        # break\n        plt.subplot(2, 4, i + 1).imshow(v)\n    plt.tight_layout()\nDEBUG=False\n","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:42:05.229846Z","iopub.execute_input":"2023-02-18T14:42:05.230233Z","iopub.status.idle":"2023-02-18T14:42:07.765832Z","shell.execute_reply.started":"2023-02-18T14:42:05.23019Z","shell.execute_reply":"2023-02-18T14:42:07.76444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_PATH='/kaggle/working/test/test_images/'\ndf_test[\"path\"] = TEST_PATH+df_test.patient_id.astype(str)+'/'+df_test.image_id.astype(str)+'.png'\n","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:42:07.767934Z","iopub.execute_input":"2023-02-18T14:42:07.768818Z","iopub.status.idle":"2023-02-18T14:42:07.779769Z","shell.execute_reply.started":"2023-02-18T14:42:07.768765Z","shell.execute_reply":"2023-02-18T14:42:07.778983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\nclass RSNADataset():\n    def __init__(self, df,transform=None): \n        self.df = df\n        self.transform = transform\n        if 'cancer' in df.columns:\n            self.ds_type='train'\n        else:\n            self.ds_type='test'\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self,idx):\n        row = self.df.iloc[idx]\n        img_path =row['path']\n        image = Image.open(img_path)\n#         csv_columns = ['laterality', 'view', 'age', 'implant']\n#         csv_data = np.array(row[csv_columns].values, \n#                             dtype=np.float32)\n        csv_data=0\n        if self.transform is not None:\n            image = self.transform(image)\n        image=torch.cat((image,image,image),0)\n    \n        if self.ds_type=='train':\n            label=torch.tensor(row['cancer'])\n            return {'image':image,'label': label}\n        else:\n            return {'image':image}\n\n\n\n\n# test/test_images/10008/68070693.png\n\nds_test = RSNADataset(df_test, get_transforms('valid'))\nif DEBUG:\n#     X, y_cancer = ds_test[8]\n    img=ds_test[1]['image']\n    print(img.shape)\n    img=img.permute(1,2,0)\n    print(img.shape)\n    plt.imshow(img)\n#     print(X.shape, y_cancer.shape, y_aux.shape)","metadata":{"papermill":{"duration":0.091858,"end_time":"2022-12-05T00:23:19.183339","exception":false,"start_time":"2022-12-05T00:23:19.091481","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-02-18T14:42:07.781111Z","iopub.execute_input":"2023-02-18T14:42:07.781673Z","iopub.status.idle":"2023-02-18T14:42:07.797361Z","shell.execute_reply.started":"2023-02-18T14:42:07.781632Z","shell.execute_reply":"2023-02-18T14:42:07.79636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\" style=\"font-size:25px\">\n     4. Model \n</div>\n","metadata":{"papermill":{"duration":0.006036,"end_time":"2022-12-05T00:23:19.195636","exception":false,"start_time":"2022-12-05T00:23:19.1896","status":"completed"},"tags":[]}},{"cell_type":"code","source":"class BreastCancerModel(torch.nn.Module):\n    def __init__(self, model_type, pretrained=True,dropout=0.):\n        super().__init__()        \n        self.model = create_model(model_type, pretrained=pretrained, num_classes=0, drop_rate=dropout)\n\n        self.backbone_dim = self.model(torch.randn(1, 3, 256, 256)).shape[-1]\n        \n        \n        self.nn_cancer = torch.nn.Sequential(\n            torch.nn.Linear(self.backbone_dim, 1),\n        )\n\n    def forward(self, x):\n        x = self.model(x)\n        cancer = self.nn_cancer(x).squeeze()\n        return cancer\n    \n    def predict(self, x):\n        preds=torch.sigmoid(self.forward(x))\n        return preds\n","metadata":{"papermill":{"duration":2.459131,"end_time":"2022-12-05T00:23:21.660809","exception":false,"start_time":"2022-12-05T00:23:19.201678","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-02-18T14:42:07.802439Z","iopub.execute_input":"2023-02-18T14:42:07.803335Z","iopub.status.idle":"2023-02-18T14:42:07.812548Z","shell.execute_reply.started":"2023-02-18T14:42:07.803241Z","shell.execute_reply":"2023-02-18T14:42:07.811691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models = []\n\nweights_path=[f'/kaggle/input/seresnext-models-rsna/seresnext50_32x4d_{pos}_v1.pth' for pos in range(0,4)]\n\nfor weights in tqdm(weights_path):\n    model=BreastCancerModel('seresnext50_32x4d', pretrained=False)\n    model.load_state_dict(torch.load(weights,\n                                     map_location=DEVICE))\n    model = model.to(DEVICE)\n    models.append(model)\n","metadata":{"papermill":{"duration":12.829022,"end_time":"2022-12-05T00:23:34.516853","exception":false,"start_time":"2022-12-05T00:23:21.687831","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-02-18T14:42:07.81399Z","iopub.execute_input":"2023-02-18T14:42:07.814657Z","iopub.status.idle":"2023-02-18T14:42:19.021926Z","shell.execute_reply.started":"2023-02-18T14:42:07.814624Z","shell.execute_reply":"2023-02-18T14:42:19.020896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pfbeta_vect(labels, predictions, thresh=0.5):\n    labels=torch.tensor(labels)\n    predictions=(torch.tensor(predictions))\n    labels=labels.squeeze()\n    predictions=predictions.squeeze()>thresh\n    conf_matrix={'tp':0, 'fp':0, 'fn':0, 'tn':0}\n    conf_matrix['tp']=(predictions[labels==1]).sum().item()\n    conf_matrix['tn']=(predictions==labels).sum().item()-conf_matrix['tp']\n    conf_matrix['fp']=(predictions.sum()-conf_matrix['tp']).item()\n    conf_matrix['fn']=(labels.sum()-conf_matrix['tp']).item()\n    if (conf_matrix['tp']+0.5*(conf_matrix['fp']+conf_matrix['fn']))>0:\n        score=(conf_matrix['tp'])/(conf_matrix['tp']+0.5*(conf_matrix['fp']+conf_matrix['fn']))\n    else:\n        score=0\n    return score\n    \ndef optimize_thresh(labels, predictions, verbose=False, thresh_range=None):\n    if thresh_range is None:\n        thresh_range=[thresh/100 for thresh in range(0,100,1)]\n\n    scores=[pfbeta_vect(labels, predictions, thresh) for thresh in thresh_range]\n    max_score=max(scores)\n\n    best_thresh=thresh_range[scores.index(max_score)]\n\n    if verbose: print(scores)\n    return best_thresh, max_score\n    ","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:42:19.023511Z","iopub.execute_input":"2023-02-18T14:42:19.024125Z","iopub.status.idle":"2023-02-18T14:42:19.036251Z","shell.execute_reply.started":"2023-02-18T14:42:19.024076Z","shell.execute_reply":"2023-02-18T14:42:19.035151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# test on the training data\n","metadata":{}},{"cell_type":"code","source":"# import pickle ","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:42:19.038209Z","iopub.execute_input":"2023-02-18T14:42:19.038929Z","iopub.status.idle":"2023-02-18T14:42:19.050099Z","shell.execute_reply.started":"2023-02-18T14:42:19.038893Z","shell.execute_reply":"2023-02-18T14:42:19.04896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with open('/kaggle/input/predictions/preds.bin', 'rb') as f:\n#     data=pickle.load(f)\n","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2023-02-18T14:42:19.051852Z","iopub.execute_input":"2023-02-18T14:42:19.05222Z","iopub.status.idle":"2023-02-18T14:42:19.061342Z","shell.execute_reply.started":"2023-02-18T14:42:19.052186Z","shell.execute_reply":"2023-02-18T14:42:19.060358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ensemble_preds=torch.zeros(54706)\n# thresh=[0.67, 0.62, 0.63, 0.58]\n# for pos in range(0,4):\n# #     preds=torch.where(data[f'model {pos}'][1]>thresh[pos],1,0)\n#     preds=data[f'model {pos}'][1]/4\n#     ensemble_preds+=preds\n# labels=data['model 0'][0]\n# optimize_thresh(labels, ensemble_preds)","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:42:19.063059Z","iopub.execute_input":"2023-02-18T14:42:19.063508Z","iopub.status.idle":"2023-02-18T14:42:19.073086Z","shell.execute_reply.started":"2023-02-18T14:42:19.063473Z","shell.execute_reply":"2023-02-18T14:42:19.072151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for model in data.keys():\n#     print(f\"----{model}-----\")\n#     value=data[model]\n#     print(optimize_thresh(*value))\n# # 0.67, 0.62, 0.63, 0.58","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2023-02-18T14:42:19.075743Z","iopub.execute_input":"2023-02-18T14:42:19.076186Z","iopub.status.idle":"2023-02-18T14:42:19.084207Z","shell.execute_reply.started":"2023-02-18T14:42:19.076132Z","shell.execute_reply":"2023-02-18T14:42:19.083296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def get_score(target_labels, predictions):\n#     print(pfbeta_vect(target_labels, predictions))\n\n# def prediction_on_train(model):\n# #     model=BreastCancerModel('seresnext50_32x4d', pretrained=False).to(DEVICE)\n\n#     TRAIN_PATH='/kaggle/input/rsna-cut-off-empty-space-from-images'\n#     train_ds=pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')\n#     train_ds['path']=TRAIN_PATH+'/'+train_ds['patient_id'].astype(str)+'/'+train_ds['image_id'].astype(str)+'.png'\n#     train_dataset=RSNADataset(train_ds, get_transforms('valid'))\n    \n#     dl_train = torch.utils.data.DataLoader(train_dataset, batch_size=16, \n#                                           shuffle=False, num_workers=2)\n#     predictions=torch.tensor([])\n#     target_labels=torch.tensor([])\n#     for data in tqdm(dl_train):\n#         images=data['image'].to(DEVICE)\n#         targets=data['label']\n#         preds=model.predict(images)\n#         predictions=torch.cat((predictions, preds.clone().detach().cpu()),0)\n#         target_labels=torch.cat((target_labels, targets),0)\n# #     optimize_thresh(target_labels, predictions)\n\n#     optimize_thresh(target_labels, predictions,thresh_range=[i/100 for i in range(60,70)] ,verbose=True)\n#     return target_labels, predictions\n\n\n# OUTPUT_DIR='/kaggle/working/'\n# model_wise_predictions={}\n# pos=0\n# for model in models:\n#     target_labels, predictions=prediction_on_train(model)\n#     model_wise_predictions[f'model {pos}']=(target_labels, predictions)\n#     pos+=1\n    \n# preds_file = open(OUTPUT_DIR+'preds.bin', 'wb+')\n# pickle.dump(model_wise_predictions, preds_file)\n# preds_file.close()\n\n\n# #     itr_df=iter(dl_train)\n# #     b1=next(itr_df)\n# #     img=b1['image']\n# #     img=(img.squeeze()).permute(1,2,0)/20\n# #     print(img.shape)\n# #     plt.imshow(img)\n    \n# # target_labels0, predictions0=prediction_on_train()\n# # optimize_thresh(target_labels0, predictions0)\n# # ts=[i/100 for i in range(60,80)]\n# # print(ts)\n","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2023-02-18T14:42:19.087514Z","iopub.execute_input":"2023-02-18T14:42:19.087867Z","iopub.status.idle":"2023-02-18T14:42:19.09759Z","shell.execute_reply.started":"2023-02-18T14:42:19.087829Z","shell.execute_reply":"2023-02-18T14:42:19.096767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#0.67 ! 0.62 !","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:42:19.098779Z","iopub.execute_input":"2023-02-18T14:42:19.099206Z","iopub.status.idle":"2023-02-18T14:42:19.114381Z","shell.execute_reply.started":"2023-02-18T14:42:19.099154Z","shell.execute_reply":"2023-02-18T14:42:19.113392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ts=[i/100 for i in range(60,80)]\n# print(ts)\n# optimize_thresh(target_labels0, predictions0,thresh_range=ts ,verbose=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:42:19.115827Z","iopub.execute_input":"2023-02-18T14:42:19.116213Z","iopub.status.idle":"2023-02-18T14:42:19.125056Z","shell.execute_reply.started":"2023-02-18T14:42:19.116178Z","shell.execute_reply":"2023-02-18T14:42:19.124129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\" style=\"font-size:25px\">\n     5. Submission \n</div>\n\nApparently, `pF1` metric can be improved by binarization of predicted values.\nSo we run all base model on every image from the test set, apply thresholds and then run majority voting by selecting the `median` value.","metadata":{"papermill":{"duration":0.005913,"end_time":"2022-12-05T00:23:34.529375","exception":false,"start_time":"2022-12-05T00:23:34.523462","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# # def models_predict(models, ds, max_batches=1e9):\n# #     dl_test = torch.utils.data.DataLoader(ds, batch_size=BATCH_SIZE, shuffle=False, num_workers=2)\n# #     for m in models:\n# #         m.eval()\n# #         m.to(DEVICE)\n# #     with torch.no_grad():\n# #         predictions = []\n# #         for idx, X in enumerate(tqdm(dl_test, mininterval=30)):\n            \n# #             pred = torch.zeros(len(X), len(models))\n# #             for idx, m in enumerate(models):\n# #                 preds = m.predict(X.to(DEVICE))[0].squeeze()\n# #                 pred[:, idx] = preds.cpu()\n# #             predictions.append(pred.mean(dim=-1))\n            \n# #             if idx >= max_batches:\n# #                 break\n# #         return torch.concat(predictions).numpy()\n\n# # # Quick test\n# # DEBUG=False\n# # if DEBUG:\n# #     list_of_models=[BreastCancerModel('efficientnet_b4', pretrained=False),BreastCancerModel('seresnext50_32x4d', pretrained=False)]\n# #     print(models_predict(list_of_models, ds_test))\n# def model_pred(model, ds_test):\n#     dl_test = torch.utils.data.DataLoader(ds, batch_size=BATCH_SIZE, shuffle=False, num_workers=2)\n#     model.to(device)\n#     with torch.no_grad():\n#         predictions = []\n#         for idx, X in enumerate(tqdm(dl_test, mininterval=30)):\n#             preds = model.predict(X.to(DEVICE))[0].squeeze().cpu()\n#             predictions.append(pred)\n\n#             if idx >= max_batches:\n#                 break\n#         return torch.concat(predictions).numpy()\n","metadata":{"papermill":{"duration":2.339608,"end_time":"2022-12-05T00:23:36.875001","exception":false,"start_time":"2022-12-05T00:23:34.535393","status":"completed"},"tags":[],"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2023-02-18T14:42:19.126719Z","iopub.execute_input":"2023-02-18T14:42:19.127118Z","iopub.status.idle":"2023-02-18T14:42:19.138918Z","shell.execute_reply.started":"2023-02-18T14:42:19.127084Z","shell.execute_reply":"2023-02-18T14:42:19.137995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def models_predict(models):\n\n    TEST_PATH='/kaggle/input/rsna-breast-cancer-detection/test_images'\n    test_ds=RSNADataset(df_test, get_transforms('valid'))\n    \n    dl_test = torch.utils.data.DataLoader(test_ds, batch_size=16, \n                                          shuffle=False, num_workers=2)\n    predictions=torch.tensor([])\n    models_predictions={'model-0':torch.tensor([]), \n                       'model-1':torch.tensor([]),\n                       'model-2':torch.tensor([]),\n                       'model-3':torch.tensor([])}\n    for data in tqdm(dl_test):\n        images=data['image'].to(DEVICE)\n        pos=0\n        for model in models:\n            preds=model.predict(images)\n            models_predictions[f'model-{pos}']=torch.cat((models_predictions[f'model-{pos}'], preds.clone().detach().cpu()),0)\n            pos+=1\n    return models_predictions","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:42:19.141794Z","iopub.execute_input":"2023-02-18T14:42:19.142108Z","iopub.status.idle":"2023-02-18T14:42:19.150571Z","shell.execute_reply.started":"2023-02-18T14:42:19.142084Z","shell.execute_reply":"2023-02-18T14:42:19.149553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models_pred = models_predict(models)\npredictions=0\nMODELS_THRESH=[0.67, 0.62, 0.63, 0.58]\nfor pos in range(0,4):\n    predictions+=(models_pred[f'model-{pos}']>MODELS_THRESH[pos]).float()\n\n# predictions=(predictions>THRES)","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:42:19.152036Z","iopub.execute_input":"2023-02-18T14:42:19.152388Z","iopub.status.idle":"2023-02-18T14:42:25.840664Z","shell.execute_reply.started":"2023-02-18T14:42:19.152355Z","shell.execute_reply":"2023-02-18T14:42:25.839522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# models_pred\npredictions\ndf_test['cancer']=predictions","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:42:25.842511Z","iopub.execute_input":"2023-02-18T14:42:25.843312Z","iopub.status.idle":"2023-02-18T14:42:25.856384Z","shell.execute_reply.started":"2023-02-18T14:42:25.843266Z","shell.execute_reply":"2023-02-18T14:42:25.854907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub = df_test.groupby('prediction_id')[['cancer']].mean()\ndf_sub","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:42:25.858008Z","iopub.execute_input":"2023-02-18T14:42:25.858717Z","iopub.status.idle":"2023-02-18T14:42:25.888402Z","shell.execute_reply.started":"2023-02-18T14:42:25.858677Z","shell.execute_reply":"2023-02-18T14:42:25.887208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"THRES=2.9\ndf_sub['cancer'] = (df_sub.cancer>THRES).astype(float)\ndf_sub","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:42:25.890173Z","iopub.execute_input":"2023-02-18T14:42:25.8906Z","iopub.status.idle":"2023-02-18T14:42:25.90433Z","shell.execute_reply.started":"2023-02-18T14:42:25.890566Z","shell.execute_reply":"2023-02-18T14:42:25.903362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.to_csv('submission.csv', index=True)\n!head submission.csv","metadata":{"papermill":{"duration":0.018763,"end_time":"2022-12-05T00:23:37.564404","exception":false,"start_time":"2022-12-05T00:23:37.545641","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-02-14T07:16:38.985541Z","iopub.execute_input":"2023-02-14T07:16:38.985907Z","iopub.status.idle":"2023-02-14T07:16:39.988148Z","shell.execute_reply.started":"2023-02-14T07:16:38.985876Z","shell.execute_reply":"2023-02-14T07:16:39.986929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-danger\" style=\"text-align:center; font-size:20px;\">\n    ❤️ Dont forget to ▲upvote▲ if you find this notebook usefull!  ❤️\n</div>","metadata":{"execution":{"iopub.execute_input":"2022-08-20T13:17:18.762083Z","iopub.status.busy":"2022-08-20T13:17:18.761536Z","iopub.status.idle":"2022-08-20T13:17:18.76993Z","shell.execute_reply":"2022-08-20T13:17:18.768312Z","shell.execute_reply.started":"2022-08-20T13:17:18.762038Z"},"papermill":{"duration":0.006828,"end_time":"2022-12-05T00:23:37.578569","exception":false,"start_time":"2022-12-05T00:23:37.571741","status":"completed"},"tags":[]}}]}