{"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":"gpu","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":30589,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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-14T11:53:54.889811Z","iopub.execute_input":"2023-12-14T11:53:54.890075Z","iopub.status.idle":"2023-12-14T11:54:07.383977Z","shell.execute_reply.started":"2023-12-14T11:53:54.89005Z","shell.execute_reply":"2023-12-14T11:54:07.383037Z"},"trusted":true},"execution_count":1,"outputs":[{"name":"stdout","text":"Looking in links: file:///kaggle/input/packages\nRequirement already satisfied: nibabel in /opt/conda/lib/python3.10/site-packages (5.1.0)\nRequirement already satisfied: numpy in /opt/conda/lib/python3.10/site-packages (1.24.3)\nRequirement already satisfied: pydicom in /opt/conda/lib/python3.10/site-packages (2.4.3)\nRequirement already satisfied: tqdm in /opt/conda/lib/python3.10/site-packages (4.66.1)\nRequirement already satisfied: packaging>=17 in /opt/conda/lib/python3.10/site-packages (from nibabel) (21.3)\nRequirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /opt/conda/lib/python3.10/site-packages (from packaging>=17->nibabel) (3.0.9)\n","output_type":"stream"}]},{"cell_type":"code","source":"# !pip install gdcm\n# !pip install pylibjpeg\n# !pip install pylibjpeg-libjpeg\n# !pip install --upgrade pydicom\n# !pip install --upgrade pylibjpeg pylibjpeg-libjpeg pylibjpeg-openjpeg\n# !pip install pipdeptree\n!pip install pydicom==2.4.2\n!pip install --upgrade nibabel\n!pip install nibabel==3.2.1\n!pip install nibabel numpy scipy\n","metadata":{"execution":{"iopub.status.busy":"2023-12-10T15:18:33.54613Z","iopub.execute_input":"2023-12-10T15:18:33.546849Z","iopub.status.idle":"2023-12-10T15:19:46.36544Z","shell.execute_reply.started":"2023-12-10T15:18:33.546813Z","shell.execute_reply":"2023-12-10T15:19:46.364358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install torch --no-index --find-links=file:///kaggle/input/packages\n","metadata":{"execution":{"iopub.status.busy":"2023-12-14T11:54:07.386093Z","iopub.execute_input":"2023-12-14T11:54:07.386565Z","iopub.status.idle":"2023-12-14T11:54:18.660126Z","shell.execute_reply.started":"2023-12-14T11:54:07.386526Z","shell.execute_reply":"2023-12-14T11:54:18.659181Z"},"trusted":true},"execution_count":2,"outputs":[{"name":"stdout","text":"Looking in links: file:///kaggle/input/packages\nRequirement already satisfied: torch in /opt/conda/lib/python3.10/site-packages (2.0.0)\nRequirement already satisfied: filelock in /opt/conda/lib/python3.10/site-packages (from torch) (3.12.2)\nRequirement already satisfied: typing-extensions in /opt/conda/lib/python3.10/site-packages (from torch) (4.5.0)\nRequirement already satisfied: sympy in /opt/conda/lib/python3.10/site-packages (from torch) (1.12)\nRequirement already satisfied: networkx in /opt/conda/lib/python3.10/site-packages (from torch) (3.1)\nRequirement already satisfied: jinja2 in /opt/conda/lib/python3.10/site-packages (from torch) (3.1.2)\nRequirement already satisfied: MarkupSafe>=2.0 in /opt/conda/lib/python3.10/site-packages (from jinja2->torch) (2.1.3)\nRequirement already satisfied: mpmath>=0.19 in /opt/conda/lib/python3.10/site-packages (from sympy->torch) (1.3.0)\n","output_type":"stream"}]},{"cell_type":"code","source":"import os\nimport shutil\nimport pydicom\nimport nibabel as nib\nimport numpy as np\nimport zipfile\nimport pandas as pd\nfrom scipy.ndimage import zoom\nfrom sklearn.model_selection import train_test_split\n\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,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\nroot_folder = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images\"\noutput_folder = \"/kaggle/working/output_nii_images\"\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\n# Loop through subfolders and convert DICOM to NIfTI\ncount = 0\nfor 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                        count += 1\n                        if count == 150:\n                            break\n            if count == 150:\n                    break\n        if count == 150:\n                break\n                        \n# Save the updated CSV file\nconversion_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\nprint(\"ZIP file closed.\")\n\nprint(\"Conversion, resizing, CSV update, and ZIP creation completed.\")\n","metadata":{"execution":{"iopub.status.busy":"2023-12-14T04:18:48.647584Z","iopub.execute_input":"2023-12-14T04:18:48.647963Z","iopub.status.idle":"2023-12-14T04:18:55.404604Z","shell.execute_reply.started":"2023-12-14T04:18:48.647931Z","shell.execute_reply":"2023-12-14T04:18:55.403645Z"},"trusted":true},"execution_count":39,"outputs":[{"name":"stdout","text":"Converted, resized, and saved 1 DICOM slices to a resized NIfTI image: /kaggle/working/output_nii_images/63706_39279.nii.gz\nConverted, resized, and saved 1 DICOM slices to a resized NIfTI image: /kaggle/working/output_nii_images/50046_24574.nii.gz\nConverted, resized, and saved 1 DICOM slices to a resized NIfTI image: /kaggle/working/output_nii_images/48843_62825.nii.gz\nZIP file closed.\nConversion, resizing, CSV update, and ZIP creation completed.\n","output_type":"stream"}]},{"cell_type":"code","source":"import pandas as pd\nimport torch\nimport os\nfrom torch.utils.data import DataLoader\nfrom tqdm import tqdm\nimport nibabel as nib  # Assuming you have NIfTI files and using nibabel to load them\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n# Create a DataFrame for predictions\ncolumns = [\n    \"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\nsubmission_df = pd.DataFrame(columns=columns)\n\n# Set the path to the test folder\ntest_folder = \"/kaggle/working/output_nii_images\"\n\n# Load the model\nmodel = torch.load('/kaggle/input/custom3dvit-model-weights/vit_abdominal (2).pth')\nmodel.eval()\n\n# Iterate through the files in the test folder\nfor file_name in os.listdir(test_folder):\n    if file_name.endswith(\".nii\"):\n        file_path = os.path.join(test_folder, file_name)\n        names = file_name.split(\"_\")\n        patient_id = names[0]\n        print(patient_id)\n        # Load the NIfTI image using nibabel\n        nifti_img = nib.load(file_path)\n        img_data = nifti_img.get_fdata()\n\n        # Preprocess the image data as needed (e.g., normalization)\n\n        # Convert NumPy array to PyTorch tensor\n        img_tensor = torch.from_numpy(img_data).unsqueeze(0).unsqueeze(0).float()\n\n        # Assuming mask is a 3D NumPy array with size (128, 128, 128), create a PyTorch tensor\n        mask_tensor = torch.zeros((1, 1, 128, 128, 128), dtype=torch.float32)\n\n        with torch.no_grad():\n            img_tensor = img_tensor.to(device)\n            mask_tensor = mask_tensor.to(device)\n\n            # Assuming model is your PyTorch model\n            model_output = model(img_tensor, mask_tensor)\n\n        # Convert logits to probabilities using sigmoid activation\n        probabilities = torch.sigmoid(model_output).cpu().numpy().squeeze()\n        \n        # Process the model output as needed (e.g., save results, post-processing)\n\n        # Example: Save the result as a NIfTI file\n        result_nifti = nib.Nifti1Image(probabilities, nifti_img.affine)\n        result_file_path = os.path.join(\"/kaggle/working/results\", file_name.replace(\".nii\", \"_result.nii\"))\n        nib.save(result_nifti, result_file_path)\n\n        print(f\"Processed: {file_name}, Result saved to {result_file_path}\")\n\n        # Append the predictions to the submission DataFrame\n        submission_df = submission_df.append({\n            \"patient_id\": patient_id,\n            \"bowel_healthy\": probabilities[0],\n            \"bowel_injury\": probabilities[1],\n            \"extravasation_healthy\": probabilities[2],\n            \"extravasation_injury\": probabilities[3],\n            \"kidney_healthy\": probabilities[4],\n            \"kidney_low\": probabilities[5],\n            \"kidney_high\": probabilities[6],\n            \"liver_healthy\": probabilities[7],\n            \"liver_low\": probabilities[8],\n            \"liver_high\": probabilities[9],\n            \"spleen_healthy\": probabilities[10],\n            \"spleen_low\": probabilities[11],\n            \"spleen_high\": probabilities[12]\n        }, ignore_index=True)\n\n# Save the submission DataFrame to a CSV file\nsubmission_file_path = \"/kaggle/working/submission1.csv\"\nsubmission_df.to_csv(submission_file_path, index=False)\n\nprint(f\"Submission file created: {submission_file_path}\")\n#wokring one parital","metadata":{"execution":{"iopub.status.busy":"2023-12-14T04:18:58.597015Z","iopub.execute_input":"2023-12-14T04:18:58.597839Z","iopub.status.idle":"2023-12-14T04:18:58.763403Z","shell.execute_reply.started":"2023-12-14T04:18:58.597809Z","shell.execute_reply":"2023-12-14T04:18:58.76249Z"},"trusted":true},"execution_count":40,"outputs":[{"name":"stdout","text":"Submission file created: /kaggle/working/submission1.csv\n","output_type":"stream"}]},{"cell_type":"code","source":"import torch\nmodel = torch.load(\"/kaggle/input/custom3dvit-model-weights/vit_abdominal (1).pth\")\nmodel.eval()","metadata":{"execution":{"iopub.status.busy":"2023-12-14T03:33:15.942244Z","iopub.execute_input":"2023-12-14T03:33:15.942641Z","iopub.status.idle":"2023-12-14T03:33:17.87002Z","shell.execute_reply.started":"2023-12-14T03:33:15.94261Z","shell.execute_reply":"2023-12-14T03:33:17.868985Z"},"trusted":true},"execution_count":6,"outputs":[{"execution_count":6,"output_type":"execute_result","data":{"text/plain":"Custom3DViTModel(\n  (vit_backbone): Transformer3DClassifier(\n    (embedding): Linear(in_features=2097152, out_features=16, bias=True)\n    (transformer): Transformer(\n      (encoder): TransformerEncoder(\n        (layers): ModuleList(\n          (0-5): 6 x TransformerEncoderLayer(\n            (self_attn): MultiheadAttention(\n              (out_proj): NonDynamicallyQuantizableLinear(in_features=16, out_features=16, bias=True)\n            )\n            (linear1): Linear(in_features=16, out_features=2048, bias=True)\n            (dropout): Dropout(p=0.1, inplace=False)\n            (linear2): Linear(in_features=2048, out_features=16, bias=True)\n            (norm1): LayerNorm((16,), eps=1e-05, elementwise_affine=True)\n            (norm2): LayerNorm((16,), eps=1e-05, elementwise_affine=True)\n            (dropout1): Dropout(p=0.1, inplace=False)\n            (dropout2): Dropout(p=0.1, inplace=False)\n          )\n        )\n        (norm): LayerNorm((16,), eps=1e-05, elementwise_affine=True)\n      )\n      (decoder): TransformerDecoder(\n        (layers): ModuleList(\n          (0-5): 6 x TransformerDecoderLayer(\n            (self_attn): MultiheadAttention(\n              (out_proj): NonDynamicallyQuantizableLinear(in_features=16, out_features=16, bias=True)\n            )\n            (multihead_attn): MultiheadAttention(\n              (out_proj): NonDynamicallyQuantizableLinear(in_features=16, out_features=16, bias=True)\n            )\n            (linear1): Linear(in_features=16, out_features=2048, bias=True)\n            (dropout): Dropout(p=0.1, inplace=False)\n            (linear2): Linear(in_features=2048, out_features=16, bias=True)\n            (norm1): LayerNorm((16,), eps=1e-05, elementwise_affine=True)\n            (norm2): LayerNorm((16,), eps=1e-05, elementwise_affine=True)\n            (norm3): LayerNorm((16,), eps=1e-05, elementwise_affine=True)\n            (dropout1): Dropout(p=0.1, inplace=False)\n            (dropout2): Dropout(p=0.1, inplace=False)\n            (dropout3): Dropout(p=0.1, inplace=False)\n          )\n        )\n        (norm): LayerNorm((16,), eps=1e-05, elementwise_affine=True)\n      )\n    )\n    (fc): Linear(in_features=16, out_features=14, bias=True)\n  )\n  (classification_head): Sequential(\n    (0): Linear(in_features=16, out_features=14, bias=True)\n  )\n  (segmentation_head): Sequential(\n    (0): Conv3d(1, 1, kernel_size=(1, 1, 1), stride=(1, 1, 1))\n    (1): Sigmoid()\n  )\n)"},"metadata":{}}]},{"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        segmentation_mask = segmentation_mask.to(device)\n\n        features = self.vit_backbone(x)\n        features = features.to(device)\n        \n        classification_output = features.view(self.batch_size, 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-14T06:50:04.008952Z","iopub.execute_input":"2023-12-14T06:50:04.00937Z","iopub.status.idle":"2023-12-14T06:50:06.013429Z","shell.execute_reply.started":"2023-12-14T06:50:04.009335Z","shell.execute_reply":"2023-12-14T06:50:06.012481Z"},"trusted":true},"execution_count":4,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport nibabel as nib\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms\nfrom sklearn.metrics import accuracy_score, precision_recall_fscore_support, roc_auc_score\nfrom sklearn.model_selection import train_test_split\nfrom torchvision import models\nfrom scipy.ndimage import zoom\nimport torch.nn.functional as F\nfrom PIL import Image\n\ndef move_data_to_device(data, device):\n    return data.to(torch.float32).to(device)\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#         print(image.shape)\n#         print(\"Data shape *********:\", image.dtype)\n\n        # Load the segmentation mask if available\n       \n        # Apply transformations if provided to the image\n        if self.transform:\n            image = self.transform(image)\n\n        # Apply transformations if provided to the segmentation mask\n          \n        return image\n\nimport torch\nimport torch.nn as nn\nimport torchvision.models as models\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  # Update with the correct path\ntest_df=\"kaggle/working/output_nii_images\"  # Update with the correct path\n # Number of CPU cores to use for data loading\n\ndesired_shape = (128, 128, 128)\ndevice =torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n\n# Define transformations if needed\ntransform = transforms.Compose([\n    transforms.ToTensor(),  # Convert to tensor\n    # Add more transformations if necessary\n])\n\n# Load the CSV file\n# data = pd.read_csv(csv_file).head(4480)\n\n# Filter rows where the 'mask_path' column is not empty\n#data = data[pd.notna(data['mask_path'])]\n\n# Remove the extra space from the column name\n# data.columns = data.columns.str.strip()\n\n# Assuming 'data' is your DataFrame\n# data_length = len(data)\n# print(\"Length of DataFrame:\", data_length)\n# print(data)\n# print(data.index)\n\n# Split the data into training, validation, and test sets\n# train_data, temp_data = train_test_split(data, test_size=0.2, random_state=42)\n# val_data, test_data = train_test_split(temp_data, test_size=0.5, random_state=42)\n\n# Set the display option to show all rows\n# pd.set_option('display.max_rows', None)\n\n# index_values = train_data.index.values\n\n# chunk_size = 200  # You can adjust the chunk size\n# for i in range(0, len(index_values), chunk_size):\n#     print(index_values[i:i+chunk_size])\n\n# Reset the display option to its default value (if needed)\n# pd.reset_option('display.max_rows')\n\n# Extract file paths and labels from the data\n# train_paths = train_data['file_path'].values\n# train_mask_paths = train_data['mask_path'].values\n# train_labels = train_data[['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']].values\n\n# val_paths = val_data['file_path'].values\n# val_mask_paths = val_data['mask_path'].values\n# val_labels = val_data[['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']].values\n\n# test_paths = test_data['file_path'].values\n# test_mask_paths = test_data['mask_path'].values\n# test_labels = test_data[['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']].values\n\n# Instantiate the datasets\n# train_dataset = CustomDataset(train_paths, train_mask_paths, train_labels, transform=transform)\n# print('len of train_dataset', len(train_dataset))\n# val_dataset = CustomDataset(val_paths, val_mask_paths, val_labels, transform=transform)\ntest_dataset = CustomDataset(test_paths, transform=transform)\n\n# Instantiate the data loaders\n# train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=num_workers, drop_last=True)\n# print('train_loader', len(train_loader))\n# # train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers)\n# val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers)\n# test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers)\n# print(train_loader)\n# print(\"Indices:\", train_loader.index)  # Print the indices\n        \n# Instantiate the model with the appropriate number of classes for both classification and segmentation\n# in_channels = 1  # Input channels (e.g., for grayscale images or volumes)\n# num_classes_classification = 14  # Number of classes for classification\n# num_classes_segmentation = 1    # Number of classes for segmentation (change this according to your task)\nmodel = Custom3DViTModel(in_channels, num_classes_classification, \n                            num_classes_segmentation, batch_size)\n# Count the number of parameters\n# total_params = sum(p.numel() for p in model.parameters())\n# print(f\"Total Trainable Parameters: {total_params}\")\n# #model = model.to(device)\n# # model = get_model()\n# model = model.to(device)\n# # Define loss function and optimizer\n# criterion = nn.BCELoss()  # Binary Cross-Entropy loss\n# optimizer = optim.Adam(model.parameters(), lr=0.001)\n# classification_weights = torch.tensor([1, 2, 1, 2, 1, 2, 4, 1, 2, 4, 1, 2, 4, 6], dtype=torch.float32)\n# classification_criterion=nn.BCELoss()\n# segmentation_criterion = nn.BCELoss()\n# # Training loop\n# num_epochs = 10\n# epoch_count=0\n\nfor epoch in range(num_epochs):\n    model.train()\n    running_loss = 0.0\n    correct_train = 0\n    total_train = 0\n    \n    for batch_images, batch_segmentation_masks, batch_labels in train_loader:\n        optimizer.zero_grad()\n#         classification_weights = torch.tensor([1, 2, 1, 2, 1, 2, 4, 1, 2, 4, 1, 2, 4, 6], dtype=torch.float32)\n\n        # Move data to the GPU if available\n        batch_images = batch_images.to(torch.float32).to(device)\n        batch_segmentation_masks = batch_segmentation_masks.to(torch.float32).to(device)\n        batch_labels = batch_labels.to(torch.float32).to(device)\n\n        # Assuming batch_images has shape (batch_size, num_frames, num_channels, height, width)\n        batch_images = batch_images.unsqueeze(1)  # Add a singleton dimension for channels\n        batch_segmentation_masks = batch_segmentation_masks.unsqueeze(1)\n        print(\"batch_images shape and batch_segmentation_masks shape\", \n              batch_images.shape, batch_segmentation_masks.shape)\n        print(\"batch number: \",epoch_count)\n        epoch_count=epoch_count+1\n        \n#         model = model.to(device)\n        # Move data to the XLA device\n        \n        batch_images = batch_images.to(torch.float32)  # Convert to float32 if not already\n        batch_segmentation_masks = batch_segmentation_masks.to(torch.float32)  # Convert to float32 if not already\n\n        batch_images = move_data_to_device(batch_images,device)\n        batch_segmentation_masks = move_data_to_device(batch_segmentation_masks,device)\n        batch_labels = move_data_to_device(batch_labels,device)\n        \n        \n\n        # Forward pass\n        classification_outputs, segmentation_outputs = model(batch_images, batch_segmentation_masks)\n        print(\"..........................................................\")\n        \n        # Apply sigmoid activation to the classification outputs\n        classification_outputs = torch.sigmoid(classification_outputs)\n        \n        print(\"classification_outputs.shape, batch_labels shape\", \n              classification_outputs.shape, batch_labels.shape)\n        \n        \n        # Calculate binary cross-entropy loss for each class separately\n        losses = []\n        for class_index in range(num_classes_classification):\n            class_labels = batch_labels[:, class_index]  # Select labels for the current class\n            class_outputs = classification_outputs[:, class_index]  # Select model outputs for the current class\n            class_loss = classification_criterion(class_outputs, class_labels)\n#             print(class_loss)\n            losses.append(class_loss)\n\n        # Calculate the total loss as the sum of individual class losses\n#         print(sum(losses),\"sum of losses\")\n        total_loss = sum(losses)/num_classes_classification\n#         print(\"total classification loss\",total_loss)\n\n        # Check if segmentation mask is available\n#         if batch_segmentation_masks is not None:\n#             # Ensure that both input and target tensors are of type torch.float32\n#             batch_segmentation_masks = batch_segmentation_masks.to(torch.float32)\n            \n#             # Apply sigmoid activation to segmentation_outputs\n#             segmentation_outputs = torch.sigmoid(segmentation_outputs)\n#             segmentation_outputs = segmentation_outputs.to(torch.float32)\n\n            # Calculate segmentation loss\n#             segmentation_loss = segmentation_criterion(segmentation_outputs, batch_segmentation_masks)\n#             print(segmentation_loss)\n#             total_loss += segmentation_loss\n            \n\n        running_loss += total_loss.item()\n#         running_loss=running_loss/2\n#         print(running_loss,\"running_loss\")\n        \n        # Calculate accuracy for each class separately\n        accuracies = []\n        for class_index in range(num_classes_classification):\n            class_labels = batch_labels[:, class_index]  # Select labels for the current class\n            class_outputs = classification_outputs[:, class_index]  # Select model outputs for the current class\n            \n            # Calculate binary predictions based on a threshold (e.g., 0.5)\n            predicted = (class_outputs > 0.5).float()\n            \n            class_accuracy = accuracy_score(class_labels.cpu(), predicted.cpu())\n            accuracies.append(class_accuracy)\n        \n        # Calculate overall accuracy\n        batch_accuracy = sum(accuracies) / num_classes_classification\n        correct_train += batch_accuracy\n        total_train += 1\n        print(\"batch accuracy: \",batch_accuracy)\n        print(\"batch loss: \",total_loss)\n        \n        # Backpropagation and optimization\n        total_loss.backward()\n   \n        optimizer.step()\n\n    # Calculate and print average training accuracy and loss\n    avg_train_accuracy = correct_train / len(train_loader)\n    avg_train_loss = running_loss / len(train_loader)\n    \n    print(f\"Epoch [{epoch+1}/{num_epochs}]\")\n    print(f\"Train Accuracy: {avg_train_accuracy:.4f} | Train Loss: {avg_train_loss:.4f}\")\n","metadata":{},"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 pandas as pd\nfrom scipy.ndimage import zoom\nfrom sklearn.model_selection import train_test_split\nimport torch\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, 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\n# Load the trained model\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = torch.load('/kaggle/input/custom3dvit-model-weights/vit_abdominal (2).pth')\nmodel.eval()\n\n# Create a DataFrame for predictions\ncolumns = [\n    \"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\nsubmission_df = pd.DataFrame(columns=columns)\n\nroot_folder = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images\"\noutput_folder = \"/kaggle/working/output_nii_images\"\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_test.csv\"\n    conversion_records.to_csv(csv_file_path_new, index=False)\nelse:\n    conversion_records = pd.DataFrame(columns=[\"patient_id\"])\n\n# Loop through subfolders and convert DICOM to NIfTI\ncount = 0\nfor 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            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\n                    # Load the NIfTI image\n                    nifti_img = nib.load(os.path.join(output_folder, f\"{converted_patient_id}.nii.gz\"))\n                    img_data = nifti_img.get_fdata()\n\n                    # Convert NumPy array to PyTorch tensor\n                    img_tensor = torch.from_numpy(img_data).unsqueeze(0).unsqueeze(0).float()\n\n                    # Assuming mask is a 3D NumPy array with size (128, 128, 128), create a PyTorch tensor\n                    mask_tensor = torch.zeros((1, 1, 128, 128, 128), dtype=torch.float32)\n\n                    with torch.no_grad():\n                        img_tensor = img_tensor.to(device)\n                        mask_tensor = mask_tensor.to(device)\n\n                        # Assuming model is your PyTorch model\n                        model_output = model(img_tensor, mask_tensor)\n\n                    # Convert logits to probabilities using sigmoid activation\n                    probabilities = torch.sigmoid(model_output).cpu().numpy().squeeze()\n\n                    # Append the predictions to the submission DataFrame\n                    submission_df = submission_df.append({\n                        \"patient_id\": converted_patient_id,\n                        \"bowel_healthy\": probabilities[0],\n                        \"bowel_injury\": probabilities[1],\n                        \"extravasation_healthy\": probabilities[2],\n                        \"extravasation_injury\": probabilities[3],\n                        \"kidney_healthy\": probabilities[4],\n                        \"kidney_low\": probabilities[5],\n                        \"kidney_high\": probabilities[6],\n                        \"liver_healthy\": probabilities[7],\n                        \"liver_low\": probabilities[8],\n                        \"liver_high\": probabilities[9],\n                        \"spleen_healthy\": probabilities[10],\n                        \"spleen_low\": probabilities[11],\n                        \"spleen_high\": probabilities[12]\n                    }, ignore_index=True)\n\n                    count += 1\n                    if count == 150:\n                        break\n\n            if count == 150:\n                break\n\n        if count == 150:\n            break\n\n# Save the updated CSV file\nconversion_records.to_csv(csv_file_path_new, index=False)\n\n# Save the submission DataFrame to a CSV file\nsubmission_file_path = \"/kaggle/working/submission.csv\"\nsubmission_df.to_csv(submission_file_path, index=False)\n\nprint(\"Conversion, resizing, prediction, and submission completed.\")\n","metadata":{"execution":{"iopub.status.busy":"2023-12-14T06:48:55.295218Z","iopub.execute_input":"2023-12-14T06:48:55.295638Z","iopub.status.idle":"2023-12-14T06:49:01.032096Z","shell.execute_reply.started":"2023-12-14T06:48:55.295603Z","shell.execute_reply":"2023-12-14T06:49:01.03074Z"},"trusted":true},"execution_count":3,"outputs":[{"name":"stderr","text":"/opt/conda/lib/python3.10/site-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.16.5 and <1.23.0 is required for this version of SciPy (detected version 1.24.3\n  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n","output_type":"stream"},{"name":"stdout","text":"Converted, resized, and saved 1 DICOM slices to a resized NIfTI image: /kaggle/working/output_nii_images/63706_39279.nii.gz\n","output_type":"stream"},{"traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mRuntimeError\u001b[0m                              Traceback (most recent call last)","Cell \u001b[0;32mIn[3], line 120\u001b[0m\n\u001b[1;32m    117\u001b[0m     mask_tensor \u001b[38;5;241m=\u001b[39m mask_tensor\u001b[38;5;241m.\u001b[39mto(device)\n\u001b[1;32m    119\u001b[0m     \u001b[38;5;66;03m# Assuming model is your PyTorch model\u001b[39;00m\n\u001b[0;32m--> 120\u001b[0m     model_output \u001b[38;5;241m=\u001b[39m \u001b[43mmodel\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg_tensor\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmask_tensor\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    122\u001b[0m \u001b[38;5;66;03m# Convert logits to probabilities using sigmoid activation\u001b[39;00m\n\u001b[1;32m    123\u001b[0m probabilities \u001b[38;5;241m=\u001b[39m torch\u001b[38;5;241m.\u001b[39msigmoid(model_output)\u001b[38;5;241m.\u001b[39mcpu()\u001b[38;5;241m.\u001b[39mnumpy()\u001b[38;5;241m.\u001b[39msqueeze()\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1501\u001b[0m, in \u001b[0;36mModule._call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m   1496\u001b[0m \u001b[38;5;66;03m# If we don't have any hooks, we want to skip the rest of the logic in\u001b[39;00m\n\u001b[1;32m   1497\u001b[0m \u001b[38;5;66;03m# this function, and just call forward.\u001b[39;00m\n\u001b[1;32m   1498\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m (\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_backward_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_backward_pre_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_forward_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_forward_pre_hooks\n\u001b[1;32m   1499\u001b[0m         \u001b[38;5;129;01mor\u001b[39;00m _global_backward_pre_hooks \u001b[38;5;129;01mor\u001b[39;00m _global_backward_hooks\n\u001b[1;32m   1500\u001b[0m         \u001b[38;5;129;01mor\u001b[39;00m _global_forward_hooks \u001b[38;5;129;01mor\u001b[39;00m _global_forward_pre_hooks):\n\u001b[0;32m-> 1501\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mforward_call\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1502\u001b[0m \u001b[38;5;66;03m# Do not call functions when jit is used\u001b[39;00m\n\u001b[1;32m   1503\u001b[0m full_backward_hooks, non_full_backward_hooks \u001b[38;5;241m=\u001b[39m [], []\n","Cell \u001b[0;32mIn[2], line 83\u001b[0m, in \u001b[0;36mCustom3DViTModel.forward\u001b[0;34m(self, x, segmentation_mask)\u001b[0m\n\u001b[1;32m     80\u001b[0m x \u001b[38;5;241m=\u001b[39m x\u001b[38;5;241m.\u001b[39mto(device)\n\u001b[1;32m     81\u001b[0m segmentation_mask \u001b[38;5;241m=\u001b[39m segmentation_mask\u001b[38;5;241m.\u001b[39mto(device)\n\u001b[0;32m---> 83\u001b[0m features \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvit_backbone\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     84\u001b[0m features \u001b[38;5;241m=\u001b[39m features\u001b[38;5;241m.\u001b[39mto(device)\n\u001b[1;32m     86\u001b[0m classification_output \u001b[38;5;241m=\u001b[39m features\u001b[38;5;241m.\u001b[39mview(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mnum_classes)\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1501\u001b[0m, in \u001b[0;36mModule._call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m   1496\u001b[0m \u001b[38;5;66;03m# If we don't have any hooks, we want to skip the rest of the logic in\u001b[39;00m\n\u001b[1;32m   1497\u001b[0m \u001b[38;5;66;03m# this function, and just call forward.\u001b[39;00m\n\u001b[1;32m   1498\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m (\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_backward_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_backward_pre_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_forward_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_forward_pre_hooks\n\u001b[1;32m   1499\u001b[0m         \u001b[38;5;129;01mor\u001b[39;00m _global_backward_pre_hooks \u001b[38;5;129;01mor\u001b[39;00m _global_backward_hooks\n\u001b[1;32m   1500\u001b[0m         \u001b[38;5;129;01mor\u001b[39;00m _global_forward_hooks \u001b[38;5;129;01mor\u001b[39;00m _global_forward_pre_hooks):\n\u001b[0;32m-> 1501\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mforward_call\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1502\u001b[0m \u001b[38;5;66;03m# Do not call functions when jit is used\u001b[39;00m\n\u001b[1;32m   1503\u001b[0m full_backward_hooks, non_full_backward_hooks \u001b[38;5;241m=\u001b[39m [], []\n","Cell \u001b[0;32mIn[2], line 49\u001b[0m, in \u001b[0;36mTransformer3DClassifier.forward\u001b[0;34m(self, x)\u001b[0m\n\u001b[1;32m     48\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mforward\u001b[39m(\u001b[38;5;28mself\u001b[39m, x):\n\u001b[0;32m---> 49\u001b[0m     x \u001b[38;5;241m=\u001b[39m \u001b[43mx\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mview\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msize\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m     50\u001b[0m     x\u001b[38;5;241m=\u001b[39mx\u001b[38;5;241m.\u001b[39mreshape(\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m)\n\u001b[1;32m     51\u001b[0m     x \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39membedding(x)\n","\u001b[0;31mRuntimeError\u001b[0m: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead."],"ename":"RuntimeError","evalue":"view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.","output_type":"error"}]},{"cell_type":"code","source":"import pandas as pd\nimport torch\nimport os\nfrom torch.utils.data import DataLoader\nfrom tqdm import tqdm\nimport nibabel as nib  # Assuming you have NIfTI files and using nibabel to load them\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n# Create a DataFrame for predictions\ncolumns = [\n    \"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\",\"any_injury\"\n]\n\nsubmission_df = pd.DataFrame(columns=columns)\n\n# Set the path to the test folder\ntest_folder = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images\"\n\n# Load the model\nmodel = torch.load('/kaggle/input/custom3dvit-model-weights/vit_abdominal (2).pth')\nmodel.eval()\n\n# Iterate through the files in the test folder\nfor file_name in os.listdir(test_folder):\n    if file_name.endswith(\".nii\"):\n        file_path = os.path.join(test_folder, file_name)\n        names = file_name.split(\"_\")\n        patient_id = names[0]\n        print(patient_id)\n        # Load the NIfTI image using nibabel\n        nifti_img = nib.load(file_path)\n        img_data = nifti_img.get_fdata()\n\n        # Preprocess the image data as needed (e.g., normalization)\n\n        # Convert NumPy array to PyTorch tensor\n        img_tensor = torch.from_numpy(img_data).unsqueeze(0).unsqueeze(0).float()\n\n        # Assuming mask is a 3D NumPy array with size (128, 128, 128), create a PyTorch tensor\n        mask_tensor = torch.zeros((1, 1, 128, 128, 128), dtype=torch.float32)\n\n        with torch.no_grad():\n            img_tensor = img_tensor.to(device)\n            mask_tensor = mask_tensor.to(device)\n\n            # Assuming model is your PyTorch model\n            model_output = model(img_tensor, mask_tensor)\n\n        # Convert logits to probabilities using sigmoid activation\n        probabilities = torch.sigmoid(model_output).cpu().numpy().squeeze()\n        \n        # Process the model output as needed (e.g., save results, post-processing)\n\n        # Example: Save the result as a NIfTI file\n        result_nifti = nib.Nifti1Image(probabilities, nifti_img.affine)\n        result_file_path = os.path.join(\"/kaggle/working/results\", file_name.replace(\".nii\", \"_result.nii\"))\n        nib.save(result_nifti, result_file_path)\n\n        print(f\"Processed: {file_name}, Result saved to {result_file_path}\")\n\n        # Append the predictions to the submission DataFrame\n        submission_df = submission_df.append({\n            \"patient_id\": patient_id,\n            \"bowel_healthy\": probabilities[0],\n            \"bowel_injury\": probabilities[1],\n            \"extravasation_healthy\": probabilities[2],\n            \"extravasation_injury\": probabilities[3],\n            \"kidney_healthy\": probabilities[4],\n            \"kidney_low\": probabilities[5],\n            \"kidney_high\": probabilities[6],\n            \"liver_healthy\": probabilities[7],\n            \"liver_low\": probabilities[8],\n            \"liver_high\": probabilities[9],\n            \"spleen_healthy\": probabilities[10],\n            \"spleen_low\": probabilities[11],\n            \"spleen_high\": probabilities[12]\n        }, ignore_index=True)\n\n# Save the submission DataFrame to a CSV file\nsubmission_file_path = \"/kaggle/working/submission1.csv\"\nsubmission_df.to_csv(submission_file_path, index=False)\n\nprint(f\"Submission file created: {submission_file_path}\")\n#wokring one parital","metadata":{"execution":{"iopub.status.busy":"2023-12-14T07:10:03.486273Z","iopub.execute_input":"2023-12-14T07:10:03.486732Z","iopub.status.idle":"2023-12-14T07:10:06.910565Z","shell.execute_reply.started":"2023-12-14T07:10:03.486696Z","shell.execute_reply":"2023-12-14T07:10:06.9092Z"},"trusted":true},"execution_count":12,"outputs":[{"name":"stdout","text":"Converted, resized, and saved 1 DICOM slices to a resized NIfTI image: /kaggle/working/output_nii_images/50046_24574.nii.gz\n","output_type":"stream"},{"traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mRuntimeError\u001b[0m                              Traceback (most recent call last)","Cell \u001b[0;32mIn[12], line 153\u001b[0m\n\u001b[1;32m    150\u001b[0m     mask_tensor \u001b[38;5;241m=\u001b[39m mask_tensor\u001b[38;5;241m.\u001b[39mto(device)\n\u001b[1;32m    152\u001b[0m     \u001b[38;5;66;03m# Assuming model is your PyTorch model\u001b[39;00m\n\u001b[0;32m--> 153\u001b[0m     model_output \u001b[38;5;241m=\u001b[39m \u001b[43mmodel\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg_tensor\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmask_tensor\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    155\u001b[0m \u001b[38;5;66;03m# Convert logits to probabilities using sigmoid activation\u001b[39;00m\n\u001b[1;32m    156\u001b[0m probabilities \u001b[38;5;241m=\u001b[39m torch\u001b[38;5;241m.\u001b[39msigmoid(model_output)\u001b[38;5;241m.\u001b[39mcpu()\u001b[38;5;241m.\u001b[39mnumpy()\u001b[38;5;241m.\u001b[39msqueeze()\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1501\u001b[0m, in \u001b[0;36mModule._call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m   1496\u001b[0m \u001b[38;5;66;03m# If we don't have any hooks, we want to skip the rest of the logic in\u001b[39;00m\n\u001b[1;32m   1497\u001b[0m \u001b[38;5;66;03m# this function, and just call forward.\u001b[39;00m\n\u001b[1;32m   1498\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m (\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_backward_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_backward_pre_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_forward_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_forward_pre_hooks\n\u001b[1;32m   1499\u001b[0m         \u001b[38;5;129;01mor\u001b[39;00m _global_backward_pre_hooks \u001b[38;5;129;01mor\u001b[39;00m _global_backward_hooks\n\u001b[1;32m   1500\u001b[0m         \u001b[38;5;129;01mor\u001b[39;00m _global_forward_hooks \u001b[38;5;129;01mor\u001b[39;00m _global_forward_pre_hooks):\n\u001b[0;32m-> 1501\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mforward_call\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1502\u001b[0m \u001b[38;5;66;03m# Do not call functions when jit is used\u001b[39;00m\n\u001b[1;32m   1503\u001b[0m full_backward_hooks, non_full_backward_hooks \u001b[38;5;241m=\u001b[39m [], []\n","Cell \u001b[0;32mIn[4], line 82\u001b[0m, in \u001b[0;36mCustom3DViTModel.forward\u001b[0;34m(self, x, segmentation_mask)\u001b[0m\n\u001b[1;32m     79\u001b[0m x \u001b[38;5;241m=\u001b[39m x\u001b[38;5;241m.\u001b[39mto(device)\n\u001b[1;32m     80\u001b[0m segmentation_mask \u001b[38;5;241m=\u001b[39m segmentation_mask\u001b[38;5;241m.\u001b[39mto(device)\n\u001b[0;32m---> 82\u001b[0m features \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvit_backbone\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     83\u001b[0m features \u001b[38;5;241m=\u001b[39m features\u001b[38;5;241m.\u001b[39mto(device)\n\u001b[1;32m     85\u001b[0m classification_output \u001b[38;5;241m=\u001b[39m features\u001b[38;5;241m.\u001b[39mview(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbatch_size, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mnum_classes)\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1501\u001b[0m, in \u001b[0;36mModule._call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m   1496\u001b[0m \u001b[38;5;66;03m# If we don't have any hooks, we want to skip the rest of the logic in\u001b[39;00m\n\u001b[1;32m   1497\u001b[0m \u001b[38;5;66;03m# this function, and just call forward.\u001b[39;00m\n\u001b[1;32m   1498\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m (\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_backward_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_backward_pre_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_forward_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_forward_pre_hooks\n\u001b[1;32m   1499\u001b[0m         \u001b[38;5;129;01mor\u001b[39;00m _global_backward_pre_hooks \u001b[38;5;129;01mor\u001b[39;00m _global_backward_hooks\n\u001b[1;32m   1500\u001b[0m         \u001b[38;5;129;01mor\u001b[39;00m _global_forward_hooks \u001b[38;5;129;01mor\u001b[39;00m _global_forward_pre_hooks):\n\u001b[0;32m-> 1501\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mforward_call\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1502\u001b[0m \u001b[38;5;66;03m# Do not call functions when jit is used\u001b[39;00m\n\u001b[1;32m   1503\u001b[0m full_backward_hooks, non_full_backward_hooks \u001b[38;5;241m=\u001b[39m [], []\n","Cell \u001b[0;32mIn[4], line 49\u001b[0m, in \u001b[0;36mTransformer3DClassifier.forward\u001b[0;34m(self, x)\u001b[0m\n\u001b[1;32m     48\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mforward\u001b[39m(\u001b[38;5;28mself\u001b[39m, x):\n\u001b[0;32m---> 49\u001b[0m     x \u001b[38;5;241m=\u001b[39m \u001b[43mx\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mview\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msize\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m     50\u001b[0m     x \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39membedding(x)\n\u001b[1;32m     51\u001b[0m     x \u001b[38;5;241m=\u001b[39m x\u001b[38;5;241m.\u001b[39munsqueeze(\u001b[38;5;241m0\u001b[39m)\n","\u001b[0;31mRuntimeError\u001b[0m: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead."],"ename":"RuntimeError","evalue":"view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.","output_type":"error"}]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport nibabel as nib\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms\nfrom sklearn.metrics import accuracy_score, precision_recall_fscore_support, roc_auc_score\nfrom sklearn.model_selection import train_test_split\nfrom torchvision import models\nfrom scipy.ndimage import zoom\nimport torch.nn.functional as F\nfrom PIL import Image\n\nimport os\nimport shutil\nimport pydicom\nimport nibabel as nib\nimport numpy as np\nimport zipfile\nimport pandas as pd\nfrom scipy.ndimage import zoom\nfrom sklearn.model_selection import train_test_split\n\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,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\nroot_folder = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images\"\noutput_folder = \"/kaggle/working/output_nii_images\"\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\n# Loop through subfolders and convert DICOM to NIfTI\n\nfor 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                   \n                        \n# Save the updated CSV file\nconversion_records.to_csv(csv_file_path_new, index=False)\n\n\nprint(\"ZIP file closed.\")\n\nprint(\"Conversion, resizing, CSV update, and ZIP creation completed.\")\n\ndef move_data_to_device(data, device):\n    return data.to(torch.float32).to(device)\nclass CustomDataset(Dataset):\n    def __init__(self, image_paths, mask_paths, transform=None):\n        self.image_paths = image_paths\n        self.mask_paths = mask_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        mask_path = self.mask_paths[idx]\n\n        # Load the 3D NIfTI image using nibabel\n        image = nib.load(image_path).get_fdata()\n#         print(image.shape)\n#         print(\"Data shape *********:\", image.dtype)\n\n        # Load the segmentation mask if available\n        segmentation_mask = None\n        if pd.notna(mask_path):\n            segmentation_mask = nib.load(mask_path)\n            segmentation_mask_data = segmentation_mask.get_fdata()\n            resized_data = resize_nifti(segmentation_mask_data, desired_shape)\n            segmentation_mask_data_affine = segmentation_mask.affine\n            resized_affine = segmentation_mask_data_affine\n            segmentation_mask = nib.Nifti1Image(resized_data, affine=resized_affine).get_fdata()\n\n        # Apply transformations if provided to the image\n        if self.transform:\n            image = self.transform(image)\n\n        # Apply transformations if provided to the segmentation mask\n        if segmentation_mask is not None and self.transform:\n            segmentation_mask = self.transform(segmentation_mask)\n        else:\n            segmentation_mask = torch.zeros_like(image)\n\n        return image, segmentation_mask\n\nimport torch\nimport torch.nn as nn\nimport torchvision.models as models\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# Paths and settings\nsegmentation_dir = '/kaggle/input/rsna-2023-abdominal-trauma-detection/segmentations'  # Update with the correct path\ncsv_file = '/kaggle/input/abdominal-trauma-nii-csv/abdominal_trauma_nii.csv'  # Update with the correct path\nbatch_size = 32\nnum_workers = 4  # Number of CPU cores to use for data loading\nnum_classes = 14  # Number of classes\nnum_classes_segmentation = 5  # Number of classes\ndesired_shape = (128, 128, 128)\ndevice =torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n\n# Define transformations if needed\ntransform = transforms.Compose([\n    transforms.ToTensor(),  # Convert to tensor\n    # Add more transformations if necessary\n])\n\n# Load the CSV file\ndata = pd.read_csv(csv_file).head(4480)\n\n# Filter rows where the 'mask_path' column is not empty\n#data = data[pd.notna(data['mask_path'])]\n\n# Remove the extra space from the column name\ndata.columns = data.columns.str.strip()\n\n# Assuming 'data' is your DataFrame\ndata_length = len(data)\nprint(\"Length of DataFrame:\", data_length)\n# print(data)\n# print(data.index)\n\n# Split the data into training, validation, and test sets\ntrain_data, temp_data = train_test_split(data, test_size=0.2, random_state=42)\nval_data, test_data = train_test_split(temp_data, test_size=0.5, random_state=42)\n\n# Set the display option to show all rows\npd.set_option('display.max_rows', None)\n\nindex_values = train_data.index.values\n\n# chunk_size = 200  # You can adjust the chunk size\n# for i in range(0, len(index_values), chunk_size):\n#     print(index_values[i:i+chunk_size])\n\n# Reset the display option to its default value (if needed)\npd.reset_option('display.max_rows')\n\n# Extract file paths and labels from the data\ntrain_paths = train_data['file_path'].values\ntrain_mask_paths = train_data['mask_path'].values\ntrain_labels = train_data[['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']].values\n\nval_paths = val_data['file_path'].values\nval_mask_paths = val_data['mask_path'].values\nval_labels = val_data[['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']].values\n\ntest_paths = test_data['file_path'].values\ntest_mask_paths = test_data['mask_path'].values\ntest_labels = test_data[['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']].values\n\n# Instantiate the datasets\ntrain_dataset = CustomDataset(train_paths, train_mask_paths, train_labels, transform=transform)\nprint('len of train_dataset', len(train_dataset))\nval_dataset = CustomDataset(val_paths, val_mask_paths, val_labels, transform=transform)\ntest_dataset = CustomDataset(test_paths, test_mask_paths, test_labels, transform=transform)\n\n# Instantiate the data loaders\ntrain_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=num_workers, drop_last=True)\nprint('train_loader', len(train_loader))\n# train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers)\nval_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers)\ntest_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers)\nprint(train_loader)\n# print(\"Indices:\", train_loader.index)  # Print the indices\n        \n# Instantiate the model with the appropriate number of classes for both classification and segmentation\nin_channels = 1  # Input channels (e.g., for grayscale images or volumes)\nnum_classes_classification = 14  # Number of classes for classification\nnum_classes_segmentation = 1    # Number of classes for segmentation (change this according to your task)\nmodel = Custom3DViTModel(in_channels, num_classes_classification, \n                            num_classes_segmentation, batch_size)\n# Count the number of parameters\ntotal_params = sum(p.numel() for p in model.parameters())\nprint(f\"Total Trainable Parameters: {total_params}\")\n#model = model.to(device)\n# model = get_model()\nmodel = model.to(device)\n# Define loss function and optimizer\ncriterion = nn.BCELoss()  # Binary Cross-Entropy loss\noptimizer = optim.Adam(model.parameters(), lr=0.001)\nclassification_weights = torch.tensor([1, 2, 1, 2, 1, 2, 4, 1, 2, 4, 1, 2, 4, 6], dtype=torch.float32)\nclassification_criterion=nn.BCELoss()\nsegmentation_criterion = nn.BCELoss()\n# Training loop\nnum_epochs = 10\nepoch_count=0\n\nfor epoch in range(num_epochs):\n    model.train()\n    running_loss = 0.0\n    correct_train = 0\n    total_train = 0\n    \n    for batch_images, batch_segmentation_masks, batch_labels in train_loader:\n        optimizer.zero_grad()\n        \n        # Move data to the GPU if available\n        batch_images = batch_images.to(torch.float32).to(device)\n        batch_segmentation_masks = batch_segmentation_masks.to(torch.float32).to(device)\n        batch_labels = batch_labels.to(torch.float32).to(device)\n\n        # Assuming batch_images has shape (batch_size, num_frames, num_channels, height, width)\n        batch_images = batch_images.unsqueeze(1)  # Add a singleton dimension for channels\n        batch_segmentation_masks = batch_segmentation_masks.unsqueeze(1)\n        print(\"batch_images shape and batch_segmentation_masks shape\", \n              batch_images.shape, batch_segmentation_masks.shape)\n        print(\"batch number: \",epoch_count)\n        epoch_count=epoch_count+1\n        \n#         model = model.to(device)\n        # Move data to the XLA device\n        \n        batch_images = batch_images.to(torch.float32)  # Convert to float32 if not already\n        batch_segmentation_masks = batch_segmentation_masks.to(torch.float32)  # Convert to float32 if not already\n\n        batch_images = move_data_to_device(batch_images,device)\n        batch_segmentation_masks = move_data_to_device(batch_segmentation_masks,device)\n        batch_labels = move_data_to_device(batch_labels,device)\n        \n        \n\n        # Forward pass\n        classification_outputs, segmentation_outputs = model(batch_images, batch_segmentation_masks)\n        print(\"..........................................................\")\n        \n        # Apply sigmoid activation to the classification outputs\n        classification_outputs = torch.sigmoid(classification_outputs)\n        \n        print(\"classification_outputs.shape, batch_labels shape\", \n              classification_outputs.shape, batch_labels.shape)\n        \n        \n        # Calculate binary cross-entropy loss for each class separately\n        losses = []\n        for class_index in range(num_classes_classification):\n            class_labels = batch_labels[:, class_index]  # Select labels for the current class\n            class_outputs = classification_outputs[:, class_index]  # Select model outputs for the current class\n            class_loss = classification_criterion(class_outputs, class_labels)\n\n            losses.append(class_loss)\n        total_loss = sum(losses)/num_classes_classification\n\n        running_loss += total_loss.item()\n        accuracies = []\n        for class_index in range(num_classes_classification):\n            class_labels = batch_labels[:, class_index]  # Select labels for the current class\n            class_outputs = classification_outputs[:, class_index]  # Select model outputs for the current class\n            \n            # Calculate binary predictions based on a threshold (e.g., 0.5)\n            predicted = (class_outputs > 0.5).float()\n            \n            class_accuracy = accuracy_score(class_labels.cpu(), predicted.cpu())\n            accuracies.append(class_accuracy)\n        \n        # Calculate overall accuracy\n        batch_accuracy = sum(accuracies) / num_classes_classification\n        correct_train += batch_accuracy\n        total_train += 1\n        print(\"batch accuracy: \",batch_accuracy)\n        print(\"batch loss: \",total_loss)\n        \n        # Backpropagation and optimization\n        total_loss.backward()\n   \n        optimizer.step()\n\n    # Calculate and print average training accuracy and loss\n    avg_train_accuracy = correct_train / len(train_loader)\n    avg_train_loss = running_loss / len(train_loader)\n    \n    print(f\"Epoch [{epoch+1}/{num_epochs}]\")\n    print(f\"Train Accuracy: {avg_train_accuracy:.4f} | Train Loss: {avg_train_loss:.4f}\")\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport nibabel as nib\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms\nfrom scipy.ndimage import zoom\nimport pydicom\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, 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\nroot_folder = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images\"\noutput_folder = \"/kaggle/working/output_nii_images\"\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\n# Loop through subfolders and convert DICOM to NIfTI\nfor 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\nconversion_records.to_csv(csv_file_path_new, index=False)\n\nprint(\"Conversion, resizing, CSV update, and ZIP creation completed.\")\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        # 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 = ...\nmodel = torch.load('/kaggle/input/custom3dvit-model-weights/vit_abdominal (2).pth')\nmodel.eval()\n# Load the list of resized NIfTI paths\nnifti_paths = [os.path.join(output_folder, file) for file in os.listdir(output_folder) if file.endswith('.nii.gz')]\n\n# Instantiate the test dataset\ntest_dataset = CustomDataset(nifti_paths, transform=None)  # You can add a transform if needed\n\n# Instantiate the data loader\nbatch_size = 1\nnum_workers = 4\ntest_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)\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = model.to(device)\n\n# Set the model to evaluation mode\nmodel.eval()\n\n# Loop through the test data and make predictions\npredictions = []\n\nwith torch.no_grad():\n    for batch_images in test_loader:\n#         batch_images = batch_images.to(torch.float32).to(device)\n\n        # Assuming batch_images has shape (batch_size, num_frames, num_channels, height, width)\n        batch_images = batch_images.unsqueeze(1)  # Add a singleton dimension for channels\n        \n        # Move data to the XLA device\n#         print(batch_images)\n        batch_images = batch_images.to(torch.float32) \n        # Convert to float32 if not already\n        batch_images = move_data_to_device(batch_images, device)\n        print(batch_images)\n        mask_tensor = torch.zeros((1, 1, 128, 128, 128), dtype=torch.float32)\n        mask_tensor=move_data_to_device(mask_tensor,device)\n        # Forward pass\n        classification_output,segmentation_output = model(batch_images,mask_tensor)\n        print(classification_output)\n        # Apply sigmoid activation to the outputs\n        output = torch.sigmoid(classification_output)\n        \n\n        # Convert to numpy array and append to predictions list\n        predictions.append(outputs.cpu().numpy())\n\n# Concatenate the predictions along the batch dimension\nall_predictions = np.concatenate(predictions, axis=0)\n\n# Now 'all_predictions' contains the predictions for all test samples\n","metadata":{"execution":{"iopub.status.busy":"2023-12-14T08:48:33.356129Z","iopub.execute_input":"2023-12-14T08:48:33.356577Z","iopub.status.idle":"2023-12-14T08:48:34.192261Z","shell.execute_reply.started":"2023-12-14T08:48:33.35654Z","shell.execute_reply":"2023-12-14T08:48:34.190856Z"},"trusted":true},"execution_count":48,"outputs":[{"name":"stdout","text":"Conversion, resizing, CSV update, and ZIP creation completed.\ntensor([[[[[-0., -0., -0.,  ..., -0., -0., -0.],\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           ...,\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.]],\n\n          [[-0., -0., -0.,  ..., -0., -0., -0.],\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           ...,\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           [0., 0., 0.,  ..., 0., 0., 0.]],\n\n          [[-0., -0., -0.,  ..., -0., -0., -0.],\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           ...,\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.]],\n\n          ...,\n\n          [[0., 0., 0.,  ..., 0., 0., 0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           ...,\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [0., 0., 0.,  ..., 0., 0., 0.]],\n\n          [[-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           ...,\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.]],\n\n          [[-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           ...,\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.]]]]], device='cuda:0')\ntensor([[[[[-0., -0., -0.,  ..., -0., -0., -0.],\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           ...,\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.]],\n\n          [[-0., -0., -0.,  ..., -0., -0., -0.],\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           ...,\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           [0., 0., 0.,  ..., 0., 0., 0.]],\n\n          [[-0., -0., -0.,  ..., -0., -0., -0.],\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           ...,\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.]],\n\n          ...,\n\n          [[0., 0., 0.,  ..., 0., 0., 0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           ...,\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [0., 0., 0.,  ..., 0., 0., 0.]],\n\n          [[-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           ...,\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.]],\n\n          [[-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           ...,\n           [0., 0., 0.,  ..., 0., 0., 0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.],\n           [-0., -0., -0.,  ..., -0., -0., -0.]]]]], device='cuda:0') thid id x\ntensor([[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]],\n       device='cuda:0') vksgfh\ntensor([[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]],\n       device='cuda:0')\n","output_type":"stream"},{"traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)","Cell \u001b[0;32mIn[48], line 160\u001b[0m\n\u001b[1;32m    156\u001b[0m         output \u001b[38;5;241m=\u001b[39m torch\u001b[38;5;241m.\u001b[39msigmoid(classification_output)\n\u001b[1;32m    159\u001b[0m         \u001b[38;5;66;03m# Convert to numpy array and append to predictions list\u001b[39;00m\n\u001b[0;32m--> 160\u001b[0m         predictions\u001b[38;5;241m.\u001b[39mappend(\u001b[43moutputs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcpu\u001b[49m()\u001b[38;5;241m.\u001b[39mnumpy())\n\u001b[1;32m    162\u001b[0m \u001b[38;5;66;03m# Concatenate the predictions along the batch dimension\u001b[39;00m\n\u001b[1;32m    163\u001b[0m all_predictions \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mconcatenate(predictions, axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m)\n","\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'cpu'"],"ename":"AttributeError","evalue":"'tuple' object has no attribute 'cpu'","output_type":"error"}]},{"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        segmentation_mask = segmentation_mask.to(device)\n        print(x,\"thid id x\")\n        features = self.vit_backbone(x)\n        features = features.to(device)\n       \n        classification_output = features.view(1,self.num_classes)\n        print(classification_output)\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 = 1\n\ndef get_model():\n    return Custom3DViTModel(3, 14, 14, 1)\n\ndef run(index):\n    model = get_model()\n    model = model.to(device)\n\n    batch_images = torch.randn(batch_size, 1, 128, 128, 128)\n    batch_segmentation_masks = torch.randn(batch_size, 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-14T08:32:13.233167Z","iopub.execute_input":"2023-12-14T08:32:13.234137Z","iopub.status.idle":"2023-12-14T08:32:13.710728Z","shell.execute_reply.started":"2023-12-14T08:32:13.234097Z","shell.execute_reply":"2023-12-14T08:32:13.709588Z"},"trusted":true},"execution_count":35,"outputs":[{"name":"stdout","text":"tensor([[[[[ 0.2005, -0.3849,  0.4920,  ...,  0.8065, -1.7035, -1.3616],\n           [-0.7067,  0.3687,  0.6942,  ...,  1.0807,  0.1174, -0.5721],\n           [ 0.0283, -1.4078, -1.4853,  ..., -1.5086, -0.8240,  1.4130],\n           ...,\n           [ 0.2634, -1.0498,  2.3986,  ..., -0.2144,  0.9478,  0.3124],\n           [-0.3010,  0.6132,  0.7947,  ..., -1.4901, -2.5087, -0.6631],\n           [ 1.5447,  2.2872,  0.8739,  ...,  0.3871,  1.1654,  1.2718]],\n\n          [[-0.7096,  0.2618, -0.4915,  ..., -0.1583,  0.5784, -1.3028],\n           [ 0.7596,  0.1067, -1.5939,  ..., -1.1170,  2.1796, -0.6968],\n           [ 1.9496, -0.6461,  1.6394,  ..., -0.0599, -1.1723,  1.2007],\n           ...,\n           [-0.1790,  1.5318, -0.1883,  ..., -1.8631,  1.9944, -0.5602],\n           [-0.3367, -0.9926, -0.7037,  ...,  0.0601, -0.5865,  0.9780],\n           [ 0.3359,  0.3227,  0.1864,  ..., -0.9952, -2.1782,  1.0090]],\n\n          [[ 1.1035, -0.1625, -0.1193,  ..., -0.4868, -0.0922, -0.4555],\n           [ 0.0941, -0.5852,  0.6217,  ...,  0.7801,  1.9050,  0.1373],\n           [-0.7999,  1.4470, -0.6997,  ...,  1.2402, -0.6929,  0.0086],\n           ...,\n           [-1.9360,  1.1876, -1.7143,  ..., -1.7959,  0.2677,  0.5822],\n           [ 0.0632, -0.7008,  0.0977,  ..., -0.2272,  0.0907,  0.1164],\n           [-0.1293,  0.2093, -0.0054,  ...,  0.3223,  0.7230,  0.6077]],\n\n          ...,\n\n          [[ 0.7091, -1.4321, -0.2984,  ..., -1.4162,  0.0325, -0.0347],\n           [ 1.0008, -1.4028, -0.2055,  ..., -0.4625,  0.1897,  1.2143],\n           [-0.1154, -0.0870,  0.4559,  ...,  0.7602,  1.3531,  0.8857],\n           ...,\n           [-2.3494,  2.5314,  0.0877,  ..., -0.3820,  1.1054, -1.8317],\n           [ 0.4264, -0.9545, -0.5689,  ..., -0.0857,  1.0151, -0.2439],\n           [ 2.1752,  0.2926, -1.1401,  ..., -0.0792,  1.4440,  0.0527]],\n\n          [[ 0.0493, -0.7925, -0.4522,  ...,  1.4976,  0.0454,  1.4259],\n           [ 0.7611,  0.8332, -0.2252,  ..., -0.6864,  0.4127, -1.2966],\n           [-0.2109, -0.4352,  0.6756,  ..., -0.6688,  0.7559,  0.2011],\n           ...,\n           [-0.2439, -0.8952, -0.4090,  ...,  0.3003, -0.0987,  1.4060],\n           [-1.0782,  0.9279,  2.4540,  ...,  1.0843, -0.1989, -1.0366],\n           [-0.3588, -1.1018,  1.7658,  ...,  1.1958,  1.2202, -2.7267]],\n\n          [[ 0.3328,  0.1691,  2.4968,  ...,  0.3432,  1.0092, -0.6852],\n           [ 2.0336,  1.4504,  0.7821,  ...,  1.8470, -1.4011,  0.2754],\n           [-0.1223,  1.7458,  0.3606,  ...,  0.7572,  0.4096,  0.3072],\n           ...,\n           [-0.5403,  0.5876, -0.1039,  ..., -1.4058,  0.4312, -1.1430],\n           [ 0.4474, -0.5380, -1.3748,  ...,  0.2771,  1.2039, -0.4579],\n           [-0.6505,  0.7633,  0.6432,  ...,  1.0319, -0.1325, -2.5766]]]]],\n       device='cuda:0') thid id x\ntensor([[-0.0456,  0.1953,  0.5061,  0.8359, -0.1326,  0.1431,  0.1115, -0.7457,\n          0.2823, -1.2958, -0.5433,  0.7130,  0.5325, -0.0508]],\n       device='cuda:0', grad_fn=<ViewBackward0>) vksgfh\n","output_type":"stream"}]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}