{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\ni=0\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        i+=1\n        print(os.path.join(dirname, filename))\n        if i==10:\n            break\n    if i==10:\n        break\n\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-13T17:48:03.196926Z","iopub.execute_input":"2023-10-13T17:48:03.197271Z","iopub.status.idle":"2023-10-13T17:48:03.818078Z","shell.execute_reply.started":"2023-10-13T17:48:03.197227Z","shell.execute_reply":"2023-10-13T17:48:03.817048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Installments\n#!pip install pydicom","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:11.541191Z","iopub.execute_input":"2023-10-13T17:48:11.542232Z","iopub.status.idle":"2023-10-13T17:48:11.546536Z","shell.execute_reply.started":"2023-10-13T17:48:11.542193Z","shell.execute_reply":"2023-10-13T17:48:11.545549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = '/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/'","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:12.633676Z","iopub.execute_input":"2023-10-13T17:48:12.634354Z","iopub.status.idle":"2023-10-13T17:48:12.638707Z","shell.execute_reply.started":"2023-10-13T17:48:12.634322Z","shell.execute_reply":"2023-10-13T17:48:12.637841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Importing necessary libraries\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import DataLoader\nimport torchvision.transforms as transforms\nimport torchvision.models as models\nfrom PIL import Image\nimport shutil\nimport matplotlib.pyplot as plt\nimport pydicom\nfrom glob import glob\nimport random","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:13.824118Z","iopub.execute_input":"2023-10-13T17:48:13.825196Z","iopub.status.idle":"2023-10-13T17:48:13.831181Z","shell.execute_reply.started":"2023-10-13T17:48:13.825145Z","shell.execute_reply":"2023-10-13T17:48:13.830302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define hyperparameters\nbatch_size = 32\nlearning_rate= 0.001\nnum_epochs = 3","metadata":{"execution":{"iopub.status.busy":"2023-10-13T18:33:37.766744Z","iopub.execute_input":"2023-10-13T18:33:37.767298Z","iopub.status.idle":"2023-10-13T18:33:37.771701Z","shell.execute_reply.started":"2023-10-13T18:33:37.767241Z","shell.execute_reply":"2023-10-13T18:33:37.770477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import torch\n# import torchvision.models as models\n\n# # Save ResNet-18\n# resnet_model = models.resnet18(pretrained=True)\n# resnet_model.conv1 = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\n# torch.save(resnet_model.state_dict(), '/kaggle/working/resnet18.pth')\n\n# # Save DenseNet121\n# densenet_model = models.densenet121(pretrained=True)\n# densenet_model.features.conv0 = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\n# torch.save(densenet_model.state_dict(), '/kaggle/working/densenet121.pth')","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:14.839168Z","iopub.execute_input":"2023-10-13T17:48:14.840178Z","iopub.status.idle":"2023-10-13T17:48:14.844922Z","shell.execute_reply.started":"2023-10-13T17:48:14.840138Z","shell.execute_reply":"2023-10-13T17:48:14.844019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Define data transformation\ntransform = transforms.Compose([\n    transforms.Resize((224, 224)),\n    transforms.ToTensor(),\n    #transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n    transforms.Normalize(mean=[0.5], std=[0.5])\n])\n","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:18.563172Z","iopub.execute_input":"2023-10-13T17:48:18.563856Z","iopub.status.idle":"2023-10-13T17:48:18.568791Z","shell.execute_reply.started":"2023-10-13T17:48:18.563826Z","shell.execute_reply":"2023-10-13T17:48:18.567753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the CSV file with labels\nlabels_df = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:19.137982Z","iopub.execute_input":"2023-10-13T17:48:19.138328Z","iopub.status.idle":"2023-10-13T17:48:19.150064Z","shell.execute_reply.started":"2023-10-13T17:48:19.138299Z","shell.execute_reply":"2023-10-13T17:48:19.149191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a dictionary to store labels for each patient\nlabels_dict = {row['patient_id']: row[['bowel_healthy', 'bowel_injury', 'extravasation_healthy', 'extravasation_injury',\n                                       'kidney_healthy', 'kidney_low', 'kidney_high', 'liver_healthy', 'liver_low',\n                                       'liver_high', 'spleen_healthy', 'spleen_low', 'spleen_high']].values\n               for _, row in labels_df.iterrows()}","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:19.490705Z","iopub.execute_input":"2023-10-13T17:48:19.491029Z","iopub.status.idle":"2023-10-13T17:48:21.215771Z","shell.execute_reply.started":"2023-10-13T17:48:19.491001Z","shell.execute_reply":"2023-10-13T17:48:21.214729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Defining custom dataset\nclass CustomDataset(torch.utils.data.Dataset):\n    def __init__(self, data_dir, labels_dict, transform=None):\n        self.data_dir = data_dir\n        self.labels_dict = labels_dict\n        self.transform = transform\n        self.image_paths = glob(os.path.join(data_dir, '**', '*.dcm'), recursive=True)\n\n    def __len__(self):\n        return len(self.image_paths)\n\n    def __getitem__(self, index):\n        #print(f'Loading item {index}')\n        image_path = self.image_paths[index]\n        dicom_data = pydicom.dcmread(image_path)\n        pixel_data = dicom_data.pixel_array\n        \n        # Normalize pixel data to range [0, 1]\n        pixel_data = pixel_data / 255.0  # pixel values are in the range [0, 255]\n        \n        # Convert pixel data to a PIL image\n        image = Image.fromarray(pixel_data)\n        \n        \n        patient_id = int(image_path.split('/')[-3])  # Extract patient ID from path\n        label = torch.tensor(self.labels_dict[patient_id], dtype=torch.float32).float()\n\n        if self.transform is not None:\n            image = self.transform(image)\n        #print(f'Patient ID: {patient_id}')\n        return image, label","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:21.21759Z","iopub.execute_input":"2023-10-13T17:48:21.218149Z","iopub.status.idle":"2023-10-13T17:48:21.232093Z","shell.execute_reply.started":"2023-10-13T17:48:21.218117Z","shell.execute_reply":"2023-10-13T17:48:21.231129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Loading Train Dataset and Trainloader\n\ntrain_dataset = CustomDataset(data_dir, labels_dict, transform=transform)\ntrain_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:21.233296Z","iopub.execute_input":"2023-10-13T17:48:21.23419Z","iopub.status.idle":"2023-10-13T17:48:43.252543Z","shell.execute_reply.started":"2023-10-13T17:48:21.234159Z","shell.execute_reply":"2023-10-13T17:48:43.251617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Choosing and Displaying an image to decide similarities\n# This image choosed from the middle part of the body where the organ we interested.\n# This images will use to find most similar images for each patient.\n# It could be change depends on the patient for future studies.\n\nfrom collections import defaultdict\npatient_images = defaultdict(list)\nfor image_path in train_dataset.image_paths:\n    patient_id = int(image_path.split('/')[-3])\n    patient_images[patient_id].append(image_path)\n    \ndicom_data = pydicom.dcmread(patient_images[23424][0])\npixel_data = dicom_data.pixel_array / 255.0  # Normalize pixel data\nimage = Image.fromarray(pixel_data)\nif transform is not None:\n    image = transform(image)\nplt.imshow(image.squeeze(), cmap='gray')\nplt.show();","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:43.254808Z","iopub.execute_input":"2023-10-13T17:48:43.255384Z","iopub.status.idle":"2023-10-13T17:48:44.59013Z","shell.execute_reply.started":"2023-10-13T17:48:43.255351Z","shell.execute_reply":"2023-10-13T17:48:44.589306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# patient_images is a dictionary that includes the images for patient by batient \n\nlen(patient_images) , labels_df.patient_id.nunique() # shows how many unic patient we have","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:44.591271Z","iopub.execute_input":"2023-10-13T17:48:44.592139Z","iopub.status.idle":"2023-10-13T17:48:44.59909Z","shell.execute_reply.started":"2023-10-13T17:48:44.592106Z","shell.execute_reply":"2023-10-13T17:48:44.59823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# showing the number of patient who has at least one injury\n\nsum(labels_df.any_injury)","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:44.600569Z","iopub.execute_input":"2023-10-13T17:48:44.601226Z","iopub.status.idle":"2023-10-13T17:48:44.611276Z","shell.execute_reply.started":"2023-10-13T17:48:44.601195Z","shell.execute_reply":"2023-10-13T17:48:44.610389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# showing the number of patient who has at least one injury\n\ninjury_patientid=list(labels_df.query('any_injury == 1')['patient_id'])\nlen(injury_patientid)","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:44.612802Z","iopub.execute_input":"2023-10-13T17:48:44.613433Z","iopub.status.idle":"2023-10-13T17:48:44.626617Z","shell.execute_reply.started":"2023-10-13T17:48:44.613403Z","shell.execute_reply":"2023-10-13T17:48:44.625681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking the result\n\ninjury_patientid[:10]","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:44.62804Z","iopub.execute_input":"2023-10-13T17:48:44.628426Z","iopub.status.idle":"2023-10-13T17:48:44.634565Z","shell.execute_reply.started":"2023-10-13T17:48:44.628398Z","shell.execute_reply":"2023-10-13T17:48:44.633601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a dictionary which includes patients that have at least one injury from patient images dictionary\n\ndef select_items_by_keys(input_dict, keys_list):\n    return {key: input_dict[key] for key in keys_list if key in input_dict}\n\n# Example usage \n# patient_images is a dictionary that has all images for each patient\n# injury_patientid is a list that has patient id who has any injury\n\n\ninjury_patient_images = select_items_by_keys(patient_images, injury_patientid)\nprint(list(injury_patient_images.keys())[:10])","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:44.635829Z","iopub.execute_input":"2023-10-13T17:48:44.636656Z","iopub.status.idle":"2023-10-13T17:48:44.646663Z","shell.execute_reply.started":"2023-10-13T17:48:44.636626Z","shell.execute_reply":"2023-10-13T17:48:44.645723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# copying the images to a new dictionary. This dictionary will be used in the next code cells.\nreduced_patient_images = patient_images.copy()","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:44.650394Z","iopub.execute_input":"2023-10-13T17:48:44.651003Z","iopub.status.idle":"2023-10-13T17:48:44.700002Z","shell.execute_reply.started":"2023-10-13T17:48:44.650974Z","shell.execute_reply":"2023-10-13T17:48:44.699307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking the result that everythig is fine.\nlen(reduced_patient_images)","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:44.70137Z","iopub.execute_input":"2023-10-13T17:48:44.702355Z","iopub.status.idle":"2023-10-13T17:48:44.711778Z","shell.execute_reply.started":"2023-10-13T17:48:44.702326Z","shell.execute_reply":"2023-10-13T17:48:44.710939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Our dataset is an imbalanced dataset. This dataset should be made as a balanced dataset without don't lose the patient who has at least one injury.\n# We have total 3147 patient id. 855 of them has any injury. The dataset will be decreased the number of patient id from 3147 to 1710. \n# 855 of 1710 has any injury. 855 of 1710 has no injury. These no injury patient ids will be choosed as randomly. \n# To remove 1437 patient who has no injury will increase the balancity and also the accuracy of the prediction of model. \n\ndef delete_random_items(target_dict, reference_dict, num_items_to_delete):\n    target_keys = list(target_dict.keys())\n    reference_keys = set(reference_dict.keys())\n\n    common_keys = set(target_keys) & reference_keys\n    exclusive_keys = set(target_keys) - common_keys\n\n    keys_to_delete = random.sample(exclusive_keys, num_items_to_delete)\n\n    for key in keys_to_delete:\n        target_dict.pop(key)\n\n\nnum_items_to_delete = 1437\n\ndelete_random_items(reduced_patient_images, injury_patient_images, num_items_to_delete)\n\nprint(len(reduced_patient_images))  # To verify the number of items in target_dict after deletion","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:44.712985Z","iopub.execute_input":"2023-10-13T17:48:44.713504Z","iopub.status.idle":"2023-10-13T17:48:44.725293Z","shell.execute_reply.started":"2023-10-13T17:48:44.713476Z","shell.execute_reply":"2023-10-13T17:48:44.724415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# We don't need all images for a patient. A patient has many unnecessary images. We already have an image from the middle part of the body. \n# It is easy to find most similar images to our referance images thanks to our pre-trained model RESNET\n\n# Load pre-trained ResNet model to find similarities and get rid of unnecessary images\nmodel = models.resnet18(pretrained=False)\n\n# Modify the first layer to accept 1 channel instead of 3\nmodel.conv1 = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\nmodel.load_state_dict(torch.load('/kaggle/input/densnet-and-resnet/resnet18.pth'))\n\n# Remove the last classification layer\nmodel = nn.Sequential(*list(model.children())[:-1])","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:44.726632Z","iopub.execute_input":"2023-10-13T17:48:44.727164Z","iopub.status.idle":"2023-10-13T17:48:45.829518Z","shell.execute_reply.started":"2023-10-13T17:48:44.727133Z","shell.execute_reply":"2023-10-13T17:48:45.828569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cheking the reference image\n# Load and preprocess the query image\n\ndicom_data = pydicom.dcmread(patient_images[23424][0])\npixel_data = dicom_data.pixel_array / 255.0  # Normalize pixel data\nimage = Image.fromarray(pixel_data)\nif transform is not None:\n    image = transform(image)\n\nquery_image = image.unsqueeze(0)# Add batch and channel dimensions","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:45.830842Z","iopub.execute_input":"2023-10-13T17:48:45.831414Z","iopub.status.idle":"2023-10-13T17:48:45.856013Z","shell.execute_reply.started":"2023-10-13T17:48:45.83138Z","shell.execute_reply":"2023-10-13T17:48:45.855097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extracting features\nwith torch.no_grad():\n    features = model(query_image)\n\nfeatures = features.squeeze()","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:45.857158Z","iopub.execute_input":"2023-10-13T17:48:45.85749Z","iopub.status.idle":"2023-10-13T17:48:45.920331Z","shell.execute_reply.started":"2023-10-13T17:48:45.85746Z","shell.execute_reply":"2023-10-13T17:48:45.919412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#SIMILARITIES FOR PATIENT BY PATIENT AS A DICTIONARY FORMAT (IN 80 IMAGES TOP 40 IMAGES)\n\n# Create a new dictionary to store top similar images for each patient\ntop_similar_images = defaultdict(list)\nmax_image_in_patient = 80\ntst=0\n# Iterate through each patient's images\nfor patient_id, images in reduced_patient_images.items():\n    # Iterate through each image for the patient\n    for image_path in images:\n        dicom_data = pydicom.dcmread(image_path)\n        pixel_data = dicom_data.pixel_array / 255.0\n        image = Image.fromarray(pixel_data)\n        if transform is not None:\n            image = transform(image)\n        image = image.unsqueeze(0)\n\n        with torch.no_grad():\n            image_features = model(image)\n        \n        # Calculate similarity with the selected image\n        similarity = np.dot(features.squeeze(), image_features.squeeze())  # Cosine similarity\n        \n        # Add the image path and similarity score to the list for this patient\n        top_similar_images[patient_id].append((image_path, similarity))\n        \n        if len(top_similar_images[patient_id])>=max_image_in_patient:\n            #tst+=1\n            break\n        \n    #if tst==2:\n    #    break\n# Sort the images by similarity score for each patient\nfor patient_id, images in top_similar_images.items():\n    images.sort(key=lambda x: x[1], reverse=True)\n    top_similar_images[patient_id] = images[:40]  # Keep only the top 40 similar images","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:48:45.921751Z","iopub.execute_input":"2023-10-13T17:48:45.922321Z","iopub.status.idle":"2023-10-13T17:49:01.533523Z","shell.execute_reply.started":"2023-10-13T17:48:45.922289Z","shell.execute_reply":"2023-10-13T17:49:01.532581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(top_similar_images)","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:49:01.534925Z","iopub.execute_input":"2023-10-13T17:49:01.535484Z","iopub.status.idle":"2023-10-13T17:49:01.541337Z","shell.execute_reply.started":"2023-10-13T17:49:01.535453Z","shell.execute_reply":"2023-10-13T17:49:01.540495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom_data = pydicom.dcmread(top_similar_images[32627][8][0])\npixel_data = dicom_data.pixel_array / 255.0  # Normalize pixel data\nimage = Image.fromarray(pixel_data)\nif transform is not None:\n    image = transform(image)\nplt.imshow(image.squeeze(), cmap='gray')\nplt.show();","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:49:01.542609Z","iopub.execute_input":"2023-10-13T17:49:01.543157Z","iopub.status.idle":"2023-10-13T17:49:01.790415Z","shell.execute_reply.started":"2023-10-13T17:49:01.543118Z","shell.execute_reply":"2023-10-13T17:49:01.789589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# CREATING TNE NEW DATA TRAIN LOADER FOR THE MODEL THAT PREDICT THE LABELS\n\n\n# Define a new custom dataset for the top similar images\nclass TopSimilarImagesDataset(torch.utils.data.Dataset):\n    def __init__(self, similar_images, labels_dict, transform=None):\n        self.similar_images = similar_images\n        self.labels_dict = labels_dict\n        self.transform = transform\n\n    def __len__(self):\n        return sum(len(images) for images in self.similar_images.values())\n\n    def __getitem__(self, index):\n        # Find the corresponding patient and image index\n        for patient_id, images in self.similar_images.items():\n            if index < len(images):\n                image_path, _ = images[index]\n                break\n            index -= len(images)\n        \n        dicom_data = pydicom.dcmread(image_path)\n        pixel_data = dicom_data.pixel_array / 255.0\n        image = Image.fromarray(pixel_data)\n        if self.transform is not None:\n            image = self.transform(image)\n        \n        patient_id = int(image_path.split('/')[-3])\n        label = torch.tensor(self.labels_dict[patient_id], dtype=torch.float32).float()\n\n        return image, label\n\n# Create an instance of the new dataset\ntop_similar_images_dataset = TopSimilarImagesDataset(top_similar_images, labels_dict, transform=transform)\n\n# Create a DataLoader for the new dataset\ntop_similar_images_loader = DataLoader(top_similar_images_dataset, batch_size=batch_size, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:51:13.360101Z","iopub.execute_input":"2023-10-13T17:51:13.360462Z","iopub.status.idle":"2023-10-13T17:51:13.368292Z","shell.execute_reply.started":"2023-10-13T17:51:13.360436Z","shell.execute_reply":"2023-10-13T17:51:13.367412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# CREATING MODEL TO PRODUCE LABELS\n\nclass CustomModel(nn.Module):\n    def __init__(self, num_classes):\n        super(CustomModel, self).__init__()\n        self.base_model = models.densenet121(pretrained=False)\n        in_features = self.base_model.classifier.in_features\n        \n        # Modify the first layer to accept 1 channel instead of 3\n        self.base_model.features.conv0 = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\n        #densenet_model.load_state_dict(torch.load('/kaggle/input/densnet-and-resnet/densenet121.pth'))\n        self.base_model.load_state_dict(torch.load('/kaggle/input/densnet-and-resnet/densenet121.pth'))\n        \n        self.base_model.classifier = nn.Sequential(\n            nn.Linear(in_features, num_classes),\n            #nn.Sigmoid()  # Sigmoid for multi-label classification\n            nn.Softmax(dim=1)  # Softmax for multi-label classification\n        )\n        \n    def forward(self, x):\n        return self.base_model(x)\n\nmodel = CustomModel(num_classes=13)","metadata":{"execution":{"iopub.status.busy":"2023-10-13T17:52:55.695334Z","iopub.execute_input":"2023-10-13T17:52:55.695671Z","iopub.status.idle":"2023-10-13T17:52:55.89679Z","shell.execute_reply.started":"2023-10-13T17:52:55.695647Z","shell.execute_reply":"2023-10-13T17:52:55.895847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# DEFINING THE LOSS AND OPTIMIZATION\n\ncriterion = nn.BCEWithLogitsLoss()\noptimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)","metadata":{"execution":{"iopub.status.busy":"2023-10-13T16:14:00.540918Z","iopub.execute_input":"2023-10-13T16:14:00.541276Z","iopub.status.idle":"2023-10-13T16:14:00.547799Z","shell.execute_reply.started":"2023-10-13T16:14:00.541228Z","shell.execute_reply":"2023-10-13T16:14:00.546643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# TRAINING OF MY MODEL\n\nprint_every = 40  # Define how often to print the loss\n\nfor epoch in range(num_epochs):\n    running_loss = 0.0  # Initialize a running loss variable\n    for batch_idx, (images, labels) in enumerate(top_similar_images_loader):\n        #print(f'Batch {batch_idx+1}/{len(top_similar_images_loader)}, Epoch {epoch+1}/{num_epochs}')\n        optimizer.zero_grad()\n        outputs = model(images)\n        loss = criterion(outputs, labels.float())\n        loss.backward()\n        optimizer.step()\n        running_loss += loss.item()\n        #print(f'Loss: {loss.item():.4f}')\n        \n        if (batch_idx + 1) % print_every == 0:  # Print every print_every batches\n            print(f'Epoch [{epoch+1}/{num_epochs}], Batch [{batch_idx+1}/{len(top_similar_images_loader)}], Loss: {running_loss/print_every:.4f}')\n            running_loss = 0.0  # Reset the running loss","metadata":{"execution":{"iopub.status.busy":"2023-10-13T16:14:00.548989Z","iopub.execute_input":"2023-10-13T16:14:00.550244Z","iopub.status.idle":"2023-10-13T16:16:11.58812Z","shell.execute_reply.started":"2023-10-13T16:14:00.550213Z","shell.execute_reply":"2023-10-13T16:16:11.587146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Saving model\ntorch.save(model.state_dict(), 'model.pth')","metadata":{"execution":{"iopub.status.busy":"2023-10-13T16:16:11.589418Z","iopub.execute_input":"2023-10-13T16:16:11.590418Z","iopub.status.idle":"2023-10-13T16:16:11.661659Z","shell.execute_reply.started":"2023-10-13T16:16:11.590383Z","shell.execute_reply":"2023-10-13T16:16:11.660689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use Testing images to see the labels\n\nfrom PIL import Image\nimport numpy as np\n\n# Assuming `model` is your trained model\n# Assuming you have defined `CustomModel`\n\n# Load the saved model state\nmodel = CustomModel(num_classes=13)\nmodel.load_state_dict(torch.load('model.pth'))\nmodel.eval()\n\n# Define your image transformation\ntransform = transforms.Compose([\n    transforms.Resize((224, 224)),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.5], std=[0.5])\n])\n\n# Assuming `testing_dir` is your directory containing testing images\ntesting_dir = '/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images/'\nimage_paths=[]\nfor dirname, _, filenames in os.walk(testing_dir):\n    \n    for filename in filenames:\n        \n        image_paths.append(os.path.join(dirname, filename))\n\n# List of file paths for your testing images\n#image_paths = glob(os.path.join(testing_dir, '*.dcm'))\n#print(image_paths)\n# Iterate over each image\nfor image_path in image_paths:\n    print(image_path)\n    dicom_data = pydicom.dcmread(image_path)\n    pixel_data = dicom_data.pixel_array / 255.0\n    image = Image.fromarray(pixel_data)\n    \n    if transform is not None:\n        image = transform(image)\n        image = image.unsqueeze(0)  # Add batch dimension\n    \n    with torch.no_grad():\n        outputs = model(image)\n        labels = (outputs ).float()  # Assuming binary classification\n\n    print(f'Predicted Labels for {image_path}: {labels}')","metadata":{"execution":{"iopub.status.busy":"2023-10-13T16:16:11.66314Z","iopub.execute_input":"2023-10-13T16:16:11.663491Z","iopub.status.idle":"2023-10-13T16:16:12.271189Z","shell.execute_reply.started":"2023-10-13T16:16:11.663459Z","shell.execute_reply":"2023-10-13T16:16:12.270231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\n\n#  Creating the csv file\ntesting_dir = '/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images/'\nimage_paths=[]\nfor dirname, _, filenames in os.walk(testing_dir):\n    \n    for filename in filenames:\n        \n        image_paths.append(os.path.join(dirname, filename))\n\n# List to store the results\nresults = []\n\n# Iterate over each image\nfor image_path in image_paths:\n    print(image_path)\n    dicom_data = pydicom.dcmread(image_path)\n    pixel_data = dicom_data.pixel_array / 255.0\n    image = Image.fromarray(pixel_data)\n    \n    if transform is not None:\n        image = transform(image)\n        image = image.unsqueeze(0)  # Add batch dimension\n    \n    with torch.no_grad():\n        outputs = model(image)\n        labels = (outputs ).float()  # Assuming binary classification\n\n    # Get patient ID from image path\n    patient_id = image_path.split('/')[-2]\n\n    # Convert labels to list for CSV\n    labels_list = labels.squeeze().tolist()\n\n    # Append results\n    results.append([patient_id] + labels_list)\n\n# Define the CSV file path\ncsv_file_path = 'submission.csv'\n\n# Write results to a CSV file\nwith open(csv_file_path, mode='w', newline='') as file:\n    writer = csv.writer(file)\n    # Write the header\n    writer.writerow(['patient_id', 'bowel_healthy', 'bowel_injury', 'extravasation_healthy', 'extravasation_injury', 'kidney_healthy', 'kidney_low', 'kidney_high', 'liver_healthy', 'liver_low', 'liver_high', 'spleen_healthy', 'spleen_low', 'spleen_high'])\n    # Write the data\n    writer.writerows(results)\n\nprint(f'Results saved to {csv_file_path}')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-13T16:16:12.272807Z","iopub.execute_input":"2023-10-13T16:16:12.273143Z","iopub.status.idle":"2023-10-13T16:16:12.590922Z","shell.execute_reply.started":"2023-10-13T16:16:12.273112Z","shell.execute_reply":"2023-10-13T16:16:12.589938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv('submission.csv')\n    ","metadata":{"execution":{"iopub.status.busy":"2023-10-13T16:16:12.592315Z","iopub.execute_input":"2023-10-13T16:16:12.592653Z","iopub.status.idle":"2023-10-13T16:16:12.61341Z","shell.execute_reply.started":"2023-10-13T16:16:12.592621Z","shell.execute_reply":"2023-10-13T16:16:12.612439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}