{"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 tensorflow as tf \nimport rasterio\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np\nimport cv2\nimport tifffile\nimport pandas as pd\nimport os, re, math\nimport PIL\nimport shutil\nfrom tqdm.notebook import tqdm\nimport gc\nimport sys\n\nprint(\"ALL MODULES IMPORTED\")","metadata":{"execution":{"iopub.status.busy":"2022-09-24T06:39:43.493174Z","iopub.execute_input":"2022-09-24T06:39:43.493626Z","iopub.status.idle":"2022-09-24T06:39:49.404054Z","shell.execute_reply.started":"2022-09-24T06:39:43.493535Z","shell.execute_reply":"2022-09-24T06:39:49.402669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = \"../input/mayo-clinic-strip-ai\"\nTEST_DIR = \"../input/mayo-clinic-strip-ai/test.csv\"","metadata":{"execution":{"iopub.status.busy":"2022-09-24T06:39:49.405905Z","iopub.execute_input":"2022-09-24T06:39:49.406482Z","iopub.status.idle":"2022-09-24T06:39:49.411588Z","shell.execute_reply.started":"2022-09-24T06:39:49.40645Z","shell.execute_reply":"2022-09-24T06:39:49.410404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    os.mkdir(\"./test_jpg\")\nexcept:\n    pass","metadata":{"execution":{"iopub.status.busy":"2022-09-24T06:39:49.412947Z","iopub.execute_input":"2022-09-24T06:39:49.413296Z","iopub.status.idle":"2022-09-24T06:39:49.423948Z","shell.execute_reply.started":"2022-09-24T06:39:49.413264Z","shell.execute_reply":"2022-09-24T06:39:49.422727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(TEST_DIR)\ntest_df","metadata":{"execution":{"iopub.status.busy":"2022-09-24T06:39:49.42658Z","iopub.execute_input":"2022-09-24T06:39:49.426923Z","iopub.status.idle":"2022-09-24T06:39:49.460598Z","shell.execute_reply.started":"2022-09-24T06:39:49.426892Z","shell.execute_reply":"2022-09-24T06:39:49.459478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_path(id):\n    return f\"../input/mayo-clinic-strip-ai/test/{id}.tif\"\n\ndef get_jpg_path(id):\n    # ./test_jpg/008e5c_0.jpg\n    return f\"./test_jpg/{id}.jpg\"    ","metadata":{"execution":{"iopub.status.busy":"2022-09-24T06:39:49.462175Z","iopub.execute_input":"2022-09-24T06:39:49.462645Z","iopub.status.idle":"2022-09-24T06:39:49.467988Z","shell.execute_reply.started":"2022-09-24T06:39:49.462614Z","shell.execute_reply":"2022-09-24T06:39:49.466734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[\"image_path\"] = test_df[\"image_id\"].apply(get_path)\ntest_df[\"image_path_jpg\"] = test_df[\"image_id\"].apply(get_jpg_path)\ntest_df","metadata":{"execution":{"iopub.status.busy":"2022-09-24T06:39:49.4697Z","iopub.execute_input":"2022-09-24T06:39:49.470065Z","iopub.status.idle":"2022-09-24T06:39:49.495064Z","shell.execute_reply.started":"2022-09-24T06:39:49.470035Z","shell.execute_reply":"2022-09-24T06:39:49.493891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.load_model(\"../input/stripai-models/pEFFENET2.h5\")\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-24T06:39:49.496519Z","iopub.execute_input":"2022-09-24T06:39:49.496939Z","iopub.status.idle":"2022-09-24T06:40:05.514Z","shell.execute_reply.started":"2022-09-24T06:39:49.496907Z","shell.execute_reply":"2022-09-24T06:40:05.512705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(range(test_df.shape[0])):\n    img_id = test_df.iloc[i].image_id\n    sz = os.path.getsize(test_df.iloc[i].image_path)\n    if(sz >= 1000000000):\n        ig = np.zeros((480, 480, 3), np.uint8)\n    else:\n         ig = cv2.resize(tifffile.imread(test_df.iloc[i].image_path), (480, 480))\n    cv2.imwrite(f\"./test_jpg/{img_id}.jpg\", ig)\n    del ig\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-09-24T06:40:05.516018Z","iopub.execute_input":"2022-09-24T06:40:05.516899Z","iopub.status.idle":"2022-09-24T06:40:46.387337Z","shell.execute_reply.started":"2022-09-24T06:40:05.51685Z","shell.execute_reply":"2022-09-24T06:40:46.386454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# MAKE A FUNCTION\ndef get_input(path):\n    try:\n        img = cv2.imread(path)\n        img = cv2.resize(img, (480, 480))\n    except:\n        img = np.ones((480, 480, 3), np.uint8)\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-09-24T06:40:46.388481Z","iopub.execute_input":"2022-09-24T06:40:46.389113Z","iopub.status.idle":"2022-09-24T06:40:46.395466Z","shell.execute_reply.started":"2022-09-24T06:40:46.389078Z","shell.execute_reply":"2022-09-24T06:40:46.394232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_paths = test_df.image_path_jpg.values\ntest_image_paths","metadata":{"execution":{"iopub.status.busy":"2022-09-24T06:40:46.399305Z","iopub.execute_input":"2022-09-24T06:40:46.399691Z","iopub.status.idle":"2022-09-24T06:40:46.413575Z","shell.execute_reply.started":"2022-09-24T06:40:46.399658Z","shell.execute_reply":"2022-09-24T06:40:46.41226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_paths = test_df.image_path_jpg.values\n\npreds = []\nfor image_path in test_image_paths:\n    preds.append(model(tf.expand_dims(get_input(image_path), axis=0)).numpy())\n    tf.keras.backend.clear_session(); gc.collect(); gc.collect();\npreds = np.concatenate(preds, axis=0)\nprint(preds.shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T06:40:46.416894Z","iopub.execute_input":"2022-09-24T06:40:46.417902Z","iopub.status.idle":"2022-09-24T06:40:52.740174Z","shell.execute_reply.started":"2022-09-24T06:40:46.417866Z","shell.execute_reply":"2022-09-24T06:40:52.738546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"S2I_LBL_MAP = {\"CE\":0, \"LAA\":1}\nI2S_LBL_MAP = {v:k for k,v in S2I_LBL_MAP.items()}","metadata":{"execution":{"iopub.status.busy":"2022-09-24T06:40:52.741689Z","iopub.execute_input":"2022-09-24T06:40:52.742165Z","iopub.status.idle":"2022-09-24T06:40:52.749054Z","shell.execute_reply.started":"2022-09-24T06:40:52.742118Z","shell.execute_reply":"2022-09-24T06:40:52.747704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss_df = pd.read_csv(\"../input/mayo-clinic-strip-ai/sample_submission.csv\")\nss_df","metadata":{"execution":{"iopub.status.busy":"2022-09-24T06:40:52.750739Z","iopub.execute_input":"2022-09-24T06:40:52.751246Z","iopub.status.idle":"2022-09-24T06:40:52.776232Z","shell.execute_reply.started":"2022-09-24T06:40:52.751199Z","shell.execute_reply":"2022-09-24T06:40:52.77524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for lbl, idx in S2I_LBL_MAP.items():\n    test_df[lbl]=preds[:, idx]\n\nCE_test_prob_map = test_df.groupby(\"patient_id\")[\"CE\"].mean().to_dict()\nLAA_test_prob_map = test_df.groupby(\"patient_id\")[\"LAA\"].mean().to_dict()\n\nss_df[\"CE\"] = ss_df[\"patient_id\"].map(lambda x: CE_test_prob_map.get(x, 0.5))\nss_df[\"LAA\"] = ss_df[\"patient_id\"].map(lambda x: LAA_test_prob_map.get(x, 0.5))\nss_df.to_csv(\"submission.csv\", index=False)\n\ndisplay(ss_df)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T06:40:52.777382Z","iopub.execute_input":"2022-09-24T06:40:52.777701Z","iopub.status.idle":"2022-09-24T06:40:52.802654Z","shell.execute_reply.started":"2022-09-24T06:40:52.777663Z","shell.execute_reply":"2022-09-24T06:40:52.801619Z"},"trusted":true},"execution_count":null,"outputs":[]}]}