{"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"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":52254,"databundleVersionId":6307054,"sourceType":"competition"},{"sourceId":144847122,"sourceType":"kernelVersion"}],"dockerImageVersionId":30553,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np, pandas as pd, tensorflow as tf, os, pydicom as dicom, cv2\n\nTEST_IMG_PATH = '/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images'\nIMG_SIZE = [256, 256]\nBATCH_SIZE = 16\nORIGINAL_COLS = [\n        \"bowel_healthy\", \"bowel_injury\", \"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    ]","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-02T07:30:49.780279Z","iopub.execute_input":"2023-10-02T07:30:49.780754Z","iopub.status.idle":"2023-10-02T07:30:49.787799Z","shell.execute_reply.started":"2023-10-02T07:30:49.780716Z","shell.execute_reply":"2023-10-02T07:30:49.786148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.load_model('/kaggle/input/rsna-train/rsna-atd.keras')\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-02T07:30:49.789769Z","iopub.execute_input":"2023-10-02T07:30:49.790703Z","iopub.status.idle":"2023-10-02T07:30:54.156163Z","shell.execute_reply.started":"2023-10-02T07:30:49.790661Z","shell.execute_reply":"2023-10-02T07:30:54.154827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def post_proc(pred):\n    proc_pred = np.empty((len(pred[0]), 2*2 + 3*3), dtype=\"float32\")\n\n    # bowel, extravasation\n    proc_pred[:,0] = np.transpose(np.array(pred[:][0]))\n    proc_pred[:, 1] = 1 - proc_pred[:, 0]\n    proc_pred[:, 2] = np.transpose(np.array(pred[:][1]))\n    proc_pred[:, 3] = 1 - proc_pred[:, 2]\n    \n    # liver, kidney, sneel\n    proc_pred[:, 4:7] = pred[:][2]\n    proc_pred[:, 7:10] = pred[:][3]\n    proc_pred[:, 10:13] = pred[:][4]\n\n    return proc_pred\n\ntest_ims = []\npatient_ids = os.listdir(TEST_IMG_PATH)\nfor patient_id in patient_ids:\n    directory = os.listdir(os.path.join(TEST_IMG_PATH, patient_id))[0] #taking only the first one, dont know what the second does yet\n    file = os.listdir(os.path.join(TEST_IMG_PATH, patient_id, directory))[0]\n    file_path = os.path.join(TEST_IMG_PATH, patient_id, directory, file)\n    #print(file_path)\n    #print('/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images/63706/39279/30.dcm')\n    ds = dicom.dcmread(file_path)\n    res = cv2.resize(ds.pixel_array, dsize=IMG_SIZE, interpolation=cv2.INTER_CUBIC)\n    res = np.stack([res]*3,axis=-1)\n    test_ims.append([res])\ntest_dataset = tf.data.Dataset.from_tensor_slices((test_ims))\ntest_dataset.batch(BATCH_SIZE)\n#predictions = model.predict(test_dataset)\npredictions = pd.DataFrame(post_proc(model.predict(test_dataset)))\npredictions.columns = ORIGINAL_COLS\n#predictions.set_index([pd.Index(patient_ids), 'patient_id'])\n# Set the named indices as the index of the DataFrame\n#predictions = predictions.rename_axis(index={i: name for i, name in enumerate(patient_ids)})\npredictions['patient_id'] = patient_ids\n#predictions.set_index('patient_id')\n# Align with sample submission\nsub_df = pd.read_csv(f\"/kaggle/input/rsna-2023-abdominal-trauma-detection/sample_submission.csv\")\nsub_df = sub_df[[\"patient_id\"]]\nsub_df['patient_id'] = sub_df['patient_id'].astype(np.int64)\npredictions['patient_id'] = predictions['patient_id'].astype(np.int64)\nsub_df = sub_df.merge(predictions, on=\"patient_id\", how=\"left\")\ndisplay(sub_df)\n    \n# Store submission\nsub_df.to_csv(\"submission.csv\",index=False)\nsub_df.head(2)","metadata":{"execution":{"iopub.status.busy":"2023-10-02T07:30:54.15812Z","iopub.execute_input":"2023-10-02T07:30:54.158487Z","iopub.status.idle":"2023-10-02T07:30:55.755577Z","shell.execute_reply.started":"2023-10-02T07:30:54.158457Z","shell.execute_reply":"2023-10-02T07:30:55.754391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Credits \n\nhttps://www.kaggle.com/code/metric/rsna-trauma-metric/notebook\n\nhttps://keras.io/examples/keras_recipes/tfrecord/\n\nhttps://www.kaggle.com/code/amyjang/tensorflow-pneumonia-classification-on-x-rays\n\nhttps://www.kaggle.com/code/aritrag/kerascv-starter-notebook-infer","metadata":{}}]}