{"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\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-15T09:57:58.363529Z","iopub.execute_input":"2023-10-15T09:57:58.364262Z","iopub.status.idle":"2023-10-15T09:57:58.368475Z","shell.execute_reply.started":"2023-10-15T09:57:58.36423Z","shell.execute_reply":"2023-10-15T09:57:58.367497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### credits ##\n\n## -- Mr. WITOLD NOWOGÓRSK  ---\n## https://www.kaggle.com/code/witoldnowogrski/abdominal-trauma-detection-efficientnet-pytorch/notebook","metadata":{"execution":{"iopub.status.busy":"2023-10-15T09:57:59.370501Z","iopub.execute_input":"2023-10-15T09:57:59.371426Z","iopub.status.idle":"2023-10-15T09:57:59.376154Z","shell.execute_reply.started":"2023-10-15T09:57:59.371385Z","shell.execute_reply":"2023-10-15T09:57:59.375134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(\"/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv\")\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-15T09:58:00.142358Z","iopub.execute_input":"2023-10-15T09:58:00.142679Z","iopub.status.idle":"2023-10-15T09:58:00.16263Z","shell.execute_reply.started":"2023-10-15T09:58:00.142653Z","shell.execute_reply":"2023-10-15T09:58:00.161592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-15T09:58:01.385896Z","iopub.execute_input":"2023-10-15T09:58:01.386564Z","iopub.status.idle":"2023-10-15T09:58:01.39204Z","shell.execute_reply.started":"2023-10-15T09:58:01.386531Z","shell.execute_reply":"2023-10-15T09:58:01.391131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    BASE_DIR = '/kaggle/input/rsna-atd-512x512-png-v2-dataset'\n    SEED = 12\n    IMAGE_SIZE = (224, 224)\n    BATCH_SIZE = 2\n    TARGET_COLUMNS = ['bowel_healthy', 'bowel_injury',\n                      'extravasation_healthy', 'extravasation_injury',\n                      'kidney_healthy', 'kidney_low', 'kidney_high',\n                      'liver_healthy', 'liver_low', 'liver_high',\n                      'spleen_healthy', 'spleen_low', 'spleen_high',\n                     ]\n    \n    \nconfig = Config()","metadata":{"execution":{"iopub.status.busy":"2023-10-15T09:58:02.178952Z","iopub.execute_input":"2023-10-15T09:58:02.179678Z","iopub.status.idle":"2023-10-15T09:58:02.190241Z","shell.execute_reply.started":"2023-10-15T09:58:02.17963Z","shell.execute_reply":"2023-10-15T09:58:02.188149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\ndf = pd.read_csv(os.path.join(config.BASE_DIR, 'train.csv'))\ndf.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-10-15T09:58:03.534012Z","iopub.execute_input":"2023-10-15T09:58:03.534383Z","iopub.status.idle":"2023-10-15T09:58:03.592392Z","shell.execute_reply.started":"2023-10-15T09:58:03.534356Z","shell.execute_reply":"2023-10-15T09:58:03.59145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n\ndef split_group(group, test_size=0.2):\n    if len(group) == 1:\n        return (group, pd.DataFrame()) if np.random.rand() < test_size else (pd.DataFrame(), group)\n    else:\n        return train_test_split(group, test_size=test_size, random_state=config.SEED)\n\ntrain_set = pd.DataFrame()\nvalidation_set = pd.DataFrame()\n\nfor _, group in df.groupby(config.TARGET_COLUMNS):\n    \n    train_group, val_group = split_group(group)\n    train_set = pd.concat([train_set, train_group], ignore_index=True)\n    validation_set = pd.concat([validation_set, val_group], ignore_index=True)\n    \nprint(train_set.shape, validation_set.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-15T09:58:04.394248Z","iopub.execute_input":"2023-10-15T09:58:04.394581Z","iopub.status.idle":"2023-10-15T09:58:04.476637Z","shell.execute_reply.started":"2023-10-15T09:58:04.394555Z","shell.execute_reply":"2023-10-15T09:58:04.475555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train_set\ntrain_set.head(3)","metadata":{"execution":{"iopub.status.busy":"2023-10-15T09:58:05.349276Z","iopub.execute_input":"2023-10-15T09:58:05.349608Z","iopub.status.idle":"2023-10-15T09:58:05.364062Z","shell.execute_reply.started":"2023-10-15T09:58:05.349581Z","shell.execute_reply":"2023-10-15T09:58:05.362833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch , torchvision\nimport torch.nn as nn\nfrom PIL import Image\nfrom torchvision import models , transforms \nimport torch.optim as optim\nfrom torch.utils.data import Dataset , DataLoader","metadata":{"execution":{"iopub.status.busy":"2023-10-15T09:58:06.287019Z","iopub.execute_input":"2023-10-15T09:58:06.287677Z","iopub.status.idle":"2023-10-15T09:58:06.292127Z","shell.execute_reply.started":"2023-10-15T09:58:06.287646Z","shell.execute_reply":"2023-10-15T09:58:06.291131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Data Augmentation ###\n\ndevice = ('cuda' if torch.cuda.is_available() else 'cpu')\n\nmean =np.array([0.5 , 0.5 , 0.5])\nstd = np.array([0.25 , 0.25 , 0.25])\n\ndata_transforms = {'train' : transforms.Compose([ transforms.RandomResizedCrop(224) , \n                                                 transforms.RandomHorizontalFlip() , \n                                                transforms.ToTensor() , \n                                                transforms.Lambda(lambda x : x / 255)]) ,  \n                   \n                   'val' : transforms.Compose([transforms.Resize(224) ,\n                                              transforms.ToTensor() ,\n                                              transforms.Lambda(lambda x : x / 255)]) }\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-15T09:58:07.894434Z","iopub.execute_input":"2023-10-15T09:58:07.894775Z","iopub.status.idle":"2023-10-15T09:58:07.900621Z","shell.execute_reply.started":"2023-10-15T09:58:07.894747Z","shell.execute_reply":"2023-10-15T09:58:07.899739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nfrom PIL import Image\n\n\n\nclass Dataset(Dataset):\n    def __init__(self, df, transform=None, labeled=True):\n        self.df = df\n        \n        self.transform = transform\n        self.labeled = labeled\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        \n        image_path = self.df['image_path'][idx]\n        dicom_file = pydicom.dcmread(image_path)\n        pixel_array = dicom_file.pixel_array.astype(np.int16)\n        image = Image.fromarray(pixel_array)\n        image = self.transform(image)\n        \n        if self.labeled:\n            target = self.df[config.TARGET_COLUMNS].iloc[idx]\n            target = torch.tensor(target.values, dtype=torch.float32)\n            return image, target[:2], target[2:4], target[4:7], target[7:10], target[10:13]\n        else:\n            patient_id = self.df['patient_id'][idx]\n            return patient_id, image\n        \n# training_set = Dataset(train_set, data_transforms['train'])\n# validation_set = Dataset(validation_set, data_transforms['val'])\n\ntraining_set = Dataset(train_set, data_transforms['train'])\nvalidation_set = Dataset(validation_set, data_transforms['val'])\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-15T09:58:08.889041Z","iopub.execute_input":"2023-10-15T09:58:08.889403Z","iopub.status.idle":"2023-10-15T09:58:08.897227Z","shell.execute_reply.started":"2023-10-15T09:58:08.889377Z","shell.execute_reply":"2023-10-15T09:58:08.89611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataloader = DataLoader(training_set , batch_size = 3  , shuffle = True )\nval_dataloader = DataLoader(validation_set , batch_size = 1 , shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2023-10-15T09:58:12.787287Z","iopub.execute_input":"2023-10-15T09:58:12.787622Z","iopub.status.idle":"2023-10-15T09:58:12.792773Z","shell.execute_reply.started":"2023-10-15T09:58:12.787596Z","shell.execute_reply":"2023-10-15T09:58:12.791419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base = models.resnet18(weights=True)\n\n# Explore the keys in the state dictionary\nfor key in base.state_dict().keys():\n    print(key)","metadata":{"execution":{"iopub.status.busy":"2023-10-15T09:58:15.915285Z","iopub.execute_input":"2023-10-15T09:58:15.915611Z","iopub.status.idle":"2023-10-15T09:58:16.160972Z","shell.execute_reply.started":"2023-10-15T09:58:15.915587Z","shell.execute_reply":"2023-10-15T09:58:16.160032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomResNet18Model(nn.Module):\n    def __init__(self):\n        super(CustomResNet18Model, self).__init__()\n        \n        # Create the ResNet-18 backbone\n        self.resnet18 = models.resnet18(weights=True)\n        \n        # Replace the first convolutional layer to accept 1 channel input\n        self.resnet18.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)\n        \n        # Modify the final fully connected layer for your specific classification task\n        num_features = self.resnet18.fc.in_features\n        \n\n\n        self.base = self.resnet18 \n        self.layer1 = nn.Linear(1000 , 512)\n        self.ba1 =  nn.BatchNorm1d(512)\n        self.layer2 = nn.Linear(512 , 256)\n        self.ba2 = nn.BatchNorm1d(256)\n        self.layer3 = nn.Linear(256 , 64)\n        self.ba3  = nn.BatchNorm1d(64)\n        self.out_bowel =  nn.Linear(64 , 2)\n        self.out_extravasation = nn.Linear(64 , 2)\n        self.out_kidney = nn.Linear(64 , 3)\n        self.out_liver = nn.Linear(64 , 3)\n        self.out_spleen = nn.Linear(64 , 3)\n        self.dropout = nn.Dropout(0.25)\n        \n    def forward(self , x):\n        x = self.base(x)\n        x = self.layer1(x)\n        x = self.dropout(x)\n        x = self.ba1(x)\n        x = self.layer2(x)\n        x =  self.dropout(x)\n        x =  self.ba2(x)\n        x = self.layer3(x)\n        x = self.dropout(x)\n        x = self.ba3(x)\n        \n        return self.out_bowel(x) , self.out_extravasation(x) , self.out_kidney(x) , self.out_liver(x) , self.out_spleen(x)\n        \n\n        \n\n        \n        \n\n    \nmodel = CustomResNet18Model()","metadata":{"execution":{"iopub.status.busy":"2023-10-15T09:58:17.271628Z","iopub.execute_input":"2023-10-15T09:58:17.272265Z","iopub.status.idle":"2023-10-15T09:58:17.490385Z","shell.execute_reply.started":"2023-10-15T09:58:17.272236Z","shell.execute_reply":"2023-10-15T09:58:17.489227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel.to(device)\n\ncriterion = nn.CrossEntropyLoss()\n\noptimizer = torch.optim.Adam(model.parameters())\n\nscheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', patience=5, factor=0.5, verbose=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-15T09:58:18.399555Z","iopub.execute_input":"2023-10-15T09:58:18.400652Z","iopub.status.idle":"2023-10-15T09:58:18.426333Z","shell.execute_reply.started":"2023-10-15T09:58:18.400615Z","shell.execute_reply":"2023-10-15T09:58:18.425357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_losses = []\nval_losses = []\nepochs = 10\nfor epoch in range(epochs):\n        \n        print(f'EPOCH: {epoch + 1}/{epochs}')\n        \n        train_loss = 0.0\n        validation_loss = 0.0\n        \n        model.train()\n        for batch_num, data in enumerate(train_dataloader):\n            inputs, labels_b, labels_e, labels_k, labels_l, labels_s = data\n#             print(inputs)\n            inputs = inputs.to(device)\n            labels_b = labels_b.to(device)\n            labels_e = labels_e.to(device)\n            labels_k = labels_k.to(device)\n            labels_l = labels_l.to(device)\n            labels_s = labels_s.to(device)\n            \n            optimizer.zero_grad()\n\n            out_b, out_e, out_k, out_l, out_s = model(inputs)\n\n            loss_b = criterion(out_b, labels_b)\n            loss_e = criterion(out_e, labels_e)\n            loss_k = criterion(out_k, labels_k)\n            loss_l = criterion(out_l, labels_l)\n            loss_s = criterion(out_s, labels_s)\n            \n            total_loss = loss_b + loss_e + loss_k + loss_l + loss_s\n            total_loss.backward()\n            \n            optimizer.step()\n            \n            train_loss += total_loss.item()\n            \n        train_loss = train_loss/len(train_dataloader)\n        train_losses.append(train_loss)\n        print(f'train loss: {train_loss}')\n        \n        model.eval()\n        with torch.no_grad():\n            for batch_num, data in enumerate(val_dataloader):\n                inputs, labels_b, labels_e, labels_k, labels_l, labels_s = data\n                inputs = inputs.to(device)\n                labels_b = labels_b.to(device)\n                labels_e = labels_e.to(device)\n                labels_k = labels_k.to(device)\n                labels_l = labels_l.to(device)\n                labels_s = labels_s.to(device)\n                \n                out_b, out_e, out_k, out_l, out_s = model(inputs)\n\n                loss_b = criterion(out_b, labels_b)\n                loss_e = criterion(out_e, labels_e)\n                loss_k = criterion(out_k, labels_k)\n                loss_l = criterion(out_l, labels_l)\n                loss_s = criterion(out_s, labels_s)\n\n                total_loss = loss_b + loss_e + loss_k + loss_l + loss_s\n                validation_loss += total_loss.item()\n                \n        validation_loss = validation_loss/len(val_dataloader)\n        val_losses.append(validation_loss)\n        print(f'validation loss: {validation_loss}')","metadata":{"execution":{"iopub.status.busy":"2023-10-15T09:58:19.567003Z","iopub.execute_input":"2023-10-15T09:58:19.567697Z","iopub.status.idle":"2023-10-15T10:40:33.128064Z","shell.execute_reply.started":"2023-10-15T09:58:19.567666Z","shell.execute_reply":"2023-10-15T10:40:33.12698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.subplot(1, 2, 1)\nepochs = range(1, epochs + 1)\nplt.plot(epochs, train_losses, label='Training loss')\nplt.plot(epochs, val_losses, label='Validation loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-15T10:41:00.823784Z","iopub.execute_input":"2023-10-15T10:41:00.824167Z","iopub.status.idle":"2023-10-15T10:41:01.111935Z","shell.execute_reply.started":"2023-10-15T10:41:00.824121Z","shell.execute_reply":"2023-10-15T10:41:01.110962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set = pd.read_csv(os.path.join(config.BASE_DIR, 'test.csv'))\ntest_set","metadata":{"execution":{"iopub.status.busy":"2023-10-15T10:41:13.645379Z","iopub.execute_input":"2023-10-15T10:41:13.645742Z","iopub.status.idle":"2023-10-15T10:41:13.672406Z","shell.execute_reply.started":"2023-10-15T10:41:13.645713Z","shell.execute_reply":"2023-10-15T10:41:13.671317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set = Dataset(test_set, transform = data_transforms['val'], labeled=False)\n\ntest_dataloader = DataLoader(\n    test_set,\n    batch_size=1,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2023-10-15T10:41:14.937068Z","iopub.execute_input":"2023-10-15T10:41:14.938218Z","iopub.status.idle":"2023-10-15T10:41:14.944111Z","shell.execute_reply.started":"2023-10-15T10:41:14.938148Z","shell.execute_reply":"2023-10-15T10:41:14.942849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = []\nindex = []\n\nfor patient_id, image in test_dataloader:\n    image = image.to(device)\n    pred_row = []\n    for out in model(image):\n        out = torch.nn.Softmax()(out.cpu())\n        pred_row.extend(out[0].tolist())\n    print(pred_row)\n    predictions.append(pred_row)\n    index.append(patient_id.item())\n    \npredictions = pd.DataFrame(predictions, index=index, columns=config.TARGET_COLUMNS)\npredictions = predictions.rename_axis('patient_id')\npredictions.sort_index(inplace=True)\npredictions","metadata":{"execution":{"iopub.status.busy":"2023-10-15T10:41:15.968892Z","iopub.execute_input":"2023-10-15T10:41:15.969611Z","iopub.status.idle":"2023-10-15T10:41:16.073477Z","shell.execute_reply.started":"2023-10-15T10:41:15.969582Z","shell.execute_reply":"2023-10-15T10:41:16.072461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-15T10:41:17.852228Z","iopub.execute_input":"2023-10-15T10:41:17.853206Z","iopub.status.idle":"2023-10-15T10:41:17.871941Z","shell.execute_reply.started":"2023-10-15T10:41:17.853171Z","shell.execute_reply":"2023-10-15T10:41:17.870897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# submission = predictions.reset_index()","metadata":{"execution":{"iopub.status.busy":"2023-10-15T10:41:19.287765Z","iopub.execute_input":"2023-10-15T10:41:19.288302Z","iopub.status.idle":"2023-10-15T10:41:19.292426Z","shell.execute_reply.started":"2023-10-15T10:41:19.288272Z","shell.execute_reply":"2023-10-15T10:41:19.291374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# submission.to_csv(\"/kaggle/working/submission.csv\" , index = False)","metadata":{"execution":{"iopub.status.busy":"2023-10-15T10:41:19.705794Z","iopub.execute_input":"2023-10-15T10:41:19.706641Z","iopub.status.idle":"2023-10-15T10:41:19.712985Z","shell.execute_reply.started":"2023-10-15T10:41:19.706575Z","shell.execute_reply":"2023-10-15T10:41:19.711869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model ,\"/kaggle/working/model.pth\")","metadata":{"execution":{"iopub.status.busy":"2023-10-15T10:41:20.679434Z","iopub.execute_input":"2023-10-15T10:41:20.679762Z","iopub.status.idle":"2023-10-15T10:41:20.781559Z","shell.execute_reply.started":"2023-10-15T10:41:20.679736Z","shell.execute_reply":"2023-10-15T10:41:20.78059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.to_csv(\"/kaggle/working/submission.csv\" , index = True)","metadata":{"execution":{"iopub.status.busy":"2023-10-15T10:41:21.907211Z","iopub.execute_input":"2023-10-15T10:41:21.90758Z","iopub.status.idle":"2023-10-15T10:41:21.917332Z","shell.execute_reply.started":"2023-10-15T10:41:21.907551Z","shell.execute_reply":"2023-10-15T10:41:21.916181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv(\"/kaggle/working/submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-10-15T10:41:24.483152Z","iopub.execute_input":"2023-10-15T10:41:24.48406Z","iopub.status.idle":"2023-10-15T10:41:24.503633Z","shell.execute_reply.started":"2023-10-15T10:41:24.48403Z","shell.execute_reply":"2023-10-15T10:41:24.502565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}