{"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":"none","dataSources":[{"sourceId":52254,"databundleVersionId":6863140,"sourceType":"competition"},{"sourceId":6376425,"sourceType":"datasetVersion","datasetId":3674352},{"sourceId":7189970,"sourceType":"datasetVersion","datasetId":4141765},{"sourceId":7196952,"sourceType":"datasetVersion","datasetId":4123452}],"dockerImageVersionId":30626,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install nibabel numpy pydicom tqdm --no-index --find-links=file:///kaggle/input/packages\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-18T05:11:33.334249Z","iopub.execute_input":"2023-12-18T05:11:33.335083Z","iopub.status.idle":"2023-12-18T05:11:48.240097Z","shell.execute_reply.started":"2023-12-18T05:11:33.335033Z","shell.execute_reply":"2023-12-18T05:11:48.238445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install torch --no-index --find-links=file:///kaggle/input/packages","metadata":{"execution":{"iopub.status.busy":"2023-12-18T05:11:48.242662Z","iopub.execute_input":"2023-12-18T05:11:48.243167Z","iopub.status.idle":"2023-12-18T05:12:00.668128Z","shell.execute_reply.started":"2023-12-18T05:11:48.243104Z","shell.execute_reply":"2023-12-18T05:12:00.666697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# import shutil\n# import pydicom\n# import nibabel as nib\n# import numpy as np\n# import zipfile\n# import pandas as pd\n# from scipy.ndimage import zoom\n# from sklearn.model_selection import train_test_split\n\n\n# # Function to resize NIfTI data\n# def resize_nifti(nifti_data, target_shape):\n#     factors = (target_shape[0] / nifti_data.shape[0],\n#                target_shape[1] / nifti_data.shape[1],\n#                target_shape[2] / nifti_data.shape[2])\n#     resized_data = zoom(nifti_data, factors, order=3)  # Cubic interpolation (higher quality)\n#     return resized_data\n\n# # Function to convert DICOM to resized NIfTI\n# def dicom_to_resized_nii(input_folder, output_folder, desired_shape,patientname):\n#     dicom_files = [os.path.join(input_folder, file) for file in os.listdir(input_folder) if file.endswith('.dcm')]\n\n#     if not dicom_files:\n#         return\n\n#     dicom_files.sort(key=lambda file: pydicom.dcmread(file).ImagePositionPatient[-1])\n\n#     ds = pydicom.dcmread(dicom_files[0])\n#     pixel_spacing = ds.PixelSpacing\n#     slice_thickness = ds.SliceThickness\n\n#     affine = np.eye(4)\n#     affine[0, 0] = pixel_spacing[1]\n#     affine[1, 1] = pixel_spacing[0]\n#     affine[2, 2] = slice_thickness\n\n#     volume = np.zeros((ds.Rows, ds.Columns, len(dicom_files)), dtype=ds.pixel_array.dtype)\n\n#     for i, dicom_file in enumerate(dicom_files):\n#         ds = pydicom.dcmread(dicom_file)\n#         volume[:, :, i] = ds.pixel_array\n\n#     nii_image = nib.Nifti1Image(volume, affine=affine)\n\n#     resized_data = resize_nifti(nii_image.get_fdata(), desired_shape)\n#     resized_affine = nii_image.affine\n\n#     resized_nii_image = nib.Nifti1Image(resized_data, affine=resized_affine)\n\n#     if not os.path.exists(output_folder):\n#         os.makedirs(output_folder)\n#     output_nii_path = os.path.join(output_folder, f\"{os.path.basename(patientname)}.nii.gz\")\n#     nib.save(resized_nii_image, output_nii_path)\n\n#     print(f\"Converted, resized, and saved {len(dicom_files)} DICOM slices to a resized NIfTI image: {output_nii_path}\")\n#     return os.path.basename(patientname)\n# image_dir = '/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images'\n# series_df = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/test_series_meta.csv')\n# valid_df  = pd.read_csv( '/kaggle/input/rsna-2023-abdominal-trauma-detection/sample_submission.csv') \n# valid_id  = list(zip(series_df.patient_id, series_df.series_id))\n\n# root_folder = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images\"\n# output_folder = \"/kaggle/working/output_nii_images\"\n# desired_shape = (128,128,128)\n               \n# # Load or create the CSV file\n# csv_file_path = \"/kaggle/input/abdominal-trauma-records-2/conversion_records.csv\"\n# if os.path.exists(csv_file_path):\n#     conversion_records = pd.read_csv(csv_file_path)\n#     # Save the updated CSV file to kaggle/working directory\n#     csv_file_path_new = \"/kaggle/working/conversion_records.csv\"\n#     conversion_records.to_csv(csv_file_path_new, index=False)\n# else:\n#     conversion_records = pd.DataFrame(columns=[\"patient_id\"])\n\n# # Loop through subfolders and convert DICOM to NIfTI\n\n# # for subfolder in os.listdir(root_folder):\n# #     subfolder_path = os.path.join(root_folder, subfolder)\n    \n# #     if os.path.isdir(subfolder_path):\n# #         for onemore_subfolder in os.listdir(subfolder_path):\n# #             onemore_subfolder_path = os.path.join(subfolder_path, onemore_subfolder)\n# #             patient_id = subfolder+\"_\"+onemore_subfolder\n           \n\n# #             if os.path.isdir(onemore_subfolder_path) and patient_id not in conversion_records[\"patient_id\"].values:\n# #                     converted_patient_id = dicom_to_resized_nii(onemore_subfolder_path, output_folder, desired_shape,patient_id)\n# #                     if converted_patient_id:\n# #                         conversion_records.loc[len(conversion_records)] = [converted_patient_id]\n# # new_patient=[]             \n# for t,(patient_id, series_id) in enumerate(valid_id):\n        \n#     if series_id in [5842]:\n#             continue\n#     dcm_dir = f'{image_dir}/{patient_id}/{series_id}'\n#     print(dcm_dir)\n#     if os.path.exists(dcm_dir):\n#         print(dcm_dir)\n#         converted_patient_id = dicom_to_resized_nii(dcm_dir, output_folder, desired_shape,str(patient_id)+\"_\"+str(series_id))\n            \n# # nifti_paths=['/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10004/21057','/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10004/51033']\n# # patient_ids=[10004]\n# # converted_patient_id = dicom_to_resized_nii('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10004/21057', output_folder, desired_shape,'10004_21057')\n# # converted_patient_id1 = dicom_to_resized_nii('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10004/51033', output_folder, desired_shape,'10004_51033')\n            \n# # Save the updated CSV file\n# conversion_records.to_csv(csv_file_path_new, index=False)\n\n# # Create a ZIP file containing the converted NIfTI images\n# # zip_filename = \"/kaggle/working/output_nii_images.zip\"\n# # with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:\n# #     for root, _, files in os.walk(output_folder):\n# #         for file in files:\n# #             file_path = os.path.join(root, file)\n# #             zipf.write(file_path, os.path.relpath(file_path, output_folder))\n# #             os.remove(file_path)  # Delete the file after adding to the ZIP folder\n\n# # # Close the ZIP file\n# # zipf.close()\n\n# print(\"ZIP file closed.\")\n\n# print(\"Conversion, resizing, CSV update, and ZIP creation completed.\")","metadata":{"execution":{"iopub.status.busy":"2023-12-15T12:59:01.522086Z","iopub.execute_input":"2023-12-15T12:59:01.522532Z","iopub.status.idle":"2023-12-15T12:59:10.340353Z","shell.execute_reply.started":"2023-12-15T12:59:01.522496Z","shell.execute_reply":"2023-12-15T12:59:10.338994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.nn import Transformer\n\ndef linear(input, weight, bias=None):\n    if bias is not None:\n        return torch.matmul(input, weight.t()) + bias\n    else:\n        return torch.matmul(input, weight.t())\n\ndef layer_norm(input, normalized_shape, weight=None, bias=None, eps=1e-5):\n    mean = input.mean(dim=-1, keepdim=True)\n    var = input.var(dim=-1, unbiased=False, keepdim=True)\n    \n    if weight is not None:\n        weight = weight.view(*input.shape[-len(normalized_shape):])\n    if bias is not None:\n        bias = bias.view(*input.shape[-len(normalized_shape):])\n    \n    input = (input - mean) / torch.sqrt(var + eps)\n    \n    if weight is not None:\n        input = input * weight\n    if bias is not None:\n        input = input + bias\n    \n    return input\n\nclass Transformer3DClassifier(nn.Module):\n    def __init__(self, input_shape, num_classes, num_layers=6, d_model=16, nhead=8, dim_feedforward=2048, dropout=0.1):\n        super(Transformer3DClassifier, self).__init__()\n        \n        self.d_model = d_model\n        d_in = input_shape[0] * input_shape[1] * input_shape[2]\n        self.embedding = nn.Linear(d_in, d_model)\n        \n        self.transformer = Transformer(\n            d_model=d_model,\n            nhead=nhead,\n            num_encoder_layers=num_layers,\n            dim_feedforward=dim_feedforward,\n            dropout=dropout\n        )\n        \n        self.fc = nn.Linear(d_model, num_classes)\n\n    def forward(self, x):\n        x = x.view(x.size(0), -1)\n        x = self.embedding(x)\n        x = x.unsqueeze(0)\n        tgt = torch.zeros(1, x.size(1), self.d_model).to(x.device)\n        output = self.transformer(x, tgt)\n        logits = self.fc(output)\n        logits = logits.unsqueeze(0)\n        return logits\n\nclass Custom3DViTModel(nn.Module):\n    def __init__(self, in_channels, num_classes, num_classes_segmentation, batch_size):\n        super(Custom3DViTModel, self).__init__()\n        self.batch_size = batch_size\n        self.num_classes = num_classes\n        \n        self.vit_backbone = Transformer3DClassifier(\n            input_shape=(128, 128, 128),\n            num_classes=num_classes\n        )\n\n        self.classification_head = nn.Sequential(\n            nn.Linear(self.vit_backbone.d_model, num_classes)\n        )\n\n        self.segmentation_head = nn.Sequential(\n            nn.Conv3d(1, num_classes_segmentation, kernel_size=1),\n            nn.Sigmoid()\n        )\n\n    def forward(self, x, segmentation_mask):\n        x = x.to(device)\n        if segmentation_mask is not None:\n            segmentation_mask = segmentation_mask.to(device)\n\n        features = self.vit_backbone(x)\n        features = features.to(device)\n        print(features.size())\n        classification_output = features.view(-1, self.num_classes)\n        \n        segmentation_output = self.segmentation_head(x)\n        segmentation_output = nn.functional.interpolate(segmentation_output, size=segmentation_mask.shape[2:], mode='trilinear')\n        segmentation_output = segmentation_output * segmentation_mask\n\n        return classification_output, segmentation_output\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nbatch_size = 32\n\ndef get_model():\n    return Custom3DViTModel(3, 14, 5, batch_size)\n\ndef run(index):\n    model = get_model()\n    model = model.to(device)\n\n    batch_images = torch.randn(32, 1, 128, 128, 128)\n    batch_segmentation_masks = torch.randn(32, 1, 128, 128, 128)\n\n    batch_images = batch_images.to(device)\n    batch_segmentation_masks = batch_segmentation_masks.to(device)\n\n    classification_outputs, segmentation_outputs = model(batch_images, batch_segmentation_masks)\n\nif __name__ == '__main__':\n    run(0)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T05:12:00.670143Z","iopub.execute_input":"2023-12-18T05:12:00.670511Z","iopub.status.idle":"2023-12-18T05:12:15.158753Z","shell.execute_reply.started":"2023-12-18T05:12:00.670477Z","shell.execute_reply":"2023-12-18T05:12:15.157757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# import pandas as pd\n# import numpy as np\n# import nibabel as nib\n# import torch\n# import torch.nn as nn\n# import torch.optim as optim\n# from torch.utils.data import Dataset, DataLoader\n# from torchvision import transforms\n# from scipy.ndimage import zoom\n# import pydicom\n\n# # Function to resize NIfTI data\n# def resize_nifti(nifti_data, target_shape):\n#     factors = (target_shape[0] / nifti_data.shape[0],\n#                target_shape[1] / nifti_data.shape[1],\n#                target_shape[2] / nifti_data.shape[2])\n#     resized_data = zoom(nifti_data, factors, order=3)  # Cubic interpolation (higher quality)\n#     return resized_data\n\n# # Function to convert DICOM to resized NIfTI\n# def dicom_to_resized_nii(input_folder, output_folder, desired_shape, patientname):\n#     dicom_files = [os.path.join(input_folder, file) for file in os.listdir(input_folder) if file.endswith('.dcm')]\n\n#     if not dicom_files:\n#         return\n\n#     dicom_files.sort(key=lambda file: pydicom.dcmread(file).ImagePositionPatient[-1])\n\n#     ds = pydicom.dcmread(dicom_files[0])\n#     pixel_spacing = ds.PixelSpacing\n#     slice_thickness = ds.SliceThickness\n\n#     affine = np.eye(4)\n#     affine[0, 0] = pixel_spacing[1]\n#     affine[1, 1] = pixel_spacing[0]\n#     affine[2, 2] = slice_thickness\n\n#     volume = np.zeros((ds.Rows, ds.Columns, len(dicom_files)), dtype=ds.pixel_array.dtype)\n\n#     for i, dicom_file in enumerate(dicom_files):\n#         ds = pydicom.dcmread(dicom_file)\n#         volume[:, :, i] = ds.pixel_array\n\n#     nii_image = nib.Nifti1Image(volume, affine=affine)\n\n#     # Inside dicom_to_resized_nii function, after resizing\n#     resized_data = resize_nifti(nii_image.get_fdata(), desired_shape)\n\n#     # Check for NaN values and handle them\n#     if np.isnan(resized_data).any():\n#         resized_data[np.isnan(resized_data)] = 0\n\n#     resized_affine = nii_image.affine\n\n#     resized_nii_image = nib.Nifti1Image(resized_data, affine=resized_affine)\n\n#     if not os.path.exists(output_folder):\n#         os.makedirs(output_folder)\n#     output_nii_path = os.path.join(output_folder, f\"{os.path.basename(patientname)}.nii.gz\")\n#     nib.save(resized_nii_image, output_nii_path)\n\n#     print(f\"Converted, resized, and saved {len(dicom_files)} DICOM slices to a resized NIfTI image: {output_nii_path}\")\n#     return os.path.basename(patientname)\n\n# root_folder = '/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images'\n\n# output_folder = \"/kaggle/working/output_nii_images\"\n# desired_shape = (128, 128, 128)\n\n# # Load or create the CSV file\n# csv_file_path = \"/kaggle/input/abdominal-trauma-records-2/conversion_records.csv\"\n# if os.path.exists(csv_file_path):\n#     conversion_records = pd.read_csv(csv_file_path)\n#     # Save the updated CSV file to kaggle/working directory\n#     csv_file_path_new = \"/kaggle/working/conversion_records.csv\"\n#     conversion_records.to_csv(csv_file_path_new, index=False)\n# else:\n#     conversion_records = pd.DataFrame(columns=[\"patient_id\"])\n\n# # Loop through subfolders and convert DICOM to NIfTI\n# for subfolder in os.listdir(root_folder):\n#     subfolder_path = os.path.join(root_folder, subfolder)\n\n#     if os.path.isdir(subfolder_path):\n#         # Assuming each patient folder contains 2D DICOM slices\n#         patient_name = dicom_to_resized_nii(subfolder_path, output_folder, desired_shape, subfolder)\n\n#         if patient_name:\n#             conversion_records.loc[len(conversion_records)] = [patient_name]\n\n# # Save the updated CSV file\n# conversion_records.to_csv(csv_file_path_new, index=False)\n\n# print(\"Conversion, resizing, CSV update, and ZIP creation completed.\")\n\n# def move_data_to_device(data, device):\n#     return data.to(torch.float32).to(device)\n\n# class CustomDataset(Dataset):\n#     def __init__(self, image_paths, transform=None):\n#         self.image_paths = image_paths\n#         self.transform = transform\n\n#     def __len__(self):\n#         return len(self.image_paths)\n\n#     def __getitem__(self, idx):\n#         image_path = self.image_paths[idx]\n\n#         # Load the 3D NIfTI image using nibabel\n#         image = nib.load(image_path).get_fdata()\n\n#         # Apply transformations if provided to the image\n#         if self.transform:\n#             image = self.transform(image)\n\n#         return image\n\n# # Assuming your model is already defined and loaded\n# # model = ...\n# model = torch.load('/kaggle/input/custom3dvit-model-weights/vit_abdominal (2).pth', map_location=device)\n# model.eval()\n\n# # Load the list of resized NIfTI paths\n# nifti_paths = [os.path.join(output_folder, file) for file in os.listdir(output_folder) if file.endswith('.nii.gz')]\n# patient_ids = [os.path.splitext(os.path.basename(file))[0].split('_')[0] for file in os.listdir(output_folder) if file.endswith('.nii.gz')]\n# print(\"thisis\",patient_ids)\n# print(\"Adf\",nifti_paths)\n# valid_df  = pd.read_csv( '/kaggle/input/rsna-2023-abdominal-trauma-detection/sample_submission.csv') \n# # Instantiate the test dataset\n\n# test_dataset = CustomDataset(nifti_paths, transform=None)  # You can add a transform if needed\n\n# # Instantiate the data loader\n# batch_size = 1\n# num_workers = 4\n# test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers)\n\n# # Move the model to the device (e.g., GPU)\n# device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n# model = model.to(device)\n\n# # Set the model to evaluation mode\n# model.eval()\n\n# # Loop through the test data and make predictions\n\n# valid_df  = pd.read_csv( '/kaggle/input/rsna-2023-abdominal-trauma-detection/sample_submission.csv') \n# predictions = []\n# with torch.no_grad():\n#     for  batch_images in test_loader:\n        \n#         batch_images = batch_images.to(torch.float32).to(device)\n       \n#         # Check for NaN values and handle them\n#         if torch.isnan(batch_images).any():\n#             batch_images[torch.isnan(batch_images)] = 0\n#         # Assuming batch_images has shape (batch_size, num_channels, height, width, depth)\n#         batch_images = batch_images.unsqueeze(0)  # Add a singleton dimension for batch\n\n#         # Create a dummy segmentapatition mask (assuming your model requires it)\n#         dummy_segmentation_mask = torch.zeros_like(batch_images)\n\n#         # Forward pass\n#         outputs, _ = model(batch_images, dummy_segmentation_mask)\n\n#         # Apply sigmoid activation to the outputs\n#         outputs = torch.sigmoid(outputs)\n        \n#         # Convert to numpy array and append to predictions list\n#         print(valid_df,\"valid_df\")\n#         predictions.append(outputs.cpu().numpy())\n\n# # Assuming batch_paths is a list of patient IDs\n\n# #         print(predictions,\"oredictions\")\n# #         valid_df['patient_id']=batch_paths\n# #         valid_df['bowel_healthy']=predictions[0][0]\n# #         valid_df['bowel_injury']=predictions[1]\n# #         valid_df['extravasation_healthy']=predictions[2]\n# #         valid_df['extravasation_injury']=predictions[3]\n# #         valid_df['kidney_healthy']=predictions[4]\n# #         valid_df['kidney_low']=predictions[5]\n# #         valid_df['kidney_high']=predictions[6]\n# #         valid_df['liver_healthy']=predictions[7]\n# #         valid_df['liver_low']=predictions[8]\n# #         valid_df['liver_high']=predictions[9]\n# #         valid_df['spleen_healthy']=predictions[10]\n# #         valid_df['spleen_low']=predictions[11]\n# #         valid_df['spleen_high']=predictions[12]\n        \n     \n# print(valid_df)\n# # Concatenate the predictions along the batch dimension\n# all_predictions = np.concatenate(predictions, axis=0)\n\n# print(all_predictions)\n# # After making predictions\n# if np.isnan(all_predictions).any():\n#     all_predictions[np.isnan(all_predictions)] = 0\n\n# # Print or use the predictions as needed\n# # print(all_predictions)\n# column_names = [\n#     'bowel_healthy',\n#     'bowel_injury',\n#     'extravasation_healthy',\n#     'extravasation_injury',\n#     'kidney_healthy',\n#     'kidney_low',\n#     'kidney_high',\n#     'liver_healthy',\n#     'liver_low',\n#     'liver_high',\n#     'spleen_healthy',\n#     'spleen_low',\n#     'spleen_high'\n# ]\n\n# # Iterate over predictions and update the DataFrame columns\n\n# for i, column_name in enumerate(column_names):\n#             valid_df[column_name] = [prediction[i] for prediction in all_predictions]\n#         # Iterate over predictionpatis and update the DataFrame columns\n# valid_df['patient_id']=patient_ids \n# valid_df=valid_df.groupby('patient_id').mean()\n# # class_names = [\"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\",\"any_injury\"]\n# # predictions_df = pd.DataFrame(data=all_predictions, columns=class_names)\n\n# # predictions_df = predictions_df.iloc[:, :-1]\n# # predictions_df.insert(0, \"patient_id\", patient_ids)\n# # predictions_df=predictions_df.groupby('patient_id').mean()\n# # print(predictions_df,\"presdicationsdfnad\")\n\n# # Save the DataFrame to a CSV file\n# # csv_output_path = \"/kaggle/working/submission.csv\"\n# valid_df.to_csv('submission.csv', index=False)\n# print(valid_df)\n# # print(f\"Predictions saved to: {csv_output_path}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-15T12:59:39.921333Z","iopub.execute_input":"2023-12-15T12:59:39.922194Z","iopub.status.idle":"2023-12-15T12:59:42.547401Z","shell.execute_reply.started":"2023-12-15T12:59:39.922145Z","shell.execute_reply":"2023-12-15T12:59:42.546005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image_dir = '/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images'\n# series_df = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/test_series_meta.csv')\n# valid_df  = pd.read_csv( '/kaggle/input/rsna-2023-abdominal-trauma-detection/sample_submission.csv') \n# valid_id  = list(zip(series_df.patient_id, series_df.series_id))\n# for t,(patient_id, series_id) in enumerate(valid_id):\n#     dcm_dir = f'{image_dir}/{patient_id}/{series_id}'","metadata":{"execution":{"iopub.status.busy":"2023-12-15T08:16:10.202477Z","iopub.execute_input":"2023-12-15T08:16:10.203028Z","iopub.status.idle":"2023-12-15T08:16:10.677095Z","shell.execute_reply.started":"2023-12-15T08:16:10.202979Z","shell.execute_reply":"2023-12-15T08:16:10.675655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# import shutil\n# import pydicom\n# import nibabel as nib\n# import numpy as np\n# import zipfile\n\n# from torch.utils.data import Dataset, DataLoader\n# import pandas as pd\n# from scipy.ndimage import zoom\n# from sklearn.model_selection import train_test_split\n\n# def move_data_to_device(data, device):\n#     return data.to(torch.float32).to(device)\n\n# class CustomDataset(Dataset):\n#     def __init__(self, image_paths, transform=None):\n#         self.image_paths = image_paths\n#         self.transform = transform\n\n#     def __len__(self):\n#         return len(self.image_paths)\n\n#     def __getitem__(self, idx):\n#         image_path = self.image_paths[idx]\n\n#         # Load the 3D NIfTI image using nibabel\n#         image = nib.load(image_path).get_fdata()\n\n#         # Apply transformations if provided to the image\n#         if self.transform:\n#             image = self.transform(image)\n\n#         return image\n# # Function to resize NIfTI data\n# def resize_nifti(nifti_data, target_shape):\n#     factors = (target_shape[0] / nifti_data.shape[0],\n#                target_shape[1] / nifti_data.shape[1],\n#                target_shape[2] / nifti_data.shape[2])\n#     resized_data = zoom(nifti_data, factors, order=3)  # Cubic interpolation (higher quality)\n#     return resized_data\n\n# # Function to convert DICOM to resized NIfTI\n# def dicom_to_resized_nii(input_folder, output_folder, desired_shape,patientname):\n#     dicom_files = [os.path.join(input_folder, file) for file in os.listdir(input_folder) if file.endswith('.dcm')]\n\n#     if not dicom_files:\n#         return\n\n#     dicom_files.sort(key=lambda file: pydicom.dcmread(file).ImagePositionPatient[-1])\n\n#     ds = pydicom.dcmread(dicom_files[0])\n#     pixel_spacing = ds.PixelSpacing\n#     slice_thickness = ds.SliceThickness\n\n#     affine = np.eye(4)\n#     affine[0, 0] = pixel_spacing[1]\n#     affine[1, 1] = pixel_spacing[0]\n#     affine[2, 2] = slice_thickness\n\n#     volume = np.zeros((ds.Rows, ds.Columns, len(dicom_files)), dtype=ds.pixel_array.dtype)\n\n#     for i, dicom_file in enumerate(dicom_files):\n#         ds = pydicom.dcmread(dicom_file)\n#         volume[:, :, i] = ds.pixel_array\n\n#     nii_image = nib.Nifti1Image(volume, affine=affine)\n\n#     resized_data = resize_nifti(nii_image.get_fdata(), desired_shape)\n#     resized_affine = nii_image.affine\n\n#     resized_nii_image = nib.Nifti1Image(resized_data, affine=resized_affine)\n\n#     if not os.path.exists(output_folder):\n#         os.makedirs(output_folder)\n#     output_nii_path = os.path.join(output_folder, f\"{os.path.basename(patientname)}.nii.gz\")\n#     nib.save(resized_nii_image, output_nii_path)\n\n#     print(f\"Converted, resized, and saved {len(dicom_files)} DICOM slices to a resized NIfTI image: {output_nii_path}\")\n#     return os.path.basename(patientname)\n# image_dir = '/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images'\n# series_df = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/test_series_meta.csv')\n# valid_df  = pd.read_csv( '/kaggle/input/rsna-2023-abdominal-trauma-detection/sample_submission.csv') \n# valid_id  = list(zip(series_df.patient_id, series_df.series_id))\n# batch_size = 1\n# num_workers = 4\n# root_folder = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images\"\n# output_folder = \"/kaggle/working/output_nii_images\"\n# desired_shape = (128,128,128)\n               \n# # Load or create the CSV file\n# csv_file_path = \"/kaggle/input/abdominal-trauma-records-2/conversion_records.csv\"\n# if os.path.exists(csv_file_path):\n#     conversion_records = pd.read_csv(csv_file_path)\n#     # Save the updated CSV file to kaggle/working directory\n#     csv_file_path_new = \"/kaggle/working/conversion_records.csv\"\n#     conversion_records.to_csv(csv_file_path_new, index=False)\n# else:\n#     conversion_records = pd.DataFrame(columns=[\"patient_id\"])\n \n# device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n# try:\n\n#     model = torch.load('/kaggle/input/custom3dvit-model-weights/vit_abdominal (2).pth', map_location=device)\n#     model = model.to(device)\n#     model.eval()\n# except Exception as e:\n#         print(f\"Could not load model from {model_path}: {e}\")\n# patient_ids=[]\n# # Set the model to evaluation mode\n\n# predictions=[]\n# for t,(patient_id, series_id) in enumerate(valid_id):\n#     try:    \n#         if series_id in [5842]:\n#             predictions.append([1,0,1,0,1,0,0,1,0,0,1,0,0,1])\n#             continue\n#         dcm_dir = f'{image_dir}/{patient_id}/{series_id}'\n#         print(dcm_dir)\n#         if os.path.exists(dcm_dir):\n#             print(dcm_dir)\n#             converted_patient_id = dicom_to_resized_nii(dcm_dir, output_folder, desired_shape,str(patient_id)+\"_\"+str(series_id))\n        \n#             batch_path=[os.path.join(output_folder,str(patient_id)+\"_\"+str(series_id)+\".nii.gz\" )]\n#             print(\"pat\",batch_path)\n#             if os.path.exists(batch_path[0]):\n#                 test_dataset=CustomDataset(batch_path,transform=None)\n#                 test_loader = DataLoader(test_dataset, batch_size=1, shuffle=False, num_workers=num_workers)\n#                 patient_ids.append(patient_id)\n#                 with torch.no_grad():\n#                     for  batch_images in test_loader:\n        \n#                         batch_images = batch_images.to(torch.float32).to(device)\n       \n#         # Check for NaN values and handle them\n#                         if torch.isnan(batch_images).any():\n#                             batch_images[torch.isnan(batch_images)] = 0\n#         # Assuming batch_images has shape (batch_size, num_channels, height, width, depth)\n#                         batch_images = batch_images.unsqueeze(0)  # Add a singleton dimension for batch\n\n#         # Create a dummy segmentapatition mask (assuming your model requires it)\n#                         dummy_segmentation_mask = torch.zeros_like(batch_images)\n\n#         # Forward pass\n#                         outputs, _ = model(batch_images, dummy_segmentation_mask)\n\n#         # Apply sigmoid activation to the outputs\n#                         outputs = torch.sigmoid(outputs)\n        \n#         # Convert to numpy array and append to predictions list\n#                         print(valid_df,\"valid_df\")\n#                         predictions.append(outputs.cpu().numpy())\n#                         print(predictions)\n#                         os.remove(batch_path[0])\n#                         batch_path=[]\n            \n            \n#     except Exception as e:\n#         print(f\"Could not load model from {model_path}: {e}\")\n                    \n# # print(valid_df)\n# # Concatenate the predictions along the batch dimension\n# all_predictions = np.concatenate(predictions, axis=0)\n\n# print(all_predictions)\n# # After making predictions\n# if np.isnan(all_predictions).any():\n#     all_predictions[np.isnan(all_predictions)] = 0\n\n# # Print or use the predictions as needed\n# # print(all_predictions)\n# column_names = [\n#     'bowel_healthy',\n#     'bowel_injury',\n#     'extravasation_healthy',\n#     'extravasation_injury',\n#     'kidney_healthy',\n#     'kidney_low',\n#     'kidney_high',\n#     'liver_healthy',\n#     'liver_low',\n#     'liver_high',\n#     'spleen_healthy',\n#     'spleen_low',\n#     'spleen_high'\n# ]\n\n# # Iterate over predictions and update the DataFrame columns\n\n# for i, column_name in enumerate(column_names):\n#             valid_df[column_name] = [prediction[i] for prediction in all_predictions]\n#         # Iterate over predictionpatis and update the DataFrame columns\n# valid_df['patient_id']=patient_ids \n# valid_df=valid_df.groupby('patient_id').mean()\n\n# # csv_output_path = \"/kaggle/working/submission.csv\"\n# valid_df.to_csv('submission.csv', index=False)\n# print(valid_df)\n# # print(f\"Predictions saved to: {csv_output_path}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-16T06:18:18.539373Z","iopub.execute_input":"2023-12-16T06:18:18.539809Z","iopub.status.idle":"2023-12-16T06:18:26.951497Z","shell.execute_reply.started":"2023-12-16T06:18:18.539769Z","shell.execute_reply":"2023-12-16T06:18:26.950331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n\n# # Specify the path to the folder you want to inspect\n# folder_path = \"/kaggle/working/output_nii_images\"\n\n# # List all files in the folder\n# files_in_folder = os.listdir(folder_path)\n\n# # Print the list of files\n# print(\"Files in the folder:\")\n# for file_name in files_in_folder:\n#     print(file_name)","metadata":{"execution":{"iopub.status.busy":"2023-12-16T06:19:23.803942Z","iopub.execute_input":"2023-12-16T06:19:23.804409Z","iopub.status.idle":"2023-12-16T06:19:23.811103Z","shell.execute_reply.started":"2023-12-16T06:19:23.804366Z","shell.execute_reply":"2023-12-16T06:19:23.810234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\nimport pydicom\nimport nibabel as nib\nimport numpy as np\nimport zipfile\nimport torch\nfrom torch.utils.data import Dataset, DataLoader\nimport pandas as pd\nfrom scipy.ndimage import zoom\nfrom sklearn.model_selection import train_test_split\n\ndef move_data_to_device(data, device):\n    return data.to(torch.float32).to(device)\n\nclass CustomDataset(Dataset):\n    def __init__(self, image_paths, transform=None):\n        self.image_paths = image_paths\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.image_paths)\n\n    def __getitem__(self, idx):\n        image_path = self.image_paths[idx]\n\n        # Load the 3D NIfTI image using nibabel\n        image = nib.load(image_path).get_fdata()\n\n        # Apply transformations if provided to the image\n        if self.transform:\n            image = self.transform(image)\n\n        return image\n\n# Function to resize NIfTI data\ndef resize_nifti(nifti_data, target_shape):\n    factors = (target_shape[0] / nifti_data.shape[0],\n               target_shape[1] / nifti_data.shape[1],\n               target_shape[2] / nifti_data.shape[2])\n    resized_data = zoom(nifti_data, factors, order=3)  # Cubic interpolation (higher quality)\n    return resized_data\n\n# Function to convert DICOM to resized NIfTI\ndef dicom_to_resized_nii(input_folder, output_folder, desired_shape, patient_name):\n    \"\"\"\n    Converts DICOM files to a resized NIfTI image.\n\n    Args:\n        input_folder: Path to the folder containing DICOM files.\n        output_folder: Path to the folder to save the resized NIfTI image.\n        desired_shape: Desired shape of the resized NIfTI image.\n        patient_name: Patient name for identification.\n\n    Returns:\n        Patient name if successful, None if no DICOM files found.\n    \"\"\"\n\n    dicom_files = [os.path.join(input_folder, file)\n                   for file in os.listdir(input_folder)\n                   if file.endswith('.dcm')]\n\n    if not dicom_files:\n        return None\n\n    dicom_files.sort(key=lambda file: pydicom.dcmread(file).ImagePositionPatient[-1])\n\n    try:\n        ds = pydicom.dcmread(dicom_files[0])\n        pixel_spacing = ds.PixelSpacing\n        slice_thickness = ds.SliceThickness\n    except Exception as e:\n        print(f\"Error reading DICOM file: {e}\")\n        return None\n\n    affine = np.eye(4)\n    affine[0, 0] = pixel_spacing[1]\n    affine[1, 1] = pixel_spacing[0]\n    affine[2, 2] = slice_thickness\n\n    volume = np.zeros((ds.Rows, ds.Columns, len(dicom_files)), dtype=ds.pixel_array.dtype)\n\n    for i, dicom_file in enumerate(dicom_files):\n        try:\n            ds = pydicom.dcmread(dicom_file)\n            volume[:, :, i] = ds.pixel_array\n        except Exception as e:\n            print(f\"Error reading DICOM file: {e}\")\n            return None\n\n    nii_image = nib.Nifti1Image(volume, affine=affine)\n    resized_data = resize_nifti(nii_image.get_fdata(), desired_shape)\n    resized_affine = nii_image.affine\n\n    resized_nii_image = nib.Nifti1Image(resized_data, affine=resized_affine)\n\n    if not os.path.exists(output_folder):\n        os.makedirs(output_folder)\n\n    output_nii_path = os.path.join(output_folder, f\"{patient_name}.nii.gz\")\n    nib.save(resized_nii_image, output_nii_path)\n\n    print(f\"Converted, resized, and saved {len(dicom_files)} DICOM slices to a resized NIfTI image: {output_nii_path}\")\n    return patient_name\n\n\n\n\nimage_dir = '/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images'\nprint(image_dir)\nseries_df = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/test_series_meta.csv')\nvalid_df = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/sample_submission.csv')\n\nvalid_id = list(zip(series_df.patient_id, series_df.series_id))\nprint(valid_id)\nbatch_size = 1\nnum_workers = 4\nroot_folder = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images\"\n# output_folder = \"/kaggle/working/output_nii_images\"\noutput_folder=\"/tmp/Dataset/rsna-atd\"\n\ndesired_shape = (128, 128, 128)\n\n# Load or create the CSV file\ncsv_file_path = \"/kaggle/input/abdominal-trauma-records-2/conversion_records.csv\"\nif os.path.exists(csv_file_path):\n    conversion_records = pd.read_csv(csv_file_path)\n    # Save the updated CSV file to kaggle/working directory\n    csv_file_path_new = \"/kaggle/working/conversion_records.csv\"\n    conversion_records.to_csv(csv_file_path_new, index=False)\nelse:\n    conversion_records = pd.DataFrame(columns=[\"patient_id\"])\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\ntry:\n    model_path = '/kaggle/input/custom3dvit-model-weights/vit_abdominal (2).pth'\n    model = torch.load(model_path, map_location=device)\n    model = model.to(device)\n    model.eval()\nexcept Exception as e:\n    print(f\"Could not load model from {model_path}: {e}\")\n    \n\npatient_ids = []\n# Set the model to evaluation mode\npredictions = []\n\nfor t, (patient_id, series_id) in enumerate(valid_id):\n    try:\n        print(patient_id,series_id)\n        if series_id in [5842]:\n            predictions.append([1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1])\n            continue\n\n        dcm_dir = f'{image_dir}/{patient_id}/{series_id}'\n        print(dcm_dir)\n\n        if os.path.exists(dcm_dir):\n            print(dcm_dir)\n            converted_patient_id = dicom_to_resized_nii(dcm_dir, output_folder, desired_shape, str(patient_id) + \"_\" + str(series_id))\n\n            batch_path = [os.path.join(output_folder, str(patient_id) + \"_\" + str(series_id) + \".nii.gz\")]\n            print(\"pat\", batch_path)\n\n            if os.path.exists(batch_path[0]):\n                test_dataset = CustomDataset(batch_path, transform=None)\n                test_loader = DataLoader(test_dataset, batch_size=1, shuffle=False, num_workers=num_workers)\n                patient_ids.append(patient_id)\n\n                with torch.no_grad():\n                    for batch_images in test_loader:\n                        batch_images = move_data_to_device(batch_images, device)\n\n                        # Check for NaN values and handle them\n                        if torch.isnan(batch_images).any():\n                            batch_images[torch.isnan(batch_images)] = 0\n\n                        # Assuming batch_images has shape (batch_size, num_channels, height, width, depth)\n                        batch_images = batch_images.unsqueeze(0)  # Add a singleton dimension for batch\n\n                        # Create a dummy segmentation mask (assuming your model requires it)\n                        dummy_segmentation_mask = torch.zeros_like(batch_images)\n\n                        # Forward pass\n                        outputs, _ = model(batch_images, dummy_segmentation_mask)\n\n                        # Apply sigmoid activation to the outputs\n                        outputs = torch.sigmoid(outputs)\n\n                        # Convert to numpy array and append to predictions list\n                        print(valid_df, \"valid_df\")\n                        predictions.append(outputs.cpu().numpy())\n                        print(predictions)\n\n                        os.remove(batch_path[0])\n                        batch_path = []\n\n    except Exception as e:\n        print(f\"Error processing {patient_id}-{series_id}: {e}\")\n        continue\n\n# Concatenate the predictions along the batch dimension\nall_predictions = np.concatenate(predictions, axis=0)\n\nprint(all_predictions)\n\n# After making predictions\nif np.isnan(all_predictions).any():\n    all_predictions[np.isnan(all_predictions)] = 0\n\n# Print or use the predictions as needed\ncolumn_names = [\n    'bowel_healthy',\n    'bowel_injury',\n    'extravasation_healthy',\n    'extravasation_injury',\n    'kidney_healthy',\n    'kidney_low',\n    'kidney_high',\n    'liver_healthy',\n    'liver_low',\n    'liver_high',\n    'spleen_healthy',\n    'spleen_low',\n    'spleen_high'\n]\n\n# Iterate over predictions and update the DataFrame columns\nfor i, column_name in enumerate(column_names):\n    valid_df[column_name] = [prediction[i] for prediction in all_predictions]\n\n# Iterate over predictions and update the DataFrame columns\nvalid_df['patient_id'] = patient_ids\nvalid_df = valid_df.groupby('patient_id').mean()\n\n# Save the submission.csv file\nvalid_df.to_csv('submission.csv', index=False)\nprint(valid_df)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T05:15:19.963466Z","iopub.execute_input":"2023-12-18T05:15:19.9642Z","iopub.status.idle":"2023-12-18T05:15:32.145969Z","shell.execute_reply.started":"2023-12-18T05:15:19.964148Z","shell.execute_reply":"2023-12-18T05:15:32.144481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-12-16T18:03:26.728509Z","iopub.execute_input":"2023-12-16T18:03:26.729076Z","iopub.status.idle":"2023-12-16T18:03:35.662955Z","shell.execute_reply.started":"2023-12-16T18:03:26.729031Z","shell.execute_reply":"2023-12-16T18:03:35.659926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}