{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":52254,"databundleVersionId":6307054,"sourceType":"competition"},{"sourceId":6557727,"sourceType":"datasetVersion","datasetId":3789183},{"sourceId":6565645,"sourceType":"datasetVersion","datasetId":3793153},{"sourceId":6565732,"sourceType":"datasetVersion","datasetId":3793186},{"sourceId":6613069,"sourceType":"datasetVersion","datasetId":3816113},{"sourceId":6613074,"sourceType":"datasetVersion","datasetId":3816116},{"sourceId":6613091,"sourceType":"datasetVersion","datasetId":3816129},{"sourceId":6619573,"sourceType":"datasetVersion","datasetId":3820468},{"sourceId":6627394,"sourceType":"datasetVersion","datasetId":3825862},{"sourceId":6641388,"sourceType":"datasetVersion","datasetId":3833963},{"sourceId":6653149,"sourceType":"datasetVersion","datasetId":3839699},{"sourceId":6688425,"sourceType":"datasetVersion","datasetId":3857354},{"sourceId":6688531,"sourceType":"datasetVersion","datasetId":3857381},{"sourceId":144490686,"sourceType":"kernelVersion"}],"dockerImageVersionId":30559,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from collections import defaultdict\nfrom model import MultiHeadResNet50\nfrom pathlib import Path\nfrom torch.utils.data import Dataset,DataLoader\n\n\nimport cv2\nimport numpy as np\nimport os\nimport pydicom\nimport pandas as pd\nimport random\nimport torch\nimport torch.nn.functional as F","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-14T00:22:27.17608Z","iopub.execute_input":"2023-10-14T00:22:27.176444Z","iopub.status.idle":"2023-10-14T00:22:27.181673Z","shell.execute_reply.started":"2023-10-14T00:22:27.176415Z","shell.execute_reply":"2023-10-14T00:22:27.180692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading the model ##","metadata":{}},{"cell_type":"code","source":"trained_model_path = '/kaggle/input/modelv10/resnet_50_1.644.pth'\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nmodel = torch.load(trained_model_path)\nmodel.to(device)\nmodel.eval()","metadata":{"execution":{"iopub.status.busy":"2023-10-14T00:22:27.55977Z","iopub.execute_input":"2023-10-14T00:22:27.560671Z","iopub.status.idle":"2023-10-14T00:22:28.931882Z","shell.execute_reply.started":"2023-10-14T00:22:27.560639Z","shell.execute_reply":"2023-10-14T00:22:28.931003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Get test images and convert to PNGs ##","metadata":{}},{"cell_type":"code","source":"def get_patient_images_paths(test_images):\n\n    folders = [f for f in os.listdir(test_images) if os.path.isdir(os.path.join(test_images, f))]\n    #print(folders)\n    patients = defaultdict(list) # a dictionary of patient with all his/her images\n\n    for f in folders:\n        instances = Path(test_images + '/' + f)\n        #print(instances)\n        for i in instances.iterdir():\n            for file in i.iterdir():\n                patients[f].append(file)\n                \n    patient_randomsingle_image = {}\n\n    for key, value in patients.items():\n        patient_randomsingle_image[key]=random.choice(value)\n                \n    #return patients\n    return patient_randomsingle_image\n\ndef dicom_to_png(dicom_png):\n    \"\"\"\n    Read the dicom file and preprocess appropriately.\n    \"\"\"\n    def standardize_image(dicom_image):\n        pixel_array = dicom_image.pixel_array   \n\n        if dicom_image.PixelRepresentation == 1:\n            bit_shift = dicom_image.BitsAllocated - dicom_image.BitsStored\n            dtype = pixel_array.dtype \n            pixel_array = (pixel_array << bit_shift).astype(dtype) >>  bit_shift\n\n        if dicom_image.PhotometricInterpretation == \"MONOCHROME1\":\n            pixel_array = 1 - pixel_array\n\n        # transform to hounsfield units\n        intercept = dicom_image.RescaleIntercept\n        slope = dicom_image.RescaleSlope\n        pixel_array = pixel_array * slope + intercept\n        \n        return pixel_array\n    \n    def apply_window(image_stack, window, normalize = True):\n        \n        window_width = window[0]\n        window_center = window[1]\n        img_min = window_center - window_width//2 #minimum HU level\n        img_max = window_center + window_width//2 #maximum HU level\n        img = np.clip(image_stack, img_min, img_max)\n        \n        if normalize:\n            img = (img - img_min) / (img_max - img_min)\n        return img\n    \n    \n    pixel_array = standardize_image(dicom_png)\n    \n    windows = [(2,1),(2048,1),(300,150)]\n    \n    imgs = []\n    \n    for window in windows:\n        windowed_array = apply_window(pixel_array,window, normalize = True)\n        img = (windowed_array * 255).astype(np.uint8)\n        img = cv2.resize(img, (256,256))\n        imgs.append(img)\n    \n    return np.stack(imgs,axis=-1)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T00:22:28.933743Z","iopub.execute_input":"2023-10-14T00:22:28.934311Z","iopub.status.idle":"2023-10-14T00:22:28.945095Z","shell.execute_reply.started":"2023-10-14T00:22:28.934278Z","shell.execute_reply":"2023-10-14T00:22:28.944166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prediction and Submissions ##","metadata":{}},{"cell_type":"code","source":"def predictions(patient_images_map,submission_df,model,device):\n \n    for patient_id,image_paths in patient_images_map.items():\n        #print(patient_id)\n        bowel_healthy, bowel_injury = [],[]\n        extravasation_healthy,extravasation_injury = [],[]\n        kidney_healthy, kidney_low, kidney_high = [],[],[]\n        liver_healthy, liver_low, liver_high = [], [], []\n        spleen_healthy, spleen_low, spleen_high = [], [], []\n        \n        \n        #for image in image_paths:\n        image = pydicom.dcmread(image_paths)\n        image = dicom_to_png(image) #change dicom to png\n            #change image to tensor\n        image = torch.tensor(image, dtype=torch.float32).permute(2,0,1)  #(3,256,256)\n        image = image.unsqueeze(0) # Reshape the tensor to (1, 3, 256, 256)\n        image = image.to(device)\n            \n        b,e,k,l,s = model(image)\n            \n        #bowel \n        prob_bowel =  F.softmax(b, dim = 1)\n        prob_bowel = prob_bowel.detach().cpu().numpy()\n        #print(f\"hureee: {prob_bowel}\")\n        bowel_healthy.append(prob_bowel[0][0]) \n        bowel_injury.append(prob_bowel[0][1])\n                    \n        #extravasation\n        prob_extra =  F.softmax(e, dim = 1)\n        prob_bowel = prob_extra.detach().cpu().numpy()\n        extravasation_healthy.append(prob_bowel[0][0]) \n        extravasation_injury.append(prob_bowel[0][1])\n        \n        #kidney\n        prob_kidney =  F.softmax(k, dim = 1)\n        prob_kidney = prob_kidney.detach().cpu().numpy()\n        kidney_healthy.append(prob_kidney[0][0]) \n        kidney_low.append(prob_kidney[0][1])\n        kidney_high.append(prob_kidney[0][2])\n\n             \n        #liver\n        prob_liver =  F.softmax(l, dim = 1)\n        prob_liver = prob_liver.detach().cpu().numpy()\n        liver_healthy.append(prob_liver[0][0])\n        liver_low.append(prob_liver[0][1])\n        liver_high.append(prob_liver[0][2])\n        #spleen\n        prob_spleen =  F.softmax(s, dim = 1)\n        prob_spleen = prob_spleen.detach().cpu().numpy()\n        spleen_healthy.append(prob_spleen[0][0])\n        spleen_low.append(prob_spleen[0][1]) \n        spleen_high.append(prob_spleen[0][2])\n            \n           \n            \n        submission_df.loc[len(submission_df)] = [patient_id,\n                                    format(np.average(bowel_healthy),'.1f'),\n                                    format(np.average(bowel_injury),'.1f'),\n                                                   \n                                    format(np.average(extravasation_healthy),'.1f'),\n                                    format(np.average(extravasation_injury),'.1f'),\n                                                   \n                                    np.average(kidney_healthy),\n                                    np.average(kidney_low), \n                                    np.average(kidney_high),\n                                                   \n                                    np.average(liver_healthy),\n                                    np.average(liver_low), \n                                    np.average(liver_high),\n                                                   \n                                    np.average(spleen_healthy),\n                                    np.average(spleen_low), \n                                    np.average(spleen_high)\n                                   ]\n   \n            \n            \n        \n    return submission_df    ","metadata":{"execution":{"iopub.status.busy":"2023-10-14T00:22:28.94663Z","iopub.execute_input":"2023-10-14T00:22:28.947232Z","iopub.status.idle":"2023-10-14T00:22:28.963356Z","shell.execute_reply.started":"2023-10-14T00:22:28.947201Z","shell.execute_reply":"2023-10-14T00:22:28.962497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = '/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images'\n\n#dataset = RSNATestDataset( test_images )\n#test_dataloader = DataLoader(dataset, batch_size=16, shuffle=True)\n\ncolumn_names = ['patient_id','bowel_healthy','bowel_injury','extravasation_healthy','extravasation_injury','kidney_healthy','kidney_low','kidney_high','liver_healthy','liver_low','liver_high','spleen_healthy','spleen_low','spleen_high']\nsubmission_df = pd.DataFrame(columns=column_names)\nimag_map = get_patient_images_paths(test_images)\nsubmission_dataframe = predictions(imag_map,submission_df,model,device) \n#submission_dataframe= predictions(model,device,test_dataloader,submission_df)\nsubmission_csv = submission_dataframe.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T00:22:28.965419Z","iopub.execute_input":"2023-10-14T00:22:28.966386Z","iopub.status.idle":"2023-10-14T00:22:29.263307Z","shell.execute_reply.started":"2023-10-14T00:22:28.966341Z","shell.execute_reply":"2023-10-14T00:22:29.262337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_dataframe","metadata":{"execution":{"iopub.status.busy":"2023-10-14T00:22:29.485385Z","iopub.execute_input":"2023-10-14T00:22:29.485693Z","iopub.status.idle":"2023-10-14T00:22:29.503565Z","shell.execute_reply.started":"2023-10-14T00:22:29.485671Z","shell.execute_reply":"2023-10-14T00:22:29.502438Z"},"trusted":true},"execution_count":null,"outputs":[]}]}