{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        #print(os.path.join(dirname, filename))\n        pass\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-08T13:50:04.800022Z","iopub.execute_input":"2022-02-08T13:50:04.800453Z","iopub.status.idle":"2022-02-08T13:50:40.629702Z","shell.execute_reply.started":"2022-02-08T13:50:04.800335Z","shell.execute_reply":"2022-02-08T13:50:40.628967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\nimport pydicom\nimport cv2\nimport matplotlib.pyplot as plt\nfrom tqdm.notebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:50:40.631373Z","iopub.execute_input":"2022-02-08T13:50:40.631862Z","iopub.status.idle":"2022-02-08T13:50:41.141894Z","shell.execute_reply.started":"2022-02-08T13:50:40.631825Z","shell.execute_reply":"2022-02-08T13:50:41.141130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv(\"/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv\")\nlabels","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:50:41.143093Z","iopub.execute_input":"2022-02-08T13:50:41.143381Z","iopub.status.idle":"2022-02-08T13:50:41.225020Z","shell.execute_reply.started":"2022-02-08T13:50:41.143343Z","shell.execute_reply":"2022-02-08T13:50:41.224319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = labels.drop_duplicates(\"patientId\")\nlabels","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:50:41.227016Z","iopub.execute_input":"2022-02-08T13:50:41.229348Z","iopub.status.idle":"2022-02-08T13:50:41.261186Z","shell.execute_reply.started":"2022-02-08T13:50:41.229317Z","shell.execute_reply":"2022-02-08T13:50:41.260524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT_PATH = Path(\"/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images\")\nSAVE_PATH = Path('/kaggle/output/kaggle/working/processed')","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:50:41.262267Z","iopub.execute_input":"2022-02-08T13:50:41.263428Z","iopub.status.idle":"2022-02-08T13:50:41.267506Z","shell.execute_reply.started":"2022-02-08T13:50:41.263388Z","shell.execute_reply":"2022-02-08T13:50:41.266731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axis = plt.subplots(3,3, figsize=(9,9))\nc =0\nfor i in range(3):\n    for j in range(3):\n        patient_id = labels.patientId.iloc[c]\n        dcm_path = ROOT_PATH/patient_id\n        dcm_path = dcm_path.with_suffix(\".dcm\")\n        dcm = pydicom.read_file(dcm_path).pixel_array\n        \n        label = labels['Target'].iloc[c]\n        axis[i][j].imshow(dcm, cmap=\"bone\")\n        axis[i][j].set_title(label)\n        c+=1","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:50:41.269052Z","iopub.execute_input":"2022-02-08T13:50:41.269563Z","iopub.status.idle":"2022-02-08T13:50:43.796512Z","shell.execute_reply.started":"2022-02-08T13:50:41.269524Z","shell.execute_reply":"2022-02-08T13:50:43.795836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(dcm.shape)\ndcm","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:50:43.797477Z","iopub.execute_input":"2022-02-08T13:50:43.797719Z","iopub.status.idle":"2022-02-08T13:50:43.805944Z","shell.execute_reply.started":"2022-02-08T13:50:43.797684Z","shell.execute_reply":"2022-02-08T13:50:43.805046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dcm_array = cv2.resize(dcm, (224, 224)).astype(np.float16)\nprint(dcm_array.shape)\nprint(dcm_array)\nprint(dcm_array.shape[0])\nprint(dcm_array.shape[1]) \nprint(dcm_array.shape[0] * dcm_array.shape[1])","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:50:43.807712Z","iopub.execute_input":"2022-02-08T13:50:43.808517Z","iopub.status.idle":"2022-02-08T13:50:43.828232Z","shell.execute_reply.started":"2022-02-08T13:50:43.808480Z","shell.execute_reply":"2022-02-08T13:50:43.827551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sums, sums_squared = 0, 0\n\nfor c, patientid in enumerate(tqdm(labels.patientId)): \n    patient_id = labels.patientId.iloc[c]\n    dcm_path = ROOT_PATH/patient_id\n    dcm_path = dcm_path.with_suffix(\".dcm\")\n    dcm = pydicom.read_file(dcm_path).pixel_array / 255\n    \n    dcm_array = cv2.resize(dcm, (224,224)).astype(np.float16)\n    \n    label = labels.Target.iloc[c]\n    \n    train_or_val = \"train\" if c < 24000 else \"val\"\n    \n    current_save_path = SAVE_PATH/train_or_val/str(label)\n    current_save_path.mkdir(parents=True, exist_ok=True)\n    np.save(current_save_path/patient_id, dcm_array)\n    \n    normalizer = 224*224\n    if train_or_val == \"train\":\n        sums += np.sum(dcm_array) / normalizer\n        sums_squared += (dcm_array ** 2).sum() / normalizer","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:50:43.829325Z","iopub.execute_input":"2022-02-08T13:50:43.829516Z","iopub.status.idle":"2022-02-08T13:59:31.736818Z","shell.execute_reply.started":"2022-02-08T13:50:43.829493Z","shell.execute_reply":"2022-02-08T13:59:31.736084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean = sums / 24000\nstd = np.sqrt((sums_squared / 24000) - mean**2)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:59:31.739931Z","iopub.execute_input":"2022-02-08T13:59:31.740637Z","iopub.status.idle":"2022-02-08T13:59:31.745453Z","shell.execute_reply.started":"2022-02-08T13:59:31.740596Z","shell.execute_reply":"2022-02-08T13:59:31.744464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean, std","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:59:31.747163Z","iopub.execute_input":"2022-02-08T13:59:31.747696Z","iopub.status.idle":"2022-02-08T13:59:31.761074Z","shell.execute_reply.started":"2022-02-08T13:59:31.747655Z","shell.execute_reply":"2022-02-08T13:59:31.760239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torchvision\nfrom torchvision import transforms\nimport torchmetrics\nimport pytorch_lightning as pl\nfrom pytorch_lightning.callbacks import ModelCheckpoint\nfrom pytorch_lightning.loggers import TensorBoardLogger\nfrom tqdm.notebook import tqdm\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:59:31.762538Z","iopub.execute_input":"2022-02-08T13:59:31.762915Z","iopub.status.idle":"2022-02-08T13:59:39.987895Z","shell.execute_reply.started":"2022-02-08T13:59:31.762788Z","shell.execute_reply":"2022-02-08T13:59:39.987041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_file(path):\n    return np.load(path).astype(np.float32)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:59:39.989824Z","iopub.execute_input":"2022-02-08T13:59:39.990079Z","iopub.status.idle":"2022-02-08T13:59:39.996316Z","shell.execute_reply.started":"2022-02-08T13:59:39.990042Z","shell.execute_reply":"2022-02-08T13:59:39.995339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transforms = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize(0.49, 0.248),\n    transforms.RandomAffine(degrees=(-5, 5), translate=(0, 0.05), scale=(0.9, 1.1)),\n    transforms.RandomResizedCrop((224, 224), scale=(0.35, 1))\n])","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:59:39.997312Z","iopub.execute_input":"2022-02-08T13:59:39.997556Z","iopub.status.idle":"2022-02-08T13:59:40.113697Z","shell.execute_reply.started":"2022-02-08T13:59:39.997522Z","shell.execute_reply":"2022-02-08T13:59:40.112828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_transforms = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize(0.49, 0.248)\n])","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:59:40.116193Z","iopub.execute_input":"2022-02-08T13:59:40.116596Z","iopub.status.idle":"2022-02-08T13:59:40.122723Z","shell.execute_reply.started":"2022-02-08T13:59:40.116475Z","shell.execute_reply":"2022-02-08T13:59:40.121929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = torchvision.datasets.DatasetFolder(SAVE_PATH/\"train/\", loader=load_file, extensions=\"npy\", transform=train_transforms)\nval_dataset = torchvision.datasets.DatasetFolder(SAVE_PATH/\"val/\", loader=load_file, extensions=\"npy\", transform=val_transforms)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:59:40.123874Z","iopub.execute_input":"2022-02-08T13:59:40.126046Z","iopub.status.idle":"2022-02-08T13:59:40.292746Z","shell.execute_reply.started":"2022-02-08T13:59:40.126012Z","shell.execute_reply":"2022-02-08T13:59:40.292028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:59:40.294008Z","iopub.execute_input":"2022-02-08T13:59:40.294269Z","iopub.status.idle":"2022-02-08T13:59:40.301447Z","shell.execute_reply.started":"2022-02-08T13:59:40.294235Z","shell.execute_reply":"2022-02-08T13:59:40.300336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_dataset","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:59:40.302687Z","iopub.execute_input":"2022-02-08T13:59:40.303075Z","iopub.status.idle":"2022-02-08T13:59:40.312486Z","shell.execute_reply.started":"2022-02-08T13:59:40.303025Z","shell.execute_reply":"2022-02-08T13:59:40.311664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axis = plt.subplots(2,2, figsize=(9,9))\nfor i in range(2):\n    for j in range(2):\n        random_index = np.random.randint(0, 24000)\n        x_ray, label = train_dataset[random_index]\n        axis[i][j].imshow(x_ray[0], cmap=\"bone\")\n        axis[i][j].set_title(label)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:59:40.314322Z","iopub.execute_input":"2022-02-08T13:59:40.314928Z","iopub.status.idle":"2022-02-08T13:59:41.139666Z","shell.execute_reply.started":"2022-02-08T13:59:40.314889Z","shell.execute_reply":"2022-02-08T13:59:41.136516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size=64\nnum_workers = 4\n\ntrain_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size,num_workers=num_workers, shuffle=True)\nval_loader = torch.utils.data.DataLoader(val_dataset, batch_size=batch_size,num_workers=num_workers, shuffle=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:59:41.141125Z","iopub.execute_input":"2022-02-08T13:59:41.141625Z","iopub.status.idle":"2022-02-08T13:59:41.150569Z","shell.execute_reply.started":"2022-02-08T13:59:41.141587Z","shell.execute_reply":"2022-02-08T13:59:41.149743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(train_dataset.targets, return_counts=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:59:41.152262Z","iopub.execute_input":"2022-02-08T13:59:41.152818Z","iopub.status.idle":"2022-02-08T13:59:41.164400Z","shell.execute_reply.started":"2022-02-08T13:59:41.152777Z","shell.execute_reply":"2022-02-08T13:59:41.162909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class PneumoniaModel(pl.LightningModule):\n    \n    def __init__(self):\n        super(PneumoniaModel, self).__init__()\n        self.model = torchvision.models.resnet18()\n        self.model.conv1 = torch.nn.Conv2d(1, 64, kernel_size=(7,7), stride=(2,2), padding=(3,3), bias=False)\n        self.model.fc=torch.nn.Linear(in_features=512, out_features=1, bias=True)\n        \n        self.optimizer = torch.optim.Adam(self.model.parameters(), lr=1e-4)\n        self.loss_fn = torch.nn.BCEWithLogitsLoss(pos_weight=torch.tensor([3]))\n        \n        self.train_acc = torchmetrics.Accuracy()\n        self.val_acc = torchmetrics.Accuracy()\n        \n    def forward(self, data):\n        pred = self.model(data)\n        return pred\n\n    def training_step(self, batch, batch_ids):\n        x_ray, label = batch\n        label = label.float()\n        pred = self(x_ray)[:, 0]\n        loss = self.loss_fn(pred, label)\n        \n        self.log(\"Train loss\", loss)\n        self.log(\"Step Train ACC\", self.train_acc(torch.sigmoid(pred), label.int()))\n        \n        return loss\n\n    def training_epoch_end(self, outs):\n        self.log(\"Train ACC\", self.train_acc.compute())\n    \n    def validation_step(self, batch, batch_ids):\n        x_ray, label = batch\n        label = label.float()\n        pred = self(x_ray)[:, 0]\n        loss = self.loss_fn(pred, label)\n        \n        self.log(\"Val loss\", loss)\n        self.log(\"Step Val ACC\", self.val_acc(torch.sigmoid(pred), label.int()))\n\n    def validation_epoch_end(self, outs):\n        self.log(\"Val ACC\", self.val_acc.compute())\n        \n    def configure_optimizers(self):\n        return [self.optimizer]","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:59:41.166505Z","iopub.execute_input":"2022-02-08T13:59:41.166793Z","iopub.status.idle":"2022-02-08T13:59:41.181125Z","shell.execute_reply.started":"2022-02-08T13:59:41.166753Z","shell.execute_reply":"2022-02-08T13:59:41.180411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = PneumoniaModel()","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:59:41.182936Z","iopub.execute_input":"2022-02-08T13:59:41.183478Z","iopub.status.idle":"2022-02-08T13:59:41.385075Z","shell.execute_reply.started":"2022-02-08T13:59:41.183441Z","shell.execute_reply":"2022-02-08T13:59:41.384314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_callback = ModelCheckpoint(\n    monitor=\"Val ACC\",\n    save_top_k=10,\n    mode=\"max\"\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:59:41.386375Z","iopub.execute_input":"2022-02-08T13:59:41.386678Z","iopub.status.idle":"2022-02-08T13:59:41.391971Z","shell.execute_reply.started":"2022-02-08T13:59:41.386640Z","shell.execute_reply":"2022-02-08T13:59:41.391338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gpus = 1\ntrainer = pl.Trainer(gpus=gpus, logger=TensorBoardLogger(save_dir=\"./logs\"), log_every_n_steps=1,\n                    callbacks=checkpoint_callback, max_epochs=35)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:59:41.393465Z","iopub.execute_input":"2022-02-08T13:59:41.394225Z","iopub.status.idle":"2022-02-08T13:59:41.448185Z","shell.execute_reply.started":"2022-02-08T13:59:41.394185Z","shell.execute_reply":"2022-02-08T13:59:41.447412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer.fit(model, train_loader, val_loader)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T13:59:41.450352Z","iopub.execute_input":"2022-02-08T13:59:41.450624Z","iopub.status.idle":"2022-02-08T14:39:20.208323Z","shell.execute_reply.started":"2022-02-08T13:59:41.450584Z","shell.execute_reply":"2022-02-08T14:39:20.207530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer.save_checkpoint(\"weights/weights_1.ckpt\")","metadata":{"execution":{"iopub.status.busy":"2022-02-08T14:39:20.210090Z","iopub.execute_input":"2022-02-08T14:39:20.210370Z","iopub.status.idle":"2022-02-08T14:39:20.485860Z","shell.execute_reply.started":"2022-02-08T14:39:20.210331Z","shell.execute_reply":"2022-02-08T14:39:20.485099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\nprint(device)\nmodel = PneumoniaModel.load_from_checkpoint(\"weights/weights_1.ckpt\")\nmodel.eval()\nmodel.to(device)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-08T14:39:20.489782Z","iopub.execute_input":"2022-02-08T14:39:20.490257Z","iopub.status.idle":"2022-02-08T14:39:20.794185Z","shell.execute_reply.started":"2022-02-08T14:39:20.490225Z","shell.execute_reply":"2022-02-08T14:39:20.793342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = []\nlabels = []\n\nwith torch.no_grad():\n    for data, label in tqdm(val_dataset):\n        data = data.to(device).float().unsqueeze(0)\n        pred = torch.sigmoid(model(data)[0].cpu())\n        preds.append(pred)\n        labels.append(label)\npreds = torch.tensor(preds)\nlabels = torch.tensor(labels).int()","metadata":{"execution":{"iopub.status.busy":"2022-02-08T14:39:20.795397Z","iopub.execute_input":"2022-02-08T14:39:20.795734Z","iopub.status.idle":"2022-02-08T14:39:32.553301Z","shell.execute_reply.started":"2022-02-08T14:39:20.795695Z","shell.execute_reply":"2022-02-08T14:39:32.552614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = torchmetrics.Accuracy()(preds, labels)\nprecision = torchmetrics.Precision()(preds, labels)\nrecall = torchmetrics.Recall()(preds, labels)\ncm = torchmetrics.ConfusionMatrix(num_classes=2)(preds, labels)\n\nprint(f\"Val Accuracy {acc}\")\nprint(f\"Val precision {precision}\")\nprint(f\"Val Recall {recall}\")\nprint(f\"Confusion Matrix {cm}\")\n","metadata":{"execution":{"iopub.status.busy":"2022-02-08T14:39:32.554416Z","iopub.execute_input":"2022-02-08T14:39:32.554690Z","iopub.status.idle":"2022-02-08T14:39:32.584251Z","shell.execute_reply.started":"2022-02-08T14:39:32.554652Z","shell.execute_reply":"2022-02-08T14:39:32.583439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_file(path):\n    return np.load(path).astype(np.float32)\n\nval_transforms = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize(0.49, 0.248)\n])\n\nval_dataset = torchvision.datasets.DatasetFolder(SAVE_PATH/\"val/\", loader=load_file, extensions=\"npy\", transform=val_transforms)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T15:27:18.024884Z","iopub.execute_input":"2022-02-08T15:27:18.025223Z","iopub.status.idle":"2022-02-08T15:27:18.050257Z","shell.execute_reply.started":"2022-02-08T15:27:18.025189Z","shell.execute_reply":"2022-02-08T15:27:18.049505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_model = torchvision.models.resnet18()\ntemp_model","metadata":{"execution":{"iopub.status.busy":"2022-02-08T15:28:12.160731Z","iopub.execute_input":"2022-02-08T15:28:12.161476Z","iopub.status.idle":"2022-02-08T15:28:12.341200Z","shell.execute_reply.started":"2022-02-08T15:28:12.161438Z","shell.execute_reply":"2022-02-08T15:28:12.340344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class PnemoniaModel(pl.LightningModule):\n    def __init__(self):\n        super().__init__()\n        self.model = torchvision.models.resnet18()\n        self.model.conv1 = torch.nn.Conv2d(1, 64, kernel_size=(7,7), stride=(2,2), padding=(3,3), bias= False)\n        self.model.fc = torch.nn.Linear(in_features=512, out_features=1)\n        \n        self.feature_map = torch.nn.Sequential(*list(self.model.children())[:-2])\n        \n        \n    def forward(self, data):\n        feature_map = self.feature_map(data)\n        avg_pool_output = torch.nn.functional.adaptive_avg_pool2d(input=feature_map, output_size=(1,1))\n        avg_output_flattened = torch.flatten(avg_pool_output)\n        pred = self.model.fc(avg_output_flattened)\n        return pred, feature_map","metadata":{"execution":{"iopub.status.busy":"2022-02-08T15:54:05.515776Z","iopub.execute_input":"2022-02-08T15:54:05.516059Z","iopub.status.idle":"2022-02-08T15:54:05.525183Z","shell.execute_reply.started":"2022-02-08T15:54:05.516027Z","shell.execute_reply":"2022-02-08T15:54:05.523232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = PnemoniaModel.load_from_checkpoint(\"weights/weights_1.ckpt\", strict=False)\nmodel.eval();","metadata":{"execution":{"iopub.status.busy":"2022-02-08T15:56:18.760987Z","iopub.execute_input":"2022-02-08T15:56:18.761270Z","iopub.status.idle":"2022-02-08T15:56:19.005965Z","shell.execute_reply.started":"2022-02-08T15:56:18.761240Z","shell.execute_reply":"2022-02-08T15:56:19.005237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cam(model, img):\n    with torch.no_grad():\n        pred, features = model(img.unsqueeze(0))\n    features = features.reshape((512,49))\n    weight_params = list(model.model.fc.parameters())[0]\n    weight = weight_params[0].detach()\n    \n    cam = torch.matmul(weight, features)\n    cam_img = cam.reshape(7,7).cpu()\n    return cam_img, torch.sigmoid(pred)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T16:11:30.547330Z","iopub.execute_input":"2022-02-08T16:11:30.547862Z","iopub.status.idle":"2022-02-08T16:11:30.554248Z","shell.execute_reply.started":"2022-02-08T16:11:30.547823Z","shell.execute_reply":"2022-02-08T16:11:30.552934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize(img, cam, pred):\n    img = img[0]\n    cam = transforms.functional.resize(cam.unsqueeze(0), (224,224))[0]\n    \n    fig, axis = plt.subplots(1,2)\n    axis[0].imshow(img, cmap=\"bone\")\n    axis[1].imshow(img, cmap=\"bone\")\n    axis[1].imshow(img, alpha=0.5, cmap=\"jet\")\n    plt.title(pred > 0.5)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-02-08T16:11:34.185991Z","iopub.execute_input":"2022-02-08T16:11:34.186247Z","iopub.status.idle":"2022-02-08T16:11:34.191884Z","shell.execute_reply.started":"2022-02-08T16:11:34.186217Z","shell.execute_reply":"2022-02-08T16:11:34.191197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = val_dataset[-6][0]\nactivation_map, pred = cam(model, img)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T16:11:36.090875Z","iopub.execute_input":"2022-02-08T16:11:36.091405Z","iopub.status.idle":"2022-02-08T16:11:36.147567Z","shell.execute_reply.started":"2022-02-08T16:11:36.091363Z","shell.execute_reply":"2022-02-08T16:11:36.146830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize(img, activation_map, pred )","metadata":{"execution":{"iopub.status.busy":"2022-02-08T16:11:56.671748Z","iopub.execute_input":"2022-02-08T16:11:56.672290Z","iopub.status.idle":"2022-02-08T16:11:57.031600Z","shell.execute_reply.started":"2022-02-08T16:11:56.672241Z","shell.execute_reply":"2022-02-08T16:11:57.030955Z"},"trusted":true},"execution_count":null,"outputs":[]}]}