{"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":"## **Section-1 : Probelm description** <a class=\"anchor\" id=\"section-1\"></a>\n\n- **Notation** :  <div class=\"alert alert-info\">since the competition is ended 3 years ago here i just tried give a briefy implementation of<strong> Pytorch **Lighiting**</strong></div> <br>\n\n* in this notebook will go for methodolgy pipline ML to solve the task '\n  - data Processing ( in this step will look at the data )\n  - data augementation ( here we will augement the samples of data to avoid the overfitting)\n  - split the data into test and training \n  - build the model CNN \n  - using pytorch-lighiting for automatic Multui-GPU '\n    * compute the CAM activation map to extarct the knowgled of the model \n    * test the data \n    * save the submission file predction \n\n","metadata":{"papermill":{"duration":0.011447,"end_time":"2022-11-19T14:58:58.033853","exception":false,"start_time":"2022-11-19T14:58:58.022406","status":"completed"},"tags":[]}},{"cell_type":"code","source":"%%capture \n!pip install Pathlib\n!jupyter nbextension enable --py --sys-prefix widgetsnbextension\n","metadata":{"papermill":{"duration":11.199892,"end_time":"2022-11-19T14:59:09.244471","exception":false,"start_time":"2022-11-19T14:58:58.044579","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T13:41:07.995066Z","iopub.execute_input":"2022-11-23T13:41:07.995643Z","iopub.status.idle":"2022-11-23T13:41:18.903320Z","shell.execute_reply.started":"2022-11-23T13:41:07.995579Z","shell.execute_reply":"2022-11-23T13:41:18.901980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\nimport matplotlib.pyplot as plt\nimport pydicom as dc \nfrom pathlib import Path \nimport numpy as np \nimport pandas as pd \nimport os \nimport cv2\nfrom tqdm.notebook import tqdm\nimport glob \n\nimport 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 pytorch_lightning.plugins import DDPPlugin\n\n#from pytorch_lightning.plugins.training_type.ddp import DDPPlugin\nimport warnings \ndef fxn():\n    warnings.warn(\"deprecated\", DeprecationWarning)\n\nwith warnings.catch_warnings():\n    warnings.simplefilter(\"ignore\")\n    fxn()","metadata":{"papermill":{"duration":4.994876,"end_time":"2022-11-19T14:59:14.246376","exception":false,"start_time":"2022-11-19T14:59:09.251500","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T16:26:22.832206Z","iopub.execute_input":"2022-11-23T16:26:22.832995Z","iopub.status.idle":"2022-11-23T16:26:22.845795Z","shell.execute_reply.started":"2022-11-23T16:26:22.832952Z","shell.execute_reply":"2022-11-23T16:26:22.844557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **first Part Look at the data and Per-Processing** <a class=\"anchor\" id=\"subsection1\"></a>\n - virtualize some few samples in the data set\n - convert the data into Float fotmat for more effencie traning ","metadata":{"papermill":{"duration":0.00714,"end_time":"2022-11-19T14:59:14.262431","exception":false,"start_time":"2022-11-19T14:59:14.255291","status":"completed"},"tags":[]}},{"cell_type":"code","source":"#### Drop the duplicated Label Patients already exit \nload_label = pd.read_csv(\"../input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv\")\nload_label=load_label.drop_duplicates(\"patientId\")\nprint(len(load_label.patientId))\nload_label.head(6)","metadata":{"papermill":{"duration":0.110272,"end_time":"2022-11-19T14:59:14.379386","exception":false,"start_time":"2022-11-19T14:59:14.269114","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T13:32:20.948418Z","iopub.execute_input":"2022-11-23T13:32:20.948830Z","iopub.status.idle":"2022-11-23T13:32:21.013423Z","shell.execute_reply.started":"2022-11-23T13:32:20.948789Z","shell.execute_reply":"2022-11-23T13:32:21.012279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#### Virtualiza some images \nRoot_path_images = Path(\"../input/rsna-pneumonia-detection-challenge/stage_2_train_images/\")","metadata":{"papermill":{"duration":0.016469,"end_time":"2022-11-19T14:59:14.403344","exception":false,"start_time":"2022-11-19T14:59:14.386875","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T13:32:24.713530Z","iopub.execute_input":"2022-11-23T13:32:24.713927Z","iopub.status.idle":"2022-11-23T13:32:24.718547Z","shell.execute_reply.started":"2022-11-23T13:32:24.713894Z","shell.execute_reply":"2022-11-23T13:32:24.717511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counter = 0 \nfig , axes = plt.subplots(3,3,figsize=(8,8))\nfor i in range(3):\n    for j in range(3):\n        full_path_image = os.path.join(Root_path_images , load_label[\"patientId\"].iloc[counter])\n        full_path_dicom = full_path_image + \".dcm\"\n        read_dicom = dc.read_file(full_path_dicom).pixel_array \n        axes[i][j].imshow(read_dicom, cmap=\"gray\")\n        axes[i][j].set_title(load_label[\"Target\"].iloc[counter])\n        counter += 1 \n        \n        ","metadata":{"papermill":{"duration":2.573281,"end_time":"2022-11-19T14:59:16.983545","exception":false,"start_time":"2022-11-19T14:59:14.410264","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T13:32:27.922639Z","iopub.execute_input":"2022-11-23T13:32:27.923787Z","iopub.status.idle":"2022-11-23T13:32:30.492757Z","shell.execute_reply.started":"2022-11-23T13:32:27.923693Z","shell.execute_reply":"2022-11-23T13:32:30.491763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#### Per-Processing the data \n\nsums , sum_square = 0,0\nSave_Processed_Image = Path(\"./processed/\") \nfor index_patient , patien_ID in enumerate(tqdm(load_label.patientId)):\n        Patient_ID = load_label[\"patientId\"].iloc[index_patient]\n        full_path_image = os.path.join(Root_path_images , load_label[\"patientId\"].iloc[index_patient])\n        full_path_dicom = full_path_image + \".dcm\"\n        read_dicom = dc.read_file(full_path_dicom).pixel_array / 255\n        resize_image = cv2.resize(read_dicom , (128,128)).astype(np.float16)\n        \n#         label = load_label.Target.iloc[index_patient]\n\n        train_or_val = 'train' if index_patient < 24000 else \"val\"\n#         current_save_path = Save_Processed_Image/train_or_val/str(label)\n#         ### the struct dircotry is \n#         ## Train/\n#         #       lable_0/\n#         #              Patine_ID\n#         #        labek_1/\n#         #              Patien_ID \n#         #####\n#         current_save_path.mkdir(parents=True , exist_ok=True)\n#         np.save(current_save_path/Patient_ID,resize_image)\n        normalizer = 120*120\n        if train_or_val == 'train':\n            sums += np.sum(resize_image) / normalizer \n            sum_square += (resize_image ** 2).sum() / normalizer \n        \n        \n    ","metadata":{"papermill":{"duration":496.021818,"end_time":"2022-11-19T15:07:33.015248","exception":false,"start_time":"2022-11-19T14:59:16.993430","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T13:33:43.702302Z","iopub.execute_input":"2022-11-23T13:33:43.702698Z","iopub.status.idle":"2022-11-23T13:39:18.387990Z","shell.execute_reply.started":"2022-11-23T13:33:43.702662Z","shell.execute_reply":"2022-11-23T13:39:18.386888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean = sums / 24000\nstd = np.sqrt(sum_square / 24000 - mean**2)","metadata":{"papermill":{"duration":0.017391,"end_time":"2022-11-19T15:07:33.042739","exception":false,"start_time":"2022-11-19T15:07:33.025348","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T13:41:27.296901Z","iopub.execute_input":"2022-11-23T13:41:27.297297Z","iopub.status.idle":"2022-11-23T13:41:27.303082Z","shell.execute_reply.started":"2022-11-23T13:41:27.297262Z","shell.execute_reply":"2022-11-23T13:41:27.302038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean , std","metadata":{"papermill":{"duration":0.020313,"end_time":"2022-11-19T15:07:33.072168","exception":false,"start_time":"2022-11-19T15:07:33.051855","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T13:41:30.962442Z","iopub.execute_input":"2022-11-23T13:41:30.962868Z","iopub.status.idle":"2022-11-23T13:41:30.971310Z","shell.execute_reply.started":"2022-11-23T13:41:30.962827Z","shell.execute_reply":"2022-11-23T13:41:30.970064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Traning Step using Pytorch Lighting** <a class=\"anchor\" id=\"Section-2\"></a>\n* loading the data has been Processed \n  - function to load and save ans float \n     - augementation \n     - Dataloader \n     - model CNN from Renstnet","metadata":{"papermill":{"duration":0.008677,"end_time":"2022-11-19T15:07:33.089764","exception":false,"start_time":"2022-11-19T15:07:33.081087","status":"completed"},"tags":[]}},{"cell_type":"code","source":"### Loading the numpy format as .npy\ndef Loader_Data(path):\n    return np.load(path).astype(np.float32)\n### Augmenetation pipline from transform Objet trochvision \n\nTransform_aug_Train = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize(0.25,0.49),\n    transforms.RandomAffine(degrees=(-5, 5), translate=(0, 0.05), scale=(0.9, 1.1)),\n    transforms.RandomResizedCrop((120, 120), scale=(0.35, 1))\n    ])\nTransform_aug_Val = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize(0.25,0.49)\n    ])\nLoading_DataFolder_Train = torchvision.datasets.DatasetFolder(root=\"./processed/train\",\n                                                              loader=Loader_Data , \n                                                              extensions=\"npy\",\n                                                              transform=Transform_aug_Train)\nLoading_DataFolder_Val= torchvision.datasets.DatasetFolder(root=\"./processed/val\",\n                                                           loader=Loader_Data , \n                                                           extensions=\"npy\",\n                                                           transform=Transform_aug_Val)\n\n","metadata":{"papermill":{"duration":0.119356,"end_time":"2022-11-19T15:07:33.217961","exception":false,"start_time":"2022-11-19T15:07:33.098605","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T13:41:36.882596Z","iopub.execute_input":"2022-11-23T13:41:36.883581Z","iopub.status.idle":"2022-11-23T13:41:37.003257Z","shell.execute_reply.started":"2022-11-23T13:41:36.883538Z","shell.execute_reply":"2022-11-23T13:41:37.002149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#### Virtualize few samples from Data after augmenration .\nfig , axes = plt.subplots(3,3,figsize=(8,8))\ncounter_Img = 0\nfor i in range(3):\n    for j in range(3):\n        rand_indx = np.random.randint(0,2000) ### this will return value betweem [0,24000]\n        Image_ray , label = Loading_DataFolder_Train[rand_indx] # will retrun tuple correspond image X_ray woht label \n        if label == 0 :\n            class_ = \"Negative\"\n        else :\n            class_ = \"Positive\"\n        axes[i][j].imshow(Image_ray[0],cmap=\"gray\")\n        axes[i][j].set_title(f\"label class is : {class_}\")","metadata":{"papermill":{"duration":1.117619,"end_time":"2022-11-19T15:07:34.345292","exception":false,"start_time":"2022-11-19T15:07:33.227673","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T13:41:39.602222Z","iopub.execute_input":"2022-11-23T13:41:39.602605Z","iopub.status.idle":"2022-11-23T13:41:40.748764Z","shell.execute_reply.started":"2022-11-23T13:41:39.602574Z","shell.execute_reply":"2022-11-23T13:41:40.747678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##### check the data if is imblance or not by seeing the distrubtion \nnp.unique(Loading_DataFolder_Train.targets, return_counts=True)\n### here we can see that the data is imblanc but we can go through this even do ","metadata":{"papermill":{"duration":0.024544,"end_time":"2022-11-19T15:07:34.380861","exception":false,"start_time":"2022-11-19T15:07:34.356317","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T13:41:46.085358Z","iopub.execute_input":"2022-11-23T13:41:46.085741Z","iopub.status.idle":"2022-11-23T13:41:46.095609Z","shell.execute_reply.started":"2022-11-23T13:41:46.085688Z","shell.execute_reply":"2022-11-23T13:41:46.094529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 64\nTraining_Loader = torch.utils.data.DataLoader(Loading_DataFolder_Train,batch_size=batch_size, shuffle=True)\nValidation_Loader = torch.utils.data.DataLoader(Loading_DataFolder_Val,batch_size=batch_size, shuffle=False)\nprint(f\"lenght of training data is :{len(Training_Loader)}\\nlenght of validationi s: {len(Validation_Loader)}\")","metadata":{"papermill":{"duration":0.021358,"end_time":"2022-11-19T15:07:34.412740","exception":false,"start_time":"2022-11-19T15:07:34.391382","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T13:41:47.692294Z","iopub.execute_input":"2022-11-23T13:41:47.692676Z","iopub.status.idle":"2022-11-23T13:41:47.700655Z","shell.execute_reply.started":"2022-11-23T13:41:47.692645Z","shell.execute_reply":"2022-11-23T13:41:47.698955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Pipline model** Building model <a class=\"anchor\" id=\"Section3\">\n- by calling this API torchvision \n  * torchvision.models.resnet152()\n    * change the number of channel Conv1 from 3 to 1 \n       * **(conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)**\n    * change the output of last layer fully connected layer to 1 \n       * **(fc): Linear(in_features=2048, out_features=1000, bias=True)**\n- **Notation** :  <div class=\"alert alert-info\">since the competition is ended 3 years ago here i just tried give a briefy implementation of<strong> Pytorch **Lighiting**</strong></div> <br>\n    - **that's why we will just run the Model on 10 Epochs and you can raise it to wished value if you would like**\n\n","metadata":{"papermill":{"duration":0.010525,"end_time":"2022-11-19T15:07:34.433929","exception":false,"start_time":"2022-11-19T15:07:34.423404","status":"completed"},"tags":[]}},{"cell_type":"code","source":"###### Creating the model \nclass PenuModle(pl.LightningModule):\n    def __init__(self,weight=1):\n        super().__init__()\n        ### the PL of model \n        self.model = torchvision.models.resnet152()\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=2048, out_features=1)\n        ### setup the Optimizer and loss functions \n        self.Optimizer = torch.optim.Adam(self.model.parameters(),lr = 1e-4)\n        self.Loss_F = torch.nn.BCEWithLogitsLoss(pos_weight=torch.Tensor([weight]))\n         # simple accuracy computation\n        self.train_acc = torchmetrics.Accuracy()\n        self.val_acc = torchmetrics.Accuracy()\n        \n    def forward(self, input_):\n        Predicted_label = self.model(input_)\n        return Predicted_label\n        \n    def training_step(self,batch , batch_idx):\n        Image , label = batch \n        label= label.float()\n        Predicted_label = self(Image)[:,0]\n        loss = self.Loss_F(Predicted_label,label)\n        # Log loss and batch accuracy\n        self.log(\"Train Loss\", loss)\n        self.log(\"Step Train Acc\", self.train_acc(torch.sigmoid(Predicted_label), label.int()))\n        return loss\n    \n    def training_epoch_end(self, outs):\n        # After one epoch compute the whole train_data accuracy\n        self.log(\"Train Acc\", self.train_acc.compute())\n        \n    #######\n    ### here we did the same as Traiing PL we changed only the input_data disttro\n    #######\n    def validation_step(self,batch , batch_idx):\n        Image , label = batch \n        label = label.float()\n        Predicted_label = self(Image)[:,0]\n        loss = self.Loss_F(Predicted_label,label)\n        # Log loss and batch accuracy\n        self.log(\"Val Loss\", loss)\n        self.log(\"Step Val Acc\", self.train_acc(torch.sigmoid(Predicted_label), label.int()))\n        return loss\n    \n    def validation_epoch_end(self, outs):\n        # After one epoch compute the whole train_data accuracy\n        self.log(\"Val Acc\", self.train_acc.compute())  \n        \n    def configure_optimizers(self):\n        #Caution! You always need to return a list here (just pack your optimizer into one :))\n        return [self.Optimizer]     \n        ","metadata":{"papermill":{"duration":0.027409,"end_time":"2022-11-19T15:07:34.472145","exception":false,"start_time":"2022-11-19T15:07:34.444736","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T13:42:50.221564Z","iopub.execute_input":"2022-11-23T13:42:50.221981Z","iopub.status.idle":"2022-11-23T13:42:50.235583Z","shell.execute_reply.started":"2022-11-23T13:42:50.221947Z","shell.execute_reply":"2022-11-23T13:42:50.234607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Model_ = PenuModle() ### instanace the model from the class \nCheck_Point_Callbacks = ModelCheckpoint(\n    monitor=\"Val Acc\", \n    save_top_k=12,\n    mode=\"max\")","metadata":{"papermill":{"duration":1.365129,"end_time":"2022-11-19T15:07:35.847911","exception":false,"start_time":"2022-11-19T15:07:34.482782","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T13:42:55.741578Z","iopub.execute_input":"2022-11-23T13:42:55.742290Z","iopub.status.idle":"2022-11-23T13:42:56.874564Z","shell.execute_reply.started":"2022-11-23T13:42:55.742251Z","shell.execute_reply":"2022-11-23T13:42:56.873568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the trainer\n# Change the gpus parameter to the number of available gpus on your system. Use 0 for CPU training\n\ngpus = 2 #TODO\nTrainer = pl.Trainer(accelerator='gpu', devices=2, logger=TensorBoardLogger(save_dir= \"./processed/logs_weights_ex3\"), log_every_n_steps=1,\n                     callbacks=Check_Point_Callbacks,                    \n                     max_epochs=10)\n","metadata":{"papermill":{"duration":0.89225,"end_time":"2022-11-19T15:07:36.752130","exception":false,"start_time":"2022-11-19T15:07:35.859880","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T13:53:54.738538Z","iopub.execute_input":"2022-11-23T13:53:54.738941Z","iopub.status.idle":"2022-11-23T13:53:55.516886Z","shell.execute_reply.started":"2022-11-23T13:53:54.738907Z","shell.execute_reply":"2022-11-23T13:53:55.515446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Trainer.fit(Model_,Training_Loader,Validation_Loader)","metadata":{"papermill":{"duration":4691.341309,"end_time":"2022-11-19T16:25:48.104595","exception":false,"start_time":"2022-11-19T15:07:36.763286","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T13:53:58.748056Z","iopub.execute_input":"2022-11-23T13:53:58.748442Z","iopub.status.idle":"2022-11-23T14:22:07.636882Z","shell.execute_reply.started":"2022-11-23T13:53:58.748389Z","shell.execute_reply":"2022-11-23T14:22:07.635424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Evaluation Model** <a class=\"anchor\" id=\"Secteion-Eval\"> </a>\n* here we will load the weights the model \n* preduct few samples from the validation dataset \n* save the reults in tensor data type \n* compute few matrices results ","metadata":{"papermill":{"duration":0.015419,"end_time":"2022-11-19T16:25:48.135482","exception":false,"start_time":"2022-11-19T16:25:48.120063","status":"completed"},"tags":[]}},{"cell_type":"code","source":"### Loading the model check point weight \ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\nmodel_load = PenuModle.load_from_checkpoint(\"./processed/logs_weights_ex3/lightning_logs/version_0/checkpoints/epoch=9-step=1880.ckpt\")\nmodel_load.to(device)\nmodel_load.eval();\n\n","metadata":{"papermill":{"duration":3.371888,"end_time":"2022-11-19T16:25:51.522486","exception":false,"start_time":"2022-11-19T16:25:48.150598","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T14:25:29.536559Z","iopub.execute_input":"2022-11-23T14:25:29.537267Z","iopub.status.idle":"2022-11-23T14:25:31.650572Z","shell.execute_reply.started":"2022-11-23T14:25:29.537226Z","shell.execute_reply":"2022-11-23T14:25:31.649521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_ = []\nlabels_ = []\nwith torch.no_grad():\n    for image , label in tqdm(Loading_DataFolder_Val):\n        ## here we will need to load the data into device by hand \n        image = image.to(device).float().unsqueeze(0)\n        label = label\n        predicted= torch.sigmoid(model_load(image)[0]).cpu()\n        predicted_.append(predicted)\n        labels_.append(label)\n    print(image.shape) \n    print(predicted.shape)\n            \nTensor_Pre = torch.tensor(predicted_)        \nTensor_Lab = torch.tensor(labels_).int() \n","metadata":{"papermill":{"duration":61.259477,"end_time":"2022-11-19T16:26:52.806887","exception":false,"start_time":"2022-11-19T16:25:51.547410","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T14:25:42.917101Z","iopub.execute_input":"2022-11-23T14:25:42.917476Z","iopub.status.idle":"2022-11-23T14:26:48.054133Z","shell.execute_reply.started":"2022-11-23T14:25:42.917441Z","shell.execute_reply":"2022-11-23T14:26:48.053023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Tensor_Lab.shape , Tensor_Pre.shape","metadata":{"papermill":{"duration":0.025215,"end_time":"2022-11-19T16:26:52.848527","exception":false,"start_time":"2022-11-19T16:26:52.823312","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T14:27:04.352333Z","iopub.execute_input":"2022-11-23T14:27:04.352691Z","iopub.status.idle":"2022-11-23T14:27:04.359475Z","shell.execute_reply.started":"2022-11-23T14:27:04.352659Z","shell.execute_reply":"2022-11-23T14:27:04.358554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = torchmetrics.Accuracy()(Tensor_Pre, Tensor_Lab)\nprecision = torchmetrics.Precision()(Tensor_Pre, Tensor_Lab)\nrecall = torchmetrics.Recall()(Tensor_Pre, Tensor_Lab)\ncm = torchmetrics.ConfusionMatrix(num_classes=2)(Tensor_Pre, Tensor_Lab)\ncm_threshed = torchmetrics.ConfusionMatrix(num_classes=2, threshold=0.25)(Tensor_Pre, Tensor_Lab)\n\nprint(f\"Val Accuracy: {acc}\")\nprint(f\"Val Precision: {precision}\")\nprint(f\"Val Recall: {recall}\")\nprint(f\"Confusion Matrix:\\n {cm}\")\nprint(f\"Confusion Matrix 2:\\n {cm_threshed}\")","metadata":{"papermill":{"duration":0.037365,"end_time":"2022-11-19T16:26:52.901085","exception":false,"start_time":"2022-11-19T16:26:52.863720","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T14:27:09.050595Z","iopub.execute_input":"2022-11-23T14:27:09.051369Z","iopub.status.idle":"2022-11-23T14:27:09.074498Z","shell.execute_reply.started":"2022-11-23T14:27:09.051328Z","shell.execute_reply":"2022-11-23T14:27:09.073568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Submission Files** <a class=\"anchor\" id=\"Section-Submission\"></a> \n* here will predict the sampels from vaidations data and save the Patient_ID and predicte value ","metadata":{"papermill":{"duration":0.015828,"end_time":"2022-11-19T16:26:53.015469","exception":false,"start_time":"2022-11-19T16:26:52.999641","status":"completed"},"tags":[]}},{"cell_type":"code","source":"#### Virtualiza some images \nDATA_DIR = \"../input/rsna-pneumonia-detection-challenge/\"\n# Directory to save logs and trained model\nROOT_DIR = '/kaggle/working'\n\ntest_dicom_dir = os.path.join(DATA_DIR, 'stage_2_test_images')","metadata":{"papermill":{"duration":0.023786,"end_time":"2022-11-19T16:26:53.055623","exception":false,"start_time":"2022-11-19T16:26:53.031837","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T14:29:48.983841Z","iopub.execute_input":"2022-11-23T14:29:48.984225Z","iopub.status.idle":"2022-11-23T14:29:48.989465Z","shell.execute_reply.started":"2022-11-23T14:29:48.984195Z","shell.execute_reply":"2022-11-23T14:29:48.988430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_dicom_fps(dicom_dir):\n    dicom_fps = glob.glob(dicom_dir+'/'+'*.dcm')\n    return list(set(dicom_fps))\n","metadata":{"papermill":{"duration":0.02362,"end_time":"2022-11-19T16:26:53.094789","exception":false,"start_time":"2022-11-19T16:26:53.071169","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T14:29:59.916784Z","iopub.execute_input":"2022-11-23T14:29:59.917142Z","iopub.status.idle":"2022-11-23T14:29:59.922259Z","shell.execute_reply.started":"2022-11-23T14:29:59.917110Z","shell.execute_reply":"2022-11-23T14:29:59.921057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get filenames of test dataset DICOM images\ntest_image_fps = get_dicom_fps(test_dicom_dir)\n","metadata":{"papermill":{"duration":0.228878,"end_time":"2022-11-19T16:26:53.339541","exception":false,"start_time":"2022-11-19T16:26:53.110663","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T14:31:19.490232Z","iopub.execute_input":"2022-11-23T14:31:19.490606Z","iopub.status.idle":"2022-11-23T14:31:19.978592Z","shell.execute_reply.started":"2022-11-23T14:31:19.490574Z","shell.execute_reply":"2022-11-23T14:31:19.977621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on test images, write out sample submission\npatient_ID = []\nScore_ID= []\nmy_formatter = \"{0:.2f}\"\ndef predict(image_fps, min_conf=0.90):\n        with torch.no_grad():\n            for image_id in tqdm(image_fps):\n                ds = dc.read_file(image_id)\n                image = ds.pixel_array / 255\n                # If grayscale. Convert to RGB for consistency.\n                #if len(image.shape) != 3 or image.shape[2] != 3:\n                #image = np.stack((image,) * 3, -1)\n                image = cv2.resize(image , (128,128)).astype(np.float32)\n                image = np.expand_dims(image,axis=0)\n                patient_id = os.path.splitext(os.path.basename(image_id))[0]\n                patient_ID.append(patient_id)\n                image= torch.from_numpy(image)\n                image = image.to(device).float().unsqueeze(0)\n                results = torch.sigmoid(model_load(image)[0]).cpu()\n                score =  my_formatter.format(results[0].item())\n                Score = str(score) +\" Postive but sign pneumonia\" if float(score) > 0.60 else str(score) + \" Negative No sign pneumonia\"\n                Score_ID.append(Score)\n#                 print(patient_ID)\n#                 print(Score_ID)\n\n","metadata":{"papermill":{"duration":0.027166,"end_time":"2022-11-19T16:26:53.384539","exception":false,"start_time":"2022-11-19T16:26:53.357373","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T14:40:43.563138Z","iopub.execute_input":"2022-11-23T14:40:43.564085Z","iopub.status.idle":"2022-11-23T14:40:43.575188Z","shell.execute_reply.started":"2022-11-23T14:40:43.564035Z","shell.execute_reply":"2022-11-23T14:40:43.573906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict(test_image_fps)\n","metadata":{"papermill":{"duration":120.540825,"end_time":"2022-11-19T16:28:53.940833","exception":false,"start_time":"2022-11-19T16:26:53.400008","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T14:40:46.305859Z","iopub.execute_input":"2022-11-23T14:40:46.306834Z","iopub.status.idle":"2022-11-23T14:43:00.938624Z","shell.execute_reply.started":"2022-11-23T14:40:46.306771Z","shell.execute_reply":"2022-11-23T14:43:00.937748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create an Empty DataFrame object\nfilepath='submission.csv'\nsubmission_fp = os.path.join(ROOT_DIR, filepath)\ndf = pd.DataFrame(columns=['patientId','PredictionString'])\nfor idx in range(len(patient_ID)):\n    df = df.append({'patientId': patient_ID[idx],'PredictionString': Score_ID[idx]}, ignore_index=True)\ndf.to_csv(submission_fp,index=False)\noutput = pd.read_csv(submission_fp)\noutput.head(3)","metadata":{"papermill":{"duration":4.908824,"end_time":"2022-11-19T16:28:58.866735","exception":false,"start_time":"2022-11-19T16:28:53.957911","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-23T14:44:29.329984Z","iopub.execute_input":"2022-11-23T14:44:29.330360Z","iopub.status.idle":"2022-11-23T14:44:35.042197Z","shell.execute_reply.started":"2022-11-23T14:44:29.330328Z","shell.execute_reply":"2022-11-23T14:44:35.041062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Activation Map** <a class=\"anchor\" id=\"Section-Map\"></a>\n1. **this section containe activation map extrat feature from Layer Convolution**\n     - this technic is based on save the weights of layer in list Sequencetial model </br>\n       and calculate the weights on Validation test \n     - i did virtualization plot to check heatMap ","metadata":{}},{"cell_type":"code","source":"#### Loader data function \ndef loader(path):\n    return np.load(path).astype(np.float32)\n#### set the transfoms augmentation \ntransform = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize(0.46,0.35)])\nvalidation_set = Path(\"./processed/val/\")\n\nload_data = torchvision.datasets.DatasetFolder(root=validation_set , loader=loader,extensions=\"npy\",transform=transform)","metadata":{"execution":{"iopub.status.busy":"2022-11-23T14:57:59.184047Z","iopub.execute_input":"2022-11-23T14:57:59.184451Z","iopub.status.idle":"2022-11-23T14:57:59.207651Z","shell.execute_reply.started":"2022-11-23T14:57:59.184420Z","shell.execute_reply":"2022-11-23T14:57:59.206507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### check the model building blocks \n# model_ = torchvision.models.resnet152()\n# blocks = torch.nn.Sequential(*list(model_.children()))\n# blocks","metadata":{"execution":{"iopub.status.busy":"2022-11-23T15:38:11.154403Z","iopub.execute_input":"2022-11-23T15:38:11.154781Z","iopub.status.idle":"2022-11-23T15:38:11.159215Z","shell.execute_reply.started":"2022-11-23T15:38:11.154747Z","shell.execute_reply":"2022-11-23T15:38:11.158242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#### building the model \n\nclass PeunModel(pl.LightningModule):\n    def __init__(self):\n        super().__init__()\n        self.model = torchvision.models.resnet152()\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=2048, out_features=1)\n        \n        ### get the features map from the model 2\n        self.feature_map = torch.nn.Sequential(*list(self.model.children())[:-2])\n        \n    def forward(self, input_):\n        feature_map = self.feature_map(input_)\n        pooling_avg = torch.nn.functional.adaptive_avg_pool2d(feature_map , output_size=(1,1))\n        flatten_pool = torch.flatten(pooling_avg)\n        Predict_= self.model.fc(flatten_pool)\n        return feature_map ,Predict_ \n        ","metadata":{"execution":{"iopub.status.busy":"2022-11-23T16:15:26.767726Z","iopub.execute_input":"2022-11-23T16:15:26.768358Z","iopub.status.idle":"2022-11-23T16:15:26.776588Z","shell.execute_reply.started":"2022-11-23T16:15:26.768320Z","shell.execute_reply":"2022-11-23T16:15:26.775565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### intialze the model \nModel_peun = PeunModel()\nModel_peun.load_from_checkpoint(\"./processed/logs_weights_ex3/lightning_logs/version_0/checkpoints/epoch=9-step=1880.ckpt\",strict=False)\nModel_peun.eval();","metadata":{"execution":{"iopub.status.busy":"2022-11-23T16:15:29.709638Z","iopub.execute_input":"2022-11-23T16:15:29.710351Z","iopub.status.idle":"2022-11-23T16:15:32.383691Z","shell.execute_reply.started":"2022-11-23T16:15:29.710313Z","shell.execute_reply":"2022-11-23T16:15:32.382643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### compute the Grad-Cam function \n\ndef Cam(image,model):\n    with torch.no_grad():\n        image = image[0]\n        feature_map , pred = Model_peun(image.unsqueeze(0))\n    print(feature_map.shape)\n    feature = feature_map.reshape((2048,4*4))\n    weights = list(model.model.fc.parameters())[0]\n    weights_param = weights[0].detach()\n    cam = torch.matmul(weights_param,feature) \n    print(cam.shape)\n    cam_img = cam.reshape(4,4).cpu()\n    return cam_img , torch.sigmoid(pred)\n        \n    ","metadata":{"execution":{"iopub.status.busy":"2022-11-23T16:22:37.144845Z","iopub.execute_input":"2022-11-23T16:22:37.145312Z","iopub.status.idle":"2022-11-23T16:22:37.153955Z","shell.execute_reply.started":"2022-11-23T16:22:37.145270Z","shell.execute_reply":"2022-11-23T16:22:37.152779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def virtulizer(img,cam ,pred):\n    img = img[0]\n    cam_img =transforms.functional.resize(cam.unsqueeze(0),(128,128))[0]\n    fig ,axis = plt.subplots(1,2 , figsize=(6,6))\n    axis[0].imshow(img,cmap=\"bone\")\n    axis[1].imshow(img,cmap=\"bone\")\n    axis[1].imshow(cam_img,cmap=\"jet\")\n    if pred >0.5 :\n        plt.title(\"positive\")\n    else:\n        plt.title(\"negative\")\n        ","metadata":{"execution":{"iopub.status.busy":"2022-11-23T16:24:02.812325Z","iopub.execute_input":"2022-11-23T16:24:02.812722Z","iopub.status.idle":"2022-11-23T16:24:02.819836Z","shell.execute_reply.started":"2022-11-23T16:24:02.812669Z","shell.execute_reply":"2022-11-23T16:24:02.818756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##### testing \n# 1. compute \nimg = load_data[-5][0]\nmap_activation , Predicted = Cam(img.unsqueeze(0),Model_peun)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-23T16:26:01.814014Z","iopub.execute_input":"2022-11-23T16:26:01.814464Z","iopub.status.idle":"2022-11-23T16:26:02.011352Z","shell.execute_reply.started":"2022-11-23T16:26:01.814422Z","shell.execute_reply":"2022-11-23T16:26:02.009150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"virtulizer(img,map_activation,Predicted)","metadata":{"execution":{"iopub.status.busy":"2022-11-23T16:26:03.852898Z","iopub.execute_input":"2022-11-23T16:26:03.853253Z","iopub.status.idle":"2022-11-23T16:26:04.181868Z","shell.execute_reply.started":"2022-11-23T16:26:03.853221Z","shell.execute_reply":"2022-11-23T16:26:04.180869Z"},"trusted":true},"execution_count":null,"outputs":[]}]}