{"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":"import os\nimport gc\nimport cv2\nimport copy\nimport time\nimport random\nimport glob as glob\nimport string\nimport joblib\nimport tifffile\nimport numpy as np \nimport pandas as pd \nimport torch\nfrom torch import nn\nimport seaborn as sns\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nfrom torchvision import models\nfrom scipy.special import expit\nfrom pydicom import dcmread\nimport matplotlib.pyplot as plt\nfrom torchvision import models as models\nimport torch.optim as optim\nfrom torchvision.models import resnet50\nimport torch\nimport torchvision\nimport torchvision.transforms as transforms\nfrom torch.utils.data import Dataset, DataLoader\nfrom sklearn.model_selection import train_test_split\nimport torch.nn.functional as F\nfrom tqdm import tqdm\nfrom torch.optim import lr_scheduler\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-29T05:55:37.393959Z","iopub.execute_input":"2022-09-29T05:55:37.394426Z","iopub.status.idle":"2022-09-29T05:55:40.6677Z","shell.execute_reply.started":"2022-09-29T05:55:37.39433Z","shell.execute_reply":"2022-09-29T05:55:40.666759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:55:40.669273Z","iopub.execute_input":"2022-09-29T05:55:40.670312Z","iopub.status.idle":"2022-09-29T05:55:40.675447Z","shell.execute_reply.started":"2022-09-29T05:55:40.670273Z","shell.execute_reply":"2022-09-29T05:55:40.673945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv =pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/train.csv\")\ntest_csv","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:55:40.676634Z","iopub.execute_input":"2022-09-29T05:55:40.67704Z","iopub.status.idle":"2022-09-29T05:55:40.728795Z","shell.execute_reply.started":"2022-09-29T05:55:40.676974Z","shell.execute_reply":"2022-09-29T05:55:40.727891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/test.csv\")\ntest_csv","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:55:40.73173Z","iopub.execute_input":"2022-09-29T05:55:40.73218Z","iopub.status.idle":"2022-09-29T05:55:40.748617Z","shell.execute_reply.started":"2022-09-29T05:55:40.732143Z","shell.execute_reply":"2022-09-29T05:55:40.747448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_path = glob.glob(\"../input/rsna-2022-cervical-spine-fracture-detection/test_images/**/*\")\nlen(test_image_path)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:55:40.750392Z","iopub.execute_input":"2022-09-29T05:55:40.751161Z","iopub.status.idle":"2022-09-29T05:55:40.865038Z","shell.execute_reply.started":"2022-09-29T05:55:40.751121Z","shell.execute_reply":"2022-09-29T05:55:40.863508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row_id = []\nfor i in range(len(test_image_path)):\n    temp = str(test_image_path[i])\n    temp1 = temp.split(\"/\")\n    temp2 =temp1[-2]\n    temp3 = temp2 +\"_C1\"\n    row_id.append(temp3)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:55:40.867118Z","iopub.execute_input":"2022-09-29T05:55:40.867697Z","iopub.status.idle":"2022-09-29T05:55:40.876231Z","shell.execute_reply.started":"2022-09-29T05:55:40.867633Z","shell.execute_reply":"2022-09-29T05:55:40.87471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(row_id)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:55:40.877941Z","iopub.execute_input":"2022-09-29T05:55:40.878929Z","iopub.status.idle":"2022-09-29T05:55:40.896384Z","shell.execute_reply.started":"2022-09-29T05:55:40.878883Z","shell.execute_reply":"2022-09-29T05:55:40.895111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_path = glob.glob(\"../input/rsna-150-9204-25-data/test_150_rsna/test_150/*\")\nlen(test_image_path)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:55:40.898559Z","iopub.execute_input":"2022-09-29T05:55:40.900126Z","iopub.status.idle":"2022-09-29T05:55:41.333Z","shell.execute_reply.started":"2022-09-29T05:55:40.900072Z","shell.execute_reply":"2022-09-29T05:55:41.331618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.DataFrame({'row_id':row_id,'image':test_image_path})\ntest_df.groupby([\"row_id\"]).image.count()","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:55:41.33438Z","iopub.execute_input":"2022-09-29T05:55:41.335521Z","iopub.status.idle":"2022-09-29T05:55:41.357198Z","shell.execute_reply.started":"2022-09-29T05:55:41.335481Z","shell.execute_reply":"2022-09-29T05:55:41.356016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:55:41.362106Z","iopub.execute_input":"2022-09-29T05:55:41.362501Z","iopub.status.idle":"2022-09-29T05:55:41.373906Z","shell.execute_reply.started":"2022-09-29T05:55:41.362466Z","shell.execute_reply":"2022-09-29T05:55:41.372744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class dataset(Dataset):\n    def __init__(self,csv,train, test):\n        self.csv = csv\n        self.train = train\n        self.test = test\n        self.all_patientids_names = self.csv[:]['image']\n        self.all_labels = np.array(self.csv.drop(['image'],axis=1))\n        self.train_ratio= int(0.80*len(self.csv))\n        self.valid_ratio = len(self.csv) - self.train_ratio\n        \n        if self.train==True:\n            print(f\"Number of Training Images : {self.train_ratio}\")\n            self.image_names = list(self.all_patientids_names[:self.train_ratio])\n            self.labels = list(self.all_labels[:self.train_ratio])\n            \n            #train transoforms\n            \n            self.transform = transforms.Compose([\n                  transforms.ToPILImage(),\n                  transforms.Resize((150,150)),\n                  transforms.RandomHorizontalFlip(p=0.50),\n                  transforms.RandomRotation(degrees=45),\n                  transforms.ToTensor(),\n                  transforms.Normalize(torch.Tensor([0.2963,0.2963,0.2963]),torch.Tensor([0.1987,0.1987,0.1987]))\n                                      ])\n            \n        \n        elif self.train==False and self.test==False:\n            \n        \n            print(f\"Number of Validation Images:{self.valid_ratio}\")\n                \n            self.image_names = list(self.all_patientids_names[-self.valid_ratio:])\n            self.labels = list(self.all_labels[-self.valid_ratio:])\n\n            self.transform = transforms.Compose([\n                transforms.ToPILImage(),\n                transforms.Resize((150,150)),\n                transforms.ToTensor(),\n                transforms.Normalize(torch.Tensor([0.2963,0.2963,0.2963]),torch.Tensor([0.1987,0.1987,0.1987]))\n                ])\n\n        elif self.test==True and self.train == False:\n            \n            self.image_names =list(self.all_patientids_names)\n            print(f\"Number of Testing Images:{len(self.image_names)}\")\n            self.labels = list(self.all_labels)\n            self.transform = transforms.Compose([\n                   transforms.ToPILImage(),\n                   transforms.Resize((150,150)),\n                   transforms.ToTensor(),\n                   transforms.Normalize(torch.Tensor([0.2963,0.2963,0.2963]),torch.Tensor([0.1987,0.1987,0.1987]))\n                                        ])\n    def __len__(self):\n        return len(self.image_names)\n    \n                    \n    def __getitem__(self,index):\n        \n        image = cv2.imread(self.image_names[index])\n        image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)\n                    \n        image = self.transform(image)\n        if self.train==True:\n            targets = self.labels[index]\n            return {\n                \"image\": torch.tensor(image,dtype=torch.float32),\n                \"label\" : torch.tensor(targets,dtype= torch.float32)\n                 }\n        elif self.train==False and self.test==False:\n            targets = self.labels[index]\n            return {\n                \"image\": torch.tensor(image,dtype=torch.float32),\n                \"label\" : torch.tensor(targets,dtype= torch.float32)\n                 }\n            \n        elif self.test==True and self.train == False:\n            return {\n                \"image\": torch.tensor(image,dtype=torch.float32),\n            }","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:55:41.375952Z","iopub.execute_input":"2022-09-29T05:55:41.376289Z","iopub.status.idle":"2022-09-29T05:55:41.397121Z","shell.execute_reply.started":"2022-09-29T05:55:41.37626Z","shell.execute_reply":"2022-09-29T05:55:41.395628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Test Dataset\ntest_csv =test_df\ntest_data = dataset(\n    test_csv, train=False, test=True)\n#Test Loader\ntest_loader = DataLoader(\n    test_data, \n    batch_size=1,\n    shuffle=True,num_workers=2)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:55:41.398423Z","iopub.execute_input":"2022-09-29T05:55:41.398778Z","iopub.status.idle":"2022-09-29T05:55:41.416227Z","shell.execute_reply.started":"2022-09-29T05:55:41.398747Z","shell.execute_reply":"2022-09-29T05:55:41.414912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = resnet50(progress=True,pretrained = False,num_classes=7)\nmodel.load_state_dict(torch.load(\"../input/resnet50-1l7-full/Traning_7/best_metric_model_1L_7.pth\",map_location=device))\nbatch_size = 1\n#criterion = nn.CrossEntropyLoss()\ncriterion = nn.BCELoss()\nmodel.eval()","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:55:41.41726Z","iopub.execute_input":"2022-09-29T05:55:41.417588Z","iopub.status.idle":"2022-09-29T05:55:43.5967Z","shell.execute_reply.started":"2022-09-29T05:55:41.417557Z","shell.execute_reply":"2022-09-29T05:55:43.595606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"testing\")\nmodel.eval()\n\noutputs_list=[]\nwith torch.no_grad():\n    for i , data in tqdm(enumerate(test_loader),total=int(len(test_data )/test_loader.batch_size)):\n    \n        data = data['image'].to(device)\n        outputs = model(data)\n\n        outputs = torch.sigmoid(outputs)\n        outputs = outputs.numpy().squeeze()\n        outputs_list.append(outputs)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:55:43.598201Z","iopub.execute_input":"2022-09-29T05:55:43.599439Z","iopub.status.idle":"2022-09-29T05:57:07.375391Z","shell.execute_reply.started":"2022-09-29T05:55:43.599387Z","shell.execute_reply":"2022-09-29T05:57:07.373947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(outputs_list)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:57:07.377226Z","iopub.execute_input":"2022-09-29T05:57:07.378039Z","iopub.status.idle":"2022-09-29T05:57:07.385373Z","shell.execute_reply.started":"2022-09-29T05:57:07.377983Z","shell.execute_reply":"2022-09-29T05:57:07.384043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_pd = pd.DataFrame(outputs_list)\nout_pd[\"row_id\"]=test_df[\"row_id\"]\nout_pd.rename(columns={0:\"C1\",1:\"C2\",2:\"C3\",3:\"C4\",4:\"C5\",5:\"C6\",6:\"C7\"},inplace= True)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:57:07.386829Z","iopub.execute_input":"2022-09-29T05:57:07.3875Z","iopub.status.idle":"2022-09-29T05:57:07.41101Z","shell.execute_reply.started":"2022-09-29T05:57:07.387459Z","shell.execute_reply":"2022-09-29T05:57:07.409774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_pd[\"over_all\"]=out_pd.max(axis=\"columns\",  numeric_only=True,)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:57:07.41297Z","iopub.execute_input":"2022-09-29T05:57:07.413434Z","iopub.status.idle":"2022-09-29T05:57:07.424649Z","shell.execute_reply.started":"2022-09-29T05:57:07.413386Z","shell.execute_reply":"2022-09-29T05:57:07.423519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_pd","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:57:07.425949Z","iopub.execute_input":"2022-09-29T05:57:07.426898Z","iopub.status.idle":"2022-09-29T05:57:07.450994Z","shell.execute_reply.started":"2022-09-29T05:57:07.42686Z","shell.execute_reply":"2022-09-29T05:57:07.449558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C1=out_pd.groupby([\"row_id\"]).C1.mean().to_list()\nC2=out_pd.groupby([\"row_id\"]).C2.mean().to_list()\nC3=out_pd.groupby([\"row_id\"]).C3.mean().to_list()\nC4=out_pd.groupby([\"row_id\"]).C4.mean().to_list()\nC5=out_pd.groupby([\"row_id\"]).C5.mean().to_list()\nC6=out_pd.groupby([\"row_id\"]).C6.mean().to_list()\nC7=out_pd.groupby([\"row_id\"]).C7.mean().to_list()\nover_all=out_pd.groupby([\"row_id\"]).over_all.mean().to_list()\n","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:57:07.452843Z","iopub.execute_input":"2022-09-29T05:57:07.453177Z","iopub.status.idle":"2022-09-29T05:57:07.471011Z","shell.execute_reply.started":"2022-09-29T05:57:07.453148Z","shell.execute_reply":"2022-09-29T05:57:07.469857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission1 = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/sample_submission.csv\")\nsubmission1","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:57:07.472826Z","iopub.execute_input":"2022-09-29T05:57:07.473172Z","iopub.status.idle":"2022-09-29T05:57:07.48956Z","shell.execute_reply.started":"2022-09-29T05:57:07.473139Z","shell.execute_reply":"2022-09-29T05:57:07.488436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission1.loc[0]=[\"1.2.826.0.1.3680043.10197_C1\",C1[0]]\nsubmission1.loc[1]=[\"1.2.826.0.1.3680043.10197_C2\",C2[0]]\nsubmission1.loc[2]=[\"1.2.826.0.1.3680043.10197_C3\",C3[0]]\nsubmission1.loc[3]=[\"1.2.826.0.1.3680043.10197_C4\",C4[0]]\nsubmission1.loc[4]=[\"1.2.826.0.1.3680043.10197_C5\",C5[0]]\nsubmission1.loc[5]=[\"1.2.826.0.1.3680043.10197_C6\",C6[0]]\nsubmission1.loc[6]=[\"1.2.826.0.1.3680043.10197_C7\",C7[0]]\nsubmission1.loc[7]=[\"1.2.826.0.1.3680043.10197_patient_overall\",over_all[0]]\n","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:57:07.491077Z","iopub.execute_input":"2022-09-29T05:57:07.491793Z","iopub.status.idle":"2022-09-29T05:57:07.516997Z","shell.execute_reply.started":"2022-09-29T05:57:07.491747Z","shell.execute_reply":"2022-09-29T05:57:07.515871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission1.loc[8]=[\"1.2.826.0.1.3680043.10454_C1\",C1[1]]\nsubmission1.loc[9]=[\"1.2.826.0.1.3680043.10454_C2\",C2[1]]\nsubmission1.loc[10]=[\"1.2.826.0.1.3680043.10454_C3\",C3[1]]\nsubmission1.loc[11]=[\"1.2.826.0.1.3680043.10454_C4\",C4[1]]\nsubmission1.loc[12]=[\"1.2.826.0.1.3680043.10454_C5\",C5[1]]\nsubmission1.loc[13]=[\"1.2.826.0.1.3680043.10454_C6\",C6[1]]\nsubmission1.loc[14]=[\"1.2.826.0.1.3680043.10454_C7\",C7[1]]\nsubmission1.loc[15]=[\"1.2.826.0.1.3680043.10454_patient_overall\",over_all[1]]","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:57:07.518487Z","iopub.execute_input":"2022-09-29T05:57:07.519018Z","iopub.status.idle":"2022-09-29T05:57:07.553751Z","shell.execute_reply.started":"2022-09-29T05:57:07.518974Z","shell.execute_reply":"2022-09-29T05:57:07.55254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission1.loc[16]=[\"1.2.826.0.1.3680043.10690_C1\",C1[2]]\nsubmission1.loc[17]=[\"1.2.826.0.1.3680043.10690_C2\",C2[2]]\nsubmission1.loc[18]=[\"1.2.826.0.1.3680043.10690_C3\",C3[2]]\nsubmission1.loc[19]=[\"1.2.826.0.1.3680043.10690_C4\",C4[2]]\nsubmission1.loc[20]=[\"1.2.826.0.1.3680043.10690_C5\",C5[2]]\nsubmission1.loc[21]=[\"1.2.826.0.1.3680043.10690_C6\",C6[2]]\nsubmission1.loc[22]=[\"1.2.826.0.1.3680043.10690_C7\",C7[2]]\nsubmission1.loc[23]=[\"1.2.826.0.1.3680043.10690_patient_overall\",over_all[2]]","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:57:07.554982Z","iopub.execute_input":"2022-09-29T05:57:07.555617Z","iopub.status.idle":"2022-09-29T05:57:07.590819Z","shell.execute_reply.started":"2022-09-29T05:57:07.555569Z","shell.execute_reply":"2022-09-29T05:57:07.589443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission1","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:57:07.592933Z","iopub.execute_input":"2022-09-29T05:57:07.59329Z","iopub.status.idle":"2022-09-29T05:57:07.612383Z","shell.execute_reply.started":"2022-09-29T05:57:07.593256Z","shell.execute_reply":"2022-09-29T05:57:07.611401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission1.to_csv(\"submission.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:57:07.613359Z","iopub.execute_input":"2022-09-29T05:57:07.613697Z","iopub.status.idle":"2022-09-29T05:57:07.627242Z","shell.execute_reply.started":"2022-09-29T05:57:07.613651Z","shell.execute_reply":"2022-09-29T05:57:07.62593Z"},"trusted":true},"execution_count":null,"outputs":[]}]}