{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd  \nimport seaborn as sns \nimport matplotlib.pyplot as plt  \nimport pandas.api.types\nimport sklearn.metrics  \nimport nibabel as nib\nimport os\nimport pydicom\nfrom glob import glob\nfrom tqdm import tqdm, trange\nimport torch\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms\nfrom torchvision.transforms import Resize, ToPILImage, ToTensor\nfrom timm import create_model\nimport torch.nn as nn\nimport torch.optim as optim","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:28.219955Z","iopub.execute_input":"2023-10-06T06:35:28.220323Z","iopub.status.idle":"2023-10-06T06:35:35.641308Z","shell.execute_reply.started":"2023-10-06T06:35:28.220296Z","shell.execute_reply":"2023-10-06T06:35:35.640244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_path = \"/kaggle/input/rsna-2023-abdominal-trauma-detection\"","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:40.32329Z","iopub.execute_input":"2023-10-06T06:35:40.323652Z","iopub.status.idle":"2023-10-06T06:35:40.328712Z","shell.execute_reply.started":"2023-10-06T06:35:40.323627Z","shell.execute_reply":"2023-10-06T06:35:40.327453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Target_cols = [\"bowel_healthy\", \"bowel_injury\", \"extravasation_healthy\",\n                   \"extravasation_injury\", \"kidney_healthy\", \"kidney_low\",\n                   \"kidney_high\", \"liver_healthy\", \"liver_low\", \"liver_high\",\n                   \"spleen_healthy\", \"spleen_low\", \"spleen_high\",\"any_injury\"]","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:41.582573Z","iopub.execute_input":"2023-10-06T06:35:41.583299Z","iopub.status.idle":"2023-10-06T06:35:41.58844Z","shell.execute_reply.started":"2023-10-06T06:35:41.583267Z","shell.execute_reply":"2023-10-06T06:35:41.587375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_path = \"/kaggle/input/rsna-2023-abdominal-trauma-detection\"\ntrain_csv=f\"{file_path}/train.csv\" \ntrain=pd.read_csv(train_csv) \ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:42.752766Z","iopub.execute_input":"2023-10-06T06:35:42.753834Z","iopub.status.idle":"2023-10-06T06:35:42.789639Z","shell.execute_reply.started":"2023-10-06T06:35:42.753795Z","shell.execute_reply":"2023-10-06T06:35:42.788658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"organs_healthy = [ \n          'bowel_healthy',\n          'extravasation_healthy',\n          'kidney_healthy',\n          'liver_healthy',\n          'spleen_healthy'\n] \n#calculate and plot \ncorr_matrix1=train[organs_healthy].corr()\nsns.heatmap(corr_matrix1,annot=True); \nplt.title('Correlation Heatmap for healthy organs')","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:42.953032Z","iopub.execute_input":"2023-10-06T06:35:42.953367Z","iopub.status.idle":"2023-10-06T06:35:43.389264Z","shell.execute_reply.started":"2023-10-06T06:35:42.953342Z","shell.execute_reply":"2023-10-06T06:35:43.388272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"low_high = [\n            'bowel_injury',\n            'extravasation_injury',\n            'kidney_low',\n            'kidney_high',\n            'liver_high',\n            'liver_low' , \n            'spleen_low',\n            'spleen_high',\n            'any_injury'\n] \n#calculate and plot \ncorr_matrix2 = train[low_high].corr()\nsns.heatmap(corr_matrix2,annot=True , linewidths=1) \nplt.title('Correlation heatmap for injury organs')","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:43.391031Z","iopub.execute_input":"2023-10-06T06:35:43.392042Z","iopub.status.idle":"2023-10-06T06:35:43.997075Z","shell.execute_reply.started":"2023-10-06T06:35:43.392004Z","shell.execute_reply":"2023-10-06T06:35:43.996196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series_meta = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_series_meta.csv')\ntrain_series_meta.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:43.998048Z","iopub.execute_input":"2023-10-06T06:35:43.998414Z","iopub.status.idle":"2023-10-06T06:35:44.018489Z","shell.execute_reply.started":"2023-10-06T06:35:43.998379Z","shell.execute_reply":"2023-10-06T06:35:44.017627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_series_meta=pd.read_csv(\"/kaggle/input/rsna-2023-abdominal-trauma-detection/test_series_meta.csv\") \ntest_series_meta.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:45.442641Z","iopub.execute_input":"2023-10-06T06:35:45.443306Z","iopub.status.idle":"2023-10-06T06:35:45.459793Z","shell.execute_reply.started":"2023-10-06T06:35:45.443274Z","shell.execute_reply":"2023-10-06T06:35:45.458327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_labels = pd.read_csv( \"/kaggle/input/rsna-2023-abdominal-trauma-detection/image_level_labels.csv\")\nimage_labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:45.607677Z","iopub.execute_input":"2023-10-06T06:35:45.608409Z","iopub.status.idle":"2023-10-06T06:35:45.631192Z","shell.execute_reply.started":"2023-10-06T06:35:45.608378Z","shell.execute_reply":"2023-10-06T06:35:45.630108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Paths Models\nBASE_DIR = \"/kaggle/input/rsna-2023-abdominal-trauma-detection\"\nTRAIN_CSV_PATH = os.path.join(BASE_DIR, \"train.csv\")\nTRAIN_IMAGES_PATH = os.path.join(BASE_DIR, \"train_images\")\nTEST_IMAGES_PATH = os.path.join(BASE_DIR, \"test_images\")\nSAMPLE_SUBMISSION_PATH = os.path.join(BASE_DIR, \"sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:45.807885Z","iopub.execute_input":"2023-10-06T06:35:45.808439Z","iopub.status.idle":"2023-10-06T06:35:45.81355Z","shell.execute_reply.started":"2023-10-06T06:35:45.80841Z","shell.execute_reply":"2023-10-06T06:35:45.812534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load CSV files\ntrain_df = pd.read_csv(TRAIN_CSV_PATH)\nsample_submission = pd.read_csv(SAMPLE_SUBMISSION_PATH)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:46.00304Z","iopub.execute_input":"2023-10-06T06:35:46.00415Z","iopub.status.idle":"2023-10-06T06:35:46.021194Z","shell.execute_reply.started":"2023-10-06T06:35:46.004108Z","shell.execute_reply":"2023-10-06T06:35:46.020267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the test dataframe\ntest_df = sample_submission[['patient_id']]","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:46.197645Z","iopub.execute_input":"2023-10-06T06:35:46.197972Z","iopub.status.idle":"2023-10-06T06:35:46.203484Z","shell.execute_reply.started":"2023-10-06T06:35:46.197945Z","shell.execute_reply":"2023-10-06T06:35:46.202524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Function to get DICOM paths\ndef get_dicom_paths(patient_id, is_train=True):\n    folder = TRAIN_IMAGES_PATH if is_train else TEST_IMAGES_PATH\n    paths = []\n    for dirpath, _, filenames in os.walk(os.path.join(folder, patient_id)):\n        for file in filenames:\n            paths.append(os.path.join(dirpath, file))\n    return paths","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:46.36793Z","iopub.execute_input":"2023-10-06T06:35:46.368603Z","iopub.status.idle":"2023-10-06T06:35:46.374541Z","shell.execute_reply.started":"2023-10-06T06:35:46.36857Z","shell.execute_reply":"2023-10-06T06:35:46.373509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Transformations added\ntransform = transforms.Compose([\n    transforms.ToTensor(),\n])","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:47.007892Z","iopub.execute_input":"2023-10-06T06:35:47.008437Z","iopub.status.idle":"2023-10-06T06:35:47.013056Z","shell.execute_reply.started":"2023-10-06T06:35:47.008408Z","shell.execute_reply":"2023-10-06T06:35:47.01202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CTDataset(Dataset):\n    def __init__(self, dataframe, root_dir, transform=None, is_train=True):\n        self.dataframe = dataframe\n        self.root_dir = root_dir\n        self.transform = transform\n        self.is_train = is_train\n\n    def __len__(self):\n        return len(self.dataframe)\n\n    def __getitem__(self, idx):\n        patient_id = str(self.dataframe.iloc[idx, 0])\n\n        # Get all DICOM paths for the patient and use the first one\n        dicom_paths = get_dicom_paths(patient_id, self.is_train)\n        if not dicom_paths:\n            raise FileNotFoundError(f\"No DICOM images found for patient {patient_id}\")\n\n        # Load the first DICOM image for simplicity\n        image = pydicom.dcmread(dicom_paths[0]).pixel_array\n\n        # Convert to float32 and normalize to [0, 1]\n        image = image.astype(np.float32) / 65535.0  # 65535 is the maximum value for uint16\n\n        # Convert single channel to three-channel by repeating\n        image = np.stack((image,) * 3, axis=-1)\n\n        # Multiply by 255 and convert to uint8 for PIL conversion\n        image *= 255\n        image = image.astype(np.uint8)\n\n        # Convert to PIL Image and then resize\n        to_pil = ToPILImage()\n        image = to_pil(image)\n        resize_transform = Resize((512, 512))\n        image = resize_transform(image)\n\n        # Convert back to tensor\n        to_tensor = ToTensor()\n        image = to_tensor(image)\n        \n        \n        # Get labels\n        labels = self.dataframe.iloc[idx, 1:].values\n        labels = torch.tensor(labels, dtype=torch.float32)\n\n        # Apply other transformations to the image \n        if self.transform:\n            image = self.transform(image)\n\n        return image, labels\n","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:47.153049Z","iopub.execute_input":"2023-10-06T06:35:47.154251Z","iopub.status.idle":"2023-10-06T06:35:47.163996Z","shell.execute_reply.started":"2023-10-06T06:35:47.154176Z","shell.execute_reply":"2023-10-06T06:35:47.162894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nbatch_size = 8\n\n# Check data loaders\ntrain_dataset = CTDataset(dataframe=train_df, root_dir=TRAIN_IMAGES_PATH, is_train=True)\ntrain_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:47.352751Z","iopub.execute_input":"2023-10-06T06:35:47.353082Z","iopub.status.idle":"2023-10-06T06:35:47.361388Z","shell.execute_reply.started":"2023-10-06T06:35:47.353056Z","shell.execute_reply":"2023-10-06T06:35:47.360292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Create the DataLoader for test set\ntest_dataset = CTDataset(dataframe=test_df, root_dir=TEST_IMAGES_PATH, is_train=False)\ntest_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:47.522751Z","iopub.execute_input":"2023-10-06T06:35:47.523386Z","iopub.status.idle":"2023-10-06T06:35:47.528634Z","shell.execute_reply.started":"2023-10-06T06:35:47.523354Z","shell.execute_reply":"2023-10-06T06:35:47.527524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loader","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:48.747839Z","iopub.execute_input":"2023-10-06T06:35:48.748631Z","iopub.status.idle":"2023-10-06T06:35:48.7548Z","shell.execute_reply.started":"2023-10-06T06:35:48.748595Z","shell.execute_reply":"2023-10-06T06:35:48.753699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset, train_loader","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:48.927884Z","iopub.execute_input":"2023-10-06T06:35:48.928267Z","iopub.status.idle":"2023-10-06T06:35:48.934565Z","shell.execute_reply.started":"2023-10-06T06:35:48.928229Z","shell.execute_reply":"2023-10-06T06:35:48.933494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model('efficientnet_b4', pretrained=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:49.167821Z","iopub.execute_input":"2023-10-06T06:35:49.168137Z","iopub.status.idle":"2023-10-06T06:35:49.594365Z","shell.execute_reply.started":"2023-10-06T06:35:49.168111Z","shell.execute_reply":"2023-10-06T06:35:49.593368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model.state_dict(), 'efficientnet_b4_weights.pth')","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:49.708438Z","iopub.execute_input":"2023-10-06T06:35:49.709254Z","iopub.status.idle":"2023-10-06T06:35:49.856233Z","shell.execute_reply.started":"2023-10-06T06:35:49.709188Z","shell.execute_reply":"2023-10-06T06:35:49.855049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model Building\n\n# Path for saved weights\nweights_path = 'efficientnet_b4_weights.pth'\n\n# Function to build the model\ndef build_model(num_classes):\n    # Create the model WITHOUT pre-trained weights (since there's no internet connection)\n    model = create_model('efficientnet_b4', pretrained=False)\n    \n    # If the weights file exists, load it\n    if os.path.exists(weights_path):\n        model.load_state_dict(torch.load(weights_path, map_location='cpu'), strict=False)  \n    else:\n        print(f'Warning: Weights file not found in path {weights_path}, training from scratch.')\n    \n    # Modify the classifier layer to have the desired number of output classes\n    model.classifier = nn.Linear(model.classifier.in_features, num_classes)\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:49.90256Z","iopub.execute_input":"2023-10-06T06:35:49.903172Z","iopub.status.idle":"2023-10-06T06:35:49.909553Z","shell.execute_reply.started":"2023-10-06T06:35:49.903133Z","shell.execute_reply":"2023-10-06T06:35:49.908616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = build_model(len(train_df.columns) - 1).to(device)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:50.083148Z","iopub.execute_input":"2023-10-06T06:35:50.084146Z","iopub.status.idle":"2023-10-06T06:35:54.132565Z","shell.execute_reply.started":"2023-10-06T06:35:50.08411Z","shell.execute_reply":"2023-10-06T06:35:54.131482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = torch.nn.BCEWithLogitsLoss()\noptimizer = optim.Adam(model.parameters(), lr=0.001)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:54.134779Z","iopub.execute_input":"2023-10-06T06:35:54.135225Z","iopub.status.idle":"2023-10-06T06:35:54.143315Z","shell.execute_reply.started":"2023-10-06T06:35:54.13517Z","shell.execute_reply":"2023-10-06T06:35:54.141369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 4\n\n\nfor epoch in range(num_epochs):\n    model.train()\n    running_loss = 0.0\n    for i, (inputs, labels) in enumerate(train_loader):\n        inputs, labels = inputs.to(device), labels.to(device)\n        optimizer.zero_grad()\n        outputs = model(inputs)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n\n        running_loss += loss.item()\n\n    print(f\"Epoch {epoch+1}, Loss: {running_loss/len(train_loader)}\")\n\nprint(\"Finished Training\")","metadata":{"execution":{"iopub.status.busy":"2023-10-06T06:35:54.144565Z","iopub.execute_input":"2023-10-06T06:35:54.145424Z","iopub.status.idle":"2023-10-06T07:00:43.742981Z","shell.execute_reply.started":"2023-10-06T06:35:54.145392Z","shell.execute_reply":"2023-10-06T07:00:43.741999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluation & Prediction\n\nmodel.eval()\npredictions = []\n\nwith torch.no_grad():\n    for images, _ in test_loader:  # unpacking two values\n        images = images.to(device)\n        outputs = torch.sigmoid(model(images))\n        predictions.append(outputs.cpu().numpy())","metadata":{"execution":{"iopub.status.busy":"2023-10-06T07:01:20.868652Z","iopub.execute_input":"2023-10-06T07:01:20.868989Z","iopub.status.idle":"2023-10-06T07:01:21.012607Z","shell.execute_reply.started":"2023-10-06T07:01:20.868961Z","shell.execute_reply":"2023-10-06T07:01:21.011606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.vstack(predictions)\nprint(predictions.shape)\nprint(sample_submission.columns[1:])","metadata":{"execution":{"iopub.status.busy":"2023-10-06T07:01:31.942945Z","iopub.execute_input":"2023-10-06T07:01:31.943692Z","iopub.status.idle":"2023-10-06T07:01:31.94975Z","shell.execute_reply.started":"2023-10-06T07:01:31.943661Z","shell.execute_reply":"2023-10-06T07:01:31.948385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = predictions[:, :-1]","metadata":{"execution":{"iopub.status.busy":"2023-10-06T07:01:36.387927Z","iopub.execute_input":"2023-10-06T07:01:36.388691Z","iopub.status.idle":"2023-10-06T07:01:36.394124Z","shell.execute_reply.started":"2023-10-06T07:01:36.388655Z","shell.execute_reply":"2023-10-06T07:01:36.393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame(sample_submission['patient_id'], columns=['patient_id'])\npredictions_df = pd.DataFrame(predictions, columns=sample_submission.columns[1:])\nsubmission_df = pd.concat([submission_df, predictions_df], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T07:01:38.73296Z","iopub.execute_input":"2023-10-06T07:01:38.733949Z","iopub.status.idle":"2023-10-06T07:01:38.74225Z","shell.execute_reply.started":"2023-10-06T07:01:38.733914Z","shell.execute_reply":"2023-10-06T07:01:38.741276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save to CSV.\nsubmission_filename = \"submission.csv\"\nsubmission_df.to_csv(submission_filename, index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-06T07:01:42.16332Z","iopub.execute_input":"2023-10-06T07:01:42.163724Z","iopub.status.idle":"2023-10-06T07:01:42.177283Z","shell.execute_reply.started":"2023-10-06T07:01:42.163692Z","shell.execute_reply":"2023-10-06T07:01:42.176034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-06T07:02:03.432944Z","iopub.execute_input":"2023-10-06T07:02:03.433353Z","iopub.status.idle":"2023-10-06T07:02:03.453532Z","shell.execute_reply.started":"2023-10-06T07:02:03.433325Z","shell.execute_reply":"2023-10-06T07:02:03.452443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}