{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# import shutil\n\n# # Specify the source path of the CSV file\n# source_path = '/kaggle/input/conversion-abdominaltraumacsv/combined-all - Sheet1 final.csv'\n\n# # Specify the destination path in the Kaggle working directory\n# destination_path = '/kaggle/working/train.csv'\n\n# # Copy the CSV file\n# shutil.copy(source_path, destination_path)\n\n# print(f\"CSV file copied to: {destination_path}\")\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install nibabel","metadata":{"execution":{"iopub.status.busy":"2023-09-20T15:54:37.483081Z","iopub.execute_input":"2023-09-20T15:54:37.483372Z","iopub.status.idle":"2023-09-20T15:54:44.206058Z","shell.execute_reply.started":"2023-09-20T15:54:37.483347Z","shell.execute_reply":"2023-09-20T15:54:44.204994Z"},"trusted":true},"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 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 torch\nimport torch.nn as nn\nimport torchvision.models as models\nimport nibabel as nib\nimport pandas as pd\nfrom scipy.ndimage import zoom\n\nclass CustomDataset(Dataset):\n    def __init__(self, image_paths, mask_paths, labels, target_shape, transform=None):\n        self.image_paths = image_paths\n        self.mask_paths = mask_paths\n        self.labels = labels\n        self.target_shape = target_shape\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\n        # Load the segmentation mask if available\n        segmentation_mask = None\n        if pd.notna(mask_path):\n            segmentation_mask = nib.load(mask_path).get_fdata()\n            segmentation_mask = self.resize_nifti(segmentation_mask, self.target_shape)\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        # Detach the tensors to make them resizable\n        image = image.detach()\n        segmentation_mask = segmentation_mask.detach()\n\n        label = torch.tensor(self.labels[idx], dtype=torch.float32)\n        \n        return image, segmentation_mask, label\n\n    def resize_nifti(self, nifti_data, target_shape):\n        factors = (\n            target_shape[0] / nifti_data.shape[0],\n            target_shape[1] / nifti_data.shape[1],\n            target_shape[2] / nifti_data.shape[2]\n        )\n        resized_data = zoom(nifti_data, factors, order=3)  # Cubic interpolation (higher quality)\n        return resized_data\n\n\n# Define your target shape (e.g., [128, 128, 128])\n\nclass MultiLabel3DAttentionModel(nn.Module):\n    def __init__(self, num_classes, num_classes_segmentation):\n        super(MultiLabel3DAttentionModel, self).__init__()\n\n        # Load a pre-trained ResNet3D backbone\n        self.backbone = models.video.r3d_18(pretrained=True)\n        \n               \n        # Modify the stem to accept the correct input channels (128)\n        self.backbone.stem[0] = nn.Sequential(\n            nn.Conv3d(1, 64, kernel_size=(3, 7, 7), stride=(1, 2, 2), padding=(1, 3, 3)),\n            nn.BatchNorm3d(64),\n            nn.ReLU(inplace=True))\n\n        # Attention block\n        self.attention = nn.Sequential(\n            nn.Conv3d(1, 128, kernel_size=1),\n            nn.ReLU(inplace=True),\n            nn.Conv3d(128, 1, kernel_size=1),\n            nn.Sigmoid()\n        )\n        \n        # Classification head\n        self.classification_head = nn.Sequential(\n            nn.AdaptiveAvgPool3d(1),\n            nn.Flatten(),\n            nn.Linear(1, 64),\n            nn.ReLU(inplace=True),\n            nn.Linear(64, num_classes),\n            nn.Sigmoid()\n        )\n        \n        # Segmentation head\n        self.segmentation_head = nn.Sequential(\n            nn.Conv3d(1, 128, kernel_size=1),\n            nn.ReLU(inplace=True),\n            nn.Conv3d(128, num_classes_segmentation, kernel_size=1),\n            nn.Sigmoid()\n        )\n        \n    def forward(self, x, segmentation_mask):\n        #print( '...............', x.dtype, segmentation_mask.dtype)\n        print(\"X shape\", x.shape)\n        \n\n        features = self.backbone(x)\n        print(\"features shape\", features.shape)\n        # Apply attention to features\n        # Assuming features has shape [batch_size, num_features]\n        # Reshape features to [batch_size, 1, 1, 1, num_features]\n        features = features.view(features.size(0), 1, 1, 1, features.size(1))\n        \n        attention_weights = self.attention(features)\n        \n        print('attention_weights shape', attention_weights.shape)\n        attended_features = features * attention_weights\n        print('attended_features', attended_features.shape)\n        \n        # Classification branch\n        classification_output = self.classification_head(attended_features)\n        \n        # Reshape attended_features to match segmentation_mask's shape along dimensions 2, 3, and 4\n        #attended_features = attended_features.expand(-1, -1, 128, 128, 128)\n\n        print('segmentation_mask', segmentation_mask.shape)\n        \n        # Segmentation branch\n        # Reshape attended_features to match segmentation_mask's shape along dimensions 2, 3, and 4\n        #attended_features = attended_features.expand(-1, -1, segmentation_mask.size(2), segmentation_mask.size(3), segmentation_mask.size(4))\n        \n        #segmentation_output = self.segmentation_head(attended_features) * segmentation_mask\n        \n        # Modify the segmentation head to handle the different number of channels\n        segmentation_output = self.segmentation_head(attended_features)\n         # You can use interpolation or other methods to match the shape.\n        segmentation_output = F.interpolate(segmentation_output, size=segmentation_mask.shape[2:], mode='trilinear')\n        \n        print('segmentation_output', segmentation_output.shape)\n        \n        segmentation_output = segmentation_output * segmentation_mask  # Element-wise multiplication\n        \n        \n        return classification_output, segmentation_output\n\n# Paths and settings\nsegmentation_dir = '/kaggle/input/rsna-2023-abdominal-trauma-detection/segmentations'  # Update with the correct path\ncsv_file = '/kaggle/input/train-paths-abdominalcsv/finalabdominal.csv'  # Update with the correct path\nbatch_size = 8\nnum_workers = 4  # Number of CPU cores to use for data loading\nnum_classes = 14  # Number of classes\ndesired_shape = (128, 128, 128)\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\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)\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.3, 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,desired_shape ,transform=transforms.Compose([transforms.ToTensor()]))\nprint('len of train_dataset', len(train_dataset))\nval_dataset = CustomDataset(val_paths, val_mask_paths, val_labels, desired_shape ,transform=transforms.Compose([transforms.ToTensor()]))\ntest_dataset = CustomDataset(test_paths, test_mask_paths, test_labels, desired_shape ,transform=transforms.Compose([transforms.ToTensor()]))\n# Instantiate the data loaders\n# desired_shape = [128, 128, 128]\n\n# Example usage:\n# Create an instance of the CustomDataset class\n# custom_dataset = CustomDataset(image_paths, mask_paths, labels, desired_shape, transform=transforms.Compose([transforms.ToTensor()]))\n\n# Create a data loader\n# train_loader = DataLoader(custom_dataset, batch_size=batch_size, shuffle=True)\n\n\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\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 = MultiLabel3DAttentionModel(num_classes_classification, num_classes_segmentation)\n\n\n# Define loss function and optimizer\ncriterion = nn.BCELoss()  # Binary Cross-Entropy loss\noptimizer = optim.Adam(model.parameters(), lr=0.001)\n\n# def print_parameter_data_types(module):\n#     for name, param in module.named_parameters():\n#         print(f\"Parameter: {name}, Data Type: {param.dtype}\")\n\n# # Call the function to print data types of parameters in the backbone module\n# print_parameter_data_types(model.backbone)\n\n\n# Training loop\nnum_epochs = 1\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\n        # Forward pass\n        classification_outputs, segmentation_outputs = model(batch_images, batch_segmentation_masks)\n        \n        # Apply sigmoid activation to the classification outputs\n        classification_outputs = torch.sigmoid(classification_outputs)\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 = criterion(class_outputs, class_labels)\n            losses.append(class_loss)\n\n        # Calculate the total loss as the sum of individual class losses\n        total_loss = sum(losses)\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 = criterion(segmentation_outputs, batch_segmentation_masks)\n            total_loss += segmentation_loss\n\n        running_loss += total_loss.item()\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        \n        # Backpropagation and optimization\n        total_loss.backward()\n        optimizer.step()\n\n    # Calculate and print average training accuracy and loss\n    avg_train_accuracy = correct_train / total_train\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\n    # Validation loop\n    model.eval()\n    total_val_loss = 0.0\n    correct_val = 0\n    total_val = 0\n\n    with torch.no_grad():\n        for batch_images, batch_segmentation_masks, batch_labels in val_loader:\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            batch_images = batch_images.unsqueeze(1)\n            batch_segmentation_masks = batch_segmentation_masks.unsqueeze(1)\n\n            # Forward pass\n            classification_outputs, segmentation_outputs = model(batch_images, batch_segmentation_masks)\n            \n            # Apply sigmoid activation to the classification outputs\n            classification_outputs = torch.sigmoid(classification_outputs)\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]\n                class_outputs = classification_outputs[:, class_index]\n                class_loss = criterion(class_outputs, class_labels)\n                losses.append(class_loss)\n\n            total_loss = sum(losses)\n\n            # Check if segmentation mask is available\n            if batch_segmentation_masks is not None:\n                batch_segmentation_masks = batch_segmentation_masks.to(torch.float64)\n                \n                # Apply sigmoid activation to segmentation_outputs\n                segmentation_outputs = torch.sigmoid(segmentation_outputs)\n                segmentation_outputs = segmentation_outputs.to(torch.float64)\n                \n                segmentation_loss = criterion(segmentation_outputs, batch_segmentation_masks)\n                total_loss = total_loss + segmentation_loss\n            \n            total_val_loss += total_loss.item()\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]\n                class_outputs = classification_outputs[:, class_index]\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            batch_accuracy = sum(accuracies) / num_classes_classification\n            correct_val += batch_accuracy\n            total_val += batch_labels.size(0)\n\n    val_accuracy = correct_val / total_val\n    avg_val_loss = total_val_loss / len(val_loader)\n\n    print(f\"Validation Accuracy: {val_accuracy:.4f} | Validation Loss: {avg_val_loss:.4f}\")\n\n# Test loop\nmodel.eval()\ntotal_correct = 0\ntotal_samples = 0\n# Initialize lists to store per-class metrics\nprecision_list = []\nrecall_list = []\nf1_list = []\n\nwith torch.no_grad():\n    for batch_images, batch_segmentation_masks, batch_labels in test_loader:\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        batch_images = batch_images.unsqueeze(1)\n        batch_segmentation_masks = batch_segmentation_masks.unsqueeze(1)\n\n        # Forward pass\n        classification_outputs, segmentation_outputs = model(batch_images, batch_segmentation_masks)\n        \n        # Apply sigmoid activation to the classification outputs\n        classification_outputs = torch.sigmoid(classification_outputs)\n            \n        # Initialize batch-level variables for accuracy calculation\n        batch_correct = 0\n        batch_samples = batch_labels.size(0)\n        \n        for class_index in range(num_classes_classification):\n            class_labels = batch_labels[:, class_index]\n            class_outputs = classification_outputs[:, class_index]\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            batch_correct += class_accuracy\n            \n            # Calculate precision, recall, and F1-score for the current class\n            precision, recall, f1, _ = precision_recall_fscore_support(\n                class_labels.cpu(), predicted.cpu(), average='binary')\n            \n            precision_list.append(precision)\n            recall_list.append(recall)\n            f1_list.append(f1)\n\n        # Accumulate batch-level accuracy\n        total_correct += batch_correct\n        total_samples += batch_samples\n    \n    test_accuracy = correct_test / total_samples\n    print(f\"Test Accuracy: {test_accuracy:.4f}\")\n\n    # Calculate average precision, recall, and F1-score across all classes\n    avg_precision = sum(precision_list) / num_classes_classification\n    avg_recall = sum(recall_list) / num_classes_classification\n    avg_f1 = sum(f1_list) / num_classes_classification\n\n    print(f\"Average Precision: {avg_precision:.4f}\")\n    print(f\"Average Recall: {avg_recall:.4f}\")\n    print(f\"Average F1 Score: {avg_f1:.4f}\")\n","metadata":{"execution":{"iopub.status.busy":"2023-09-20T15:54:53.742507Z","iopub.execute_input":"2023-09-20T15:54:53.74288Z","iopub.status.idle":"2023-09-20T16:19:21.329487Z","shell.execute_reply.started":"2023-09-20T15:54:53.742847Z","shell.execute_reply":"2023-09-20T16:19:21.327735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pandas as pd\n\n# # Read the CSV file into a DataFrame\n# csv_file_path = '/kaggle/working/train.csv'  # Update with the correct path\n# data = pd.read_csv(csv_file_path)\n\n# # Add a new column named \"new_column\" with some values\n# data['file_path'] = None \n# data['mask_path'] = None\n# data['patient']=None\n# data['sub_patient_id']=None# Update with your values\n\n# # Write the updated DataFrame back to the CSV file\n# updated_csv_file_path = '/kaggle/working/updated_csv_file.csv'  # Update with the desired output path\n# data.to_csv(updated_csv_file_path, index=False)\n\n# # Print the updated DataFrame\n# print(data)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# import pandas as pd\n\n# # Specify the path to the folder containing files\n# folder_path = '/kaggle/input/rsna-2023-abdominal-trauma-detection/segmentations'\n\n# # Path to the CSV file\n# csv_file_path = '/kaggle/working/updated_csv_file.csv'\n\n# # Read the CSV file into a DataFrame\n# data = pd.read_csv(csv_file_path)\n\n# # Get a list of all files in the folder\n# file_paths = [os.path.join(folder_path, filename) for filename in os.listdir(folder_path)]\n\n# # Loop through the file paths\n# for path in file_paths:\n#     filename = os.path.basename(path)  # Get the filename from the path\n    \n#     file_name_without_extension = filename[:-4]\n# #     print(file_name_without_extension)  # Output: \"12345\"\n    \n#     # Check if the filename is present in the 'file_path' column of the DataFrame\n#     data['sub_patient_id'] = data['patient_id'].str.split('_').str[1] \n#     data['patient'] = data['patient_id'].str.split('_').str[0] \n#     mask = data['sub_patient_id'] == file_name_without_extension\n    \n#     if mask.any():\n#         # Update the 'mask_path' column with the new file path for matching rows\n#         data.loc[mask, 'mask_path'] = path\n\n# #     if any(data['patient_id'].str.contains(file_name_without_extension)):\n# #         # Update the 'file_path' column with the new file path\n# #         data.loc[data['patient_id'].str.contains(file_name_without_extension), 'mask_path'] = path\n\n# # Save the updated DataFrame back to the CSV file\n# data.to_csv(csv_file_path, index=False)\n# print(\"done\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# import pandas as pd\n\n# # Specify the path to the folder containing files\n# folder_path = '/kaggle/input/nii-dicom-56/output_nii_images'\n# # Path to the CSV file\n# csv_file_path = '/kaggle/working/updated_csv_file.csv'\n\n# # Read the CSV file into a DataFrame\n# data = pd.read_csv(csv_file_path)\n\n# # Get a list of all files in the folder\n# file_paths = [os.path.join(folder_path, filename) for filename in os.listdir(folder_path)]\n\n# # Loop through the file paths\n# for path in file_paths:\n#     filename = os.path.basename(path)  # Get the filename from the path\n    \n#     file_name_without_extension = filename[:-4]\n# #     print(file_name_without_extension)  # Output: \"12345\"\n    \n#     # Check if the filename is present in the 'file_path' column of the DataFrame\n# #     data['sub_patient_id'] = data['patient_id'].str.split('_').str[1] \n#     mask = data['patient_id'] == file_name_without_extension\n    \n#     if mask.any():\n#         # Update the 'mask_path' column with the new file path for matching rows\n#         data.loc[mask, 'file_path'] = path\n\n# #     if any(data['patient_id'].str.contains(file_name_without_extension)):\n# #         # Update the 'file_path' column with the new file path\n# #         data.loc[data['patient_id'].str.contains(file_name_without_extension), 'mask_path'] = path\n\n# # Save the updated DataFrame back to the CSV file\n# data.to_csv(csv_file_path, index=False)\n# print(\"done\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data = pd.read_csv('/kaggle/working/check_update_file.csv')\n# data['bowel_healthy']=None\n# data['bowel_injury']=None\n# data['extravasation_healthy']=None\n# data['extravasation_injury']=None\n# data['kidney_healthy']=None\n# data['kidney_low']=None\n# data['kidney_high']=None\n# data['liver_healthy']=None\n# data['liver_low']=None\n# data['liver_high']=None\n# data['spleen_healthy']=None\n# data['spleen_low']=None\n# data['spleen_high']=None\n# data['any_injury']=None\n# print(data.head())","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# import pandas as pd\n\n# # Specify the path to the folder containing files\n# folder_path = '/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv'\n# # Path to the CSV file\n# csv_file_path = '/kaggle/working/updated_csv_file.csv'\n\n# # Read the CSV file into a DataFrame\n# data = pd.read_csv(csv_file_path)\n\n# # Get a list of all files in the folder\n# # file_paths = [os.path.join(folder_path, filename) for filename in os.listdir(folder_path)]\n\n# # Loop through the file paths\n# for path in file_paths:\n#     filename = os.path.basename(path)  # Get the filename from the path\n    \n#     file_name_without_extension = filename[:-4]\n# #     print(file_name_without_extension)  # Output: \"12345\"\n    \n#     # Check if the filename is present in the 'file_path' column of the DataFrame\n# #     data['sub_patient_id'] = data['patient_id'].str.split('_').str[1] \n#     mask = data['patient_id'] == file_name_without_extension\n    \n#     if mask.any():\n#         # Update the 'mask_path' column with the new file path for matching rows\n#         data.loc[mask, 'file_path'] = path\n\n# #     if any(data['patient_id'].str.contains(file_name_without_extension)):\n# #         # Update the 'file_path' column with the new file path\n# #         data.loc[data['patient_id'].str.contains(file_name_without_extension), 'mask_path'] = path\n\n# # Save the updated DataFrame back to the CSV file\n# data.to_csv(csv_file_path, index=False)\n# print(\"done\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # import pandas as pd\n\n# # # Load the 'updated.csv' and 'train.csv' files\n# # updated_df = pd.read_csv('/kaggle/working/check_update_file.csv')\n# # train_df = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv')\n\n# # # Get a list of all unique patient IDs in 'train.csv'\n# # unique_patient_ids = train_df['patient_id'].unique()\n\n# # # Iterate through each patient ID and copy classification values\n# # for target_patient_id in unique_patient_ids:\n# #     # Filter the rows in 'train.csv' for the current patient ID\n# #     rows = updated_df[updated_df['patient'] == target_patient_id]\n# #     val=train_df[train_df['patient_id']==target_patient_id]\n    \n# #     if rows.any():\n# #         updated_df.loc[rows, 'bowel_healthy'] = train_df.loc[val, 'bowel_healthy']\n\n# # updated_df.to_csv('updated1.csv', index=False)\n# import pandas as pd\n\n# # Load the 'updated.csv' and 'train.csv' files\n# updated_df = pd.read_csv('/kaggle/working/check_update_file.csv')\n# train_df = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv')\n\n# # Get a list of all unique patient IDs in 'train.csv'\n# unique_patient_ids = train_df['patient_id'].unique()\n\n# # Iterate through each patient ID and copy classification values\n# for target_patient_id in unique_patient_ids:\n#     # Filter the rows in 'updated_df' for the current patient ID\n#     rows = updated_df[updated_df['patient'] == target_patient_id]\n    \n#     # Filter the rows in 'train_df' for the current patient ID\n#     val = train_df[train_df['patient_id'] == target_patient_id]\n    \n#     # Check if there are matching rows in both DataFrames\n#     if not rows.empty and not val.empty:\n#         # Get the 'bowel_healthy' value from 'train_df'\n#         bowel_healthy_value = val['bowel_healthy'].iloc[0]\n#         bowel_injury_value=val['bowel_injury'].iloc[0]\n#         extravasation_healthy_value=val['extravasation_healthy'].iloc[0]\n#         extravasation_injury_value=val['extravasation_injury'].iloc[0]\n#         kidney_healthy_value=val['kidney_healthy'].iloc[0]\n#         kidney_low_value=val['kidney_low'].iloc[0]\n#         kidney_high_value=val['kidney_high'].iloc[0]\n#         liver_healthy_value=val['liver_healthy'].iloc[0]\n#         liver_low_value=val['liver_low'].iloc[0]\n#         liver_high_value=val['liver_high'].iloc[0]\n#         spleen_healthy_value=val['spleen_healthy'].iloc[0]\n#         spleen_low_value=val['spleen_low'].iloc[0]\n#         spleen_high_value=val['spleen_high'].iloc[0]\n#         any_injury_value=val['any_injury'].iloc[0]\n        \n#         # Update the 'bowel_healthy' column in 'updated_df'\n#         updated_df.loc[rows.index, 'bowel_healthy'] = int(bowel_healthy_value)\n#         updated_df.loc[rows.index, 'bowel_injury'] = int(bowel_injury_value)\n#         updated_df.loc[rows.index, 'extravasation_healthy'] = int(extravasation_healthy_value)\n#         updated_df.loc[rows.index, 'extravasation_injury'] = int(extravasation_injury_value)\n#         updated_df.loc[rows.index, 'kidney_healthy'] = int(kidney_healthy_value)\n#         updated_df.loc[rows.index, 'kidney_low'] = int(kidney_low_value)\n#         updated_df.loc[rows.index, 'kidney_high'] = int(kidney_high_value)\n#         updated_df.loc[rows.index, 'liver_healthy'] = int(liver_healthy_value)\n#         updated_df.loc[rows.index, 'liver_low'] = int(liver_low_value)\n#         updated_df.loc[rows.index, 'liver_high'] = int(liver_high_value)\n#         updated_df.loc[rows.index, 'spleen_healthy'] = int(spleen_healthy_value)\n#         updated_df.loc[rows.index, 'spleen_low'] = int(spleen_low_value)\n#         updated_df.loc[rows.index, 'spleen_high'] = int(spleen_high_value)\n#         updated_df.loc[rows.index, 'any_injury'] = int(any_injury_value)\n\n# # Save the updated 'updated.csv' DataFrame back to the file\n# updated_df.to_csv('updated1.csv', index=False)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import shutil\n\n# # Specify the source path of the CSV file\n# source_path = '/kaggle/working/updated_csv_file.csv'\n\n# # Specify the destination path in the Kaggle working directory\n# destination_path = '/kaggle/working/check_update_file.csv'\n\n# # Copy the CSV file\n# shutil.copy(source_path, destination_path)\n\n# print(f\"CSV file copied to: {destination_path}\")\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data = pd.read_csv('/kaggle/working/updated1.csv')\n# print(data.head())","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install nibabel","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# asdfasdf\n# import os\n# import pandas as pd\n# import numpy as np\n# import nibabel as nib\n# import torch\n# import torch.nn as nn\n# import torch.optim as optim\n# from torch.utils.data import Dataset, DataLoader\n# from torchvision import transforms\n# from sklearn.metrics import accuracy_score, precision_recall_fscore_support, roc_auc_score\n# from sklearn.model_selection import train_test_split\n# from torchvision import models\n# from scipy.ndimage import zoom\n# import torch.nn.functional as F\n# from PIL import Image\n\n# class CustomDataset(Dataset):\n#     def __init__(self, image_paths, mask_paths, labels, transform=None):\n#         self.image_paths = image_paths\n#         self.mask_paths = mask_paths\n#         self.labels = labels\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#         label = torch.tensor(self.labels[idx], dtype=torch.float32)\n        \n#         return image, segmentation_mask, label\n\n# import torch\n# import torch.nn as nn\n# import torchvision.models as models\n\n# # Function to resize NIfTI data\n# def resize_nifti(nifti_data, target_shape):\n#     factors = (target_shape[0] / nifti_data.shape[0],\n#                target_shape[1] / nifti_data.shape[1],\n#                target_shape[2] / nifti_data.shape[2])\n#     resized_data = zoom(nifti_data, factors, order=3)  # Cubic interpolation (higher quality)\n#     return resized_data\n\n# class MultiLabel3DAttentionModel(nn.Module):\n#     def __init__(self, num_classes, num_classes_segmentation):\n#         super(MultiLabel3DAttentionModel, self).__init__()\n\n#         # Load a pre-trained ResNet3D backbone\n#         self.backbone = models.video.r3d_18(pretrained=True)\n        \n               \n#         # Modify the stem to accept the correct input channels (128)\n#         self.backbone.stem[0] = nn.Sequential(\n#             nn.Conv3d(1, 64, kernel_size=(3, 7, 7), stride=(1, 2, 2), padding=(1, 3, 3)),\n#             nn.BatchNorm3d(64),\n#             nn.ReLU(inplace=True))\n\n#         # Attention block\n#         self.attention = nn.Sequential(\n#             nn.Conv3d(1, 128, kernel_size=1),\n#             nn.ReLU(inplace=True),\n#             nn.Conv3d(128, 1, kernel_size=1),\n#             nn.Sigmoid()\n#         )\n        \n#         # Classification head\n#         self.classification_head = nn.Sequential(\n#             nn.AdaptiveAvgPool3d(1),\n#             nn.Flatten(),\n#             nn.Linear(1, 64),\n#             nn.ReLU(inplace=True),\n#             nn.Linear(64, num_classes),\n#             nn.Sigmoid()\n#         )\n        \n#         # Segmentation head\n#         self.segmentation_head = nn.Sequential(\n#             nn.Conv3d(1, 128, kernel_size=1),\n#             nn.ReLU(inplace=True),\n#             nn.Conv3d(128, num_classes_segmentation, kernel_size=1),\n#             nn.Sigmoid()\n#         )\n        \n#     def forward(self, x, segmentation_mask):\n#         #print( '...............', x.dtype, segmentation_mask.dtype)\n#         print(\"X shape\", x.shape)\n        \n\n#         features = self.backbone(x)\n#         print(\"features shape\", features.shape)\n#         # Apply attention to features\n#         # Assuming features has shape [batch_size, num_features]\n#         # Reshape features to [batch_size, 1, 1, 1, num_features]\n#         features = features.view(features.size(0), 1, 1, 1, features.size(1))\n        \n#         attention_weights = self.attention(features)\n        \n#         print('attention_weights shape', attention_weights.shape)\n#         attended_features = features * attention_weights\n#         print('attended_features', attended_features.shape)\n        \n#         # Classification branch\n#         classification_output = self.classification_head(attended_features)\n        \n#         # Reshape attended_features to match segmentation_mask's shape along dimensions 2, 3, and 4\n#         #attended_features = attended_features.expand(-1, -1, 128, 128, 128)\n\n#         print('segmentation_mask', segmentation_mask.shape)\n        \n#         # Segmentation branch\n#         # Reshape attended_features to match segmentation_mask's shape along dimensions 2, 3, and 4\n#         #attended_features = attended_features.expand(-1, -1, segmentation_mask.size(2), segmentation_mask.size(3), segmentation_mask.size(4))\n        \n#         #segmentation_output = self.segmentation_head(attended_features) * segmentation_mask\n        \n#         # Modify the segmentation head to handle the different number of channels\n#         segmentation_output = self.segmentation_head(attended_features)\n#          # You can use interpolation or other methods to match the shape.\n#         segmentation_output = F.interpolate(segmentation_output, size=segmentation_mask.shape[2:], mode='trilinear')\n        \n#         print('segmentation_output', segmentation_output.shape)\n        \n#         segmentation_output = segmentation_output * segmentation_mask  # Element-wise multiplication\n        \n        \n#         return classification_output, segmentation_output\n\n# # Paths and settings\n# segmentation_dir = '/kaggle/input/rsna-2023-abdominal-trauma-detection/segmentations'  # Update with the correct path\n# csv_file = '/kaggle/input/train-paths-abdominalcsv/finalabdominal.csv'  # Update with the correct path\n# batch_size = 16\n# num_workers = 4  # Number of CPU cores to use for data loading\n# num_classes = 14  # Number of classes\n# desired_shape = (128, 128, 128)\n# device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n\n# # Define transformations if needed\n# transform = 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)\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.3, 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)\n# test_dataset = CustomDataset(test_paths, test_mask_paths, test_labels, 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# 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)\n# model = MultiLabel3DAttentionModel(num_classes_classification, num_classes_segmentation).to(device)\n\n\n# # Define loss function and optimizer\n# criterion = nn.BCELoss()  # Binary Cross-Entropy loss\n# optimizer = optim.Adam(model.parameters(), lr=0.001)\n\n# # def print_parameter_data_types(module):\n# #     for name, param in module.named_parameters():\n# #         print(f\"Parameter: {name}, Data Type: {param.dtype}\")\n\n# # # Call the function to print data types of parameters in the backbone module\n# # print_parameter_data_types(model.backbone)\n\n\n# # Training loop\n# num_epochs = 1\n\n# for 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(device).to(torch.float32)\n#         batch_segmentation_masks = batch_segmentation_masks.to(device).to(torch.float32)\n#         batch_labels = batch_labels.to(device).to(torch.float32)\n\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\n#         # Forward pass\n#         classification_outputs, segmentation_outputs = model(batch_images, batch_segmentation_masks)\n\n        \n#         # Apply sigmoid activation to the classification outputs\n#         classification_outputs = torch.sigmoid(classification_outputs)\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 = criterion(class_outputs, class_labels)\n#             losses.append(class_loss)\n\n#         # Calculate the total loss as the sum of individual class losses\n#         total_loss = sum(losses)\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 = criterion(segmentation_outputs, batch_segmentation_masks)\n#             total_loss += segmentation_loss\n\n#         running_loss += total_loss.item()\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        \n#         # Backpropagation and optimization\n#         total_loss.backward()\n#         optimizer.step()\n\n#     # Calculate and print average training accuracy and loss\n#     avg_train_accuracy = correct_train / total_train\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\n#     # Validation loop\n#     model.eval()\n#     total_val_loss = 0.0\n#     correct_val = 0\n#     total_val = 0\n\n#     with torch.no_grad():\n#         for batch_images, batch_segmentation_masks, batch_labels in val_loader:\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#             batch_images = batch_images.unsqueeze(1)\n#             batch_segmentation_masks = batch_segmentation_masks.unsqueeze(1)\n\n#             # Forward pass\n#             classification_outputs, segmentation_outputs = model(batch_images, batch_segmentation_masks)\n            \n#             # Apply sigmoid activation to the classification outputs\n#             classification_outputs = torch.sigmoid(classification_outputs)\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]\n#                 class_outputs = classification_outputs[:, class_index]\n#                 class_loss = criterion(class_outputs, class_labels)\n#                 losses.append(class_loss)\n\n#             total_loss = sum(losses)\n\n#             # Check if segmentation mask is available\n#             if batch_segmentation_masks is not None:\n#                 batch_segmentation_masks = batch_segmentation_masks.to(torch.float64)\n                \n#                 # Apply sigmoid activation to segmentation_outputs\n#                 segmentation_outputs = torch.sigmoid(segmentation_outputs)\n#                 segmentation_outputs = segmentation_outputs.to(torch.float64)\n                \n#                 segmentation_loss = criterion(segmentation_outputs, batch_segmentation_masks)\n#                 total_loss = total_loss + segmentation_loss\n            \n#             total_val_loss += total_loss.item()\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]\n#                 class_outputs = classification_outputs[:, class_index]\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#             batch_accuracy = sum(accuracies) / num_classes_classification\n#             correct_val += batch_accuracy\n#             total_val += batch_labels.size(0)\n\n#     val_accuracy = correct_val / total_val\n#     avg_val_loss = total_val_loss / len(val_loader)\n\n#     print(f\"Validation Accuracy: {val_accuracy:.4f} | Validation Loss: {avg_val_loss:.4f}\")\n\n# # Test loop\n# model.eval()\n# total_correct = 0\n# total_samples = 0\n# # Initialize lists to store per-class metrics\n# precision_list = []\n# recall_list = []\n# f1_list = []\n\n# with torch.no_grad():\n#     for batch_images, batch_segmentation_masks, batch_labels in test_loader:\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#         batch_images = batch_images.unsqueeze(1)\n#         batch_segmentation_masks = batch_segmentation_masks.unsqueeze(1)\n\n#         # Forward pass\n#         classification_outputs, segmentation_outputs = model(batch_images, batch_segmentation_masks)\n        \n#         # Apply sigmoid activation to the classification outputs\n#         classification_outputs = torch.sigmoid(classification_outputs)\n            \n#         # Initialize batch-level variables for accuracy calculation\n#         batch_correct = 0\n#         batch_samples = batch_labels.size(0)\n        \n#         for class_index in range(num_classes_classification):\n#             class_labels = batch_labels[:, class_index]\n#             class_outputs = classification_outputs[:, class_index]\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#             batch_correct += class_accuracy\n            \n#             # Calculate precision, recall, and F1-score for the current class\n#             precision, recall, f1, _ = precision_recall_fscore_support(\n#                 class_labels.cpu(), predicted.cpu(), average='binary')\n            \n#             precision_list.append(precision)\n#             recall_list.append(recall)\n#             f1_list.append(f1)\n\n#         # Accumulate batch-level accuracy\n#         total_correct += batch_correct\n#         total_samples += batch_samples\n    \n#     test_accuracy = correct_test / total_samples\n#     print(f\"Test Accuracy: {test_accuracy:.4f}\")\n\n#     # Calculate average precision, recall, and F1-score across all classes\n#     avg_precision = sum(precision_list) / num_classes_classification\n#     avg_recall = sum(recall_list) / num_classes_classification\n#     avg_f1 = sum(f1_list) / num_classes_classification\n\n#     print(f\"Average Precision: {avg_precision:.4f}\")\n#     print(f\"Average Recall: {avg_recall:.4f}\")\n#     print(f\"Average F1 Score: {avg_f1:.4f}\")\n","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:38:52.58829Z","iopub.execute_input":"2023-09-17T16:38:52.588715Z","iopub.status.idle":"2023-09-17T16:56:15.047309Z","shell.execute_reply.started":"2023-09-17T16:38:52.588681Z","shell.execute_reply":"2023-09-17T16:56:15.045634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# import pandas as pd\n# import numpy as np\n# import nibabel as nib\n# import torch\n# import torch.nn as nn\n# import torch.optim as optim\n# from torch.utils.data import Dataset, DataLoader\n# from torchvision import transforms\n# from sklearn.metrics import accuracy_score, precision_recall_fscore_support, roc_auc_score\n# from sklearn.model_selection import train_test_split\n# from torchvision import models\n# from scipy.ndimage import zoom\n# import torch.nn.functional as F\n# from PIL import Image\n\n# # Function to preprocess NIfTI data and resize it\n# def preprocess_nifti(image_path, mask_path, desired_shape):\n#     image = nib.load(image_path).get_fdata()\n\n#     if pd.notna(mask_path):\n#         segmentation_mask = nib.load(mask_path).get_fdata()\n#     else:\n#         segmentation_mask = np.zeros_like(image)\n\n#     # Resize image and segmentation mask\n#     image = resize_nifti(image, desired_shape)\n#     segmentation_mask = resize_nifti(segmentation_mask, desired_shape)\n\n#     return image, segmentation_mask\n\n# # Define your CustomDataset class with preprocessed data\n# class CustomDataset(Dataset):\n#     def __init__(self, image_paths, mask_paths, labels, transform=None, desired_shape=(128, 128, 128)):\n#         self.image_paths = image_paths\n#         self.mask_paths = mask_paths\n#         self.labels = labels\n#         self.transform = transform\n#         self.desired_shape = desired_shape\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#         image, segmentation_mask = preprocess_nifti(image_path, mask_path, self.desired_shape)\n\n#         if self.transform:\n#             image = self.transform(image)\n#             segmentation_mask = self.transform(segmentation_mask)\n\n#         label = torch.tensor(self.labels[idx], dtype=torch.float32)\n\n#         return image, segmentation_mask, label\n# class MultiLabel3DAttentionModel(nn.Module):\n#     def __init__(self, num_classes, num_classes_segmentation):\n#         super(MultiLabel3DAttentionModel, self).__init__()\n\n#         # Load a pre-trained ResNet3D backbone\n#         self.backbone = models.video.r3d_18(pretrained=True)\n        \n               \n#         # Modify the stem to accept the correct input channels (128)\n#         self.backbone.stem[0] = nn.Sequential(\n#             nn.Conv3d(1, 64, kernel_size=(3, 7, 7), stride=(1, 2, 2), padding=(1, 3, 3)),\n#             nn.BatchNorm3d(64),\n#             nn.ReLU(inplace=True))\n\n#         # Attention block\n#         self.attention = nn.Sequential(\n#             nn.Conv3d(1, 128, kernel_size=1),\n#             nn.ReLU(inplace=True),\n#             nn.Conv3d(128, 1, kernel_size=1),\n#             nn.Sigmoid()\n#         )\n        \n#         # Classification head\n#         self.classification_head = nn.Sequential(\n#             nn.AdaptiveAvgPool3d(1),\n#             nn.Flatten(),\n#             nn.Linear(1, 64),\n#             nn.ReLU(inplace=True),\n#             nn.Linear(64, num_classes),\n#             nn.Sigmoid()\n#         )\n        \n#         # Segmentation head\n#         self.segmentation_head = nn.Sequential(\n#             nn.Conv3d(1, 128, kernel_size=1),\n#             nn.ReLU(inplace=True),\n#             nn.Conv3d(128, num_classes_segmentation, kernel_size=1),\n#             nn.Sigmoid()\n#         )\n        \n#     def forward(self, x, segmentation_mask):\n#         #print( '...............', x.dtype, segmentation_mask.dtype)\n#         print(\"X shape\", x.shape)\n        \n\n#         features = self.backbone(x)\n#         print(\"features shape\", features.shape)\n#         # Apply attention to features\n#         # Assuming features has shape [batch_size, num_features]\n#         # Reshape features to [batch_size, 1, 1, 1, num_features]\n#         features = features.view(features.size(0), 1, 1, 1, features.size(1))\n        \n#         attention_weights = self.attention(features)\n        \n#         print('attention_weights shape', attention_weights.shape)\n#         attended_features = features * attention_weights\n#         print('attended_features', attended_features.shape)\n        \n#         # Classification branch\n#         classification_output = self.classification_head(attended_features)\n        \n#         # Reshape attended_features to match segmentation_mask's shape along dimensions 2, 3, and 4\n#         #attended_features = attended_features.expand(-1, -1, 128, 128, 128)\n\n#         print('segmentation_mask', segmentation_mask.shape)\n        \n#         # Segmentation branch\n#         # Reshape attended_features to match segmentation_mask's shape along dimensions 2, 3, and 4\n#         #attended_features = attended_features.expand(-1, -1, segmentation_mask.size(2), segmentation_mask.size(3), segmentation_mask.size(4))\n        \n#         #segmentation_output = self.segmentation_head(attended_features) * segmentation_mask\n        \n#         # Modify the segmentation head to handle the different number of channels\n#         segmentation_output = self.segmentation_head(attended_features)\n#          # You can use interpolation or other methods to match the shape.\n#         segmentation_output = F.interpolate(segmentation_output, size=segmentation_mask.shape[2:], mode='trilinear')\n        \n#         print('segmentation_output', segmentation_output.shape)\n        \n#         segmentation_output = segmentation_output * segmentation_mask  # Element-wise multiplication\n        \n        \n#         return classification_output, segmentation_output\n\n# # Paths and settings\n# segmentation_dir = '/kaggle/input/rsna-2023-abdominal-trauma-detection/segmentations'  # Update with the correct path\n# csv_file = '/kaggle/input/train-paths-abdominalcsv/finalabdominal.csv'  # Update with the correct path\n# batch_size = 1\n# num_workers = 4  # Number of CPU cores to use for data loading\n# num_classes = 14  # Number of classes\n# desired_shape = (128, 128, 128)\n# device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n\n# # Define transformations if needed\n# transform = 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)\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.3, 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# train_dataset = CustomDataset(train_paths, train_mask_paths, train_labels, transform=transform, desired_shape=desired_shape)\n# val_dataset = CustomDataset(val_paths, val_mask_paths, val_labels, transform=transform, desired_shape=desired_shape)\n# test_dataset = CustomDataset(test_paths, test_mask_paths, test_labels, transform=transform, desired_shape=desired_shape)\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# 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\n        \n# # Instantiate the model with the appropriate number of classes for both classification and segmentation\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)\n# model = MultiLabel3DAttentionModel(num_classes_classification, num_classes_segmentation).to(device)\n\n\n# # Define loss function and optimizer\n# criterion = nn.BCELoss()  # Binary Cross-Entropy loss\n# optimizer = optim.Adam(model.parameters(), lr=0.001)\n\n# # def print_parameter_data_types(module):\n# #     for name, param in module.named_parameters():\n# #         print(f\"Parameter: {name}, Data Type: {param.dtype}\")\n\n# # # Call the function to print data types of parameters in the backbone module\n# # print_parameter_data_types(model.backbone)\n\n\n# # Training loop\n# num_epochs = 1\n# for 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(device).to(torch.float32)\n#         batch_segmentation_masks = batch_segmentation_masks.to(device).to(torch.float32)\n#         batch_labels = batch_labels.to(device).to(torch.float32)\n\n#         # Forward pass and loss calculation\n#         classification_outputs, segmentation_outputs = model(batch_images, batch_segmentation_masks)\n#         classification_outputs = torch.sigmoid(classification_outputs)\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 = criterion(class_outputs, class_labels)\n#             losses.append(class_loss)\n\n#         # Calculate the total loss as the sum of individual class losses\n#         total_loss = sum(losses)\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 = criterion(segmentation_outputs, batch_segmentation_masks)\n#             total_loss += segmentation_loss\n\n#         running_loss += total_loss.item()\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        \n#         # Backpropagation and optimization\n#         total_loss.backward()\n#         optimizer.step()\n\n#     # Calculate and print average training accuracy and loss\n#     avg_train_accuracy = correct_train / total_train\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\n#     # Validation loop\n#     model.eval()\n#     total_val_loss = 0.0\n#     correct_val = 0\n#     total_val = 0\n\n#     with torch.no_grad():\n#         for batch_images, batch_segmentation_masks, batch_labels in val_loader:\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#             batch_images = batch_images.unsqueeze(1)\n#             batch_segmentation_masks = batch_segmentation_masks.unsqueeze(1)\n\n#             # Forward pass\n#             classification_outputs, segmentation_outputs = model(batch_images, batch_segmentation_masks)\n            \n#             # Apply sigmoid activation to the classification outputs\n#             classification_outputs = torch.sigmoid(classification_outputs)\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]\n#                 class_outputs = classification_outputs[:, class_index]\n#                 class_loss = criterion(class_outputs, class_labels)\n#                 losses.append(class_loss)\n\n#             total_loss = sum(losses)\n\n#             # Check if segmentation mask is available\n#             if batch_segmentation_masks is not None:\n#                 batch_segmentation_masks = batch_segmentation_masks.to(torch.float64)\n                \n#                 # Apply sigmoid activation to segmentation_outputs\n#                 segmentation_outputs = torch.sigmoid(segmentation_outputs)\n#                 segmentation_outputs = segmentation_outputs.to(torch.float64)\n                \n#                 segmentation_loss = criterion(segmentation_outputs, batch_segmentation_masks)\n#                 total_loss = total_loss + segmentation_loss\n            \n#             total_val_loss += total_loss.item()\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]\n#                 class_outputs = classification_outputs[:, class_index]\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#             batch_accuracy = sum(accuracies) / num_classes_classification\n#             correct_val += batch_accuracy\n#             total_val += batch_labels.size(0)\n\n#     val_accuracy = correct_val / total_val\n#     avg_val_loss = total_val_loss / len(val_loader)\n\n#     print(f\"Validation Accuracy: {val_accuracy:.4f} | Validation Loss: {avg_val_loss:.4f}\")\n\n# # Test loop\n# model.eval()\n# total_correct = 0\n# total_samples = 0\n# # Initialize lists to store per-class metrics\n# precision_list = []\n# recall_list = []\n# f1_list = []\n\n# with torch.no_grad():\n#     for batch_images, batch_segmentation_masks, batch_labels in test_loader:\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#         batch_images = batch_images.unsqueeze(1)\n#         batch_segmentation_masks = batch_segmentation_masks.unsqueeze(1)\n\n#         # Forward pass\n#         classification_outputs, segmentation_outputs = model(batch_images, batch_segmentation_masks)\n        \n#         # Apply sigmoid activation to the classification outputs\n#         classification_outputs = torch.sigmoid(classification_outputs)\n            \n#         # Initialize batch-level variables for accuracy calculation\n#         batch_correct = 0\n#         batch_samples = batch_labels.size(0)\n        \n#         for class_index in range(num_classes_classification):\n#             class_labels = batch_labels[:, class_index]\n#             class_outputs = classification_outputs[:, class_index]\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#             batch_correct += class_accuracy\n            \n#             # Calculate precision, recall, and F1-score for the current class\n#             precision, recall, f1, _ = precision_recall_fscore_support(\n#                 class_labels.cpu(), predicted.cpu(), average='binary')\n            \n#             precision_list.append(precision)\n#             recall_list.append(recall)\n#             f1_list.append(f1)\n\n#         # Accumulate batch-level accuracy\n#         total_correct += batch_correct\n#         total_samples += batch_samples\n    \n#     test_accuracy = correct_test / total_samples\n#     print(f\"Test Accuracy: {test_accuracy:.4f}\")\n\n#     # Calculate average precision, recall, and F1-score across all classes\n#     avg_precision = sum(precision_list) / num_classes_classification\n#     avg_recall = sum(recall_list) / num_classes_classification\n#     avg_f1 = sum(f1_list) / num_classes_classification\n\n#     print(f\"Average Precision: {avg_precision:.4f}\")\n#     print(f\"Average Recall: {avg_recall:.4f}\")\n#     print(f\"Average F1 Score: {avg_f1:.4f}\")\n\n        \n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# import pandas as pd\n# import numpy as np\n# import nibabel as nib\n# import torch\n# import torch.nn as nn\n# import torch.optim as optim\n# from torchvision import transforms\n# from sklearn.metrics import accuracy_score, precision_recall_fscore_support, roc_auc_score\n# from sklearn.model_selection import train_test_split\n# from torchvision import models\n# from scipy.ndimage import zoom\n# import torch.nn.functional as F\n# from PIL import Image\n\n# # Custom Dataset Class\n# class CustomDataset:\n#     def __init__(self, image_paths, mask_paths, labels, transform=None):\n#         self.image_paths = image_paths\n#         self.mask_paths = mask_paths\n#         self.labels = labels\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\n#         # Load the segmentation mask if available\n#         segmentation_mask = None\n#         if pd.notna(mask_path):\n#             segmentation_mask = nib.load(mask_path).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#         label = torch.tensor(self.labels[idx], dtype=torch.float32)\n\n#         return image, segmentation_mask, label\n\n# # Multi-Label 3D Attention Model Class\n# class MultiLabel3DAttentionModel(nn.Module):\n#     def __init__(self, num_classes, num_classes_segmentation):\n#         super(MultiLabel3DAttentionModel, self).__init__()\n\n#         # Load a pre-trained ResNet3D backbone\n#         self.backbone = models.video.r3d_18(pretrained=True)\n        \n               \n#         # Modify the stem to accept the correct input channels (128)\n#         self.backbone.stem[0] = nn.Sequential(\n#             nn.Conv3d(1, 64, kernel_size=(3, 7, 7), stride=(1, 2, 2), padding=(1, 3, 3)),\n#             nn.BatchNorm3d(64),\n#             nn.ReLU(inplace=True))\n\n#         # Attention block\n#         self.attention = nn.Sequential(\n#             nn.Conv3d(1, 128, kernel_size=1),\n#             nn.ReLU(inplace=True),\n#             nn.Conv3d(128, 1, kernel_size=1),\n#             nn.Sigmoid()\n#         )\n        \n#         # Classification head\n#         self.classification_head = nn.Sequential(\n#             nn.AdaptiveAvgPool3d(1),\n#             nn.Flatten(),\n#             nn.Linear(1, 64),\n#             nn.ReLU(inplace=True),\n#             nn.Linear(64, num_classes),\n#             nn.Sigmoid()\n#         )\n        \n#         # Segmentation head\n#         self.segmentation_head = nn.Sequential(\n#             nn.Conv3d(1, 128, kernel_size=1),\n#             nn.ReLU(inplace=True),\n#             nn.Conv3d(128, num_classes_segmentation, kernel_size=1),\n#             nn.Sigmoid()\n#         )\n        \n#     def forward(self, x, segmentation_mask):\n#         #print( '...............', x.dtype, segmentation_mask.dtype)\n#         print(\"X shape\", x.shape)\n        \n\n#         features = self.backbone(x)\n#         print(\"features shape\", features.shape)\n#         # Apply attention to features\n#         # Assuming features has shape [batch_size, num_features]\n#         # Reshape features to [batch_size, 1, 1, 1, num_features]\n#         features = features.view(features.size(0), 1, 1, 1, features.size(1))\n        \n#         attention_weights = self.attention(features)\n        \n#         print('attention_weights shape', attention_weights.shape)\n#         attended_features = features * attention_weights\n#         print('attended_features', attended_features.shape)\n        \n#         # Classification branch\n#         classification_output = self.classification_head(attended_features)\n        \n#         # Reshape attended_features to match segmentation_mask's shape along dimensions 2, 3, and 4\n#         #attended_features = attended_features.expand(-1, -1, 128, 128, 128)\n\n#         print('segmentation_mask', segmentation_mask.shape)\n        \n#         # Segmentation branch\n#         # Reshape attended_features to match segmentation_mask's shape along dimensions 2, 3, and 4\n#         #attended_features = attended_features.expand(-1, -1, segmentation_mask.size(2), segmentation_mask.size(3), segmentation_mask.size(4))\n        \n#         #segmentation_output = self.segmentation_head(attended_features) * segmentation_mask\n        \n#         # Modify the segmentation head to handle the different number of channels\n#         segmentation_output = self.segmentation_head(attended_features)\n#          # You can use interpolation or other methods to match the shape.\n#         segmentation_output = F.interpolate(segmentation_output, size=segmentation_mask.shape[2:], mode='trilinear')\n        \n#         print('segmentation_output', segmentation_output.shape)\n        \n#         segmentation_output = segmentation_output * segmentation_mask  # Element-wise multiplication\n        \n        \n#         return classification_output, segmentation_output\n#     # ... (Rest of the model definition as in your code)\n\n# # Paths and settings\n# segmentation_dir = '/kaggle/input/rsna-2023-abdominal-trauma-detection/segmentations'  # Update with the correct path\n# csv_file = '/kaggle/input/train-paths-abdominalcsv/finalabdominal.csv'  # Update with the correct path\n# batch_size = 4\n# num_workers = 4  # Number of CPU cores to use for data loading\n# num_classes = 14  # Number of classes\n# desired_shape = (128, 128, 128)\n# device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n\n# # Define transformations if needed\n# transform = 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)\n\n# # Split the data into training, validation, and test sets\n# train_data, temp_data = train_test_split(data, test_size=0.3, random_state=42)\n# val_data, test_data = train_test_split(temp_data, test_size=0.5, random_state=42)\n\n# # Instantiate the datasets\n# train_dataset = CustomDataset(train_data['file_path'].values, train_data['mask_path'].values, 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, transform=transform)\n# val_dataset = CustomDataset(val_data['file_path'].values, val_data['mask_path'].values, 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, transform=transform)\n# test_dataset = CustomDataset(test_data['file_path'].values, test_data['mask_path'].values, 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, transform=transform)\n\n# # Instantiate the model with the appropriate number of classes for both classification and segmentation\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)\n# model = MultiLabel3DAttentionModel(num_classes_classification, num_classes_segmentation)\n\n# # Define loss function and optimizer\n# criterion = nn.BCELoss()  # Binary Cross-Entropy loss\n# optimizer = optim.Adam(model.parameters(), lr=0.001)\n\n# # Training loop\n# num_epochs = 1\n\n# for 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_idx in range(0, len(train_dataset), batch_size):\n#         batch_images = []\n#         batch_masks = []\n#         batch_labels = []\n        \n#         for idx in range(batch_idx, min(batch_idx + batch_size, len(train_dataset))):\n#             image, segmentation_mask, label = train_dataset[idx]\n#             batch_images.append(image)\n#             batch_masks.append(segmentation_mask)\n#             batch_labels.append(label)\n        \n#         batch_images = torch.stack(batch_images)\n#         batch_masks = torch.stack(batch_masks)\n#         batch_labels = torch.stack(batch_labels)\n\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_masks = batch_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_masks = batch_masks.unsqueeze(1)\n\n#         # Forward pass\n#         classification_outputs, segmentation_outputs = model(batch_images, batch_masks)\n        \n#         # Apply sigmoid activation to the classification outputs\n#         classification_outputs = torch.sigmoid(classification_outputs)\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 = criterion(class_outputs, class_labels)\n#             losses.append(class_loss)\n\n#         # Calculate the total loss as the sum of individual class losses\n#         total_loss = sum(losses)\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 = criterion(segmentation_outputs, batch_segmentation_masks)\n\n#             # Add segmentation loss to the total loss\n#             total_loss += segmentation_loss\n\n#         # Backpropagation and optimization\n#         total_loss.backward()\n#         optimizer.step()\n\n#         # Calculate accuracy for classification\n#         predicted = (classification_outputs > 0.5).float()\n#         total_train += batch_labels.size(0)\n#         correct_train += (predicted == batch_labels).sum().item()\n\n#         # Print statistics\n#         running_loss += total_loss.item()\n\n#     # Print statistics at the end of the epoch\n#     print(f\"Epoch {epoch+1}/{num_epochs}, Loss: {running_loss:.4f}, Train Accuracy: {correct_train/total_train*100:.2f}%\")\n\n# # Validation loop\n# model.eval()\n# val_loss = 0.0\n# correct_val = 0\n# total_val = 0\n# predictions = []\n# true_labels = []\n\n# with torch.no_grad():\n#     for batch_idx in range(0, len(val_dataset), batch_size):\n#         batch_images = []\n#         batch_masks = []\n#         batch_labels = []\n\n#         for idx in range(batch_idx, min(batch_idx + batch_size, len(val_dataset))):\n#             image, segmentation_mask, label = val_dataset[idx]\n#             batch_images.append(image)\n#             batch_masks.append(segmentation_mask)\n#             batch_labels.append(label)\n\n#         batch_images = torch.stack(batch_images)\n#         batch_masks = torch.stack(batch_masks)\n#         batch_labels = torch.stack(batch_labels)\n\n#         # Move data to the GPU if available\n#         batch_images = batch_images.to(torch.float32).to(device)\n#         batch_masks = batch_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_masks = batch_masks.unsqueeze(1)\n\n#         # Forward pass\n#         classification_outputs, segmentation_outputs = model(batch_images, batch_masks)\n\n#         # Apply sigmoid activation to the classification outputs\n#         classification_outputs = torch.sigmoid(classification_outputs)\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 = criterion(class_outputs, class_labels)\n#             losses.append(class_loss)\n\n#         # Calculate the total loss as the sum of individual class losses\n#         total_loss = sum(losses)\n#         val_loss += total_loss.item()\n\n#         # Calculate accuracy for classification\n#         predicted = (classification_outputs > 0.5).float()\n#         total_val += batch_labels.size(0)\n#         correct_val += (predicted == batch_labels).sum().item()\n\n#         # Collect predictions and true labels\n#         predictions.extend(predicted.cpu().numpy())\n#         true_labels.extend(batch_labels.cpu().numpy())\n\n# # Calculate validation accuracy and other metrics\n# val_accuracy = correct_val / total_val * 100\n# val_loss /= len(val_dataset)\n# val_precision, val_recall, val_f1, _ = precision_recall_fscore_support(true_labels, predictions, average='weighted')\n# val_roc_auc = roc_auc_score(true_labels, predictions, average='weighted')\n\n# print(f\"Validation Loss: {val_loss:.4f}\")\n# print(f\"Validation Accuracy: {val_accuracy:.2f}%\")\n# print(f\"Validation Precision: {val_precision:.2f}\")\n# print(f\"Validation Recall: {val_recall:.2f}\")\n# print(f\"Validation F1 Score: {val_f1:.2f}\")\n# print(f\"Validation ROC AUC Score: {val_roc_auc:.2f}\")\n\n# # Test loop (similar to the validation loop)\n# # ... (You can implement the test loop in a similar way as the validation loop)\n\n# # Save the model\n# torch.save(model.state_dict(), 'model_weights.pth')\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Ad\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# import pandas as pd\n# import numpy as np\n# import nibabel as nib\n# import torch\n# import torch.nn as nn\n# import torch.optim as optim\n# from torchvision import transforms\n# from sklearn.metrics import accuracy_score, precision_recall_fscore_support, roc_auc_score\n# from sklearn.model_selection import train_test_split\n# from torchvision import models\n# from scipy.ndimage import zoom\n# import torch.nn.functional as F\n# from PIL import Image\n\n# # Custom Dataset Class\n# class CustomDataset:\n#     def __init__(self, image_paths, mask_paths, labels, transform=None):\n#         self.image_paths = image_paths\n#         self.mask_paths = mask_paths\n#         self.labels = labels\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\n#         # Load the segmentation mask if available\n#         segmentation_mask = None\n#         if pd.notna(mask_path):\n#             segmentation_mask = nib.load(mask_path).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#         label = torch.tensor(self.labels[idx], dtype=torch.float32)\n\n#         return image, segmentation_mask, label\n\n# # Multi-Label 3D Attention Model Class\n# class MultiLabel3DAttentionModel(nn.Module):\n#     # ... (Rest of the model definition as in your code)\n#     def __init__(self, num_classes, num_classes_segmentation):\n#         super(MultiLabel3DAttentionModel, self).__init__()\n\n#         # Load a pre-trained ResNet3D backbone\n#         self.backbone = models.video.r3d_18(pretrained=True)\n        \n               \n#         # Modify the stem to accept the correct input channels (128)\n#         self.backbone.stem[0] = nn.Sequential(\n#             nn.Conv3d(1, 64, kernel_size=(3, 7, 7), stride=(1, 2, 2), padding=(1, 3, 3)),\n#             nn.BatchNorm3d(64),\n#             nn.ReLU(inplace=True))\n\n#         # Attention block\n#         self.attention = nn.Sequential(\n#             nn.Conv3d(1, 128, kernel_size=1),\n#             nn.ReLU(inplace=True),\n#             nn.Conv3d(128, 1, kernel_size=1),\n#             nn.Sigmoid()\n#         )\n        \n#         # Classification head\n#         self.classification_head = nn.Sequential(\n#             nn.AdaptiveAvgPool3d(1),\n#             nn.Flatten(),\n#             nn.Linear(1, 64),\n#             nn.ReLU(inplace=True),\n#             nn.Linear(64, num_classes),\n#             nn.Sigmoid()\n#         )\n        \n#         # Segmentation head\n#         self.segmentation_head = nn.Sequential(\n#             nn.Conv3d(1, 128, kernel_size=1),\n#             nn.ReLU(inplace=True),\n#             nn.Conv3d(128, num_classes_segmentation, kernel_size=1),\n#             nn.Sigmoid()\n#         )\n        \n#     def forward(self, x, segmentation_mask):\n#         #print( '...............', x.dtype, segmentation_mask.dtype)\n#         print(\"X shape\", x.shape)\n        \n\n#         features = self.backbone(x)\n#         print(\"features shape\", features.shape)\n#         # Apply attention to features\n#         # Assuming features has shape [batch_size, num_features]\n#         # Reshape features to [batch_size, 1, 1, 1, num_features]\n#         features = features.view(features.size(0), 1, 1, 1, features.size(1))\n        \n#         attention_weights = self.attention(features)\n        \n#         print('attention_weights shape', attention_weights.shape)\n#         attended_features = features * attention_weights\n#         print('attended_features', attended_features.shape)\n        \n#         # Classification branch\n#         classification_output = self.classification_head(attended_features)\n        \n#         # Reshape attended_features to match segmentation_mask's shape along dimensions 2, 3, and 4\n#         #attended_features = attended_features.expand(-1, -1, 128, 128, 128)\n\n#         print('segmentation_mask', segmentation_mask.shape)\n        \n#         # Segmentation branch\n#         # Reshape attended_features to match segmentation_mask's shape along dimensions 2, 3, and 4\n#         #attended_features = attended_features.expand(-1, -1, segmentation_mask.size(2), segmentation_mask.size(3), segmentation_mask.size(4))\n        \n#         #segmentation_output = self.segmentation_head(attended_features) * segmentation_mask\n        \n#         # Modify the segmentation head to handle the different number of channels\n#         segmentation_output = self.segmentation_head(attended_features)\n#          # You can use interpolation or other methods to match the shape.\n#         segmentation_output = F.interpolate(segmentation_output, size=segmentation_mask.shape[2:], mode='trilinear')\n        \n#         print('segmentation_output', segmentation_output.shape)\n        \n#         segmentation_output = segmentation_output * segmentation_mask  # Element-wise multiplication\n        \n        \n#         return classification_output, segmentation_output\n\n# # Paths and settings\n# segmentation_dir = '/kaggle/input/rsna-2023-abdominal-trauma-detection/segmentations'  # Update with the correct path\n# csv_file = '/kaggle/input/train-paths-abdominalcsv/finalabdominal.csv'  # Update with the correct path\n# batch_size = 1\n# num_workers = 4  # Number of CPU cores to use for data loading\n# num_classes = 14  # Number of classes\n# desired_shape = (128, 128, 128)\n# device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n\n# # Define transformations if needed\n# transform = 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)\n\n# # Split the data into training, validation, and test sets\n# train_data, temp_data = train_test_split(data, test_size=0.3, random_state=42)\n# val_data, test_data = train_test_split(temp_data, test_size=0.5, random_state=42)\n\n# # Instantiate the datasets\n# train_dataset = CustomDataset(train_data['file_path'].values, train_data['mask_path'].values, 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, transform=transform)\n# val_dataset = CustomDataset(val_data['file_path'].values, val_data['mask_path'].values, 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, transform=transform)\n# test_dataset = CustomDataset(test_data['file_path'].values, test_data['mask_path'].values, 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, transform=transform)\n\n# # Instantiate the model with the appropriate number of classes for both classification and segmentation\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)\n# model = MultiLabel3DAttentionModel(num_classes_classification, num_classes_segmentation)\n\n# # Move the model to the device\n# model.to(device)\n\n# # Define loss function and optimizer\n# criterion = nn.BCELoss()  # Binary Cross-Entropy loss\n# optimizer = optim.Adam(model.parameters(), lr=0.001)\n\n# # Training loop\n# num_epochs = 1\n\n# for epoch in range(num_epochs):\n#     model.train()\n#     running_loss = 0.0\n#     correct_train = 0\n#     total_train = 0\n    \n#     indices = np.arange(len(train_dataset))\n#     np.random.shuffle(indices)\n    \n#     for batch_start in range(0, len(train_dataset), batch_size):\n#         batch_indices = indices[batch_start:batch_start+batch_size]\n#         batch_data = [train_dataset[i] for i in batch_indices]\n        \n#         images, segmentation_masks, labels = zip(*batch_data)\n#         images = torch.stack(images)\n#         labels = torch.stack(labels)\n        \n#         # Combine and stack the segmentation masks\n#         stacked_segmentation_masks = []\n#         for mask in segmentation_masks:\n#             stacked_mask = torch.stack([mask] * images.size(1))  # Repeat mask along the temporal dimension\n#             stacked_segmentation_masks.append(stacked_mask)\n#         segmentation_masks = torch.stack(stacked_segmentation_masks)\n        \n#         optimizer.zero_grad()\n        \n#         images = images.to(device).to(torch.float32)\n#         segmentation_masks = segmentation_masks.to(device).to(torch.float32)\n#         labels = labels.to(device).to(torch.float32)\n\n#         images = images.unsqueeze(1)\n#         segmentation_masks = segmentation_masks.unsqueeze(1)\n\n#         classification_outputs, segmentation_outputs = model(images, segmentation_masks)\n        \n#         classification_outputs = torch.sigmoid(classification_outputs)\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 = criterion(class_outputs, class_labels)\n#             losses.append(class_loss)\n\n#         # Calculate the total loss as the sum of individual class losses\n#         total_loss = sum(losses)\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 = criterion(segmentation_outputs, batch_segmentation_masks)\n\n#             # Add segmentation loss to the total loss\n#             total_loss += segmentation_loss\n\n#         # Backpropagation and optimization\n#         total_loss.backward()\n#         optimizer.step()\n\n#         # Calculate accuracy for classification\n#         predicted = (classification_outputs > 0.5).float()\n#         total_train += batch_labels.size(0)\n#         correct_train += (predicted == batch_labels).sum().item()\n\n#         # Print statistics\n#         running_loss += total_loss.item()\n\n#     # Print statistics at the end of the epoch\n#     print(f\"Epoch {epoch+1}/{num_epochs}, Loss: {running_loss:.4f}, Train Accuracy: {correct_train/total_train*100:.2f}%\")\n\n# # Validation loop\n\n# model.eval()\n#     total_val_loss = 0.0\n#     correct_val = 0\n#     total_val = 0\n\n#     with torch.no_grad():\n#         for batch_start in range(0, len(val_dataset), batch_size):\n#             batch_indices = indices[batch_start:batch_start+batch_size]\n#             batch_data = [val_dataset[i] for i in batch_indices]\n            \n#             images, segmentation_masks, labels = zip(*batch_data)\n#             images = torch.stack(images)\n#             segmentation_masks = torch.stack(segmentation_masks)\n#             labels = torch.stack(labels)\n\n#             images = images.to(torch.float32).to(device)\n#             segmentation_masks = segmentation_masks.to(torch.float32).to(device)\n#             labels = labels.to(torch.float32).to(device)\n            \n#             images = images.unsqueeze(1)\n#             segmentation_masks = segmentation_masks.unsqueeze(1)\n\n#             classification_outputs, segmentation_outputs = model(images, segmentation_masks)\n            \n#             classification_outputs = torch.sigmoid(classification_outputs)\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 = criterion(class_outputs, class_labels)\n#             losses.append(class_loss)\n\n#         # Calculate the total loss as the sum of individual class losses\n#         total_loss = sum(losses)\n#         val_loss += total_loss.item()\n\n#         # Calculate accuracy for classification\n#         predicted = (classification_outputs > 0.5).float()\n#         total_val += batch_labels.size(0)\n#         correct_val += (predicted == batch_labels).sum().item()\n\n#         # Collect predictions and true labels\n#         predictions.extend(predicted.cpu().numpy())\n#         true_labels.extend(batch_labels.cpu().numpy())\n\n# # Calculate validation accuracy and other metrics\n# val_accuracy = correct_val / total_val * 100\n# val_loss /= len(val_dataset)\n# val_precision, val_recall, val_f1, _ = precision_recall_fscore_support(true_labels, predictions, average='weighted')\n# val_roc_auc = roc_auc_score(true_labels, predictions, average='weighted')\n\n# print(f\"Validation Loss: {val_loss:.4f}\")\n# print(f\"Validation Accuracy: {val_accuracy:.2f}%\")\n# print(f\"Validation Precision: {val_precision:.2f}\")\n# print(f\"Validation Recall: {val_recall:.2f}\")\n# print(f\"Validation F1 Score: {val_f1:.2f}\")\n# print(f\"Validation ROC AUC Score: {val_roc_auc:.2f}\")\n\n# # Test loop (similar to the validation loop)\n# # ... (You can implement the test loop in a similar way as the validation loop)\n\n# # Save the model\n# torch.save(model.state_dict(), 'model_weights.pth')\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-09-17T16:38:35.300228Z","iopub.execute_input":"2023-09-17T16:38:35.300683Z","iopub.status.idle":"2023-09-17T16:38:35.490205Z","shell.execute_reply.started":"2023-09-17T16:38:35.300644Z","shell.execute_reply":"2023-09-17T16:38:35.489276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# del my_tensor","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}