{"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\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\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":"2022-09-12T10:40:32.437326Z","iopub.execute_input":"2022-09-12T10:40:32.438023Z","iopub.status.idle":"2022-09-12T10:40:32.450792Z","shell.execute_reply.started":"2022-09-12T10:40:32.437934Z","shell.execute_reply":"2022-09-12T10:40:32.449813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.preprocessing import LabelEncoder\nimport torch\nfrom torchvision.io import read_image\nfrom torchvision import transforms, utils\nfrom torch.utils.data import Dataset, DataLoader,ConcatDataset\nfrom sklearn.preprocessing import LabelEncoder\nfrom torch import nn\nfrom torch import optim\nimport datetime\nfrom glob import glob\nimport nibabel as nib\nimport pydicom","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:40:32.636424Z","iopub.execute_input":"2022-09-12T10:40:32.636737Z","iopub.status.idle":"2022-09-12T10:40:33.703715Z","shell.execute_reply.started":"2022-09-12T10:40:32.636709Z","shell.execute_reply":"2022-09-12T10:40:33.702706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_dataset(patient_seg_paths):\n    images = []\n    features = []\n    cnt = 0;\n    for i in patient_seg_paths:\n        if(cnt > 10):\n            break\n        seg_data = nib.load(i)\n        seg_data = seg_data.get_fdata()\n        seg_data = seg_data[:, ::-1, ::-1].transpose(2, 1, 0)\n\n        patient_id = i.split(\"/\")\n        patient_id = patient_id[-1].replace(\".nii\",\"\")\n        print(patient_id)\n        patient_dcom_path = train_dir + patient_id + \"/*\"\n        patient_dcom_files_path = glob(patient_dcom_path)\n\n        for j in patient_dcom_files_path:\n            slice_number = j.split(\"/\")\n            slice_number = slice_number[-1].replace(\".dcm\",\"\")\n            dcm_img = pydicom.dcmread(j)\n\n            if(int(slice_number) > seg_data.shape[0]):\n                continue\n            bone_index = np.unique(seg_data[int(slice_number)-1])\n\n            slice_res = [0, 0, 0, 0, 0, 0, 0]\n            for x in bone_index:\n                if(int(x) < 8 and int(x) > 0):\n                    slice_res[int(x)-1] = 1\n\n\n            images.append(dcm_img.pixel_array)\n\n            # Features \n            sliceThicknes = dcm_img.SliceThickness\n            ImagePosition_x = dcm_img.ImagePositionPatient[0]\n            ImagePosition_y = dcm_img.ImagePositionPatient[1]\n            ImagePosition_z = dcm_img.ImagePositionPatient[2]\n            PixelSpacing_x = dcm_img.PixelSpacing[0]\n            PixelSpacing_y = dcm_img.PixelSpacing[1]\n            SliceRatio = int(slice_number)/len(patient_dcom_files_path)\n\n            feature = [sliceThicknes,ImagePosition_x , ImagePosition_y,ImagePosition_z,PixelSpacing_x,PixelSpacing_y,SliceRatio,patient_id,slice_number,slice_res]\n            features.append(feature)\n        cnt = cnt + 1;\n\n\n    return images,features","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:40:33.705713Z","iopub.execute_input":"2022-09-12T10:40:33.706191Z","iopub.status.idle":"2022-09-12T10:40:33.717353Z","shell.execute_reply.started":"2022-09-12T10:40:33.706162Z","shell.execute_reply":"2022-09-12T10:40:33.715736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_seg_paths = glob('../input/rsna-2022-cervical-spine-fracture-detection/segmentations/*')\ntrain_dir = \"../input/rsna-2022-cervical-spine-fracture-detection/train_images/\"","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:40:33.719014Z","iopub.execute_input":"2022-09-12T10:40:33.719825Z","iopub.status.idle":"2022-09-12T10:40:33.733766Z","shell.execute_reply.started":"2022-09-12T10:40:33.719723Z","shell.execute_reply":"2022-09-12T10:40:33.732818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images,features = generate_dataset(patient_seg_paths)","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:40:33.737051Z","iopub.execute_input":"2022-09-12T10:40:33.737555Z","iopub.status.idle":"2022-09-12T10:41:29.433456Z","shell.execute_reply.started":"2022-09-12T10:40:33.73751Z","shell.execute_reply":"2022-09-12T10:41:29.432432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !conda install -c conda-forge pillow -y\n# !conda install -c conda-forge pydicom -y\n# !conda install -c conda-forge gdcm -y\n# !pip install pylibjpeg pylibjpeg-libjpeg\n# !conda install gdcm -c conda-forge -y","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:41:29.434913Z","iopub.execute_input":"2022-09-12T10:41:29.435285Z","iopub.status.idle":"2022-09-12T10:41:29.4408Z","shell.execute_reply.started":"2022-09-12T10:41:29.435246Z","shell.execute_reply":"2022-09-12T10:41:29.439445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"featuresdf = pd.DataFrame(features,columns = [\"sliceThicknes\",\"ImagePosition_x\" , \"ImagePosition_y\",\"ImagePosition_z\",\"PixelSpacing_x\",\"PixelSpacing_y\",\"SliceRatio\",\"patient_id\",\"slice_number\",\"res\"])\nfeaturesdf","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:41:29.442251Z","iopub.execute_input":"2022-09-12T10:41:29.442879Z","iopub.status.idle":"2022-09-12T10:41:29.48503Z","shell.execute_reply.started":"2022-09-12T10:41:29.442842Z","shell.execute_reply":"2022-09-12T10:41:29.484169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"featuresdf.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:41:29.486269Z","iopub.execute_input":"2022-09-12T10:41:29.486699Z","iopub.status.idle":"2022-09-12T10:41:29.495006Z","shell.execute_reply.started":"2022-09-12T10:41:29.48666Z","shell.execute_reply":"2022-09-12T10:41:29.493858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgtedt = pydicom.dcmread('../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10005/10.dcm')\nimgtedt.pixel_array.shape\nimgtedt=imgtedt.pixel_array.reshape(512,512,-1)","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:41:29.496583Z","iopub.execute_input":"2022-09-12T10:41:29.497299Z","iopub.status.idle":"2022-09-12T10:41:29.505871Z","shell.execute_reply.started":"2022-09-12T10:41:29.497264Z","shell.execute_reply":"2022-09-12T10:41:29.504963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgtedt.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:41:29.51201Z","iopub.execute_input":"2022-09-12T10:41:29.513019Z","iopub.status.idle":"2022-09-12T10:41:29.519758Z","shell.execute_reply.started":"2022-09-12T10:41:29.512975Z","shell.execute_reply":"2022-09-12T10:41:29.518238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomImageDataset(Dataset):\n    def __init__(self, df):\n        self.df = df\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, idx):\n        patient_id = self.df.loc[idx,'patient_id']\n        slice_number = self.df.loc[idx,'slice_number']\n        sliceThickness = torch.tensor(self.df.loc[idx,'sliceThicknes'].astype(np.float32))\n        ImagePosition_x = torch.tensor(self.df.loc[idx,'ImagePosition_x'].astype(np.float32))\n        ImagePosition_y = torch.tensor(self.df.loc[idx,'ImagePosition_y'].astype(np.float32))\n        ImagePosition_z = torch.tensor(self.df.loc[idx,'ImagePosition_z'].astype(np.float32))\n        PixelSpacing_x = torch.tensor(self.df.loc[idx,'PixelSpacing_x'].astype(np.float32))\n        PixelSpacing_y = torch.tensor(self.df.loc[idx,'PixelSpacing_y'].astype(np.float32))\n        SliceRatio = torch.tensor(self.df.loc[idx,'SliceRatio'].astype(np.float32))\n        \n        \n        \n        img_path = '../input/rsna-2022-cervical-spine-fracture-detection/train_images/' + patient_id + \"/\" + slice_number + \".dcm\"\n        dcm_img = pydicom.dcmread(img_path)\n        image = dcm_img.pixel_array\n        \n        result = self.df.loc([idx,'res'])\n        \n        numeric_features=torch.hstack((sliceThicknes,ImagePosition_x , ImagePosition_y,ImagePosition_z,PixelSpacing_x,PixelSpacing_y,SliceRatio))\n        \n        return image, numeric_features, result","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:41:29.521466Z","iopub.execute_input":"2022-09-12T10:41:29.522588Z","iopub.status.idle":"2022-09-12T10:41:29.534263Z","shell.execute_reply.started":"2022-09-12T10:41:29.522549Z","shell.execute_reply":"2022-09-12T10:41:29.533244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data1=CustomImageDataset(df=featuresdf)","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:41:29.535658Z","iopub.execute_input":"2022-09-12T10:41:29.536095Z","iopub.status.idle":"2022-09-12T10:41:29.546046Z","shell.execute_reply.started":"2022-09-12T10:41:29.536061Z","shell.execute_reply":"2022-09-12T10:41:29.545053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data,val_data=torch.utils.data.random_split(data1,[2500,901])","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:41:56.887013Z","iopub.execute_input":"2022-09-12T10:41:56.887765Z","iopub.status.idle":"2022-09-12T10:41:56.893739Z","shell.execute_reply.started":"2022-09-12T10:41:56.887728Z","shell.execute_reply":"2022-09-12T10:41:56.892655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size=64\ntrain_dataloader = DataLoader(train_data, batch_size=batch_size, shuffle=True)\nval_dataloader = DataLoader(val_data, batch_size=batch_size, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:41:58.706666Z","iopub.execute_input":"2022-09-12T10:41:58.707439Z","iopub.status.idle":"2022-09-12T10:41:58.712654Z","shell.execute_reply.started":"2022-09-12T10:41:58.7074Z","shell.execute_reply":"2022-09-12T10:41:58.711376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class NeuralNetwork(nn.Module):\n    def __init__(self):\n        super(NeuralNetwork, self).__init__()\n        self.image_features_ = nn.Sequential(\n            nn.Conv2d(1, 16, kernel_size=5, stride=2, padding=2),\n            nn.ReLU(inplace=True),\n            nn.MaxPool2d(kernel_size=3, stride=2),\n            nn.Dropout(),\n            nn.Conv2d(16, 128, kernel_size=5, padding=2),\n            nn.ReLU(inplace=True),\n            nn.MaxPool2d(kernel_size=3, stride=2),\n            nn.Dropout(),\n            nn.Conv2d(128, 256, kernel_size=5, padding=2),\n            nn.ReLU(inplace=True),\n            nn.MaxPool2d(kernel_size=3, stride=2),\n            nn.Dropout(),\n            nn.Conv2d(256, 128, kernel_size=5, padding=2),\n            nn.ReLU(inplace=True),\n            nn.MaxPool2d(kernel_size=3, stride=2),\n            nn.Conv2d(128, 64, kernel_size=5, padding=2),\n            nn.ReLU(inplace=True),\n            nn.MaxPool2d(kernel_size=2, stride=2),\n        )\n        self.numeric_features_ = nn.Sequential(\n            nn.Linear(7, 64),\n            nn.ReLU(inplace=True),\n            nn.Dropout(),\n            nn.Linear(64, 64*3),\n            nn.ReLU(inplace=True),\n            nn.Dropout(),\n            nn.Linear(64*3, 64*3*3),\n            nn.ReLU(inplace=True),\n        )\n        self.combined_features_ = nn.Sequential(\n            nn.Linear(64*3*3*2, 64*3*3*2*2),\n            nn.ReLU(inplace=True),\n            nn.Dropout(),\n            nn.Linear(64*3*3*2*2, 64*3*3*2),\n            nn.ReLU(inplace=True),\n            nn.Linear(64*3*3*2, 64),\n            nn.Linear(64, 7),\n        )\n\n    def forward(self, x,y):\n        x = self.image_features_(x)\n        x=x.view(-1, 64*3*3)\n        y=self.numeric_features_(y)\n        z=torch.cat((x,y),1)\n        z=self.combined_features_(z)\n        return z","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:42:04.266791Z","iopub.execute_input":"2022-09-12T10:42:04.267156Z","iopub.status.idle":"2022-09-12T10:42:04.281499Z","shell.execute_reply.started":"2022-09-12T10:42:04.267125Z","shell.execute_reply":"2022-09-12T10:42:04.280268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\nprint('Using {} device'.format(device))\nmodel = NeuralNetwork().to(device)\noptimizer=optim.Adam(model.parameters(),1e-3)\nloss_fn=nn.CrossEntropyLoss()","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:42:05.326492Z","iopub.execute_input":"2022-09-12T10:42:05.326872Z","iopub.status.idle":"2022-09-12T10:42:07.213387Z","shell.execute_reply.started":"2022-09-12T10:42:05.326839Z","shell.execute_reply":"2022-09-12T10:42:07.212232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_loop(dataloader, model, loss_fn):\n    size = len(dataloader.dataset)\n    num_batches = len(dataloader)\n    test_loss, correct = 0, 0\n\n    with torch.no_grad():\n        for image, numeric_features,result in dataloader:\n            image=image.to(device)\n            numeric_features=numeric_features.to(device)\n            result = result.to(device)\n            pred = model(image,numeric_features)\n            print(pred)\n            test_loss += loss_fn(pred, result).item()\n            correct += (pred.argmax(1) == result).type(torch.float).sum().item()\n\n    test_loss /= num_batches\n    correct /= size\n    print(f\"Test Error: \\n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \\n\")","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:42:08.55182Z","iopub.execute_input":"2022-09-12T10:42:08.552183Z","iopub.status.idle":"2022-09-12T10:42:08.560344Z","shell.execute_reply.started":"2022-09-12T10:42:08.552151Z","shell.execute_reply":"2022-09-12T10:42:08.559193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def training_loop(n_epochs, optimizer, model, loss_fn, train_loader, val_loader):\n    for epoch in range(1,n_epochs+1):\n        loss_train = 0.0\n        for image, numeric_features,result in train_loader:\n            print(\"grb\")\n            image=image.to(device)\n            numeric_features=numeric_features.to(device)\n            result = result.to(device)\n            output=model(image, numeric_features)\n\n            \n            loss=loss_fn(output, result)\n            \n            #L2 Regularization\n            l2_lambda=0.001\n            l2_norm=sum(p.pow(2).sum() for p in model.parameters())\n            loss=loss+l2_lambda*l2_norm\n\n            optimizer.zero_grad()\n            loss.backward()\n            optimizer.step()\n\n            loss_train += loss.item()\n\n        if epoch == 1 or epoch % 10 == 0:\n            print('{} Epoch {}, Training loss {}'.format(datetime.datetime.now(), epoch,\n                                                         loss_train / len(train_loader)))\n            test_loop(dataloader=val_loader, model=model, loss_fn=loss_fn)","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:42:09.446441Z","iopub.execute_input":"2022-09-12T10:42:09.447466Z","iopub.status.idle":"2022-09-12T10:42:09.456696Z","shell.execute_reply.started":"2022-09-12T10:42:09.447419Z","shell.execute_reply":"2022-09-12T10:42:09.455577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_loop(\nn_epochs = 1,\noptimizer = optimizer,\nmodel = model,\nloss_fn = loss_fn,\ntrain_loader = train_dataloader,\nval_loader=train_dataloader)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:42:09.961711Z","iopub.execute_input":"2022-09-12T10:42:09.962077Z","iopub.status.idle":"2022-09-12T10:42:10.029919Z","shell.execute_reply.started":"2022-09-12T10:42:09.962045Z","shell.execute_reply":"2022-09-12T10:42:10.02814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataloader[0]","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:42:14.686413Z","iopub.execute_input":"2022-09-12T10:42:14.686873Z","iopub.status.idle":"2022-09-12T10:42:14.733239Z","shell.execute_reply.started":"2022-09-12T10:42:14.686832Z","shell.execute_reply":"2022-09-12T10:42:14.729642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-09-12T10:41:29.70646Z","iopub.status.idle":"2022-09-12T10:41:29.707092Z","shell.execute_reply.started":"2022-09-12T10:41:29.706838Z","shell.execute_reply":"2022-09-12T10:41:29.706861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}