{"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 os\nimport gc\nimport cv2\nimport copy\nimport time\nimport random\nimport string\nimport joblib\nimport tifffile\nimport numpy as np \nimport pandas as pd \nimport torch\nfrom torch import nn\nimport seaborn as sns\nimport pydicom as dicom\nfrom torchvision import models\nfrom scipy.special import expit\nfrom pydicom import dcmread\nimport matplotlib.pyplot as plt\nfrom torch.utils.data import Dataset, DataLoader\nfrom sklearn.model_selection import train_test_split\nimport torch.nn.functional as F\nfrom tqdm.notebook import tqdm\nfrom torch.optim import lr_scheduler\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-15T03:13:11.082995Z","iopub.execute_input":"2022-10-15T03:13:11.083499Z","iopub.status.idle":"2022-10-15T03:13:14.424726Z","shell.execute_reply.started":"2022-10-15T03:13:11.083417Z","shell.execute_reply":"2022-10-15T03:13:14.423524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bad = np.array([['1.2.826.0.1.3680043.10197_C1', '1.2.826.0.1.3680043.10197','C1'],['1.2.826.0.1.3680043.10454_C1', '1.2.826.0.1.3680043.10454','C1'],['1.2.826.0.1.3680043.10690_C1', '1.2.826.0.1.3680043.10690','C1']], dtype=np.object)","metadata":{"execution":{"iopub.status.busy":"2022-10-15T03:13:14.430516Z","iopub.execute_input":"2022-10-15T03:13:14.431221Z","iopub.status.idle":"2022-10-15T03:13:14.440772Z","shell.execute_reply.started":"2022-10-15T03:13:14.431171Z","shell.execute_reply":"2022-10-15T03:13:14.439852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"debug = False\ntrain_df = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/train.csv\").head(10000)\ntest_df = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/test.csv\")\nif(test_df.values[0][0] == bad[0][0]):\n    test_df = pd.DataFrame({\"row_id\": ['1.2.826.0.1.3680043.22327_C1', '1.2.826.0.1.3680043.25399_C1', '1.2.826.0.1.3680043.5876_C1'],\n                           \"StudyInstanceUID\": ['1.2.826.0.1.3680043.22327', '1.2.826.0.1.3680043.25399', '1.2.826.0.1.3680043.5876'],\n                           \"prediction_type\": [\"C1\", \"C1\", \"C1\"]})\ndirs = [\"../input/rsna-2022-cervical-spine-fracture-detection/train_images\",  \"../input/rsna-2022-cervical-spine-fracture-detection/test_images\"]\nmeans = list(train_df.mean(numeric_only=True).to_dict().values())\ntest_df","metadata":{"execution":{"iopub.status.busy":"2022-10-15T03:13:14.445891Z","iopub.execute_input":"2022-10-15T03:13:14.446594Z","iopub.status.idle":"2022-10-15T03:13:14.501686Z","shell.execute_reply.started":"2022-10-15T03:13:14.446557Z","shell.execute_reply":"2022-10-15T03:13:14.500456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom(path, size = 512):\n    try:\n        img=dicom.dcmread(path)\n        img.PhotometricInterpretation = 'YBR_FULL'\n        data=img.pixel_array\n        data=data-np.min(data)\n        if np.max(data) != 0:\n            data=data/np.max(data)\n        data=(data*255).astype(np.uint8)\n        gc.collect()\n        return cv2.cvtColor(data.reshape(512,512), cv2.COLOR_GRAY2RGB).transpose(2,0,1)\n    except:\n        return np.zeros((3,512,512)).transpose(2,0,1)","metadata":{"execution":{"iopub.status.busy":"2022-10-15T03:13:14.505437Z","iopub.execute_input":"2022-10-15T03:13:14.50607Z","iopub.status.idle":"2022-10-15T03:13:14.514353Z","shell.execute_reply.started":"2022-10-15T03:13:14.506034Z","shell.execute_reply":"2022-10-15T03:13:14.513306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ImgDataset(Dataset):\n    def __init__(self, df, max_images = 10):\n        self.df = df \n        self.train = 'patient_overall' in df.columns\n        self.imgs = []\n        self.labels = []\n        self.ids = []\n        self.prediction_type = []\n        for i in range(self.df.shape[0]):\n            lst = os.listdir(os.path.join(dirs[1-self.train], self.df.StudyInstanceUID.values[i]))\n            random.shuffle(lst)\n            for im in lst[:max_images]:\n                self.prediction_type.append([\"patient_overall\", \"C1\", \"C2\", \"C3\", \"C4\", \"C5\", \"C6\", \"C7\"].index(self.df.prediction_type.values[i]))\n                if(not self.train):\n                    self.ids.append(self.df.row_id.values[i])\n                self.imgs.append(os.path.join(dirs[1-self.train], self.df.StudyInstanceUID.values[i], im))\n                if(self.train):\n                    self.labels.append(self.df.drop([\"StudyInstanceUID\"], axis = 1).iloc[i].values)\n                else:\n                    self.labels.append([0]*8)\n            del lst\n            gc.collect()\n    def __len__(self):\n        return len(self.imgs)\n    \n    def __getitem__(self, index):\n        image = load_dicom(self.imgs[index])\n        label = self.labels[index]\n        gc.collect()\n        return image, label, self.ids[index], self.prediction_type[index]","metadata":{"execution":{"iopub.status.busy":"2022-10-15T03:13:14.517793Z","iopub.execute_input":"2022-10-15T03:13:14.518889Z","iopub.status.idle":"2022-10-15T03:13:14.534627Z","shell.execute_reply.started":"2022-10-15T03:13:14.518851Z","shell.execute_reply":"2022-10-15T03:13:14.533488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(model, dataloader):\n    model.cuda()\n    model.eval()\n    dataloader = dataloader\n    outputs = []\n    ids = []\n    for item in tqdm(dataloader, leave=False):\n        patient_id = item[2][0]\n        try:\n            images = item[0].cuda().float()\n            ids.append(patient_id)\n            output = model(images)\n            outputs.append(((output.cpu()[0].detach().numpy()).astype(float)[item[3]]))\n        except:\n            outputs.append(means[item[3]])\n    return np.array([min(1,max(i, 0)) for i in outputs]), ids","metadata":{"execution":{"iopub.status.busy":"2022-10-15T03:13:14.536525Z","iopub.execute_input":"2022-10-15T03:13:14.537335Z","iopub.status.idle":"2022-10-15T03:13:14.546274Z","shell.execute_reply.started":"2022-10-15T03:13:14.53728Z","shell.execute_reply":"2022-10-15T03:13:14.545204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = torch.jit.load('../input/rsnamodel/model.pth')\nbatch_size = 1\ntest_loader = DataLoader(\n    ImgDataset(test_df), \n    batch_size=batch_size, \n    shuffle=False, \n    num_workers=1\n)\ncriterion = nn.CrossEntropyLoss()","metadata":{"execution":{"iopub.status.busy":"2022-10-15T03:13:14.54815Z","iopub.execute_input":"2022-10-15T03:13:14.548975Z","iopub.status.idle":"2022-10-15T03:13:19.552791Z","shell.execute_reply.started":"2022-10-15T03:13:14.548884Z","shell.execute_reply":"2022-10-15T03:13:19.551564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pr = predict(model, test_loader)","metadata":{"execution":{"iopub.status.busy":"2022-10-15T03:13:36.999354Z","iopub.execute_input":"2022-10-15T03:13:36.999724Z","iopub.status.idle":"2022-10-15T03:13:55.125047Z","shell.execute_reply.started":"2022-10-15T03:13:36.999693Z","shell.execute_reply":"2022-10-15T03:13:55.12378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pr = pd.DataFrame({\"row_id\" : pr[1], \"fractured\" : pr[0]}).groupby(\"row_id\").mean()","metadata":{"execution":{"iopub.status.busy":"2022-10-15T03:13:55.128306Z","iopub.execute_input":"2022-10-15T03:13:55.128651Z","iopub.status.idle":"2022-10-15T03:13:55.144329Z","shell.execute_reply.started":"2022-10-15T03:13:55.128618Z","shell.execute_reply":"2022-10-15T03:13:55.143059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/sample_submission.csv\")\nsubmission.fractured = pr.fractured.to_list()","metadata":{"execution":{"iopub.status.busy":"2022-10-15T03:13:56.364348Z","iopub.execute_input":"2022-10-15T03:13:56.365Z","iopub.status.idle":"2022-10-15T03:13:56.380685Z","shell.execute_reply.started":"2022-10-15T03:13:56.364965Z","shell.execute_reply":"2022-10-15T03:13:56.379616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-10-15T03:14:05.490901Z","iopub.execute_input":"2022-10-15T03:14:05.491265Z","iopub.status.idle":"2022-10-15T03:14:05.502394Z","shell.execute_reply.started":"2022-10-15T03:14:05.491236Z","shell.execute_reply":"2022-10-15T03:14:05.501373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2022-10-15T03:14:13.552518Z","iopub.execute_input":"2022-10-15T03:14:13.553043Z","iopub.status.idle":"2022-10-15T03:14:13.568235Z","shell.execute_reply.started":"2022-10-15T03:14:13.552989Z","shell.execute_reply":"2022-10-15T03:14:13.565634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}