{"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":"<h1 style='background:#2cab6c; border:0; color:white'><center>Importing Libraries</center></h1>","metadata":{}},{"cell_type":"code","source":"! pip install ../input/einops-030/einops-0.3.0-py2.py3-none-any.whl\n! pip install /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}","metadata":{"execution":{"iopub.status.busy":"2022-12-08T18:44:26.271012Z","iopub.execute_input":"2022-12-08T18:44:26.271385Z","iopub.status.idle":"2022-12-08T18:45:28.095992Z","shell.execute_reply.started":"2022-12-08T18:44:26.271352Z","shell.execute_reply":"2022-12-08T18:45:28.094494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\n\nsys.path.append('../input/timm-pytorch-image-models/pytorch-image-models-master')","metadata":{"execution":{"iopub.status.busy":"2022-12-08T18:45:45.833382Z","iopub.execute_input":"2022-12-08T18:45:45.834403Z","iopub.status.idle":"2022-12-08T18:45:45.840507Z","shell.execute_reply.started":"2022-12-08T18:45:45.834351Z","shell.execute_reply":"2022-12-08T18:45:45.839313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport glob\nimport timm\nimport torch\nimport wandb\nimport pydicom\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport torch.nn as nn\nfrom tqdm import tqdm\nfrom pydicom import dcmread\nfrom einops import rearrange\nfrom torchvision import transforms\nfrom matplotlib import pyplot as plt\nfrom joblib import Parallel, delayed\n\nfrom kaggle_secrets import UserSecretsClient\nfrom torch.utils.data import Dataset, DataLoader\nfrom accelerate import Accelerator, notebook_launcher","metadata":{"execution":{"iopub.status.busy":"2022-12-08T18:45:46.538654Z","iopub.execute_input":"2022-12-08T18:45:46.539058Z","iopub.status.idle":"2022-12-08T18:45:46.547947Z","shell.execute_reply.started":"2022-12-08T18:45:46.539026Z","shell.execute_reply":"2022-12-08T18:45:46.546705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style='background:#2cab6c; border:0; color:white'><center>Config</center></h1>","metadata":{}},{"cell_type":"code","source":"class CFG:\n    class data:\n        fold=0\n        batch_size=2\n        image_size=(224, 224)\n        path_to_test=\"../input/rsna-breast-cancer-detection/test.csv\"\n        path_to_images=\"../input/rsna-breast-cancer-detection/test_images\"\n        path_to_test_images=\"/kaggle/working/images/RSNA-png-256-test/\"\n        path_to_dcm_images = \"/kaggle/input/rsna-breast-cancer-detection/test_images/*/*.dcm\"\n        \n    class monitoring:\n        accelerator=Accelerator()\n        \n    class model:\n        path_to_model='../input/train-baseline-w-b-transformer-1-5fold-rsna/epoch_1_model.pth'\n        pretrained_name='vit_base_patch16_224'\n        learning_rate=1e-4\n        auto_break_n=2\n        n_epochs=2\n        n_labels=1","metadata":{"execution":{"iopub.status.busy":"2022-12-08T18:45:47.451526Z","iopub.execute_input":"2022-12-08T18:45:47.452574Z","iopub.status.idle":"2022-12-08T18:45:47.460116Z","shell.execute_reply.started":"2022-12-08T18:45:47.452537Z","shell.execute_reply":"2022-12-08T18:45:47.458793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style='background:#2cab6c; border:0; color:white'><center>Data Loading</center></h1>","metadata":{}},{"cell_type":"code","source":"test_df = pd.read_csv(CFG.data.path_to_test)\ntest_df['img_name'] = test_df['patient_id'].astype(str) + \"/\" + test_df['image_id'].astype(str) + \".png\"\n\nprint(f\"test.shape = {test_df.shape}\")\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-08T18:45:48.764726Z","iopub.execute_input":"2022-12-08T18:45:48.76511Z","iopub.status.idle":"2022-12-08T18:45:48.788775Z","shell.execute_reply.started":"2022-12-08T18:45:48.765079Z","shell.execute_reply":"2022-12-08T18:45:48.787735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style='background:#2cab6c; border:0; color:white'><center>DCM to PNG</center></h1>","metadata":{}},{"cell_type":"code","source":"def process(f, size=512, save_folder=\"\", extension=\"png\"):\n    patient = f.split('/')[-2]    \n    image = f.split('/')[-1][:-4]\n\n    os.makedirs(f\"{save_folder}/{patient}/\", exist_ok=True)\n\n    dicom = pydicom.dcmread(f)\n    img = dicom.pixel_array\n\n    img = (img - img.min()) / (img.max() - img.min())\n\n    img = cv2.resize(img, (size, size))\n    cv2.imwrite(f\"{save_folder}/{patient}/{image}.{extension}\", (img * 255).astype(np.uint8))","metadata":{"execution":{"iopub.status.busy":"2022-12-08T18:45:49.98764Z","iopub.execute_input":"2022-12-08T18:45:49.988069Z","iopub.status.idle":"2022-12-08T18:45:49.996365Z","shell.execute_reply.started":"2022-12-08T18:45:49.988034Z","shell.execute_reply":"2022-12-08T18:45:49.994997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = glob.glob(CFG.data.path_to_dcm_images)\nos.makedirs(CFG.data.path_to_test_images, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T18:45:50.51465Z","iopub.execute_input":"2022-12-08T18:45:50.515058Z","iopub.status.idle":"2022-12-08T18:45:50.523076Z","shell.execute_reply.started":"2022-12-08T18:45:50.515026Z","shell.execute_reply":"2022-12-08T18:45:50.521763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Parallel(n_jobs=4)(\n    delayed(process)(image, size=CFG.data.image_size[0], save_folder=CFG.data.path_to_test_images, extension=\"png\")\n    for image in tqdm(test_images)\n);","metadata":{"execution":{"iopub.status.busy":"2022-12-08T18:45:50.87239Z","iopub.execute_input":"2022-12-08T18:45:50.873105Z","iopub.status.idle":"2022-12-08T18:45:56.28307Z","shell.execute_reply.started":"2022-12-08T18:45:50.873068Z","shell.execute_reply":"2022-12-08T18:45:56.281755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style='background:#2cab6c; border:0; color:white'><center>Transforms & Data Generator</center></h1>","metadata":{}},{"cell_type":"code","source":"class RSNAData(Dataset):\n    def __init__(self, df, img_folder, transform=None, is_test=False):\n        self.df = df\n        self.is_test = is_test\n        self.transform = transform\n        self.img_folder = img_folder\n        \n    def __getitem__(self, idx):\n        img_path = os.path.join(self.img_folder, self.df['img_name'][idx])\n        img = cv2.imread(img_path)\n        img = cv2.resize(img, (224, 224))\n        \n        if self.transform:\n            img = self.transform(image=img)['image']\n        img = torch.tensor(img, dtype=torch.float)\n        \n        # Rearrange the image dimensions so that channels are first in format\n        # This is because VIT Model requires Channels (c) to come first\n        \n        img = rearrange(img, 'h w c -> c h w')\n        \n        if not self.is_test:\n            target = self.df['cancer'][idx]\n            target = torch.tensor(target, dtype=torch.float)\n            return {\n                \"X\": img,\n                \"y\": target,\n            }\n        return {\"X\": img,}\n    \n    def __len__(self):\n        return len(self.df)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T18:45:56.289206Z","iopub.execute_input":"2022-12-08T18:45:56.289754Z","iopub.status.idle":"2022-12-08T18:45:56.312632Z","shell.execute_reply.started":"2022-12-08T18:45:56.289711Z","shell.execute_reply":"2022-12-08T18:45:56.310682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.ToTensor()\n])","metadata":{"execution":{"iopub.status.busy":"2022-12-08T18:45:56.316872Z","iopub.execute_input":"2022-12-08T18:45:56.318757Z","iopub.status.idle":"2022-12-08T18:45:56.330122Z","shell.execute_reply.started":"2022-12-08T18:45:56.318715Z","shell.execute_reply":"2022-12-08T18:45:56.329009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = RSNAData(df=test_df, img_folder=CFG.data.path_to_test_images, is_test=True)\ntest_loader = DataLoader(test_dataset, batch_size=CFG.data.batch_size, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T18:45:56.332785Z","iopub.execute_input":"2022-12-08T18:45:56.33329Z","iopub.status.idle":"2022-12-08T18:45:56.341502Z","shell.execute_reply.started":"2022-12-08T18:45:56.33325Z","shell.execute_reply":"2022-12-08T18:45:56.340844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style='background:#2cab6c; border:0; color:white'><center>Model</center></h1>","metadata":{}},{"cell_type":"code","source":"class VITModel(nn.Module):\n    def __init__(self, pretrained=True):\n        super(VITModel, self).__init__()        \n        self.backbone = timm.create_model('vit_base_patch16_224', pretrained=False)\n        self.backbone.head = nn.Linear(self.backbone.head.in_features, 1)\n\n    def forward(self, x):\n        return self.backbone(x)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T18:45:58.543485Z","iopub.execute_input":"2022-12-08T18:45:58.544006Z","iopub.status.idle":"2022-12-08T18:45:58.55052Z","shell.execute_reply.started":"2022-12-08T18:45:58.54397Z","shell.execute_reply":"2022-12-08T18:45:58.549499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with CFG.monitoring.accelerator.main_process_first():\n    model = VITModel()\n    \nmodel.load_state_dict(torch.load(CFG.model.path_to_model)['model'])","metadata":{"execution":{"iopub.status.busy":"2022-12-08T18:45:59.202971Z","iopub.execute_input":"2022-12-08T18:45:59.203328Z","iopub.status.idle":"2022-12-08T18:46:16.478361Z","shell.execute_reply.started":"2022-12-08T18:45:59.203299Z","shell.execute_reply":"2022-12-08T18:46:16.477379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = torch.optim.Adam(params=model.parameters(), lr=CFG.model.learning_rate)\ncriterion = nn.BCEWithLogitsLoss()\n\nmodel, optimizer, test_loader = CFG.monitoring.accelerator.prepare(\n    model, optimizer, test_loader\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T18:46:16.480131Z","iopub.execute_input":"2022-12-08T18:46:16.480447Z","iopub.status.idle":"2022-12-08T18:46:16.596654Z","shell.execute_reply.started":"2022-12-08T18:46:16.480407Z","shell.execute_reply":"2022-12-08T18:46:16.595612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style='background:#2cab6c; border:0; color:white'><center>Inference</center></h1>","metadata":{}},{"cell_type":"code","source":"def inference(model, accelerator, test_loader):\n    model.eval()\n\n    all_outputs = []\n    with torch.no_grad():\n\n        for batch in tqdm(test_loader):\n\n            images = batch[\"X\"]\n\n            outputs = model(images).view(-1)\n            outputs = accelerator.gather_for_metrics((outputs))\n\n            all_outputs.extend(torch.sigmoid(outputs).cpu().detach().tolist())\n            \n    return all_outputs","metadata":{"execution":{"iopub.status.busy":"2022-12-08T18:46:21.523534Z","iopub.execute_input":"2022-12-08T18:46:21.523966Z","iopub.status.idle":"2022-12-08T18:46:21.531204Z","shell.execute_reply.started":"2022-12-08T18:46:21.52393Z","shell.execute_reply":"2022-12-08T18:46:21.529879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style='background:#2cab6c; border:0; color:white'><center>Get & Save Predict</center></h1>","metadata":{}},{"cell_type":"code","source":"predictions = inference(model, CFG.monitoring.accelerator, test_loader)\npredictions","metadata":{"execution":{"iopub.status.busy":"2022-12-08T18:46:22.767409Z","iopub.execute_input":"2022-12-08T18:46:22.768042Z","iopub.status.idle":"2022-12-08T18:46:33.138213Z","shell.execute_reply.started":"2022-12-08T18:46:22.768002Z","shell.execute_reply":"2022-12-08T18:46:33.137126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! rm -r images","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'prediction_id': test_df.prediction_id.values,\n    'cancer': predictions,\n}).groupby('prediction_id').max().reset_index()\n\nsubmission.to_csv('submission.csv', index=False)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-08T18:46:33.140447Z","iopub.execute_input":"2022-12-08T18:46:33.141146Z","iopub.status.idle":"2022-12-08T18:46:33.173718Z","shell.execute_reply.started":"2022-12-08T18:46:33.141106Z","shell.execute_reply":"2022-12-08T18:46:33.172373Z"},"trusted":true},"execution_count":null,"outputs":[]}]}