{"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 ../input/gdcm-conda-install/gdcm.tar .\n!tar -xvzf gdcm.tar\n!conda install --offline ./gdcm/gdcm-2.8.9-py37h71b2a6d_0.tar.bz2\n\nimport gdcm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-01T09:09:21.08156Z","iopub.execute_input":"2022-12-01T09:09:21.082933Z","iopub.status.idle":"2022-12-01T09:09:39.741005Z","shell.execute_reply.started":"2022-12-01T09:09:21.082777Z","shell.execute_reply":"2022-12-01T09:09:39.739724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nfrom pathlib import Path\nimport glob\nfrom PIL import Image\nfrom joblib import Parallel, delayed\nfrom tqdm import tqdm\n# import pylibjpeg\n# import libjpeg\n\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\n\nimport os\nimport gc\ngc.enable()\nimport cv2\nimport math\nimport copy\nimport time\nimport random\nimport numpy as np\nimport pandas as pd\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torch.nn.functional as F\nimport collections \nimport collections.abc\nfrom torch.optim import lr_scheduler\nfrom torch.utils.data import Dataset, DataLoader\nfrom torch.cuda import amp\nimport matplotlib.pyplot as plt\nimport joblib\nfrom tqdm import tqdm\nfrom collections import defaultdict\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import StratifiedKFold\n\n\nimport sys\nsys.path.append('../input/timm-pytorch-image-models/pytorch-image-models-master')\nimport timm\nfrom PIL import Image\nimport albumentations as A\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nfrom sklearn.metrics import top_k_accuracy_score, f1_score, precision_score, recall_score, confusion_matrix\nfrom pylab import rcParams\n","metadata":{"execution":{"iopub.status.busy":"2022-12-01T09:09:39.744239Z","iopub.execute_input":"2022-12-01T09:09:39.744623Z","iopub.status.idle":"2022-12-01T09:09:44.97088Z","shell.execute_reply.started":"2022-12-01T09:09:39.744586Z","shell.execute_reply":"2022-12-01T09:09:44.969627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CONFIG = {\"seed\": 2022, 'n_fold' : 5, 'img_size' : 512,\n          \"model_name\": \"eca_nfnet_l1\", \"num_classes\": 1, \n          \"device\": torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\"),\n          \n          'init_lr' : 1e-4,\n          'warmup_factor' : 10,\n          'warmup_epo' : 3,\n          \n          'n_epochs' : 50,\n          \n          'num_workers' : 0,\n          \n          'train_batch_size' : 8,\n          'valid_batch_size' : 8,\n          \n          'fold' : 0,\n          \n          'debug' : False,\n          \n          'exp' : \"nfnet_simple_1\",\n         }","metadata":{"execution":{"iopub.status.busy":"2022-12-01T09:09:44.975742Z","iopub.execute_input":"2022-12-01T09:09:44.976815Z","iopub.status.idle":"2022-12-01T09:09:45.057431Z","shell.execute_reply.started":"2022-12-01T09:09:44.976759Z","shell.execute_reply":"2022-12-01T09:09:45.056306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rescale_img_to_hu(dcm_ds):\n    \"\"\"Rescales the image to Hounsfield unit.\"\"\"\n    return dcm_ds.pixel_array# * dcm_ds.RescaleSlope + dcm_ds.RescaleIntercept\n\ndef read_dicom_image(image_path):\n    x = rescale_img_to_hu(pydicom.dcmread(image_path))\n    x = x - np.min(x)\n    x = x / (np.max(x) + 1e-4)\n    x = (x * 255).astype(np.uint8)\n    return x\n\ndef crop_out_image(img):\n    av = np.mean(img, axis=0)\n    mi = np.min(img, axis=0)\n    ma = np.max(img, axis=0)\n    img = img[:, (((av - mi) > 2) + ((av - ma) > 2))]\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-12-01T09:09:45.064007Z","iopub.execute_input":"2022-12-01T09:09:45.064818Z","iopub.status.idle":"2022-12-01T09:09:45.076482Z","shell.execute_reply.started":"2022-12-01T09:09:45.064772Z","shell.execute_reply":"2022-12-01T09:09:45.075398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MemmoDataset(Dataset):\n    \n    def __init__(self, df, transforms = None):\n        self.file_paths = df['file_path'].values\n        self.transforms = transforms\n        \n    def __len__(self):\n        return len(self.file_paths)\n        \n    def __getitem__(self, index):\n        \n        img_path = self.file_paths[index]\n        \n        img = read_dicom_image(img_path)\n        img = crop_out_image(img)\n        img = np.array(Image.fromarray(img).resize((1024, 1024)))\n        img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)\n        \n#         x = np.zeros(shape=(1024, 1024, 3), dtype'uint8')\n#         x[:, :, 0] = img\n#         x[:, :, 1] = img\n#         x[:, :, 2] = img\n#         img = x\n        \n        if self.transforms:\n            img = self.transforms(image=img)[\"image\"]\n            \n        img = img.astype(np.float32)\n        img /= 255\n        img = img.transpose(2, 0, 1)\n        \n        return torch.tensor(img)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T09:09:45.081571Z","iopub.execute_input":"2022-12-01T09:09:45.084253Z","iopub.status.idle":"2022-12-01T09:09:45.095952Z","shell.execute_reply.started":"2022-12-01T09:09:45.084111Z","shell.execute_reply":"2022-12-01T09:09:45.094926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MemmoModel(nn.Module):\n    def __init__(self, model_name, pretrained=False, num_classes=CONFIG['num_classes']):\n        \n        super(MemmoModel, self).__init__()\n        self.backbone = timm.create_model(model_name, pretrained=pretrained, num_classes=num_classes)\n\n    def forward(self, images):\n        features = self.backbone(images)\n        return features","metadata":{"execution":{"iopub.status.busy":"2022-12-01T09:09:45.100279Z","iopub.execute_input":"2022-12-01T09:09:45.100792Z","iopub.status.idle":"2022-12-01T09:09:45.110628Z","shell.execute_reply.started":"2022-12-01T09:09:45.100759Z","shell.execute_reply":"2022-12-01T09:09:45.109607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_epoch(loader):\n\n    model.eval()\n    \n    LOGITS = []\n    \n    with torch.no_grad():\n        \n        for (data) in tqdm(loader):\n            data = data.to(CONFIG['device'])\n            \n            output = model(data)\n            LOGITS.append(output.cpu())\n    LOGITS = torch.cat(LOGITS)\n    \n    if CONFIG['num_classes']!=1:\n        LOGITS = LOGITS.softmax(1).numpy()\n    else:\n        LOGITS = LOGITS.sigmoid().numpy()\n    \n    return LOGITS","metadata":{"execution":{"iopub.status.busy":"2022-12-01T09:09:45.115666Z","iopub.execute_input":"2022-12-01T09:09:45.116054Z","iopub.status.idle":"2022-12-01T09:09:45.127274Z","shell.execute_reply.started":"2022-12-01T09:09:45.116021Z","shell.execute_reply":"2022-12-01T09:09:45.126301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = MemmoModel(CONFIG['model_name'])\nmodel = model.to(CONFIG['device'])\nmodel.load_state_dict(torch.load(\"../input/nfnet-l1-50-epochs-simple/nfnet_simple_1_final_fold_0.pth\", map_location=CONFIG['device']))","metadata":{"execution":{"iopub.status.busy":"2022-12-01T09:09:45.132558Z","iopub.execute_input":"2022-12-01T09:09:45.135279Z","iopub.status.idle":"2022-12-01T09:09:51.700522Z","shell.execute_reply.started":"2022-12-01T09:09:45.135244Z","shell.execute_reply":"2022-12-01T09:09:51.699425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"../input/rsna-breast-cancer-detection/test.csv\")\ntest['file_path'] = '../input/rsna-breast-cancer-detection/test_images/' + test['patient_id'].astype('str') + \"/\" + + test['image_id'].astype('str') + \".dcm\"\ntest['cancer'] = 2\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T09:09:51.702172Z","iopub.execute_input":"2022-12-01T09:09:51.702947Z","iopub.status.idle":"2022-12-01T09:09:51.754924Z","shell.execute_reply.started":"2022-12-01T09:09:51.702908Z","shell.execute_reply":"2022-12-01T09:09:51.75394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transforms_val = A.Compose([\n    A.Resize(CONFIG['img_size'], CONFIG['img_size']),\n])","metadata":{"execution":{"iopub.status.busy":"2022-12-01T09:09:51.758521Z","iopub.execute_input":"2022-12-01T09:09:51.759209Z","iopub.status.idle":"2022-12-01T09:09:51.98581Z","shell.execute_reply.started":"2022-12-01T09:09:51.759174Z","shell.execute_reply":"2022-12-01T09:09:51.98443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_test = MemmoDataset(test, transforms=transforms_val)\ntest_loader = DataLoader(dataset_test, batch_size=CONFIG['valid_batch_size'], \n                              num_workers=CONFIG['num_workers'], shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T09:09:51.992078Z","iopub.execute_input":"2022-12-01T09:09:51.994963Z","iopub.status.idle":"2022-12-01T09:09:52.004322Z","shell.execute_reply.started":"2022-12-01T09:09:51.994923Z","shell.execute_reply":"2022-12-01T09:09:52.003037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(\"../input/rsna-breast-cancer-detection/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-12-01T09:09:52.005746Z","iopub.execute_input":"2022-12-01T09:09:52.006137Z","iopub.status.idle":"2022-12-01T09:09:52.022532Z","shell.execute_reply.started":"2022-12-01T09:09:52.0061Z","shell.execute_reply":"2022-12-01T09:09:52.021528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PREDS = test_epoch(test_loader)\ntest['preds'] = PREDS\nmean_test_pred = test[['prediction_id', 'preds']].groupby(['prediction_id']).mean()\nmean_test_pred = mean_test_pred.loc[sub['prediction_id']]","metadata":{"execution":{"iopub.status.busy":"2022-12-01T09:09:52.023707Z","iopub.execute_input":"2022-12-01T09:09:52.023999Z","iopub.status.idle":"2022-12-01T09:10:01.993939Z","shell.execute_reply.started":"2022-12-01T09:09:52.023974Z","shell.execute_reply":"2022-12-01T09:10:01.993064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T09:10:01.998954Z","iopub.execute_input":"2022-12-01T09:10:02.001036Z","iopub.status.idle":"2022-12-01T09:10:02.01581Z","shell.execute_reply.started":"2022-12-01T09:10:02.001001Z","shell.execute_reply":"2022-12-01T09:10:02.014909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['cancer'] = mean_test_pred['preds'].values\nsub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T09:10:02.019689Z","iopub.execute_input":"2022-12-01T09:10:02.021916Z","iopub.status.idle":"2022-12-01T09:10:02.031894Z","shell.execute_reply.started":"2022-12-01T09:10:02.021877Z","shell.execute_reply":"2022-12-01T09:10:02.030691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T09:10:02.036684Z","iopub.execute_input":"2022-12-01T09:10:02.038734Z","iopub.status.idle":"2022-12-01T09:10:02.052947Z","shell.execute_reply.started":"2022-12-01T09:10:02.038701Z","shell.execute_reply":"2022-12-01T09:10:02.051815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}