{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceType":"competition","sourceId":36363,"databundleVersionId":4050810},{"sourceType":"datasetVersion","sourceId":6874921,"datasetId":3950522,"databundleVersionId":6960797},{"sourceType":"datasetVersion","sourceId":6665364,"datasetId":3846147,"databundleVersionId":6749380},{"sourceType":"datasetVersion","sourceId":6403207,"datasetId":3692048,"databundleVersionId":6484417},{"sourceType":"datasetVersion","sourceId":7659622,"datasetId":3607309,"databundleVersionId":7756574},{"sourceType":"datasetVersion","sourceId":7213323,"datasetId":4174006,"databundleVersionId":7302824},{"sourceType":"datasetVersion","sourceId":7229250,"datasetId":4185447,"databundleVersionId":7318838},{"sourceType":"datasetVersion","sourceId":7226996,"datasetId":4183793,"databundleVersionId":7316574}],"dockerImageVersionId":30559,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install --upgrade pip --quiet\n!pip install nibabel --quiet\n!pip install torch torchvision torchaudio --quiet","metadata":{"execution":{"iopub.status.busy":"2024-03-07T18:59:48.13245Z","iopub.execute_input":"2024-03-07T18:59:48.133234Z","iopub.status.idle":"2024-03-07T19:00:35.668061Z","shell.execute_reply.started":"2024-03-07T18:59:48.133198Z","shell.execute_reply":"2024-03-07T19:00:35.666987Z"},"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\n# Constants and configuration settings\nsegmentation_dir = '/kaggle/input/rsna-2022-cervical-spine-fracture-detection/segmentations'\ncsv_file = '/kaggle/input/file-mask-path/train_file_mask_path.csv'\nbatch_size = 4\nnum_workers = 4\nnum_classes = 7\ndesired_shape = (128, 128, 128)\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-03-07T19:00:35.669979Z","iopub.execute_input":"2024-03-07T19:00:35.670296Z","iopub.status.idle":"2024-03-07T19:00:39.965554Z","shell.execute_reply.started":"2024-03-07T19:00:35.67027Z","shell.execute_reply":"2024-03-07T19:00:39.964469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Specify the path to your CSV file containing data\ncsv_file_path = '/kaggle/input/file-mask-path/train_file_mask_path.csv'\n\n# Load the CSV data using pandas\ndata_frame = pd.read_csv(csv_file_path)\n\n# Extract relevant information from the data\nimage_paths = data_frame['file_path'].values\nmask_paths = data_frame['mask_path'].values\nlabels = data_frame[['C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'patient_overall']].values\n","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:00:39.966952Z","iopub.execute_input":"2024-03-07T19:00:39.967563Z","iopub.status.idle":"2024-03-07T19:00:40.023932Z","shell.execute_reply.started":"2024-03-07T19:00:39.967523Z","shell.execute_reply":"2024-03-07T19:00:40.02295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import nibabel as nib\nimport pandas as pd\nimport torch\nfrom torch.utils.data import Dataset\n\nclass 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        image = nib.load(image_path).get_fdata()\n        segmentation_mask = None\n\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        if self.transform:\n            image = self.transform(image)\n\n        if segmentation_mask is not None and self.transform:\n            \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","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:00:40.025985Z","iopub.execute_input":"2024-03-07T19:00:40.026315Z","iopub.status.idle":"2024-03-07T19:00:40.0362Z","shell.execute_reply.started":"2024-03-07T19:00:40.026289Z","shell.execute_reply":"2024-03-07T19:00:40.035301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create an instance of the custom dataset\ndataset = CustomDataset(image_paths, mask_paths, labels, transform=None)\n\n# Define the index of the sample you want to access\nsample_index = 0  # You can change this to any index you're interested in\n\n# Access the image and mask paths for the specific sample\nimage_path = dataset.image_paths[sample_index]\nmask_path = dataset.mask_paths[sample_index]","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:00:40.037224Z","iopub.execute_input":"2024-03-07T19:00:40.037456Z","iopub.status.idle":"2024-03-07T19:00:40.049594Z","shell.execute_reply.started":"2024-03-07T19:00:40.037435Z","shell.execute_reply":"2024-03-07T19:00:40.048747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torchvision.models as models\nfrom scipy.ndimage import zoom\n# Function to resize NIfTI data\ndef resize_nifti(nifti_data, target_shape):\n    factors = (target_shape[0] / nifti_data.shape[0],\n               target_shape[1] / nifti_data.shape[1],\n               target_shape[2] / nifti_data.shape[2])\n    resized_data = zoom(nifti_data, factors, order=3)  # Cubic interpolation (higher quality)\n    return resized_data\n","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:00:40.050696Z","iopub.execute_input":"2024-03-07T19:00:40.052254Z","iopub.status.idle":"2024-03-07T19:00:40.059866Z","shell.execute_reply.started":"2024-03-07T19:00:40.052229Z","shell.execute_reply":"2024-03-07T19:00:40.058939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torchvision import models\nfrom torchvision.models.video import swin3d_b, Swin3D_B_Weights\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.swin3d_b(pretrained=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 + 1),\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        # Feature extraction with the backbone\n#         print(x.shape, \"........................\")\n        transforms = Swin3D_B_Weights.KINETICS400_V1.transforms()\n        #x = transforms(torch.rand(4, 16, 3, 128, 128))\n        x = transforms(x)\n#         print(x.shape,\"...........after transformation...........\")\n        features = self.backbone(x)\n        \n#         print(\"features shape = \",features.shape)\n\n        # Apply attention to features\n        features = features.view(features.size(0), 1, 1, 1, features.size(1))\n        attention_weights = self.attention(features)\n        attended_features = features * attention_weights\n\n        # Classification branch\n        classification_output = self.classification_head(attended_features)\n        \n        # Initialize segmentation_output as None\n        segmentation_output = None\n\n        # Check if segmentation_mask is None\n        if segmentation_mask is not None:\n            \n            # Segmentation branch\n            segmentation_output = self.segmentation_head(attended_features)\n            segmentation_output = F.interpolate(segmentation_output, size=segmentation_mask.shape[2:], mode='trilinear')\n            segmentation_output = segmentation_output * segmentation_mask\n            \n        else:\n            # No segmentation branch in the test phase\n            segmentation_mask = torch.zeros_like(x)\n\n        return classification_output, segmentation_output\n","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:00:40.060869Z","iopub.execute_input":"2024-03-07T19:00:40.061136Z","iopub.status.idle":"2024-03-07T19:00:40.075607Z","shell.execute_reply.started":"2024-03-07T19:00:40.061113Z","shell.execute_reply":"2024-03-07T19:00:40.074797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform2 = transforms.Compose([\n    transforms.ToTensor(),  # Convert to tensor\n    # Add more transformations if necessary\n])","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:00:40.076567Z","iopub.execute_input":"2024-03-07T19:00:40.076818Z","iopub.status.idle":"2024-03-07T19:00:40.088842Z","shell.execute_reply.started":"2024-03-07T19:00:40.076786Z","shell.execute_reply":"2024-03-07T19:00:40.088025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\n# Load the CSV file\ndata = pd.read_csv(csv_file)\ndata_length = len(data)\nprint(\"Length of DataFrame:\", data_length)\n\n# Remove leading and trailing whitespaces from column names\ndata.columns = data.columns.str.strip()\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# train_data.to_csv('exp_train.csv', index=False)\n# test_data.to_csv('exp_test.csv', index=False)\n# val_data.to_csv('exp_val.csv', index=False)\ntrain_data = pd.read_csv('/kaggle/input/data-divided/exp_train.csv')\ntest_data = pd.read_csv('/kaggle/input/data-divided/exp_test.csv')\nval_data = pd.read_csv('/kaggle/input/data-divided/exp_val.csv')\ntrain_data = train_data[:12]\nval_data = val_data[:12]\n# Limit the number of samples for testing purposes\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[['C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'patient_overall']].values\n\nval_paths = val_data['file_path'].values\nval_mask_paths = val_data['mask_path'].values\nval_labels = val_data[['C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'patient_overall']].values\n\ntest_paths = test_data['file_path'].values\ntest_mask_paths = test_data['mask_path'].values\ntest_labels = test_data[['C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'patient_overall']].values\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:00:40.089998Z","iopub.execute_input":"2024-03-07T19:00:40.090534Z","iopub.status.idle":"2024-03-07T19:00:40.159438Z","shell.execute_reply.started":"2024-03-07T19:00:40.090501Z","shell.execute_reply":"2024-03-07T19:00:40.158539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Instantiate the datasets\ntrain_dataset = CustomDataset(train_paths, train_mask_paths, train_labels, transform=transform2)\nval_dataset = CustomDataset(val_paths, val_mask_paths, val_labels, transform=transform2)\ntest_dataset = CustomDataset(test_paths, test_mask_paths, test_labels, transform=transform2)","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:00:40.163361Z","iopub.execute_input":"2024-03-07T19:00:40.164118Z","iopub.status.idle":"2024-03-07T19:00:40.16909Z","shell.execute_reply.started":"2024-03-07T19:00:40.164081Z","shell.execute_reply":"2024-03-07T19:00:40.168204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Instantiate the data loaders\ntrain_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=num_workers, drop_last=True)\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)","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:00:40.170216Z","iopub.execute_input":"2024-03-07T19:00:40.17054Z","iopub.status.idle":"2024-03-07T19:00:40.180614Z","shell.execute_reply.started":"2024-03-07T19:00:40.170508Z","shell.execute_reply":"2024-03-07T19:00:40.179762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Instantiate the model with the appropriate number of classes for both classification and segmentation\nnum_classes_classification = 7  # 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# print(model)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:00:40.181748Z","iopub.execute_input":"2024-03-07T19:00:40.182076Z","iopub.status.idle":"2024-03-07T19:00:44.627109Z","shell.execute_reply.started":"2024-03-07T19:00:40.182044Z","shell.execute_reply":"2024-03-07T19:00:44.626355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\nimport pickle \nimport random","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:00:44.62851Z","iopub.execute_input":"2024-03-07T19:00:44.62887Z","iopub.status.idle":"2024-03-07T19:00:44.633369Z","shell.execute_reply.started":"2024-03-07T19:00:44.628835Z","shell.execute_reply":"2024-03-07T19:00:44.63247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\ndef weighted_cross_entropy(predicted, label, weights):\n    num_samples = predicted.shape[0]\n    \n    # Calculate element-wise losses\n    losses = weights * (-label * torch.log(predicted) - (1 - label) * torch.log(1 - predicted))\n    \n    # Sum the total_loss for all inputs and divide by the sum of weights\n    total_loss = torch.sum(losses, dim=1) / torch.sum(weights, dim=1)\n    \n    # Sum the total_loss for all inputs and divide by the number of samples\n    final_loss = torch.sum(total_loss) / num_samples\n    \n    return final_loss","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:00:44.63458Z","iopub.execute_input":"2024-03-07T19:00:44.634842Z","iopub.status.idle":"2024-03-07T19:00:44.648605Z","shell.execute_reply.started":"2024-03-07T19:00:44.634818Z","shell.execute_reply":"2024-03-07T19:00:44.647866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\ndef focal_loss(predicted, label, weights, gamma=2, epsilon=1e-7):\n    # Calculate the probability of the positive class (pt) for each sample and class\n    pt = torch.where(label == 1, predicted, 1 - predicted)\n#     print(\"pt\",pt)\n    \n    # Calculate the loss components for each class\n    loss = -weights * ((1 - pt) ** gamma) * torch.log(pt + epsilon)\n#     print(\"loss\",loss)\n    # Sum the loss components for each class\n    final_loss = torch.sum(loss, dim=1) / torch.sum(weights, dim=1)\n#     print(final_loss)\n    return final_loss.mean()\n","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:00:44.649694Z","iopub.execute_input":"2024-03-07T19:00:44.649959Z","iopub.status.idle":"2024-03-07T19:00:44.659518Z","shell.execute_reply.started":"2024-03-07T19:00:44.649935Z","shell.execute_reply":"2024-03-07T19:00:44.65866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\n\ndef binary_cross_entropy_loss(segmentation_outputs, newbatch_segmentation_masks):\n    # Ensure that both batch_segmentation_masks and segmentation_outputs are tensors of type torch.float32\n    newbatch_segmentation_masks = newbatch_segmentation_masks.to(torch.float32)\n    segmentation_outputs = segmentation_outputs.to(torch.float32)\n\n    # Check shapes\n    assert newbatch_segmentation_masks.shape == segmentation_outputs.shape, \"Input shapes must match\"\n\n    # Check for NaN or Infinite values\n    assert not torch.isnan(newbatch_segmentation_masks).any() and not torch.isinf(newbatch_segmentation_masks).any(), \"Input contains NaN or Infinite values\"\n    assert not torch.isnan(segmentation_outputs).any() and not torch.isinf(segmentation_outputs).any(), \"Input contains NaN or Infinite values\"\n\n    # Clip values in newbatch_segmentation_masks to [0, 1] range\n    newbatch_segmentation_masks = torch.clamp(newbatch_segmentation_masks, 0, 1)\n\n    # Create an instance of BCELoss\n    criterion = nn.BCELoss()\n\n    # Compute the BCE loss\n    loss = criterion(segmentation_outputs, newbatch_segmentation_masks)\n\n    return loss\n","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:00:44.66084Z","iopub.execute_input":"2024-03-07T19:00:44.661119Z","iopub.status.idle":"2024-03-07T19:00:44.671173Z","shell.execute_reply.started":"2024-03-07T19:00:44.661095Z","shell.execute_reply":"2024-03-07T19:00:44.670233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nfrom torch.optim import AdamW  # Import AdamW\nfrom sklearn.metrics import accuracy_score\n\n# Function to calculate weights based on class labels\ndef weight_calculate(class_labels):\n    # Calculate the weight for each class label\n    weights = []\n    total_weight = 0\n    c = 1\n    for label in class_labels:\n        if c % 8 == 0:\n            if label == 0:\n                weights.append(7)\n                total_weight += 7\n            else:\n                weights.append(14)\n                total_weight += 14\n        else:\n            if label == 0:\n                weights.append(1)\n                total_weight += 1\n            else:\n                weights.append(2)\n                total_weight += 2\n        c += 1\n    # Calculate the overall weight based on the presence of any class label being 1\n    return torch.tensor(weights, dtype=torch.float32)\n\ndef rsnamodel(num_epochs=10, initial_epoch=0, ranOnce=False, model_path='sample.pth', history_path='sample_history.pkl'):\n    criterion = nn.BCEWithLogitsLoss()  # Binary Cross-Entropy loss\n\n    if ranOnce:\n        # Load the existing model if ranOnce is True\n        model = MultiLabel3DAttentionModel(num_classes_classification, num_classes_segmentation)\n        model.load_state_dict(torch.load(model_path))\n        model = model.to(device)\n\n        # Load the existing metrics history for plotting\n        with open(history_path, 'rb') as f:\n            metrics_history = pickle.load(f)\n\n        val_loss_history = metrics_history['val_loss_history']\n        val_acc_history = metrics_history['val_acc_history']\n        train_loss_history = metrics_history['train_loss_history']\n        train_acc_history = metrics_history['train_acc_history']\n    else:\n        # Create a new model if ranOnce is False\n        # Instantiate the model with the appropriate number of classes for both classification and segmentation\n        model = MultiLabel3DAttentionModel(num_classes_classification, num_classes_segmentation)\n        model.to(device)\n\n        # Initialize empty lists for metrics history\n        val_loss_history = []\n        val_acc_history = []\n        train_loss_history = []\n        train_acc_history = []\n\n    optimizer = optim.Adam(model.parameters(), lr=0.01)\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 tqdm(train_loader, desc=f\"Epoch {epoch+1+initial_epoch}/{num_epochs+initial_epoch} Training:\"):\n            optimizer.zero_grad()\n\n            # Move data to the GPU if available\n            batch_images = batch_images.to(torch.float32).to(device)\n            batch_segmentation_masks = batch_segmentation_masks.to(torch.float32).to(device)\n            batch_labels = batch_labels.to(torch.float32).to(device)\n\n            # Assuming batch_images has shape (batch_size, num_frames, num_channels, height, width)\n            batch_images = batch_images.unsqueeze(1)  # Add a singleton dimension for channels\n            batch_segmentation_masks = batch_segmentation_masks.unsqueeze(1)\n#             print(batch_images.shape)\n#             # Assuming batch_images has shape (4, 1, 128, 128, 128)\n#             batch_images = batch_images.squeeze(1)  # Remove the singleton dimension for channels\n            # Number of frames to add\n#             num_frames_to_add = 16\n            \n#             tensor_list = [batch_images] * num_frames_to_add\n            # Concatenate along the frames dimension (dimension 1)\n#             nbatch_images = torch.cat(tensor_list, dim=1)\n#             nbatch_segmentation_masks = torch.cat(tensor_list, dim=1)\n            \n#             newbatch_images = nbatch_images[:, :, :3, ...].contiguous()\n#             newbatch_segmentation_masks = nbatch_segmentation_masks[:, :, :3, ...].contiguous()\n            \n            # Check the current shape\n#             print(\"Current shape:\", newbatch_images.shape)\n#             print(\"Current shape:\", newbatch_segmentation_masks.shape)\n#             # Reshape to the desired shape (2, 16, 3, 224, 224)\n#             batch_images = batch_images.view(2, 16, 3, 128, 128)  # Assuming the original spatial dimensions are 128x128\n\n#             # Check the new shape\n#             print(\"New shape:\", batch_images.shape)            # Forward pass\n            newbatch_images = batch_images\n            newbatch_segmentation_masks = batch_segmentation_masks\n            classification_outputs, segmentation_outputs = model(newbatch_images, newbatch_segmentation_masks)\n            print(newbatch_images.shape)\n            print(newbatch_segmentation_masks.shape)\n            # Apply sigmoid activation to the classification outputs\n            classification_outputs = torch.sigmoid(classification_outputs)\n\n            # Calculate weights for each sample based on class labels\n            weights = torch.stack([weight_calculate(labels) for labels in batch_labels]).to(device)\n\n            # Calculate binary cross-entropy loss with weights\n            weighted_loss = focal_loss(classification_outputs, batch_labels, weights)\n\n            # Check if segmentation mask is available\n            if newbatch_segmentation_masks is not None and (newbatch_segmentation_masks != 0).any():\n                # Ensure that both input and target tensors are of type torch.float32\n                newbatch_segmentation_masks = newbatch_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 = binary_cross_entropy_loss(segmentation_outputs, newbatch_segmentation_masks)\n\n                # Add segmentation loss to the weighted loss\n                weighted_loss += segmentation_loss\n\n            running_loss += weighted_loss.item()\n\n            # Calculate accuracy for each class separately\n            accuracies = []\n            for class_index in range(num_classes_classification+1):\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            # Calculate overall accuracy\n            batch_accuracy = sum(accuracies) / (num_classes_classification+1)\n            correct_train += batch_accuracy\n            total_train += 1\n\n            # Backpropagation and optimization\n            weighted_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        train_acc_history.append(avg_train_accuracy)\n        train_loss_history.append(avg_train_loss)\n\n        print(f\"Epoch [{epoch+initial_epoch+1}/{num_epochs+initial_epoch}]\")\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 tqdm(val_loader, desc=f\"Epoch {epoch+initial_epoch+1}/{num_epochs+initial_epoch} Validation:\"):\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#                 num_frames_to_add = 16\n            \n#                 tensor_list = [batch_images] * num_frames_to_add\n#                 # Concatenate along the frames dimension (dimension 1)\n#                 nbatch_images = torch.cat(tensor_list, dim=1)\n#                 nbatch_segmentation_masks = torch.cat(tensor_list, dim=1)\n\n#                 newbatch_images = nbatch_images[:, :, :3, ...].contiguous()\n#                 newbatch_segmentation_masks = nbatch_segmentation_masks[:, :, :3, ...].contiguous()\n                # Check the current shape\n                newbatch_images = batch_images\n                newbatch_segmentation_masks = batch_segmentation_masks\n#                 print(\"Current shape:\", newbatch_images.shape)\n#                 print(\"Current shape:\", newbatch_segmentation_masks.shape)\n\n\n                # Forward pass\n                classification_outputs, segmentation_outputs = model(newbatch_images, newbatch_segmentation_masks)\n\n                # Apply sigmoid activation to the classification outputs\n                classification_outputs = torch.sigmoid(classification_outputs)\n\n                # Calculate weights for each sample based on class labels\n                weights = torch.stack([weight_calculate(labels) for labels in batch_labels]).to(device)\n\n                # Calculate binary cross-entropy loss with weights\n                weighted_loss = focal_loss(classification_outputs, batch_labels, weights)\n\n                # Check if segmentation mask is available\n                if newbatch_segmentation_masks is not None and (newbatch_segmentation_masks != 0).any():\n                    # Ensure that both input and target tensors are of type torch.float32\n                    newbatch_segmentation_masks = newbatch_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 = binary_cross_entropy_loss(segmentation_outputs, newbatch_segmentation_masks)\n\n                    # Add segmentation loss to the weighted loss\n                    weighted_loss += segmentation_loss\n\n                total_val_loss += weighted_loss.item()\n\n                # Calculate accuracy for each class separately\n                accuracies = []\n                for class_index in range(num_classes_classification+1):\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+1)\n                correct_val += batch_accuracy\n                total_val += 1\n\n        val_accuracy = correct_val / total_val\n        avg_val_loss = total_val_loss / len(val_loader)\n        val_loss_history.append(avg_val_loss)\n        val_acc_history.append(val_accuracy)\n\n        print(f\"Epoch [{epoch+1+initial_epoch}/{num_epochs+initial_epoch}]\")\n        print(f\"Validation Accuracy: {val_accuracy:.4f} | Validation Loss: {avg_val_loss:.4f}\")\n\n        if (epoch+1+initial_epoch) % 10== 0:\n            torch.save(model.state_dict(), f\"focalMvitsample_{epoch+1+initial_epoch}.pth\")\n\n            # Save the lists of metrics to a file for later plotting\n        metrics_history = {\n            'val_loss_history': val_loss_history,\n            'val_acc_history': val_acc_history,\n            'train_loss_history': train_loss_history,\n            'train_acc_history': train_acc_history,\n        }\n\n        with open(f\"focalMvitsample_metrics_history.pkl\", 'wb') as f:\n            # This will be a single file, containing all the history\n            pickle.dump(metrics_history, f)","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:00:44.672695Z","iopub.execute_input":"2024-03-07T19:00:44.673243Z","iopub.status.idle":"2024-03-07T19:00:44.709194Z","shell.execute_reply.started":"2024-03-07T19:00:44.67321Z","shell.execute_reply":"2024-03-07T19:00:44.70814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rsnamodel(num_epochs=1, initial_epoch=0, ranOnce=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:08:01.496745Z","iopub.execute_input":"2024-02-16T06:08:01.497096Z","iopub.status.idle":"2024-02-16T06:08:16.006356Z","shell.execute_reply.started":"2024-02-16T06:08:01.497063Z","shell.execute_reply":"2024-02-16T06:08:16.005008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\nimport matplotlib.pyplot as plt\n\n# history_path = '/kaggle/input/train-run-1/metrics_history.pkl'\nhistory_path = '/kaggle/input/swintrans-after-100eps/sample_metrics_history.pkl'\n\n# Load the metrics history from the saved file\nwith open(history_path, 'rb') as f:\n    metrics_history = pickle.load(f)\n\n# Extract the lists of metrics\ntrain_accuracies = metrics_history['train_acc_history']\nval_accuracies = metrics_history['val_acc_history']\ntrain_losses = metrics_history['train_loss_history']\nval_losses = metrics_history['val_loss_history']\n\n# Create a list of epoch numbers for the x-axis\nepochs = list(range(1, len(train_accuracies) + 1))\n\n# Plot training and validation accuracies\nplt.figure(figsize=(12, 5))\nplt.subplot(1, 2, 1)\nplt.plot(epochs, train_accuracies, label='Train Accuracy')\nplt.plot(epochs, val_accuracies, label='Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.title('Training and Validation Accuracies')\nplt.legend()\n\n# Plot training and validation losses\nplt.subplot(1, 2, 2)\nplt.plot(epochs, train_losses, label='Train Loss')\nplt.plot(epochs, val_losses, label='Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.title('Training and Validation Losses')\nplt.legend()\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:45:41.519063Z","iopub.execute_input":"2024-03-07T19:45:41.519729Z","iopub.status.idle":"2024-03-07T19:45:42.184271Z","shell.execute_reply.started":"2024-03-07T19:45:41.519693Z","shell.execute_reply":"2024-03-07T19:45:42.183495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom torch.utils.data import DataLoader\n\n# Assuming you have defined your dataset, let's call it CustomDataset, and you have initialized it with your test data\n# You need to replace CustomDataset with your actual dataset class\ntest_data = pd.read_csv('/kaggle/input/data-divided/exp_test.csv')  # Assuming test_data contains your test data\n\n# Assuming you have defined batch_size and num_workers\nbatch_size = 4\nnum_workers = 2\n\n# Assuming you have defined test_loader\ntest_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers)\n\n# Instantiate your model\nnum_classes_classification = 7  # 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# Move the model to the appropriate device (GPU if available, otherwise CPU)\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel.to(device)\n\n# Load the saved model checkpoint\nmodel.load_state_dict(torch.load('/kaggle/input/swintrans-after-100eps/sample_100.pth', map_location=device))\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T05:59:17.425073Z","iopub.execute_input":"2024-02-16T05:59:17.425975Z","iopub.status.idle":"2024-02-16T05:59:24.119906Z","shell.execute_reply.started":"2024-02-16T05:59:17.425928Z","shell.execute_reply":"2024-02-16T05:59:24.118988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.transforms import Compose, CenterCrop, Normalize\n\n# Define the mean and standard deviation for normalization\nmean = [0.485, 0.456, 0.406]  # Example values, replace with your actual mean\nstd = [0.229, 0.224, 0.225]   # Example values, replace with your actual std\n\n# Define your VideoClassification transformation with correct mean and std\ntransforms = Compose([\n    CenterCrop(128),   # Assuming you want to center crop to 128x128\n    Normalize(mean=mean, std=std)\n])\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:02:33.689635Z","iopub.execute_input":"2024-02-16T06:02:33.690007Z","iopub.status.idle":"2024-02-16T06:02:33.698049Z","shell.execute_reply.started":"2024-02-16T06:02:33.689977Z","shell.execute_reply":"2024-02-16T06:02:33.697253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom sklearn.metrics import f1_score, precision_score, recall_score, accuracy_score\nfrom torchvision import models\nimport numpy as np\n\n\n# Instantiate your dataset\n\n# Define evaluation metrics\ndef calculate_metrics(y_true, y_pred):\n    f1 = f1_score(y_true, y_pred, average='micro')\n    precision = precision_score(y_true, y_pred, average='micro')\n    recall = recall_score(y_true, y_pred, average='micro')\n    accuracy = accuracy_score(y_true, y_pred)\n    return f1, precision, recall, accuracy\n\n# Initialize the model\nmodel = MultiLabel3DAttentionModel(num_classes=10, num_classes_segmentation=5)\n\n# Set the model to evaluation mode\nmodel.eval()\n\n# Initialize lists to store true and predicted labels\ntrue_labels = []\npredicted_labels = []\n\n# Iterate over batches in the test set\nfor batch in test_loader:\n    inputs, segmentation_mask, labels = batch\n    \n    # Forward pass\n    with torch.no_grad():\n        classification_output, _ = model(inputs, segmentation_mask)\n    \n    # Convert output probabilities to binary predictions\n    predicted = torch.round(classification_output[:, :-1]).detach().cpu().numpy()\n    true = labels.detach().cpu().numpy()\n    \n    # Append true and predicted labels for metric calculation\n    true_labels.extend(true)\n    predicted_labels.extend(predicted)\n\n# Convert lists to numpy arrays\ntrue_labels = np.array(true_labels)\npredicted_labels = np.array(predicted_labels)\n\n# Calculate evaluation metrics\nf1, precision, recall, accuracy = calculate_metrics(true_labels, predicted_labels)\n\nprint(\"F1 Score:\", f1)\nprint(\"Precision:\", precision)\nprint(\"Recall:\", recall)\nprint(\"Accuracy:\", accuracy)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T06:02:35.878339Z","iopub.execute_input":"2024-02-16T06:02:35.878953Z","iopub.status.idle":"2024-02-16T06:02:50.476934Z","shell.execute_reply.started":"2024-02-16T06:02:35.878918Z","shell.execute_reply":"2024-02-16T06:02:50.475495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tqdm import tqdm\n# import torch\n# import pandas as pd\n# import numpy as np\n# from torch.utils.data import DataLoader\n# from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix, classification_report\n\n# # Define your test data loader here\n# test_data = pd.read_csv('/kaggle/input/data-divided/exp_test.csv')\n# test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers)\n\n# # Instantiate your model\n# model = MultiLabel3DAttentionModel(num_classes_classification, num_classes_segmentation)\n# device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n# model.to(device)\n\n# # Load the saved model checkpoint\n# model.load_state_dict(torch.load('/kaggle/input/swintrans-after-100eps/sample_100.pth'))\n\n# # Switch to evaluation mode\n# model.eval()\n\n# all_actual_labels = []\n# all_predicted_labels = []\n\n# with torch.no_grad():\n#     for batch_images, batch_segmentation_masks, batch_labels in tqdm(test_loader):\n#         # Move data to the GPU if available\n#         batch_images = batch_images.to(device, dtype=torch.float32)\n#         batch_labels = batch_labels.to(device, dtype=torch.float32)\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#         num_frames_to_add = 16\n            \n#         tensor_list = [batch_images] * num_frames_to_add\n#         # Concatenate along the frames dimension (dimension 1)\n#         nbatch_images = torch.cat(tensor_list, dim=1)\n#         nbatch_segmentation_masks = torch.cat(tensor_list, dim=1)\n\n#         newbatch_images = nbatch_images[:, :, :3, ...].contiguous()\n\n#         # Forward pass\n#         classification_outputs, _ = model(newbatch_images, None)  # No need for segmentation in the test phase\n\n#         # Apply sigmoid activation to the classification outputs\n#         classification_outputs = torch.sigmoid(classification_outputs)\n\n#         # Append actual and predicted labels for this batch\n#         all_actual_labels.append(batch_labels.cpu().numpy())\n#         all_predicted_labels.append(classification_outputs.cpu().numpy())\n\n# # Concatenate all actual and predicted labels\n# all_actual_labels = np.concatenate(all_actual_labels, axis=0)\n# all_predicted_labels = np.concatenate(all_predicted_labels, axis=0)\n\n# # Adjust the threshold\n# threshold = 0.5  # Experiment with different values\n# binary_predictions = (all_predicted_labels > threshold).astype(int)\n\n# print(\"all_actual_labels are :\", all_actual_labels)\n# print(\"binary_predictions :\", binary_predictions)\n\n# # Calculate metrics\n# accuracy = accuracy_score(all_actual_labels, binary_predictions)\n# precision = precision_score(all_actual_labels, binary_predictions, average='micro')\n# recall = recall_score(all_actual_labels, binary_predictions, average='micro')\n# f1 = f1_score(all_actual_labels, binary_predictions, average='micro')\n\n# # Confusion matrix\n# conf_matrix = confusion_matrix(all_actual_labels.ravel(), binary_predictions.ravel())\n# print(\"Confusion Matrix:\")\n# print(conf_matrix)\n\n# # Classification report\n# print(\"Classification Report:\")\n# print(classification_report(all_actual_labels.ravel(), binary_predictions.ravel()))\n\n# # Print metrics\n# print(\"Accuracy: {:.4f}\".format(accuracy))\n# print(\"Precision: {:.4f}\".format(precision))\n# print(\"Recall: {:.4f}\".format(recall))\n# print(\"F1 Score: {:.4f}\".format(f1))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\nfrom sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score\nimport torch\nimport pandas as pd\n# Define your test data loader here\ntest_data = pd.read_csv('/kaggle/input/data-divided/exp_test.csv')\ntest_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers)\n\n# Instantiate your model\nmodel = MultiLabel3DAttentionModel(num_classes_classification, num_classes_segmentation)\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel.to(device)\n\n# Load the saved model checkpoint\nmodel.load_state_dict(torch.load('/kaggle/input/swintrans-after-100eps/sample_100.pth'))\n\n\n# Assuming your test_loader is already defined\n# test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers)\n\n# Switch to evaluation mode\nmodel.eval()\n\n\n# Initialize variables for calculating test loss and accuracy\ntest_loss = 0.0\ncorrect_test = 0\ntotal_test = 0\n\nall_labels = []\nall_predictions = []\n\nwith torch.no_grad():\n    for batch_images, batch_segmentation_masks, batch_labels in tqdm(test_loader):\n    \n        \n        accuracies = []\n        \n        \n        # Move data to the GPU if available\n        batch_images = batch_images.to(device, dtype=torch.float32)\n        batch_labels = batch_labels.to(device, dtype=torch.float32)\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        num_frames_to_add = 16\n            \n        tensor_list = [batch_images] * num_frames_to_add\n        # Concatenate along the frames dimension (dimension 1)\n        nbatch_images = torch.cat(tensor_list, dim=1)\n        nbatch_segmentation_masks = torch.cat(tensor_list, dim=1)\n\n        newbatch_images = nbatch_images[:, :, :3, ...].contiguous()\n\n        # Forward pass\n        classification_outputs, _ = model(newbatch_images, None)  # No need for segmentation in the test phase\n        \n        # Apply sigmoid activation to the classification outputs\n        classification_outputs = torch.sigmoid(classification_outputs)\n        weights = torch.stack([weight_calculate(labels) for labels in batch_labels]).to(device)\n\n\n        # Calculate test loss using your defined criterion (focal_loss)\n        weighted_loss = focal_loss(classification_outputs, batch_labels, weights)\n        test_loss += weighted_loss.item()\n\n        # Calculate accuracy for each class separately (similar to training)\n        for class_index in range(num_classes_classification+1):\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\n            # Append the values to their respective list\n            accuracies.append(class_accuracy)\n\n        # Calculate overall accuracy for this batch\n        batch_accuracy = sum(accuracies) / (num_classes_classification+1)\n        \n        correct_test += batch_accuracy\n\n        total_test += 1\n\n# Calculate and print average test accuracy and loss\navg_test_accuracy = correct_test / total_test\n\n\navg_test_loss = test_loss / len(test_loader)\nprint(\"Test Accuracy: {:.4f} | Test Loss: {:.4f}\".format(avg_test_accuracy, avg_test_loss))","metadata":{"execution":{"iopub.status.busy":"2023-12-18T11:05:31.986439Z","iopub.execute_input":"2023-12-18T11:05:31.98685Z","iopub.status.idle":"2023-12-18T11:06:42.049079Z","shell.execute_reply.started":"2023-12-18T11:05:31.986812Z","shell.execute_reply":"2023-12-18T11:06:42.04783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\ndef logits_to_probabilities(logits):\n    logits_cpu = logits.cpu().detach().numpy()  # Move tensor to CPU and convert to numpy array\n    exp_logits = np.exp(logits_cpu)\n    probabilities = exp_logits / np.sum(exp_logits, axis=1, keepdims=True)\n    return probabilities\n","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:37:10.374935Z","iopub.execute_input":"2024-03-07T19:37:10.375864Z","iopub.status.idle":"2024-03-07T19:37:10.381181Z","shell.execute_reply.started":"2024-03-07T19:37:10.37583Z","shell.execute_reply":"2024-03-07T19:37:10.38014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\nimport torch\nimport pandas as pd\nimport numpy as np\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score\nfrom sklearn.preprocessing import label_binarize\n\n# Define your test data loader here\ntest_data = pd.read_csv('/kaggle/input/data-divided/exp_test.csv')\n\ntest_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers)\n\n# Instantiate your model\nmodel = MultiLabel3DAttentionModel((num_classes_classification), num_classes_segmentation)\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel.to(device)\n\n# Load the saved model checkpoint\nmodel.load_state_dict(torch.load('/kaggle/input/swintrans-after-100eps/sample_100.pth'))\n\n# Switch to evaluation mode\nmodel.eval()\n\nall_actual_labels = []\nall_predicted_labels = []\n\nwith torch.no_grad():\n    for batch_images, batch_segmentation_masks, batch_labels in tqdm(test_loader):\n        # Move data to the GPU if available\n        batch_images = batch_images.to(device, dtype=torch.float32)\n        batch_labels = batch_labels.to(device, dtype=torch.float32)\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        num_frames_to_add = 16\n            \n        tensor_list = [batch_images] * num_frames_to_add\n        # Concatenate along the frames dimension (dimension 1)\n        nbatch_images = torch.cat(tensor_list, dim=1)\n        nbatch_segmentation_masks = torch.cat(tensor_list, dim=1)\n\n        newbatch_images = nbatch_images[:, :, :3, ...].contiguous()\n\n        # Forward pass\n        classification_outputs, _ = model(newbatch_images, None)  # No need for segmentation in the test phase\n#         print(\"classification_outputs:\",classification_outputs)\n\n        # Apply sigmoid activation to the classification outputs\n#         classification_outputs = torch.sigmoid(classification_outputs, axis=1)\n#         classification_outputs = logits_to_probabilities(classification_outputs)\n        probabilities = F.softmax(classification_outputs, dim=1)\n#         print(\"probabilities:\",probabilities)\n        # Append actual and predicted labels for this batch\n        all_actual_labels.append(batch_labels.cpu().numpy())\n        all_predicted_labels.append(classification_outputs.cpu().numpy())\n\n# Concatenate all actual and predicted labels\nall_actual_labels = np.concatenate(all_actual_labels, axis=0)\nall_predicted_labels = np.concatenate(all_predicted_labels, axis=0)\n\n\nprint(\"all_actual_labels are :-\",all_actual_labels)\nprint(\"all_predicted_labels:-\",all_predicted_labels)\n# Calculate metrics using the provided function\n# def calculate_metrics(true, pred):\n#     # Convert true labels to one-hot encoding\n#     true_one_hot = label_binarize(true, classes=[0, 1, 2, 3, 4, 5, 6, 7,])\n\n#     # Calculate accuracy\n#     accuracy = accuracy_score(true, pred)\n\n#     # Calculate precision, recall, and F1-score for each class\n#     precision = precision_score(true, pred, average=None)\n#     recall = recall_score(true, pred, average=None)\n#     f1 = f1_score(true, pred, average=None)\n\n#     # Calculate macro-average precision, recall, and F1-score\n#     macro_precision = np.mean(precision)\n#     macro_recall = np.mean(recall)\n#     macro_f1 = np.mean(f1)\n\n    # Calculate micro-average precision, recall, and F1-score\n#     micro_precision = precision_score(true, pred, average='micro')\n#     micro_recall = recall_score(true, pred, average='micro')\n#     micro_f1 = f1_score(true, pred, average='micro')\n\n#     # Calculate ROC AUC\n#     roc_auc = roc_auc_score(true_one_hot, label_binarize(pred, classes=[0, 1, 2, 3, 4, 5, 6, 7,]), average='micro')    \n    \n#     return accuracy, macro_precision, macro_recall, macro_f1, micro_precision, micro_recall, micro_f1, roc_auc\n\n# # Call the function with your data\n# accuracy, macro_precision, macro_recall, macro_f1, micro_precision, micro_recall, micro_f1, roc_auc = calculate_metrics(all_actual_labels, (all_predicted_labels > 0.5).astype(int))\n\n# # Print the results\n# print(\"Accuracy: {:.4f}\".format(accuracy))\n# print(\"Macro Precision: {:.4f}\".format(macro_precision))\n# print(\"Macro Recall: {:.4f}\".format(macro_recall))\n# print(\"Macro F1 Score: {:.4f}\".format(macro_f1))\n# print(\"Micro Precision: {:.4f}\".format(micro_precision))\n# print(\"Micro Recall: {:.4f}\".format(micro_recall))\n# print(\"Micro F1 Score: {:.4f}\".format(micro_f1))\n# print(\"ROC AUC: {:.4f}\".format(roc_auc))","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:47:25.560245Z","iopub.execute_input":"2024-03-07T19:47:25.560593Z","iopub.status.idle":"2024-03-07T19:48:33.45126Z","shell.execute_reply.started":"2024-03-07T19:47:25.560567Z","shell.execute_reply":"2024-03-07T19:48:33.450173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true = all_actual_labels\ny_pred = all_predicted_labels","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:49:51.982853Z","iopub.execute_input":"2024-03-07T19:49:51.983837Z","iopub.status.idle":"2024-03-07T19:49:51.988488Z","shell.execute_reply.started":"2024-03-07T19:49:51.983793Z","shell.execute_reply":"2024-03-07T19:49:51.98742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport sklearn.metrics as skm\nfrom sklearn.metrics import multilabel_confusion_matrix\n\nthreshold = 0.5\ny_pred = (y_pred > threshold).astype(int)\ny_true = label_binarize(y_true, classes=[0, 1,2,3,4,5,6,7])\n\nt  = multilabel_confusion_matrix(np.array(y_true), y_pred)\ncm = skm.multilabel_confusion_matrix(np.array(y_true), y_pred)\nprint(skm.classification_report(np.array(y_true), y_pred))\nprint('==========================================================')\ncount = 0\n# diseases = ['Aortic enlargement','Atelectasis','Calcification','Cardiomegaly','Consolidation','ILD','Infiltration','Lung Opacity','Nodule_Mass','Other lesion','Pleural effusion','Pleural thickening','Pneumothorax','Pulmonary fibrosis','No finding']\n# diseases = list(test_data.columns[1:])\ndiseases = [\"patient_overall\",\"c1\",\"c2\",\"c3\",\"c4\",\"c5\",\"c6\",\"c7\",]\nprint(\"Disease Labels:-\", diseases)\narr_result = t\nfor i in range(8):\n    print(arr_result[i])\n    print(arr_result[i].sum())\n    #cfm_plot = sn.heatmap(arr_result[i], annot=False)\n    acc = (arr_result[i][0][0]+ arr_result[i][1][1])/arr_result[i].sum()\n    pre = arr_result[i][0][0]/(arr_result[i][0][0]+arr_result[i][0][1])\n    recall = arr_result[i][0][0]/(arr_result[i][0][0]+arr_result[i][1][0])\n    fscore = (2*pre*recall)/(pre+recall)\n    print(diseases[i],\": \",round(acc*100,2),\"%\")\n    print(\"Precision: \",round(pre,2))\n    print(\"Recall:\", round(recall,2))\n    print(\"F1-Score:\", round(fscore,2))\n    print('==========================================================')\n    #plt.show(cfm_plot)\n    count = count+1","metadata":{"execution":{"iopub.status.busy":"2024-03-07T20:00:47.280866Z","iopub.execute_input":"2024-03-07T20:00:47.281826Z","iopub.status.idle":"2024-03-07T20:00:47.311268Z","shell.execute_reply.started":"2024-03-07T20:00:47.281787Z","shell.execute_reply":"2024-03-07T20:00:47.310171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score, roc_auc_score\n\ndef calculate_metrics(y_true, y_pred, threshold=0.5):\n    # Convert probabilities to binary predictions based on threshold\n    y_pred_binary = (y_pred > threshold).astype(int)\n\n    # Initialize dictionaries to store metrics for each class\n    precision = {}\n    recall = {}\n    f1 = {}\n    auc = {}\n\n    # Iterate over each class\n    for i in range(y_true.shape[1]):\n        true_class = y_true[:, i]\n        pred_class = y_pred_binary[:, i]\n\n        # Calculate precision, recall, and F1 score for the current class\n        precision[i] = precision_score(true_class, pred_class)\n        recall[i] = recall_score(true_class, pred_class)\n        f1[i] = f1_score(true_class, pred_class)\n\n        # Calculate AUC score for the current class\n        auc[i] = roc_auc_score(true_class, y_pred[:, i])\n\n    # Calculate overall accuracy\n    accuracy = accuracy_score(y_true, y_pred_binary)\n\n    return precision, recall, f1, accuracy, auc\n\n# Example usage:\nprecision, recall, f1, accuracy, auc = calculate_metrics(y_true, y_pred)\n\nprint(\"Precision:\", precision)\nprint(\"Recall:\", recall)\nprint(\"F1 Score:\", f1)\nprint(\"Accuracy:\", accuracy)\nprint(\"AUC:\", auc)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:50:07.423256Z","iopub.execute_input":"2024-03-07T19:50:07.423614Z","iopub.status.idle":"2024-03-07T19:50:07.486532Z","shell.execute_reply.started":"2024-03-07T19:50:07.423588Z","shell.execute_reply":"2024-03-07T19:50:07.48551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn.functional as F\n\nlogits = torch.tensor([[5.6966e-09, 6.8672e-09, 4.8951e-09, 5.0824e-09, 5.7210e-09, 6.6395e-09,\n                        9.3656e-09, 3.5998e-02],\n                       [5.6966e-09, 6.8672e-09, 4.8951e-09, 5.0824e-09, 5.7210e-09, 6.6395e-09,\n                        9.3656e-09, 3.5998e-02],\n                       [5.6966e-09, 6.8672e-09, 4.8951e-09, 5.0824e-09, 5.7210e-09, 6.6395e-09,\n                        9.3656e-09, 3.5998e-02],\n                       [5.6966e-09, 6.8672e-09, 4.8951e-09, 5.0824e-09, 5.7210e-09, 6.6395e-09,\n                        9.3656e-09, 3.5998e-02]], device='cuda:0')\n\n# Apply softmax along the appropriate axis (axis=1)\nprobabilities = F.softmax(logits, dim=1)\nprint(\"Probabilities:\", probabilities)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-07T19:42:58.094664Z","iopub.execute_input":"2024-03-07T19:42:58.095673Z","iopub.status.idle":"2024-03-07T19:42:58.106694Z","shell.execute_reply.started":"2024-03-07T19:42:58.095634Z","shell.execute_reply":"2024-03-07T19:42:58.105621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}