{"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":"code","source":"!cp -r ../input/rsna2022-git/clshub/configs .\n!cp -r ../input/rsna2022-git/clshub/clshub .\n!cp -r ../input/rsna2022-libs/working/* /opt/conda/lib/python3.7/site-packages/","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-12-04T11:26:39.940161Z","iopub.execute_input":"2022-12-04T11:26:39.940485Z","iopub.status.idle":"2022-12-04T11:26:54.872455Z","shell.execute_reply.started":"2022-12-04T11:26:39.940412Z","shell.execute_reply":"2022-12-04T11:26:54.870929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --no-dependencies /kaggle/input/fork-of-rsna2022-libs/openmim-0.3.3-py2.py3-none-any.whl\n!pip install --no-dependencies /kaggle/input/fork-of-rsna2022-libs/mmcls-1.0.0rc3-py2.py3-none-any.whl\n\n!mkdir lib\n!cp -r ../input/rsna2022-git/clshub lib/\n!pip install --no-dependencies lib/clshub","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-12-04T11:26:54.876847Z","iopub.execute_input":"2022-12-04T11:26:54.877209Z","iopub.status.idle":"2022-12-04T11:28:06.37692Z","shell.execute_reply.started":"2022-12-04T11:26:54.877174Z","shell.execute_reply":"2022-12-04T11:28:06.375736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install /kaggle/input/pytorch-libs/torch-1.12.0-cp37-cp37m-manylinux1_x86_64.whl\n!pip install /kaggle/input/pytorch-libs/torchvision-0.13.0-cp37-cp37m-manylinux1_x86_64.whl","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-12-04T11:28:06.378993Z","iopub.execute_input":"2022-12-04T11:28:06.379375Z","iopub.status.idle":"2022-12-04T11:29:47.796286Z","shell.execute_reply.started":"2022-12-04T11:28:06.379332Z","shell.execute_reply":"2022-12-04T11:29:47.795033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install /kaggle/input/pydicom-offline-installer/pydicom-2.3.1-py3-none-any.whl\n!pip install /kaggle/input/pydicom-offline-installer/pylibjpeg-1.4.0-py3-none-any.whl\n!pip install /kaggle/input/pydicom-offline-installer/python_gdcm-3.0.20-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-12-04T11:29:47.799387Z","iopub.execute_input":"2022-12-04T11:29:47.79988Z","iopub.status.idle":"2022-12-04T11:31:18.063211Z","shell.execute_reply.started":"2022-12-04T11:29:47.799831Z","shell.execute_reply":"2022-12-04T11:31:18.06203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict\nfrom pathlib import Path\nfrom joblib import Parallel, delayed\n\nimport cv2\nimport pydicom\nimport pandas as pd\nimport numpy as np\nfrom tqdm import tqdm\nfrom torch.utils.data import Dataset, DataLoader\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nfrom mmcls.apis import inference_model, init_model\nfrom mmengine.runner import load_checkpoint\n\n\n\nclass DCMLoader(Dataset):\n    def __init__(self):\n        self.df = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/test.csv')\n        self.patient_ids = self.df.patient_id.values\n        self.image_ids = self.df.image_id.values\n        self.voi_lut = False\n        self.fix_monochrome = True\n        self.image_size = 512\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        patient_id = self.patient_ids[idx]\n        image_id = self.image_ids[idx]\n        path = f'/kaggle/input/rsna-breast-cancer-detection/test_images/{patient_id}/{image_id}.dcm'\n        \n        dicom = pydicom.read_file(path)\n        image_id = Path(path).stem\n\n        # VOI LUT (if available by DICOM device) is used to transform raw DICOM data to \"human-friendly\" view\n        if self.voi_lut:\n            data = apply_voi_lut(dicom.pixel_array, dicom)\n        else:\n            data = dicom.pixel_array\n\n        # depending on this value, X-ray may look inverted - fix that:\n        if self.fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n            data = np.amax(data) - data\n\n        data = data - np.min(data)\n        data = data / np.max(data)\n        data = (data * 255).astype(np.uint8)\n\n        data = self.resize_img(data)\n        # jpg loss\n        data = cv2.imdecode(cv2.imencode(\".jpg\", data)[1], flags=cv2.IMREAD_COLOR)\n\n        return data\n\n    def resize_img(\n        self,\n        img: np.array,\n    ):\n        height, width = img.shape[:2]\n        scale = self.image_size / min(width, height)\n        w = int(width * scale + 0.5)\n        h = int(height * scale + 0.5)\n        if scale > 1.0:\n            interpolation=cv2.INTER_CUBIC\n        else:\n            interpolation=cv2.INTER_AREA\n\n        return cv2.resize(img, (w, h), interpolation=interpolation)\n\n\nconfig = 'configs/projects/rsna2022/efficientnet/efficientnet-b3_2xb8_rsna2022.py'\ncheckpoint = '/kaggle/input/rsna2022weights/efficientnet-b3_2xb8_rsna2022_20221203-ab23317f.pth'\ndevice = 'cuda:0'\n\nmodel = init_model(config, checkpoint=None, device=device)\nload_checkpoint(model, checkpoint, map_location='cpu')\ntest_dataloader = DataLoader(DCMLoader(), batch_size=1, shuffle=False, num_workers=4)\n\npred = []\nfor img in test_dataloader:\n    result = inference_model(model, img[0].numpy())\n    pred.append(result['pred_scores'][0])","metadata":{"execution":{"iopub.status.busy":"2022-12-04T11:37:13.05382Z","iopub.execute_input":"2022-12-04T11:37:13.054217Z","iopub.status.idle":"2022-12-04T11:37:17.71123Z","shell.execute_reply.started":"2022-12-04T11:37:13.054185Z","shell.execute_reply":"2022-12-04T11:37:17.710023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create Sub\nimport mmengine\nimport numpy as np\nimport pandas as pd\n\ndf = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/test.csv')\ndf['prediction_id'] = df['patient_id'].astype(str) + '_' + df['laterality'].astype(str)\ndf['cancer'] = pred\ndf= df.groupby('prediction_id')['cancer'].mean().reset_index()\ndf.columns = ['prediction_id', 'cancer']\nprint(df.head())\ndf['cancer'] = np.where(df['cancer'].values > 0.4, 1, 0)\ndf.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-04T11:37:21.015436Z","iopub.execute_input":"2022-12-04T11:37:21.015849Z","iopub.status.idle":"2022-12-04T11:37:21.049524Z","shell.execute_reply.started":"2022-12-04T11:37:21.015811Z","shell.execute_reply":"2022-12-04T11:37:21.048362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r data\n!rm -r lib\n!rm -r clshub\n!rm -r config\n!rm -r work_dirs","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}