{"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":"markdown","source":"Thanks to [Vladimir Slaykovskiy](https://www.kaggle.com/vslaykovsky) and [Andrada Olteanu](https://www.kaggle.com/andradaolteanu) for their wonderful notebooks which was helpful while trying new approaches and new methods in this competetions.","metadata":{}},{"cell_type":"markdown","source":"# EFFNET-V2","metadata":{}},{"cell_type":"code","source":"try:\n    import pylibjpeg\nexcept:\n    # Offline dependencies:\n    !mkdir -p /root/.cache/torch/hub/checkpoints/\n    !cp ../input/rsna-2022-whl/efficientnet_v2_s-dd5fe13b.pth  /root/.cache/torch/hub/checkpoints/\n\n    !pip install /kaggle/input/rsna-2022-whl/{pydicom-2.3.0-py3-none-any.whl,pylibjpeg-1.4.0-py3-none-any.whl,python_gdcm-3.0.15-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl}\n    !pip install /kaggle/input/rsna-2022-whl/{torch-1.12.1-cp37-cp37m-manylinux1_x86_64.whl,torchvision-0.13.1-cp37-cp37m-manylinux1_x86_64.whl}","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\nimport glob\nimport os\nimport re\n\nimport cv2\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport pydicom as dicom\nimport torch\nimport torchvision as tv\nfrom sklearn.model_selection import GroupKFold\nfrom torch.cuda.amp import GradScaler, autocast\nfrom torchvision.models.feature_extraction import create_feature_extractor\nfrom tqdm.notebook import tqdm\n\nimport wandb\n\npd.set_option('display.max_rows', 1000)\npd.set_option('display.max_columns', 1000)\nplt.rcParams['figure.figsize'] = (20, 5)\n\n\n# Effnet\nWEIGHTS = tv.models.efficientnet.EfficientNet_V2_S_Weights.DEFAULT\nRSNA_2022_PATH = '../input/rsna-2022-cervical-spine-fracture-detection'\nTRAIN_IMAGES_PATH = f'{RSNA_2022_PATH}/train_images'\nTEST_IMAGES_PATH = f'{RSNA_2022_PATH}/test_images'\nEFFNET_CHECKPOINTS_PATH = '../input/rsna-2022-base-effnetv2'\n\n# MODEL_NAMES = [f'effnetv2']\n\n# This notebook supports ensembles and single model predictions. Uncomment to switch to ensemble prediction:\nMODEL_NAMES = [f'effnetv2-f{i}' for i in range(5)]\n\n# Common\nFRAC_COLS = [f'C{i}_effnet_frac' for i in range(1, 8)]\nVERT_COLS = [f'C{i}_effnet_vert' for i in range(1, 8)]\n\ntry:\n    from kaggle_secrets import UserSecretsClient\n    IS_KAGGLE = True\nexcept:\n    IS_KAGGLE = False\n\n\n# Switch to offline for submission\nos.environ[\"WANDB_MODE\"] = \"offline\"\n\nif os.environ[\"WANDB_MODE\"] == \"online\":\n    if IS_KAGGLE:\n        os.environ['WANDB_API_KEY'] = UserSecretsClient().get_secret(\"WANDB_API_KEY\")\n\nif not IS_KAGGLE:\n    print('Running locally')\n    RSNA_2022_PATH = '/mnt/rsna2022'\n    TRAIN_IMAGES_PATH = '/mnt/rsna2022/train_images'\n    TEST_IMAGES_PATH = '/mnt/rsna2022/test_images'\n    METADATA_PATH = '/home/vslaykovsky/Downloads/'\n    EFFNET_CHECKPOINTS_PATH = 'frac_checkpoints'\n    os.environ['WANDB_API_KEY'] = 'yourkeyhere'\n\n\nDEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'\nif DEVICE == 'cuda':\n    BATCH_SIZE = 32\nelse:\n    BATCH_SIZE = 2","metadata":{"papermill":{"duration":2.969425,"end_time":"2022-08-29T06:36:21.083846","exception":false,"start_time":"2022-08-29T06:36:18.114421","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"execution":{"iopub.status.busy":"2022-10-16T08:18:12.41151Z","iopub.execute_input":"2022-10-16T08:18:12.412157Z","iopub.status.idle":"2022-10-16T08:18:15.276652Z","shell.execute_reply.started":"2022-10-16T08:18:12.412112Z","shell.execute_reply":"2022-10-16T08:18:15.275456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\" style=\"font-size:25px\">\n    🦴 2. Loading train/eval/test dataframes 🦴\n</div>\n\n1. Loading data from competition dataset folder `../input/rsna-2022-cervical-spine-fracture-detection/test.csv`\n2. Joining data with slice information collected from test image folders `../input/rsna-2022-cervical-spine-fracture-detection/test_images/*/*`","metadata":{"papermill":{"duration":0.006232,"end_time":"2022-08-29T06:36:21.096794","exception":false,"start_time":"2022-08-29T06:36:21.090562","status":"completed"},"pycharm":{"name":"#%% md\n"},"tags":[]}},{"cell_type":"code","source":"def load_df_test():\n    df_test = pd.read_csv(f'{RSNA_2022_PATH}/test.csv')\n\n    if df_test.iloc[0].row_id == '1.2.826.0.1.3680043.10197_C1':\n        # test_images and test.csv are inconsistent in the dev dataset, fixing labels for the dev run.\n        df_test = pd.DataFrame({\n            \"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\", \"patient_overall\"]}\n        )\n    return df_test\n\ndf_test = load_df_test()\ndf_test","metadata":{"papermill":{"duration":0.045491,"end_time":"2022-08-29T06:36:21.148473","exception":false,"start_time":"2022-08-29T06:36:21.102982","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"execution":{"iopub.status.busy":"2022-10-16T08:18:15.278708Z","iopub.execute_input":"2022-10-16T08:18:15.279648Z","iopub.status.idle":"2022-10-16T08:18:15.321562Z","shell.execute_reply.started":"2022-10-16T08:18:15.279597Z","shell.execute_reply":"2022-10-16T08:18:15.320703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_slices = glob.glob(f'{TEST_IMAGES_PATH}/*/*')\ntest_slices = [re.findall(f'{TEST_IMAGES_PATH}/(.*)/(.*).dcm', s)[0] for s in test_slices]\ndf_test_slices = pd.DataFrame(data=test_slices, columns=['StudyInstanceUID', 'Slice']).astype({'Slice': int}).sort_values(['StudyInstanceUID', 'Slice']).reset_index(drop=True)\ndf_test_slices","metadata":{"papermill":{"duration":0.145663,"end_time":"2022-08-29T06:36:21.302098","exception":false,"start_time":"2022-08-29T06:36:21.156435","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"execution":{"iopub.status.busy":"2022-10-16T08:18:15.325328Z","iopub.execute_input":"2022-10-16T08:18:15.326043Z","iopub.status.idle":"2022-10-16T08:18:15.476249Z","shell.execute_reply.started":"2022-10-16T08:18:15.326014Z","shell.execute_reply":"2022-10-16T08:18:15.475188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\" style=\"font-size:25px\">\n    🦴 3. Dataset class 🦴\n</div>\n\n`EffnetDataSet` class returns images of individual slices. It uses a dataframe parameter `df` as a source of slices metadata to locate and load images from `path` folder. It accepts transforms parameter which we set to `WEIGHTS.transforms()`. This is a set of transforms used to pre-train the model on ImageNet dataset.","metadata":{"papermill":{"duration":0.006754,"end_time":"2022-08-29T06:36:21.315661","exception":false,"start_time":"2022-08-29T06:36:21.308907","status":"completed"},"pycharm":{"name":"#%% md\n"},"tags":[]}},{"cell_type":"code","source":"def load_dicom(path):\n    \"\"\"\n    This supports loading both regular and compressed JPEG images. \n    See the first sell with `pip install` commands for the necessary dependencies\n    \"\"\"\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    return cv2.cvtColor(data, cv2.COLOR_GRAY2RGB), img\n\n\nim, meta = load_dicom(f'{TRAIN_IMAGES_PATH}/1.2.826.0.1.3680043.10001/1.dcm')\nplt.figure()\nplt.imshow(im)\nplt.title('regular image')\n\nim, meta = load_dicom(f'{TRAIN_IMAGES_PATH}/1.2.826.0.1.3680043.10014/1.dcm')\nplt.figure()\nplt.imshow(im)\nplt.title('jpeg')","metadata":{"papermill":{"duration":0.619183,"end_time":"2022-08-29T06:36:21.941518","exception":false,"start_time":"2022-08-29T06:36:21.322335","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"execution":{"iopub.status.busy":"2022-10-16T08:18:15.477726Z","iopub.execute_input":"2022-10-16T08:18:15.478046Z","iopub.status.idle":"2022-10-16T08:18:16.100424Z","shell.execute_reply.started":"2022-10-16T08:18:15.478013Z","shell.execute_reply":"2022-10-16T08:18:16.099256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class EffnetDataSet(torch.utils.data.Dataset):    \n    def __init__(self, df, path, transforms=None):\n        super().__init__()\n        self.df = df\n        self.path = path\n        self.transforms = transforms\n        \n    def __getitem__(self, i):\n        path = os.path.join(self.path, self.df.iloc[i].StudyInstanceUID, f'{self.df.iloc[i].Slice}.dcm')        \n        \n        try:\n            img = load_dicom(path)[0]         \n            img = np.transpose(img, (2, 0, 1))  # Pytorch uses (batch, channel, height, width) order. Converting (height, width, channel) -> (channel, height, width)\n            if self.transforms is not None:\n                img = self.transforms(torch.as_tensor(img))\n        except Exception as ex:\n            print(ex)\n            return None\n        \n        if 'C1_fracture' in self.df:\n            frac_targets = torch.as_tensor(self.df.iloc[i][['C1_fracture', 'C2_fracture', 'C3_fracture', 'C4_fracture', 'C5_fracture', 'C6_fracture', 'C7_fracture']].astype('float32').values)\n            vert_targets = torch.as_tensor(self.df.iloc[i][['C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7']].astype('float32').values)\n            frac_targets = frac_targets * vert_targets   # we only enable targets that are visible on the current slice\n            return img, frac_targets, vert_targets\n        return img        \n    \n    def __len__(self):\n        return len(self.df)\n    ","metadata":{"papermill":{"duration":0.023057,"end_time":"2022-08-29T06:36:21.974596","exception":false,"start_time":"2022-08-29T06:36:21.951539","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"execution":{"iopub.status.busy":"2022-10-16T08:18:16.102029Z","iopub.execute_input":"2022-10-16T08:18:16.102633Z","iopub.status.idle":"2022-10-16T08:18:16.113411Z","shell.execute_reply.started":"2022-10-16T08:18:16.102595Z","shell.execute_reply":"2022-10-16T08:18:16.112233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Only X values returned by the test dataset\nds_test = EffnetDataSet(df_test_slices, TEST_IMAGES_PATH, WEIGHTS.transforms())\nX = ds_test[42]\nX.shape","metadata":{"papermill":{"duration":0.04504,"end_time":"2022-08-29T06:36:22.02892","exception":false,"start_time":"2022-08-29T06:36:21.98388","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"execution":{"iopub.status.busy":"2022-10-16T08:18:16.115198Z","iopub.execute_input":"2022-10-16T08:18:16.115592Z","iopub.status.idle":"2022-10-16T08:18:16.161507Z","shell.execute_reply.started":"2022-10-16T08:18:16.115553Z","shell.execute_reply":"2022-10-16T08:18:16.160644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\" style=\"font-size:25px\">\n    🦴 4. Model 🦴\n</div>\n\n\nIn Pytorch we use create_feature_extractor to access feature layers of pre-existing models. Final flat layer of `efficientnet_v2_s` model is called `flatten`. We'll build our classification layer on top of it. ","metadata":{"papermill":{"duration":0.008956,"end_time":"2022-08-29T06:36:22.047427","exception":false,"start_time":"2022-08-29T06:36:22.038471","status":"completed"},"pycharm":{"name":"#%% md\n"},"tags":[]}},{"cell_type":"code","source":"class EffnetModel(torch.nn.Module):\n    def __init__(self):\n        super().__init__()\n        effnet = tv.models.efficientnet_v2_s()\n        self.model = create_feature_extractor(effnet, ['flatten'])\n        self.nn_fracture = torch.nn.Sequential(\n            torch.nn.Linear(1280, 7),\n        )\n        self.nn_vertebrae = torch.nn.Sequential(\n            torch.nn.Linear(1280, 7),\n        )\n\n    def forward(self, x):\n        # returns logits\n        x = self.model(x)['flatten']\n        return self.nn_fracture(x), self.nn_vertebrae(x)\n\n    def predict(self, x):\n        frac, vert = self.forward(x)\n        return torch.sigmoid(frac), torch.sigmoid(vert)\n\nmodel = EffnetModel()\nmodel.predict(torch.randn(1, 3, 512, 512))\ndel model","metadata":{"papermill":{"duration":2.094724,"end_time":"2022-08-29T06:36:24.151144","exception":false,"start_time":"2022-08-29T06:36:22.05642","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"execution":{"iopub.status.busy":"2022-10-16T08:18:16.162838Z","iopub.execute_input":"2022-10-16T08:18:16.163151Z","iopub.status.idle":"2022-10-16T08:18:18.073514Z","shell.execute_reply.started":"2022-10-16T08:18:16.163125Z","shell.execute_reply":"2022-10-16T08:18:18.072501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_model(model, name, path='.'):\n    data = torch.load(os.path.join(path, f'{name}.tph'), map_location=DEVICE)\n    model.load_state_dict(data)\n    return model","metadata":{"papermill":{"duration":0.019322,"end_time":"2022-08-29T06:36:24.180428","exception":false,"start_time":"2022-08-29T06:36:24.161106","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"execution":{"iopub.status.busy":"2022-10-16T08:18:18.074893Z","iopub.execute_input":"2022-10-16T08:18:18.075883Z","iopub.status.idle":"2022-10-16T08:18:18.082211Z","shell.execute_reply.started":"2022-10-16T08:18:18.075831Z","shell.execute_reply":"2022-10-16T08:18:18.080734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"effnet_models = [load_model(EffnetModel(), name, EFFNET_CHECKPOINTS_PATH).to(DEVICE) for name in MODEL_NAMES]","metadata":{"papermill":{"duration":4.116502,"end_time":"2022-08-29T06:36:28.306547","exception":false,"start_time":"2022-08-29T06:36:24.190045","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"execution":{"iopub.status.busy":"2022-10-16T08:18:18.086878Z","iopub.execute_input":"2022-10-16T08:18:18.087871Z","iopub.status.idle":"2022-10-16T08:18:30.379451Z","shell.execute_reply.started":"2022-10-16T08:18:18.087834Z","shell.execute_reply":"2022-10-16T08:18:30.378441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\" style=\"font-size:25px\">\n    🦴 7. Submission 🦴\n</div>\n\n1. We run all baseline `effnet_model` on every image from the test set and average outputs \n2. We pass average outputs of the base `effnet_model` to the `lstm_model` to produce the final result for each patient.","metadata":{"papermill":{"duration":0.008912,"end_time":"2022-08-29T06:36:28.325049","exception":false,"start_time":"2022-08-29T06:36:28.316137","status":"completed"},"pycharm":{"name":"#%% md\n"},"tags":[]}},{"cell_type":"code","source":"from typing import List\n\n\ndef predict_effnet(models: List[EffnetModel], ds, max_batches=1e9):\n    dl_test = torch.utils.data.DataLoader(ds, batch_size=BATCH_SIZE, shuffle=False, num_workers=os.cpu_count())\n    for m in models:\n        m.eval()\n\n    with torch.no_grad():\n        predictions = []\n        for idx, X in enumerate(tqdm(dl_test, miniters=10)):\n            pred = torch.zeros(len(X), 14).to(DEVICE)\n            for m in models:\n                y1, y2 = m.predict(X.to(DEVICE))\n                pred += torch.concat([y1, y2], dim=1) / len(models)\n            predictions.append(pred)\n            if idx >= max_batches:\n                break\n        return torch.concat(predictions).cpu().numpy()\n\n# Quick test\npredict_effnet([EffnetModel().to(DEVICE)], ds_test, max_batches=2).shape","metadata":{"collapsed":false,"jupyter":{"outputs_hidden":false},"papermill":{"duration":3.84226,"end_time":"2022-08-29T06:36:32.176344","exception":false,"start_time":"2022-08-29T06:36:28.334084","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"execution":{"iopub.status.busy":"2022-10-16T08:18:30.3812Z","iopub.execute_input":"2022-10-16T08:18:30.381595Z","iopub.status.idle":"2022-10-16T08:18:34.437654Z","shell.execute_reply.started":"2022-10-16T08:18:30.381557Z","shell.execute_reply":"2022-10-16T08:18:34.436288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"effnet_pred = predict_effnet(effnet_models, ds_test)\n\ndf_effnet_pred = pd.DataFrame(\n    data=effnet_pred, columns=[f'C{i}_effnet_frac' for i in range(1, 8)] + [f'C{i}_effnet_vert' for i in range(1, 8)]\n)","metadata":{"papermill":{"duration":23.579326,"end_time":"2022-08-29T06:36:55.765273","exception":false,"start_time":"2022-08-29T06:36:32.185947","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"execution":{"iopub.status.busy":"2022-10-16T08:18:34.439481Z","iopub.execute_input":"2022-10-16T08:18:34.440264Z","iopub.status.idle":"2022-10-16T08:19:12.301056Z","shell.execute_reply.started":"2022-10-16T08:18:34.440221Z","shell.execute_reply":"2022-10-16T08:19:12.299758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test_pred = pd.concat([df_test_slices, df_effnet_pred], axis=1).sort_values(['StudyInstanceUID', 'Slice'])\ndf_test_pred","metadata":{"papermill":{"duration":0.063127,"end_time":"2022-08-29T06:36:55.844489","exception":false,"start_time":"2022-08-29T06:36:55.781362","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"execution":{"iopub.status.busy":"2022-10-16T08:19:12.303148Z","iopub.execute_input":"2022-10-16T08:19:12.303848Z","iopub.status.idle":"2022-10-16T08:19:12.341178Z","shell.execute_reply.started":"2022-10-16T08:19:12.303799Z","shell.execute_reply":"2022-10-16T08:19:12.339679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_sample_patient(df_pred):\n    patient = np.random.choice(df_pred.StudyInstanceUID)\n    df = df_pred.query('StudyInstanceUID == @patient').reset_index()\n\n    df[[f'C{i}_effnet_frac' for i in range(1, 8)]].plot(\n        title=f'Patient {patient}, fracture prediction',\n        ax=(plt.subplot(1, 2, 1)))\n\n    df[[f'C{i}_effnet_vert' for i in range(1, 8)]].plot(\n        title=f'Patient {patient}, vertebrae prediction',\n        ax=plt.subplot(1, 2, 2)\n    )\n\nplot_sample_patient(df_test_pred)","metadata":{"collapsed":false,"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.629707,"end_time":"2022-08-29T06:36:56.489965","exception":false,"start_time":"2022-08-29T06:36:55.860258","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"execution":{"iopub.status.busy":"2022-10-16T08:19:12.343678Z","iopub.execute_input":"2022-10-16T08:19:12.344096Z","iopub.status.idle":"2022-10-16T08:19:13.115613Z","shell.execute_reply.started":"2022-10-16T08:19:12.34406Z","shell.execute_reply":"2022-10-16T08:19:13.114593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef patient_prediction(df):\n    c1c7 = np.average(df[FRAC_COLS].values, axis=0, weights=df[VERT_COLS].values)\n    pred_patient_overall = 1 - np.prod(1 - c1c7)\n    return pd.Series(data=np.concatenate([[pred_patient_overall], c1c7]), index=['patient_overall'] + [f'C{i}' for i in range(1, 8)])\n\ndf_patient_pred = df_test_pred.groupby('StudyInstanceUID').apply(lambda df: patient_prediction(df))\ndf_patient_pred","metadata":{"collapsed":false,"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.038032,"end_time":"2022-08-29T06:36:56.54117","exception":false,"start_time":"2022-08-29T06:36:56.503138","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"execution":{"iopub.status.busy":"2022-10-16T08:19:13.117245Z","iopub.execute_input":"2022-10-16T08:19:13.117912Z","iopub.status.idle":"2022-10-16T08:19:13.146613Z","shell.execute_reply.started":"2022-10-16T08:19:13.117866Z","shell.execute_reply":"2022-10-16T08:19:13.145637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub = df_test.copy()\ndf_sub = df_sub.set_index('StudyInstanceUID').join(df_patient_pred)\ndf_sub['fractured'] = df_sub.apply(lambda r: r[r.prediction_type], axis=1)\ndf_sub","metadata":{"collapsed":false,"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.036645,"end_time":"2022-08-29T06:36:56.590835","exception":false,"start_time":"2022-08-29T06:36:56.55419","status":"completed"},"pycharm":{"name":"#%%\n"},"tags":[],"execution":{"iopub.status.busy":"2022-10-16T08:19:13.148271Z","iopub.execute_input":"2022-10-16T08:19:13.148645Z","iopub.status.idle":"2022-10-16T08:19:13.193305Z","shell.execute_reply.started":"2022-10-16T08:19:13.148607Z","shell.execute_reply":"2022-10-16T08:19:13.192118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"effnet=df_sub[['row_id', 'fractured']]","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:19:13.195771Z","iopub.execute_input":"2022-10-16T08:19:13.196533Z","iopub.status.idle":"2022-10-16T08:19:13.205052Z","shell.execute_reply.started":"2022-10-16T08:19:13.19649Z","shell.execute_reply":"2022-10-16T08:19:13.203583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"effnet['fractured']","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:19:13.208575Z","iopub.execute_input":"2022-10-16T08:19:13.21052Z","iopub.status.idle":"2022-10-16T08:19:13.224068Z","shell.execute_reply.started":"2022-10-16T08:19:13.210454Z","shell.execute_reply":"2022-10-16T08:19:13.222603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# VIT 3D","metadata":{}},{"cell_type":"code","source":"!pip install -qU ../input/for-pydicom/python_gdcm-3.0.14-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl ../input/for-pydicom/pylibjpeg-1.4.0-py3-none-any.whl --find-links frozen_packages --no-index","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:19:13.22593Z","iopub.execute_input":"2022-10-16T08:19:13.22662Z","iopub.status.idle":"2022-10-16T08:19:33.460796Z","shell.execute_reply.started":"2022-10-16T08:19:13.226577Z","shell.execute_reply":"2022-10-16T08:19:33.459584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_files = [f'../input/384-vit3d/best_fold_vit3d_{i} .pth' for i in range(0,1)]","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:19:33.46419Z","iopub.execute_input":"2022-10-16T08:19:33.464583Z","iopub.status.idle":"2022-10-16T08:19:33.472517Z","shell.execute_reply.started":"2022-10-16T08:19:33.464548Z","shell.execute_reply":"2022-10-16T08:19:33.471382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Libraries\nimport os\nimport re\nimport gc\nimport cv2\nimport wandb\nfrom PIL import Image\nimport random\nimport math\nimport shutil\nimport glob\nfrom tqdm import tqdm\nfrom pprint import pprint\nfrom time import time\nimport warnings\nimport pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib as mpl\nfrom matplotlib import cm\nimport matplotlib.patches as patches\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom matplotlib.offsetbox import AnnotationBbox, OffsetImage\nfrom matplotlib.colors import ListedColormap, LinearSegmentedColormap\nfrom matplotlib.patches import Rectangle\nfrom IPython.display import display_html\nplt.rcParams.update({'font.size': 16})\n\n# Environment check\nwarnings.filterwarnings(\"ignore\")\nos.environ[\"WANDB_SILENT\"] = \"true\"\nCONFIG = {'competition': 'RSNA_SpineFructure', '_wandb_kernel': 'aot'}\n\n# Custom colors\nclass clr:\n    S = '\\033[1m' + '\\033[94m'\n    E = '\\033[0m'\n    \nmy_colors = [\"#5EAFD9\", \"#449DD1\", \"#3977BB\", \n             \"#2D51A5\", \"#5C4C8F\", \"#8B4679\",\n             \"#C53D4C\", \"#E23836\", \"#FF4633\", \"#FF5746\"]\nCMAP1 = ListedColormap(my_colors)\n\nprint(clr.S+\"Notebook Color Schemes:\"+clr.E)\nsns.palplot(sns.color_palette(my_colors))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:19:33.473968Z","iopub.execute_input":"2022-10-16T08:19:33.474345Z","iopub.status.idle":"2022-10-16T08:19:33.635553Z","shell.execute_reply.started":"2022-10-16T08:19:33.474309Z","shell.execute_reply":"2022-10-16T08:19:33.634196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom as dicom\ndef load_dicom(path,img_size=384):\n    img=dicom.dcmread(path)\n    img.PhotometricInterpretation='YBR_FULL'\n    data=img.pixel_array\n    data=cv2.resize(data,(384,384))\n    return data","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:19:33.641859Z","iopub.execute_input":"2022-10-16T08:19:33.645482Z","iopub.status.idle":"2022-10-16T08:19:33.656658Z","shell.execute_reply.started":"2022-10-16T08:19:33.645423Z","shell.execute_reply":"2022-10-16T08:19:33.655008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install '../input/einops/einops-0.3.0-py2.py3-none-any.whl'\n\nimport os\nimport cv2\n\nimport torch\nimport torch.nn.functional as F\nfrom torch.utils.data import Dataset, DataLoader\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.utils import shuffle\n\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\n\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\n\nfrom torch import nn\nfrom torch import Tensor\nfrom torch.utils.data import Subset\n\nfrom PIL import Image\nimport pandas as pd\nimport numpy as np\n\nfrom torchvision.transforms import Compose, Resize, ToTensor\nfrom torch.optim.lr_scheduler import StepLR\nimport torch.optim as optim\n\nfrom einops import rearrange, reduce, repeat\nfrom einops.layers.torch import Rearrange, Reduce","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:19:33.663259Z","iopub.execute_input":"2022-10-16T08:19:33.664182Z","iopub.status.idle":"2022-10-16T08:20:04.207968Z","shell.execute_reply.started":"2022-10-16T08:19:33.664115Z","shell.execute_reply":"2022-10-16T08:20:04.206932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PyTorch\nimport torch\nfrom torch.utils.data import TensorDataset, DataLoader, Dataset\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom torch.optim import lr_scheduler\nfrom torch.utils.data.sampler import SubsetRandomSampler, RandomSampler, SequentialSampler\nfrom torch.optim.lr_scheduler import StepLR, ReduceLROnPlateau, CosineAnnealingLR\nimport torchvision\nimport torchvision.transforms as transforms\nimport albumentations\n\nfrom sklearn.model_selection import GroupKFold, train_test_split, StratifiedKFold\nfrom sklearn.metrics import roc_auc_score, cohen_kappa_score, confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:04.209849Z","iopub.execute_input":"2022-10-16T08:20:04.21018Z","iopub.status.idle":"2022-10-16T08:20:04.216961Z","shell.execute_reply.started":"2022-10-16T08:20:04.21015Z","shell.execute_reply":"2022-10-16T08:20:04.215575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir='../input/rsna-2022-cervical-spine-fracture-detection/test_images'\n","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:04.218357Z","iopub.execute_input":"2022-10-16T08:20:04.219015Z","iopub.status.idle":"2022-10-16T08:20:04.23004Z","shell.execute_reply.started":"2022-10-16T08:20:04.218974Z","shell.execute_reply":"2022-10-16T08:20:04.229022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nimport numpy as np\ndef load_dicom_3d(patient_id,num_imgs=96,img_size=384):\n    files=sorted(glob.glob(f\"{data_dir}/{patient_id}/*.dcm\"))\n    middle=len(files)//2\n    num_imgs2=num_imgs//2\n    p1=max(0,middle-num_imgs2)\n    p2=min(len(files),middle+num_imgs2)\n    img3d=np.stack([load_dicom(f) for f in files[p1:p2]]).T\n    if img3d.shape[-1]<num_imgs:\n        n_zero=np.zeros((img_size,img_size,num_imgs-img3d.shape[-1]))\n        img3d=np.concatenate((img3d,n_zero),axis=-1)\n    \n    if np.min(img3d)<np.max(img3d):\n        img3d=img3d-np.min(img3d)\n        img3d=img3d/np.max(img3d)\n        \n    return np.expand_dims(img3d,0)","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:04.231633Z","iopub.execute_input":"2022-10-16T08:20:04.232023Z","iopub.status.idle":"2022-10-16T08:20:04.242182Z","shell.execute_reply.started":"2022-10-16T08:20:04.231988Z","shell.execute_reply":"2022-10-16T08:20:04.241222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=load_dicom_3d(\"1.2.826.0.1.3680043.22327\")\ndata.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:04.243597Z","iopub.execute_input":"2022-10-16T08:20:04.244134Z","iopub.status.idle":"2022-10-16T08:20:04.652618Z","shell.execute_reply.started":"2022-10-16T08:20:04.244092Z","shell.execute_reply":"2022-10-16T08:20:04.651637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from einops import rearrange, repeat\nfrom einops.layers.torch import Rearrange\n\n# helpers\n\ndef pair(t):\n    return t if isinstance(t, tuple) else (t, t)\n\n# classes\n\nclass PreNorm(nn.Module):\n    def __init__(self, dim, fn):\n        super().__init__()\n        self.norm = nn.LayerNorm(dim)\n        self.fn = fn\n    def forward(self, x, **kwargs):\n        return self.fn(self.norm(x), **kwargs)\n\nclass FeedForward(nn.Module):\n    def __init__(self, dim, hidden_dim, dropout = 0.):\n        super().__init__()\n        self.net = nn.Sequential(\n            nn.Linear(dim, hidden_dim),\n            nn.GELU(),\n            nn.Dropout(dropout),\n            nn.Linear(hidden_dim, dim),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return self.net(x)\n\nclass Attention(nn.Module):\n    def __init__(self, dim, heads = 8, dim_head = 64, dropout = 0.):\n        super().__init__()\n        inner_dim = dim_head *  heads\n        project_out = not (heads == 1 and dim_head == dim)\n\n        self.heads = heads\n        self.scale = dim_head ** -0.5\n\n        self.attend = nn.Softmax(dim = -1)\n        self.to_qkv = nn.Linear(dim, inner_dim * 3, bias = False)\n\n        self.to_out = nn.Sequential(\n            nn.Linear(inner_dim, dim),\n            nn.Dropout(dropout)\n        ) if project_out else nn.Identity()\n\n    def forward(self, x):\n        qkv = self.to_qkv(x).chunk(3, dim = -1)\n        q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = self.heads), qkv)\n\n        dots = torch.matmul(q, k.transpose(-1, -2)) * self.scale\n\n        attn = self.attend(dots)\n\n        out = torch.matmul(attn, v)\n        out = rearrange(out, 'b h n d -> b n (h d)')\n        return self.to_out(out)\n\nclass Transformer(nn.Module):\n    def __init__(self, dim, depth, heads, dim_head, mlp_dim, dropout = 0.):\n        super().__init__()\n        self.layers = nn.ModuleList([])\n        mlp_dim = 2048\n        for _ in range(depth):\n            #print (dim, mlp_dim)\n            self.layers.append(nn.ModuleList([\n                PreNorm(dim, Attention(dim, heads = heads, dim_head = dim_head, dropout = dropout)),\n                PreNorm(dim, FeedForward(dim, mlp_dim, dropout = dropout))\n            ]))\n    def forward(self, x):\n        for attn, ff in self.layers:\n            x = attn(x) + x\n            x = ff(x) + x\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:04.659486Z","iopub.execute_input":"2022-10-16T08:20:04.659851Z","iopub.status.idle":"2022-10-16T08:20:04.677257Z","shell.execute_reply.started":"2022-10-16T08:20:04.659821Z","shell.execute_reply":"2022-10-16T08:20:04.675632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Model(nn.Module):\n    def __init__(self, *, image_size, patch_size, num_classes, dim, depth, heads, mlp_dim, channels = 3, dropout = 0., emb_dropout = 0.):\n        super().__init__()\n        assert image_size % patch_size == 0, 'image dimensions must be divisible by the patch size'\n        num_patches = (image_size // patch_size) *(image_size // patch_size)* 2\n        patch_dim = channels * patch_size ** 3\n\n        self.patch_size = patch_size\n\n        self.pos_embedding = nn.Parameter(torch.randn(1, num_patches + 1, dim))\n        self.patch_to_embedding = nn.Linear(patch_dim, dim)\n        self.cls_token = nn.Parameter(torch.randn(1, 1, dim))\n        self.dropout = nn.Dropout(emb_dropout)\n        #print (mlp_dim)\n        self.transformer = Transformer(dim, depth, heads, mlp_dim, dropout)\n        #print (dim)\n        self.to_cls_token = nn.Identity()\n\n        self.mlp_head = nn.Sequential(\n            nn.LayerNorm(dim),\n            nn.Linear(dim, mlp_dim),\n            nn.GELU(),\n            nn.Dropout(dropout),\n            nn.Linear(mlp_dim, num_classes),\n            nn.Dropout(dropout),\n            # add the line for sigmoid layer \n        )\n\n    def forward(self, img, mask = None):\n        p = self.patch_size\n        #print (img.shape)\n        x = rearrange(img, 'b c (h p1) (w p2) (d p3) -> b (h w d) (p1 p2 p3 c)', p1 = p, p2 = p, p3 = p)\n        #print (x.shape)\n        x = self.patch_to_embedding(x)\n        #print (x.shape)\n        cls_tokens = self.cls_token.expand(img.shape[0], -1, -1)\n        #print (cls_tokens.shape)\n        x = torch.cat((cls_tokens, x), dim=1)\n        #print (x.shape)\n        #print (self.pos_embedding.shape)\n        x += self.pos_embedding\n        x = self.dropout(x)\n\n        x = self.transformer(x)\n\n        x = self.to_cls_token(x[:, 0])\n        return self.mlp_head(x)","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:04.678905Z","iopub.execute_input":"2022-10-16T08:20:04.679632Z","iopub.status.idle":"2022-10-16T08:20:04.693025Z","shell.execute_reply.started":"2022-10-16T08:20:04.679589Z","shell.execute_reply":"2022-10-16T08:20:04.692046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')\n","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:04.694793Z","iopub.execute_input":"2022-10-16T08:20:04.695165Z","iopub.status.idle":"2022-10-16T08:20:04.707026Z","shell.execute_reply.started":"2022-10-16T08:20:04.695129Z","shell.execute_reply":"2022-10-16T08:20:04.706055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_model(model_file):\n    model = Model(\n        image_size = 384,\n        patch_size = 48,\n        num_classes = 8,\n        dim = 768,\n        depth = 2,\n        heads = 16,\n        mlp_dim = 1536,\n        channels = 1,\n        dropout = 0.1,\n        emb_dropout = 0.1\n    )\n    model.to(device)\n    try:\n        model.load_state_dict(torch.load(model_file),strict=True)\n    except:\n        state_dict=torch.load(model_file)\n        state_dict = {k[7:] if k.startswith('module.') else k: state_dict[k] for k in state_dict.keys()}\n        model.load_state_dict(state_dict, strict=True)\n    model.eval()\n    return model \ngc.collect()\nmodels=[load_model(model) for model in model_files]\nlen(models)","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:04.708259Z","iopub.execute_input":"2022-10-16T08:20:04.708656Z","iopub.status.idle":"2022-10-16T08:20:16.554887Z","shell.execute_reply.started":"2022-10-16T08:20:04.708616Z","shell.execute_reply":"2022-10-16T08:20:16.55373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import TensorDataset, DataLoader, Dataset\nnatsort = lambda s: [int(t) if t.isdigit() else t.lower() for t in re.split('(\\d+)', s)]\n\nclass RSNADataset(Dataset):\n    \n    def __init__(self, csv, mode, transform=None):\n        self.csv = csv\n        self.mode = mode\n        self.transform = transform\n        \n    def __len__(self):\n        return self.csv.shape[0]\n        \n    def __getitem__(self, index):\n        # Set Random Seed\n\n        dt = self.csv.iloc[index, :]\n        study_paths = glob.glob(f\"test_DICOM/{dt.StudyInstanceUID}/*\")\n        study_paths.sort(key=natsort)\n        \n        # Load images\n        stacked_image=load_dicom_3d(dt.StudyInstanceUID)\n        #print(\"need to sqz shape\",stacked_image.shape)\n        \n        if self.mode==\"test\":\n            return torch.tensor(stacked_image).float()\n        else:\n            targets = torch.tensor(dt[target_cols]).float()\n            return {\"X\": torch.tensor(stacked_image).float(),\"y\":targets}","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:16.556556Z","iopub.execute_input":"2022-10-16T08:20:16.556936Z","iopub.status.idle":"2022-10-16T08:20:16.57261Z","shell.execute_reply.started":"2022-10-16T08:20:16.556897Z","shell.execute_reply":"2022-10-16T08:20:16.571583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()\n","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:16.578703Z","iopub.execute_input":"2022-10-16T08:20:16.580607Z","iopub.status.idle":"2022-10-16T08:20:16.758986Z","shell.execute_reply.started":"2022-10-16T08:20:16.580541Z","shell.execute_reply":"2022-10-16T08:20:16.756951Z"},"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-16T08:20:16.76065Z","iopub.execute_input":"2022-10-16T08:20:16.761344Z","iopub.status.idle":"2022-10-16T08:20:16.768732Z","shell.execute_reply.started":"2022-10-16T08:20:16.761306Z","shell.execute_reply":"2022-10-16T08:20:16.767718Z"},"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-16T08:20:16.770118Z","iopub.execute_input":"2022-10-16T08:20:16.770643Z","iopub.status.idle":"2022-10-16T08:20:16.798493Z","shell.execute_reply.started":"2022-10-16T08:20:16.770591Z","shell.execute_reply":"2022-10-16T08:20:16.797642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_dataset=RSNADataset(csv=test_df,mode='test')\nvalidloader = DataLoader(valid_dataset, batch_size=8)","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:16.799862Z","iopub.execute_input":"2022-10-16T08:20:16.800265Z","iopub.status.idle":"2022-10-16T08:20:16.806632Z","shell.execute_reply.started":"2022-10-16T08:20:16.800225Z","shell.execute_reply":"2022-10-16T08:20:16.805358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def valid():\n    preds= []\n    with torch.no_grad():\n        for data in tqdm(validloader):\n            data = data.to(device)\n            for model in models:\n                l = model(data)\n                sig = nn.Sigmoid()\n                output= sig(l)\n                preds.append(output)\n        return torch.cat(preds).cpu().numpy().squeeze()","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:16.808362Z","iopub.execute_input":"2022-10-16T08:20:16.809232Z","iopub.status.idle":"2022-10-16T08:20:16.816376Z","shell.execute_reply.started":"2022-10-16T08:20:16.809195Z","shell.execute_reply":"2022-10-16T08:20:16.815741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.DataFrame(data=valid(),columns=[f'C{i+1}' for i in range(0,8)])\n","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:16.81765Z","iopub.execute_input":"2022-10-16T08:20:16.818455Z","iopub.status.idle":"2022-10-16T08:20:20.611483Z","shell.execute_reply.started":"2022-10-16T08:20:16.818419Z","shell.execute_reply":"2022-10-16T08:20:20.610333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test_pred = pd.concat([df,test_df], axis=1).sort_values(['StudyInstanceUID'])\ndf_test_pred.rename(columns = {'C8':'patient_overall'}, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:20.613031Z","iopub.execute_input":"2022-10-16T08:20:20.613788Z","iopub.status.idle":"2022-10-16T08:20:20.624557Z","shell.execute_reply.started":"2022-10-16T08:20:20.613744Z","shell.execute_reply":"2022-10-16T08:20:20.623612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test_pred","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:20.626522Z","iopub.execute_input":"2022-10-16T08:20:20.627582Z","iopub.status.idle":"2022-10-16T08:20:20.661942Z","shell.execute_reply.started":"2022-10-16T08:20:20.627543Z","shell.execute_reply":"2022-10-16T08:20:20.661019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_type_original= df_test_pred['prediction_type'].tolist()\nrow_id=df_test_pred['row_id'].tolist()","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:20.666512Z","iopub.execute_input":"2022-10-16T08:20:20.669054Z","iopub.status.idle":"2022-10-16T08:20:20.676354Z","shell.execute_reply.started":"2022-10-16T08:20:20.66899Z","shell.execute_reply":"2022-10-16T08:20:20.67501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions=[]\nfor j in range(len(pred_type_original)):\n    for i in pred_type_original:\n        if i in df_test_pred.columns:\n            predictions.append(df_test_pred[i].tolist())\n    break\npredictions\n\noriginal_preds=[predictions[i][i] for i in range(len(predictions))]","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:20.681819Z","iopub.execute_input":"2022-10-16T08:20:20.684454Z","iopub.status.idle":"2022-10-16T08:20:20.694113Z","shell.execute_reply.started":"2022-10-16T08:20:20.684413Z","shell.execute_reply":"2022-10-16T08:20:20.692906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"original_pred = [item * 100 for item in original_preds]","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:20.699606Z","iopub.execute_input":"2022-10-16T08:20:20.702394Z","iopub.status.idle":"2022-10-16T08:20:20.709386Z","shell.execute_reply.started":"2022-10-16T08:20:20.702356Z","shell.execute_reply":"2022-10-16T08:20:20.708045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"original_pred","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:20.713298Z","iopub.execute_input":"2022-10-16T08:20:20.714014Z","iopub.status.idle":"2022-10-16T08:20:20.72625Z","shell.execute_reply.started":"2022-10-16T08:20:20.713968Z","shell.execute_reply":"2022-10-16T08:20:20.724892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eff_preds=[]\nfor i in range(len(effnet)):\n    eff_preds.append(effnet['fractured'][i])\neff_preds","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:20.727963Z","iopub.execute_input":"2022-10-16T08:20:20.728981Z","iopub.status.idle":"2022-10-16T08:20:20.742974Z","shell.execute_reply.started":"2022-10-16T08:20:20.728933Z","shell.execute_reply":"2022-10-16T08:20:20.741901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_lt = [ original_pred[x] + eff_preds[x] for x in range (len (eff_preds))]  ","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:20.744751Z","iopub.execute_input":"2022-10-16T08:20:20.745741Z","iopub.status.idle":"2022-10-16T08:20:20.754629Z","shell.execute_reply.started":"2022-10-16T08:20:20.745693Z","shell.execute_reply":"2022-10-16T08:20:20.753598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_lt","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:20:20.756632Z","iopub.execute_input":"2022-10-16T08:20:20.757403Z","iopub.status.idle":"2022-10-16T08:20:20.766818Z","shell.execute_reply.started":"2022-10-16T08:20:20.757367Z","shell.execute_reply":"2022-10-16T08:20:20.765554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(list(zip(row_id, res_lt)),\n               columns =['row_id', 'fractured'])","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:21:05.32791Z","iopub.execute_input":"2022-10-16T08:21:05.328365Z","iopub.status.idle":"2022-10-16T08:21:05.334984Z","shell.execute_reply.started":"2022-10-16T08:21:05.328312Z","shell.execute_reply":"2022-10-16T08:21:05.333741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:21:07.771062Z","iopub.execute_input":"2022-10-16T08:21:07.772155Z","iopub.status.idle":"2022-10-16T08:21:07.783326Z","shell.execute_reply.started":"2022-10-16T08:21:07.772107Z","shell.execute_reply":"2022-10-16T08:21:07.782325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-16T08:21:48.191221Z","iopub.execute_input":"2022-10-16T08:21:48.191602Z","iopub.status.idle":"2022-10-16T08:21:48.201964Z","shell.execute_reply.started":"2022-10-16T08:21:48.19157Z","shell.execute_reply":"2022-10-16T08:21:48.200962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-danger\" style=\"text-align:center; font-size:20px;\">\n    ❤️ Dont forget to ▲upvote▲ if you find this notebook usefull!  ❤️\n</div>","metadata":{"execution":{"iopub.execute_input":"2022-08-20T13:17:18.762083Z","iopub.status.busy":"2022-08-20T13:17:18.761536Z","iopub.status.idle":"2022-08-20T13:17:18.76993Z","shell.execute_reply":"2022-08-20T13:17:18.768312Z","shell.execute_reply.started":"2022-08-20T13:17:18.762038Z"},"papermill":{"duration":0.012405,"end_time":"2022-08-29T06:36:56.655243","exception":false,"start_time":"2022-08-29T06:36:56.642838","status":"completed"},"pycharm":{"name":"#%% md\n"},"tags":[]}}]}