{"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 pandas as pd\nimport numpy as np\nimport gc\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport warnings\n#warnings.filterwarnings('ignore')\nfrom sklearn.model_selection import KFold\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.model_selection import train_test_split\nfrom tqdm import tqdm\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense\nfrom tensorflow.keras.layers import GlobalMaxPooling2D\nimport openslide\nfrom openslide import OpenSlide\nfrom openslide import ImageSlide\nimport cv2 \nfrom PIL import Image\nprint(\"imports done\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-30T02:37:41.615561Z","iopub.execute_input":"2022-12-30T02:37:41.616183Z","iopub.status.idle":"2022-12-30T02:37:49.194599Z","shell.execute_reply.started":"2022-12-30T02:37:41.616057Z","shell.execute_reply":"2022-12-30T02:37:49.193416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/mayo-clinic-strip-ai/train.csv')\ntest_df  = pd.read_csv('../input/mayo-clinic-strip-ai/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-12-30T02:37:49.196765Z","iopub.execute_input":"2022-12-30T02:37:49.197403Z","iopub.status.idle":"2022-12-30T02:37:49.223476Z","shell.execute_reply.started":"2022-12-30T02:37:49.19736Z","shell.execute_reply":"2022-12-30T02:37:49.222459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"file_path\"] = train_df[\"image_id\"].apply(lambda x: \"../input/mayo-clinic-strip-ai/train/\" + x + \".tif\")\ntest_df[\"file_path\"]  = test_df[\"image_id\"].apply(lambda x: \"../input/mayo-clinic-strip-ai/test/\" + x + \".tif\")\ntrain_df[\"target\"] = train_df[\"label\"].apply(lambda x : 1 if x==\"CE\" else 0)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-30T02:37:49.225098Z","iopub.execute_input":"2022-12-30T02:37:49.226147Z","iopub.status.idle":"2022-12-30T02:37:49.256847Z","shell.execute_reply.started":"2022-12-30T02:37:49.226106Z","shell.execute_reply":"2022-12-30T02:37:49.25591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=keras.models.load_model('../input/newcnnmodel')\nprint(model.summary())","metadata":{"execution":{"iopub.status.busy":"2022-12-30T02:37:49.258922Z","iopub.execute_input":"2022-12-30T02:37:49.259525Z","iopub.status.idle":"2022-12-30T02:37:51.727006Z","shell.execute_reply.started":"2022-12-30T02:37:49.259488Z","shell.execute_reply":"2022-12-30T02:37:51.725621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess(image_path):\n    slide=OpenSlide(image_path)\n    region= (1000,1000)    \n    size  = (5000,5000)\n    image = slide.read_region(region, 0, size)\n    image = tf.image.resize(image, (512, 512))\n    image = np.array(image)    \n    return image\n\ntest1=[]\nfor i in test_df['file_path']:\n    x1=preprocess(i)\n    test1.append(x1)\n    \ntest1=np.array(test1)\n\ncnn_pred=model.predict(test1)\nprint(test1)","metadata":{"execution":{"iopub.status.busy":"2022-12-30T02:37:51.72866Z","iopub.execute_input":"2022-12-30T02:37:51.729022Z","iopub.status.idle":"2022-12-30T02:38:12.142514Z","shell.execute_reply.started":"2022-12-30T02:37:51.728987Z","shell.execute_reply":"2022-12-30T02:38:12.141222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(cnn_pred)","metadata":{"execution":{"iopub.status.busy":"2022-12-30T02:38:12.14405Z","iopub.execute_input":"2022-12-30T02:38:12.144427Z","iopub.status.idle":"2022-12-30T02:38:12.150359Z","shell.execute_reply.started":"2022-12-30T02:38:12.144393Z","shell.execute_reply":"2022-12-30T02:38:12.149268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame(test_df[\"patient_id\"].copy())\nsub[\"CE\"] = cnn_pred\nsub[\"CE\"] = sub[\"CE\"].apply(lambda x : 0 if x<0 else x)\nsub[\"CE\"] = sub[\"CE\"].apply(lambda x : 1 if x>1 else x)\nsub[\"LAA\"] = 1- sub[\"CE\"]\n\nsub = sub.groupby(\"patient_id\").mean()\nsub =sub[[\"CE\", \"LAA\"]].round(6).reset_index()\nsub","metadata":{"execution":{"iopub.status.busy":"2022-12-30T02:38:12.152472Z","iopub.execute_input":"2022-12-30T02:38:12.152909Z","iopub.status.idle":"2022-12-30T02:38:12.185715Z","shell.execute_reply.started":"2022-12-30T02:38:12.152868Z","shell.execute_reply":"2022-12-30T02:38:12.184421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv(\"submission.csv\", index = False)\n!head submission.csv","metadata":{"execution":{"iopub.status.busy":"2022-12-30T02:38:12.187745Z","iopub.execute_input":"2022-12-30T02:38:12.188209Z","iopub.status.idle":"2022-12-30T02:38:13.300135Z","shell.execute_reply.started":"2022-12-30T02:38:12.188165Z","shell.execute_reply":"2022-12-30T02:38:13.298754Z"},"trusted":true},"execution_count":null,"outputs":[]}]}