{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":75176,"databundleVersionId":8252256,"sourceType":"competition"},{"sourceId":9148814,"sourceType":"datasetVersion","datasetId":5526258}],"dockerImageVersionId":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true},"colab":{"name":"CV_New","provenance":[]}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","id":"WpkNkS2KXgS8","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os\nfrom PIL import Image\nimport matplotlib.image as mpimg\nimport torch\nimport torch.nn as nn\nimport torchvision\nfrom torchvision.models.detection import fasterrcnn_resnet50_fpn\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor\nfrom torchvision.transforms import functional as F\n\nfrom torchvision.models.detection import FasterRCNN\nfrom torchvision.models.detection.rpn import AnchorGenerator\nfrom torchvision.models.densenet import densenet121\nfrom torchvision.ops import MultiScaleRoIAlign\nfrom torch.utils.data import DataLoader, Dataset\nimport torchvision.transforms as T\nfrom torchvision import transforms\nfrom sklearn.metrics import accuracy_score\nfrom torch.utils.tensorboard import SummaryWriter\nfrom tqdm import tqdm\nfrom sklearn.model_selection import train_test_split\n\n\n\n\n#from thoracic_dataset import ThoracicDataset\n\n\n# Load the dataset\n\ntrain_df = pd.read_csv(\"/kaggle/input/amia-public-challenge-2024/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/amia-public-challenge-2024/test.csv\")\nimg_size_df = pd.read_csv(\"/kaggle/input/amia-public-challenge-2024/img_size.csv\")\ndata = pd.read_csv(\"/kaggle/input/amia-public-challenge-2024/train.csv\")\ndata","metadata":{"id":"XUnKugcMXgS8","outputId":"d110b3ae-d453-4149-db7d-1f62d31cb19c","execution":{"iopub.status.busy":"2024-08-13T22:38:32.164542Z","iopub.execute_input":"2024-08-13T22:38:32.165397Z","iopub.status.idle":"2024-08-13T22:38:32.368272Z","shell.execute_reply.started":"2024-08-13T22:38:32.165364Z","shell.execute_reply":"2024-08-13T22:38:32.367162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualize an example image\ndef display_image(image_id):\n    img_path = f\"/kaggle/input/amia-public-challenge-2024/train/train/{image_id}.png\"\n    img = mpimg.imread(img_path)\n    plt.imshow(img)\n    plt.imshow(img, cmap='gray')\n    #plt.title(f\"Image ID: {image_id}\")\n    plt.show()\n\nexample_image_id = train_df['image_id'].iloc[0]\ndisplay_image(example_image_id)","metadata":{"id":"WvULbrUVXgS9","outputId":"4a7a8f48-20dd-4374-94ad-7771afe5a927","execution":{"iopub.status.busy":"2024-08-13T22:38:32.370684Z","iopub.execute_input":"2024-08-13T22:38:32.371469Z","iopub.status.idle":"2024-08-13T22:38:32.843453Z","shell.execute_reply.started":"2024-08-13T22:38:32.371426Z","shell.execute_reply":"2024-08-13T22:38:32.842516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndata['class_id'].value_counts()\n","metadata":{"id":"rqjFP-ACXgS9","outputId":"c7528ae3-d5e1-4e55-bfc6-b502469beb2b","execution":{"iopub.status.busy":"2024-08-13T22:38:33.433719Z","iopub.execute_input":"2024-08-13T22:38:33.434079Z","iopub.status.idle":"2024-08-13T22:38:33.446294Z","shell.execute_reply.started":"2024-08-13T22:38:33.434052Z","shell.execute_reply":"2024-08-13T22:38:33.445435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"groupby_Id = data.groupby(\"class_id\").count()\ngroupby_Id","metadata":{"id":"MTHLhT8zXgS9","outputId":"6f428a7d-1360-4a23-aecb-375d34a3d316","execution":{"iopub.status.busy":"2024-08-13T22:38:33.572426Z","iopub.execute_input":"2024-08-13T22:38:33.573046Z","iopub.status.idle":"2024-08-13T22:38:33.601955Z","shell.execute_reply.started":"2024-08-13T22:38:33.573014Z","shell.execute_reply":"2024-08-13T22:38:33.601080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Group data by image_id and concatenate class_ids into a single string\ngrouped_data = train_df.groupby(\"image_id\")[\"class_id\"].apply(lambda x: ','.join(x.astype(str))).reset_index()\ngrouped_data\n","metadata":{"id":"jZ-mYJoQXgS9","outputId":"c17802ef-6daa-4ada-b4a3-f606dd3c9232","execution":{"iopub.status.busy":"2024-08-13T22:38:34.701343Z","iopub.execute_input":"2024-08-13T22:38:34.701983Z","iopub.status.idle":"2024-08-13T22:38:35.400383Z","shell.execute_reply.started":"2024-08-13T22:38:34.701949Z","shell.execute_reply":"2024-08-13T22:38:35.399412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to create one-hot encoding for class_ids\ndef create_one_hot_encoding(class_ids):\n    encoding = np.zeros(15, dtype=int)  # Create an array of zeros with 15 elements\n    for class_id in class_ids:\n        encoding[class_id] = 1  # Set the value at the index corresponding to the class_id to 1\n    return encoding\n\n# Apply one-hot encoding to the grouped data\ngrouped_data = train_df.groupby(\"image_id\")[\"class_id\"].apply(lambda x: ','.join(x.astype(str))) \\\n                  .reset_index(name='class_ids') \\\n                  .assign(one_hot_encoding=lambda x: x['class_ids'].apply(lambda ids: create_one_hot_encoding([int(id) for id in ids.split(',')])))\n\n# Display the grouped data with one-hot encodings\ngrouped_data","metadata":{"id":"PQiFzyT4XgS-","outputId":"0f0b08ab-94cc-47fa-d913-124203828e53","execution":{"iopub.status.busy":"2024-08-13T22:38:35.402037Z","iopub.execute_input":"2024-08-13T22:38:35.402327Z","iopub.status.idle":"2024-08-13T22:38:36.130775Z","shell.execute_reply.started":"2024-08-13T22:38:35.402295Z","shell.execute_reply":"2024-08-13T22:38:36.129813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, val_df = train_test_split(grouped_data, test_size=0.2, random_state=42)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-13T22:45:05.424282Z","iopub.execute_input":"2024-08-13T22:45:05.425237Z","iopub.status.idle":"2024-08-13T22:45:05.434360Z","shell.execute_reply.started":"2024-08-13T22:45:05.425193Z","shell.execute_reply":"2024-08-13T22:45:05.433382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ThoracicDataset(Dataset):\n    def __init__(self, dataframe, img_dir, transform=None):\n        self.dataframe = dataframe\n        self.img_dir = img_dir\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.dataframe)\n\n    def __getitem__(self, idx):\n        img_name = f\"{self.img_dir}/{self.dataframe.iloc[idx, 0]}.png\"\n        image = Image.open(img_name).convert(\"RGB\")  # Convert to RGB\n        label = torch.tensor(self.dataframe.iloc[idx, 2], dtype=torch.float32)\n\n        if self.transform:\n            image = self.transform(image)\n\n        return image, label\n\n\n# Image transformations\ntransform = transforms.Compose([\n    transforms.Resize((224, 224)),  # Resize images to 224x224 for DenseNet\n    transforms.ToTensor(),  # Convert PIL image to tensor\n    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])  # Normalize using ImageNet means and std\n])\n\n# Create the train dataset and dataloader\ntrain_dataset = ThoracicDataset(dataframe=grouped_data, img_dir=\"/kaggle/input/amia-public-challenge-2024/train/train\", transform=transform)\ntrain_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)\n\n# Create the validation dataset and dataloader\nval_dataset = ThoracicDataset(dataframe=val_df, img_dir=\"/kaggle/input/amia-public-challenge-2024/train/train\", transform=transform)\nval_loader = DataLoader(val_dataset, batch_size=32, shuffle=False)\n","metadata":{"id":"L_ZhUK8mXgS-","execution":{"iopub.status.busy":"2024-08-13T22:45:05.627861Z","iopub.execute_input":"2024-08-13T22:45:05.628149Z","iopub.status.idle":"2024-08-13T22:45:05.638185Z","shell.execute_reply.started":"2024-08-13T22:45:05.628125Z","shell.execute_reply":"2024-08-13T22:45:05.637304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\n\n# Load a pre-trained DenseNet model\ndensenet = densenet121(weights=None)\n\n# Modify the final layer to output 15 classes\nnum_features = densenet.classifier.in_features\ndensenet.classifier = nn.Linear(num_features, 15)  # 15 classes for multi-label classification\n\n# Move the model to GPU if available\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\ndensenet = densenet.to(device)","metadata":{"id":"fElTJPYwXgS-","execution":{"iopub.status.busy":"2024-08-13T22:45:11.187190Z","iopub.execute_input":"2024-08-13T22:45:11.188036Z","iopub.status.idle":"2024-08-13T22:45:11.632590Z","shell.execute_reply.started":"2024-08-13T22:45:11.187993Z","shell.execute_reply":"2024-08-13T22:45:11.631600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn.functional as F\n\n# Training parameters\nepochs = 10\ncriterion = nn.BCEWithLogitsLoss()\noptimizer = torch.optim.Adam(densenet.parameters(), lr=1e-4)\n\nfor epoch in range(epochs):\n    densenet.train()  # Set the model to training mode\n    running_loss = 0.0\n\n    for i, (images, labels) in enumerate(train_loader):\n        images = images.to(device)\n        labels = labels.to(device)\n\n        optimizer.zero_grad()\n        outputs = densenet(images)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n\n        running_loss += loss.item()\n\n        if i % 10 == 9:\n            print(f\"Epoch [{epoch+1}/{epochs}], Batch [{i+1}/{len(train_loader)}], Loss: {loss.item():.4f}\")\n\n    # Calculate average loss for the epoch\n    epoch_loss = running_loss / len(train_loader)\n    print(f\"Epoch [{epoch+1}/{epochs}] completed. Average Loss: {epoch_loss:.4f}\")\n\n    # Validation phase\n    densenet.eval()  # Set the model to evaluation mode\n    val_loss = 0.0\n    correct_predictions = 0\n    total_predictions = 0\n\n    with torch.no_grad():  # Disable gradient calculations for validation\n        for images, labels in val_loader:\n            images = images.to(device)\n            labels = labels.to(device)\n\n            outputs = densenet(images)\n            val_loss += criterion(outputs, labels).item()\n\n            # Calculate accuracy\n            predicted = torch.sigmoid(outputs) > 0.5  # Apply sigmoid and threshold at 0.5\n            correct_predictions += (predicted == labels).sum().item()\n            total_predictions += labels.numel()\n\n    val_loss /= len(val_loader)\n    accuracy = correct_predictions / total_predictions\n\n    print(f\"Validation Loss: {val_loss:.4f}, Accuracy: {accuracy:.4f}\\n\")\n","metadata":{"execution":{"iopub.status.busy":"2024-08-13T22:45:17.882924Z","iopub.execute_input":"2024-08-13T22:45:17.883281Z","iopub.status.idle":"2024-08-13T23:33:17.653347Z","shell.execute_reply.started":"2024-08-13T22:45:17.883251Z","shell.execute_reply":"2024-08-13T23:33:17.652426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Validation phase\ndensenet.eval()  # Set the model to evaluation mode\nval_loss = 0.0\ncorrect_predictions = 0\ntotal_predictions = 0\n\nwith torch.no_grad():  # Disable gradient calculations for validation\n    for images, labels in val_loader:\n        images = images.to(device)\n        labels = labels.to(device)\n\n        outputs = densenet(images)\n        val_loss += criterion(outputs, labels).item()\n\n        # Calculate accuracy\n        predicted = torch.sigmoid(outputs) > 0.5  # Apply sigmoid and threshold at 0.5\n        correct_predictions += (predicted == labels).sum().item()\n        total_predictions += labels.numel()\n\nval_loss /= len(val_loader)\naccuracy = correct_predictions / total_predictions\n\nprint(f\"Validation Loss: {val_loss:.4f}, Accuracy: {accuracy:.4f}\\n\")","metadata":{"execution":{"iopub.status.busy":"2024-08-13T23:33:17.655149Z","iopub.execute_input":"2024-08-13T23:33:17.655578Z","iopub.status.idle":"2024-08-13T23:33:58.880322Z","shell.execute_reply.started":"2024-08-13T23:33:17.655544Z","shell.execute_reply":"2024-08-13T23:33:58.879327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_path = \"densenet_model.pth\"\ntorch.save(densenet.state_dict(), model_path)","metadata":{"execution":{"iopub.status.busy":"2024-08-14T00:12:05.015773Z","iopub.execute_input":"2024-08-14T00:12:05.016640Z","iopub.status.idle":"2024-08-14T00:12:05.132361Z","shell.execute_reply.started":"2024-08-14T00:12:05.016583Z","shell.execute_reply":"2024-08-14T00:12:05.131579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nprint(os.listdir('.'))","metadata":{"execution":{"iopub.status.busy":"2024-08-14T00:15:24.665421Z","iopub.execute_input":"2024-08-14T00:15:24.666220Z","iopub.status.idle":"2024-08-14T00:15:24.670968Z","shell.execute_reply.started":"2024-08-14T00:15:24.666188Z","shell.execute_reply":"2024-08-14T00:15:24.670037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# criterion = nn.BCEWithLogitsLoss()\n# optimizer = torch.optim.Adam(densenet.parameters(), lr=1e-4)\n# epochs = 5  # Set the number of epochs\n\n# for epoch in range(epochs):\n#     densenet.train()  # Set the model to training mode\n#     running_loss = 0.0\n\n#     # Loop through batches in the training data\n#     for i, (images, labels) in enumerate(train_loader):\n#         images = images.to(device)\n#         labels = labels.to(device)\n\n#         # Zero the parameter gradients\n#         optimizer.zero_grad()\n\n#         # Forward pass\n#         outputs = densenet(images)\n#         loss = criterion(outputs, labels)\n\n#         # Backward pass and optimization\n#         loss.backward()\n#         optimizer.step()\n\n#         # Accumulate the running loss\n#         running_loss += loss.item()\n\n#         # Print loss for every 10 batches\n#         if i % 10 == 9:  # Print every 10th batch\n#             print(f\"Epoch [{epoch+1}/{epochs}], Batch [{i+1}/{len(train_loader)}], Loss: {loss.item():.4f}\")\n\n#     # Print the average loss at the end of the epoch\n#     epoch_loss = running_loss / len(train_loader)\n#     print(f\"Epoch [{epoch+1}/{epochs}] completed. Average Loss: {epoch_loss:.4f}\\n\")\n","metadata":{"id":"53nLiUQWXgS_","outputId":"249e7a94-4768-40ce-dcec-b0961336d3b6","execution":{"iopub.status.busy":"2024-08-13T20:30:48.776040Z","iopub.execute_input":"2024-08-13T20:30:48.776429Z","iopub.status.idle":"2024-08-13T20:54:01.995628Z","shell.execute_reply.started":"2024-08-13T20:30:48.776396Z","shell.execute_reply":"2024-08-13T20:54:01.994363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_path = \"densenet_model.pth\"\ntorch.save(densenet.state_dict(), model_path)","metadata":{"id":"Z1hYj07zXgS_","execution":{"iopub.status.busy":"2024-08-13T20:54:01.997092Z","iopub.execute_input":"2024-08-13T20:54:01.997465Z","iopub.status.idle":"2024-08-13T20:54:02.127504Z","shell.execute_reply.started":"2024-08-13T20:54:01.997429Z","shell.execute_reply":"2024-08-13T20:54:02.126527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import torch.nn as nn\n\n# # Load a pre-trained DenseNet model\n# densenet = densenet121(weights=None)\n\n# # Modify the final layer to output 15 classes\n# # num_features = densenet.classifier.in_features\n# # densenet.classifier = nn.Linear(num_features, 15)  # 15 classes for multi-label classification\n\n# # Move the model to GPU if available\n# device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n# densenet = densenet.to(device)","metadata":{"id":"UNCCvHbvXgS_","execution":{"iopub.status.busy":"2024-08-13T20:54:02.128851Z","iopub.execute_input":"2024-08-13T20:54:02.129161Z","iopub.status.idle":"2024-08-13T20:54:02.134037Z","shell.execute_reply.started":"2024-08-13T20:54:02.129135Z","shell.execute_reply":"2024-08-13T20:54:02.133058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Step 2: Load the saved state_dict\n# model_path = \"/kaggle/input/densenet121-weights/densenet121-a639ec97.pth\"\n# densenet.load_state_dict(torch.load(model_path))\n","metadata":{"id":"hixtf_c7XgS_","execution":{"iopub.status.busy":"2024-08-13T20:54:02.135290Z","iopub.execute_input":"2024-08-13T20:54:02.135583Z","iopub.status.idle":"2024-08-13T20:54:02.148330Z","shell.execute_reply.started":"2024-08-13T20:54:02.135557Z","shell.execute_reply":"2024-08-13T20:54:02.147227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"uXZyPKz0XgS_"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\n\ndensenet.eval()  # Set the model to evaluation mode\nall_labels = []\nall_outputs = []\n\nwith torch.no_grad():\n    for images, labels in train_loader:  # In practice, use a separate validation loader\n        images = images.to(device)\n        labels = labels.to(device)\n\n        outputs = densenet(images)\n        all_labels.append(labels.cpu().numpy())\n        all_outputs.append(torch.sigmoid(outputs).cpu().numpy())  # Apply sigmoid to get probabilities\n\n# Calculate AUC for each class\nall_labels = np.concatenate(all_labels)\nall_outputs = np.concatenate(all_outputs)\n\nfor i in range(15):\n    auc = roc_auc_score(all_labels[:, i], all_outputs[:, i])\n    print(f\"AUC for class {i}: {auc}\")\n","metadata":{"id":"S1Uh6gfGXgS_","outputId":"1d9d0519-9deb-45c4-afcd-e52ba86fa6ab","execution":{"iopub.status.busy":"2024-08-14T00:18:26.350159Z","iopub.execute_input":"2024-08-14T00:18:26.351016Z","iopub.status.idle":"2024-08-14T00:21:56.648632Z","shell.execute_reply.started":"2024-08-14T00:18:26.350983Z","shell.execute_reply":"2024-08-14T00:21:56.647451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The point on the ROC curve that is farthest from the diagonal (line of no discrimination) is typically considered the optimal threshold\n\nfrom sklearn.metrics import roc_curve\n\noptimal_thresholds = []\n\nfor i in range(15):\n    fpr, tpr, thresholds = roc_curve(all_labels[:, i], all_outputs[:, i])\n    youden_index = tpr - fpr\n    optimal_threshold = thresholds[np.argmax(youden_index)]\n    optimal_thresholds.append(optimal_threshold)\n    print(f\"Optimal threshold for class {i}: {optimal_threshold:.4f}\")\n","metadata":{"id":"GgzdRrXaXgS_","outputId":"a99b2821-6f08-4216-93b7-09b72e8332fb","execution":{"iopub.status.busy":"2024-08-14T00:21:56.650552Z","iopub.execute_input":"2024-08-14T00:21:56.650910Z","iopub.status.idle":"2024-08-14T00:21:56.695742Z","shell.execute_reply.started":"2024-08-14T00:21:56.650878Z","shell.execute_reply":"2024-08-14T00:21:56.694632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To select the threshold that maximizes the F1 score, which balances precision and recall.\n\nfrom sklearn.metrics import f1_score\n\noptimal_thresholds = []\n\nfor i in range(15):\n    f1_scores = []\n    thresholds = np.arange(0.0, 1.0, 0.01)\n    for threshold in thresholds:\n        y_pred = (all_outputs[:, i] >= threshold).astype(int)\n        f1 = f1_score(all_labels[:, i], y_pred)\n        f1_scores.append(f1)\n    optimal_threshold = thresholds[np.argmax(f1_scores)]\n    optimal_thresholds.append(optimal_threshold)\n    print(f\"Optimal threshold for class {i}: {optimal_threshold:.4f}\")\n","metadata":{"id":"NlVVnoR5XgS_","outputId":"727f1807-00cc-4f3b-a441-52b86ab0e187","execution":{"iopub.status.busy":"2024-08-14T00:21:56.697048Z","iopub.execute_input":"2024-08-14T00:21:56.697508Z","iopub.status.idle":"2024-08-14T00:22:03.902518Z","shell.execute_reply.started":"2024-08-14T00:21:56.697472Z","shell.execute_reply":"2024-08-14T00:22:03.901402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Apply optimal thresholds to get binary predictions\ny_pred_binary = np.zeros_like(all_outputs)\n\nfor i in range(15):\n    y_pred_binary[:, i] = (all_outputs[:, i] >= optimal_thresholds[i]).astype(int)\n\n# Now calculate accuracy or any other metric\nfor i in range(15):\n    accuracy = accuracy_score(all_labels[:, i], y_pred_binary[:, i])\n    print(f\"Accuracy for class {i} with optimal threshold: {accuracy:.4f}\")\n","metadata":{"id":"pIi1EJMXXgTA","outputId":"c9924acf-2840-4169-a9fb-3036f44b1ac7","execution":{"iopub.status.busy":"2024-08-14T00:22:03.905570Z","iopub.execute_input":"2024-08-14T00:22:03.906427Z","iopub.status.idle":"2024-08-14T00:22:03.936804Z","shell.execute_reply.started":"2024-08-14T00:22:03.906397Z","shell.execute_reply":"2024-08-14T00:22:03.935839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ThoracicDataset1(Dataset):\n    def __init__(self, dataframe, img_dir, transform=None):\n        self.dataframe = dataframe\n        self.img_dir = img_dir\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.dataframe)\n\n    def __getitem__(self, idx):\n        img_name = f\"{self.img_dir}/{self.dataframe.iloc[idx, 0]}.png\"\n        image = Image.open(img_name).convert(\"RGB\")  # Convert to RGB\n\n        # Test dataset does not have labels\n        if self.transform:\n            image = self.transform(image)\n\n        return image\n\n# Define the test dataset and dataloader\ntest_dataset = ThoracicDataset1(dataframe=test_df, img_dir=\"/kaggle/input/amia-public-challenge-2024/test/test\", transform=transform)\ntest_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)\n\ndensenet.eval()  # Set the model to evaluation mode\nall_test_outputs = []\n\nwith torch.no_grad():\n    num_batches = len(test_loader)\n    for batch_idx, images in enumerate(test_loader):\n        images = images.to(device)\n\n        outputs = densenet(images)\n        all_test_outputs.append(torch.sigmoid(outputs).cpu().numpy())  # Apply sigmoid to get probabilities\n\n        # Print progress\n        if batch_idx % 10 == 0:  # Print every 10 batches\n            print(f\"Processing batch {batch_idx + 1}/{num_batches}\")\n\n# Convert list to numpy array\nall_test_outputs = np.concatenate(all_test_outputs)\n\n# Convert predictions to a DataFrame\npredictions_df = pd.DataFrame(all_test_outputs, columns=[f'class_{i}' for i in range(15)])\n\n# Add image_id column to match the test_df\npredictions_df['image_id'] = test_df['image_id']\n\n# Save predictions to CSV\npredictions_df.to_csv('submission_predictions.csv', index=False)\n\n# Example of creating a CSV for submission\nsubmission = pd.DataFrame(all_test_outputs, columns=[f'class_{i}' for i in range(15)])\nsubmission['image_id'] = test_df['image_id']\nsubmission = submission[['image_id'] + [f'class_{i}' for i in range(15)]]\nsubmission.to_csv('submission_predictions.csv', index=False)\n","metadata":{"id":"_3y7RSdkXgTA","outputId":"2b9a4a38-424a-4270-be74-8a374e551ab2","execution":{"iopub.status.busy":"2024-08-14T00:22:03.938473Z","iopub.execute_input":"2024-08-14T00:22:03.938962Z","iopub.status.idle":"2024-08-14T00:32:02.334041Z","shell.execute_reply.started":"2024-08-14T00:22:03.938923Z","shell.execute_reply":"2024-08-14T00:32:02.332951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_df.head()","metadata":{"id":"JNDklR3FXgTA","outputId":"00d8a565-2e03-4dcc-ec98-154f94a52293","execution":{"iopub.status.busy":"2024-08-14T00:32:02.335341Z","iopub.execute_input":"2024-08-14T00:32:02.335648Z","iopub.status.idle":"2024-08-14T00:32:02.356101Z","shell.execute_reply.started":"2024-08-14T00:32:02.335624Z","shell.execute_reply":"2024-08-14T00:32:02.355066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"i_DgLnrqXgTA","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Apply the corresponding threshold for each class to convert predictions into one-hot encoding\none_hot_predictions = np.zeros_like(all_test_outputs, dtype=int)\n\nfor i in range(15):\n    one_hot_predictions[:, i] = (all_test_outputs[:, i] >= optimal_thresholds[i]).astype(int)\n\n# Convert the one-hot encoded predictions to a DataFrame\none_hot_df = pd.DataFrame(one_hot_predictions, columns=[f'class_{i}' for i in range(15)])\n\n# Add the image_id column to match with the original test_df\none_hot_df['image_id'] = predictions_df['image_id']\n\n# Rearrange columns so that 'image_id' is the first column\none_hot_df = one_hot_df[['image_id'] + [f'class_{i}' for i in range(15)]]\n\n# Save the one-hot encoded predictions to a CSV file\none_hot_df.to_csv('one_hot_encoded_predictions.csv', index=False)\n\n# Display the first few rows of the one-hot encoded DataFrame\none_hot_df.head()","metadata":{"id":"HrECZuUyXgTA","outputId":"c4448222-3525-42c8-9289-36c84b45e765","execution":{"iopub.status.busy":"2024-08-14T00:32:02.357084Z","iopub.execute_input":"2024-08-14T00:32:02.357315Z","iopub.status.idle":"2024-08-14T00:32:02.514733Z","shell.execute_reply.started":"2024-08-14T00:32:02.357295Z","shell.execute_reply":"2024-08-14T00:32:02.513845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the one-hot encoded predictions to a CSV file\noutput_path = 'one_hot_encoded_predictions.csv'\none_hot_df.to_csv(output_path, index=False)","metadata":{"id":"XQJb4xJxXgTA","execution":{"iopub.status.busy":"2024-08-14T00:33:23.612769Z","iopub.execute_input":"2024-08-14T00:33:23.613394Z","iopub.status.idle":"2024-08-14T00:33:23.754182Z","shell.execute_reply.started":"2024-08-14T00:33:23.613353Z","shell.execute_reply":"2024-08-14T00:33:23.753362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"Hy4cgLenXgTA"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Load the test dataset\n# test_dataset = ThoracicDataset(test_df, \"/kaggle/input/amia-public-challenge-2024/test/test\", get_transform(train=False))\n# test_loader = DataLoader(test_dataset, batch_size=2, shuffle=False, num_workers=4, collate_fn=lambda x: tuple(zip(*x)))\n\n# # Load the trained model\n# model.load_state_dict(torch.load(\"fasterrcnn_resnet50_fpn.pth\"))\n# model.eval()\n\n# # Inference and submission\n# submission = []\n# for images, targets in test_loader:\n#     images = list(image.to(device) for image in images)\n#     outputs = model(images)\n\n#     for i, output in enumerate(outputs):\n#         boxes = output['boxes'].cpu().detach().numpy()\n#         scores = output['scores'].cpu().detach().numpy()\n#         labels = output['labels'].cpu().detach().numpy()\n\n#         for box, score, label in zip(boxes, scores, labels):\n#             if score >= 0.5:  # Filter out low confidence predictions\n#                 x_min, y_min, x_max, y_max = box\n#                 submission.append({\n#                     'image_id': targets[i]['image_id'].item(),\n#                     'class_id': label,\n#                     'score': score,\n#                     'x_min': x_min,\n#                     'y_min': y_min,\n#                     'x_max': x_max,\n#                     'y_max': y_max\n#                 })\n\n# submission_df = pd.DataFrame(submission)\n# submission_df.to_csv('submission.csv', index=False)\n","metadata":{"id":"2jF6RYYEXgTA","execution":{"iopub.status.busy":"2024-08-13T21:10:08.585337Z","iopub.execute_input":"2024-08-13T21:10:08.586100Z","iopub.status.idle":"2024-08-13T21:10:08.592020Z","shell.execute_reply.started":"2024-08-13T21:10:08.586062Z","shell.execute_reply":"2024-08-13T21:10:08.590993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n#plt.figure(figsize=(8, 6))\nplt.hist(data['class_id'], color='skyblue', edgecolor='black')\nplt.title('Distribution of Number of Classes per Image')\nplt.xlabel('Number of Classes')\nplt.ylabel('Frequency')\nplt.grid(True)\nplt.show()","metadata":{"id":"FbTTN_scXgTA","outputId":"228df69e-6a7c-4464-d671-ae3788f5cb11","execution":{"iopub.status.busy":"2024-08-14T00:34:45.934786Z","iopub.execute_input":"2024-08-14T00:34:45.935163Z","iopub.status.idle":"2024-08-14T00:34:46.241971Z","shell.execute_reply.started":"2024-08-14T00:34:45.935133Z","shell.execute_reply":"2024-08-14T00:34:46.241113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Bounding Box","metadata":{"id":"v_JtBr0BXgTA","execution":{"iopub.status.busy":"2024-08-14T00:35:13.243682Z","iopub.execute_input":"2024-08-14T00:35:13.244042Z","iopub.status.idle":"2024-08-14T00:35:13.248316Z","shell.execute_reply.started":"2024-08-14T00:35:13.244013Z","shell.execute_reply":"2024-08-14T00:35:13.247263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ndata = pd.read_csv(\"/kaggle/input/amia-public-challenge-2024/train.csv\")\ndata\n\n# Filter out rows with NaN bounding box values\ndata_filtered = data.dropna(subset=['x_min', 'y_min', 'x_max', 'y_max'])\n\n# Then use data_filtered for splitting into train and test sets\ntrain_df, test_df = train_test_split(data_filtered, test_size=0.2, random_state=42)\n\n# Save the splits for later use\ntrain_df.to_csv('/kaggle/working/train_dataset.csv', index=False)\ntest_df.to_csv('/kaggle/working/test_dataset.csv', index=False)","metadata":{"id":"7KaLszHAXgTA","execution":{"iopub.status.busy":"2024-08-13T21:10:08.962787Z","iopub.execute_input":"2024-08-13T21:10:08.963152Z","iopub.status.idle":"2024-08-13T21:10:09.323250Z","shell.execute_reply.started":"2024-08-13T21:10:08.963117Z","shell.execute_reply":"2024-08-13T21:10:09.322165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomDataset(Dataset):\n    def __init__(self, dataframe, img_dir, transforms=None):\n        self.dataframe = dataframe\n        self.img_dir = img_dir\n        self.transforms = transforms\n\n        # Ensure only rows with valid bounding boxes are included\n        self.dataframe = self.dataframe.dropna(subset=['x_min', 'y_min', 'x_max', 'y_max'])\n\n    def __len__(self):\n        return len(self.dataframe)\n\n    def __getitem__(self, idx):\n        img_name = os.path.join(self.img_dir, self.dataframe.iloc[idx, 0] + '.png')\n        image = Image.open(img_name).convert(\"RGB\")\n\n        # Extract bounding box coordinates\n        boxes = self.dataframe.iloc[idx, 4:8].values.astype(np.float32)\n        boxes = torch.tensor(boxes).reshape(-1, 4)  # Ensure shape is [N, 4]\n\n        # Get class label\n        label = torch.tensor(self.dataframe.iloc[idx, 2], dtype=torch.int64)  # class_id\n        labels = torch.tensor([label])\n\n        if self.transforms:\n            image = self.transforms(image)\n\n        target = {\n            \"boxes\": boxes,\n            \"labels\": labels\n        }\n\n        return image, target\n","metadata":{"id":"afRjiLPbXgTA","execution":{"iopub.status.busy":"2024-08-13T21:10:09.324442Z","iopub.execute_input":"2024-08-13T21:10:09.324777Z","iopub.status.idle":"2024-08-13T21:10:09.334822Z","shell.execute_reply.started":"2024-08-13T21:10:09.324748Z","shell.execute_reply":"2024-08-13T21:10:09.333728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor\n\n# Load a pre-trained Faster R-CNN model\nmodel = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)\n\n# Replace the head with the required number of classes\nnum_classes = len(train_df['class_id'].unique()) + 1  # Add 1 for the background class\nin_features = model.roi_heads.box_predictor.cls_score.in_features\nmodel.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)\n\nmodel = model.to(device)\n","metadata":{"id":"IARDe7xZXgTA","outputId":"1a27fde2-443b-412d-b1af-ca8ee67053ce","execution":{"iopub.status.busy":"2024-08-13T21:10:09.336274Z","iopub.execute_input":"2024-08-13T21:10:09.336937Z","iopub.status.idle":"2024-08-13T21:10:11.583517Z","shell.execute_reply.started":"2024-08-13T21:10:09.336902Z","shell.execute_reply":"2024-08-13T21:10:11.582611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision.transforms as T\n\n# Define transforms\ntransform = T.Compose([\n    T.ToTensor()\n])\n\n# Define image directory (replace 'train_images_dir' with actual image directory path)\nimg_dir = \"/kaggle/input/amia-public-challenge-2024/train/train\"\n\n# Create datasets\ntrain_dataset = CustomDataset(dataframe=train_df, img_dir=img_dir, transforms=transform)\ntest_dataset = CustomDataset(dataframe=test_df, img_dir=img_dir, transforms=transform)\n\n# Custom collate function to batch the data properly\ndef collate_fn(batch):\n    return tuple(zip(*batch))\n\n# Create data loaders\ntrain_loader = DataLoader(train_dataset, batch_size=4, shuffle=True, num_workers=4, collate_fn=collate_fn)\ntest_loader = DataLoader(test_dataset, batch_size=4, shuffle=False, num_workers=4, collate_fn=collate_fn)\n","metadata":{"id":"5kAgj716XgTB","outputId":"919be0a3-a315-4705-da36-4d51be86ad69","execution":{"iopub.status.busy":"2024-08-13T21:10:11.584834Z","iopub.execute_input":"2024-08-13T21:10:11.585247Z","iopub.status.idle":"2024-08-13T21:10:11.600334Z","shell.execute_reply.started":"2024-08-13T21:10:11.585210Z","shell.execute_reply":"2024-08-13T21:10:11.599106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initialize TensorBoard SummaryWriter\nwriter = SummaryWriter('/kaggle/working/runs/experiment')\n\n# Training loop with monitoring\nnum_epochs = 10\noptimizer = torch.optim.Adam(model.parameters(), lr=0.001)\n\nfor epoch in range(num_epochs):\n    model.train()\n    epoch_loss = 0\n    progress_bar = tqdm(train_loader, desc=f\"Epoch {epoch+1}/{num_epochs}\")\n\n    for batch_idx, (images, targets) in enumerate(progress_bar):\n        images = list(image.to(device) for image in images)\n        targets = [{k: v.to(device) for k, v in t.items()} for t in targets]\n\n        loss_dict = model(images, targets)\n        losses = sum(loss for loss in loss_dict.values())\n\n        optimizer.zero_grad()\n        losses.backward()\n        optimizer.step()\n\n        epoch_loss += losses.item()\n\n        # Update progress bar\n        progress_bar.set_postfix(loss=losses.item())\n\n        # Log loss to TensorBoard\n        writer.add_scalar('Loss/train_batch', losses.item(), epoch * len(train_loader) + batch_idx)\n\n    avg_epoch_loss = epoch_loss / len(train_loader)\n    print(f\"Epoch {epoch+1}, Loss: {avg_epoch_loss}\")\n\n    # Log average epoch loss to TensorBoard\n    writer.add_scalar('Loss/train_epoch', avg_epoch_loss, epoch)\n\n# Close the TensorBoard writer\nwriter.close()\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_path1 = \"fasterRNN.pth\"\ntorch.save(model.state_dict(), model_path1)","metadata":{"id":"XLB6pZtwXgTB","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\ntest_results = []\n\nwith torch.no_grad():\n    for images, targets in test_loader:\n        images = list(image.to(device) for image in images)\n        outputs = model(images)\n\n        for i, output in enumerate(outputs):\n            boxes = output['boxes'].cpu().numpy()\n            labels = output['labels'].cpu().numpy()\n            scores = output['scores'].cpu().numpy()\n\n            test_results.append((boxes, labels, scores))\n\n# Here you would compare these results with your ground truth from test_df\n","metadata":{"id":"FMTCsT95XgTB","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"b48idFHiXgTB","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"JewDGORnXgTB"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"gk5YfdIGXgTB"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"ZOa1vEILXgTG"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}