{"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\nfrom sklearn import svm\nfrom sklearn.svm import SVC\nfrom sklearn.preprocessing import StandardScaler\nprint(\"imports done\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-30T05:27:30.305092Z","iopub.execute_input":"2022-12-30T05:27:30.305512Z","iopub.status.idle":"2022-12-30T05:27:36.72489Z","shell.execute_reply.started":"2022-12-30T05:27:30.305474Z","shell.execute_reply":"2022-12-30T05:27:36.723498Z"},"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-30T05:27:36.727304Z","iopub.execute_input":"2022-12-30T05:27:36.728296Z","iopub.status.idle":"2022-12-30T05:27:36.751141Z","shell.execute_reply.started":"2022-12-30T05:27:36.728255Z","shell.execute_reply":"2022-12-30T05:27:36.750056Z"},"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-30T05:27:36.75272Z","iopub.execute_input":"2022-12-30T05:27:36.753276Z","iopub.status.idle":"2022-12-30T05:27:36.80278Z","shell.execute_reply.started":"2022-12-30T05:27:36.753243Z","shell.execute_reply":"2022-12-30T05:27:36.801626Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2022-12-30T05:27:36.805434Z","iopub.execute_input":"2022-12-30T05:27:36.806229Z","iopub.status.idle":"2022-12-30T05:27:54.675047Z","shell.execute_reply.started":"2022-12-30T05:27:36.806193Z","shell.execute_reply":"2022-12-30T05:27:54.673914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model=keras.models.load_model('../input/cnn-model78')\nprint(cnn_model.summary())\n\nimport pickle\n\n# load model\nknn_model = pickle.load(open(\"../input/finalsvm/svm_model.pickle\", \"rb\"))","metadata":{"execution":{"iopub.status.busy":"2022-12-30T05:27:54.67666Z","iopub.execute_input":"2022-12-30T05:27:54.677774Z","iopub.status.idle":"2022-12-30T05:27:56.72584Z","shell.execute_reply.started":"2022-12-30T05:27:54.677736Z","shell.execute_reply":"2022-12-30T05:27:56.724689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pop all layers of the cnn except flatten\ncnn_model.pop()\ncnn_model.pop()\nprint(cnn_model.summary())","metadata":{"execution":{"iopub.status.busy":"2022-12-30T05:27:56.727397Z","iopub.execute_input":"2022-12-30T05:27:56.727937Z","iopub.status.idle":"2022-12-30T05:27:56.737954Z","shell.execute_reply.started":"2022-12-30T05:27:56.727893Z","shell.execute_reply":"2022-12-30T05:27:56.736667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features=cnn_model(test1)\nfrom sklearn.preprocessing import StandardScaler\nscaler = StandardScaler().fit(features)\nfeatures = scaler.transform(features)\nprint(features.shape)","metadata":{"execution":{"iopub.status.busy":"2022-12-30T05:27:56.739949Z","iopub.execute_input":"2022-12-30T05:27:56.740428Z","iopub.status.idle":"2022-12-30T05:27:57.132465Z","shell.execute_reply.started":"2022-12-30T05:27:56.740342Z","shell.execute_reply":"2022-12-30T05:27:57.131264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = knn_model.predict(features)","metadata":{"execution":{"iopub.status.busy":"2022-12-30T05:27:57.133699Z","iopub.execute_input":"2022-12-30T05:27:57.134032Z","iopub.status.idle":"2022-12-30T05:27:57.139893Z","shell.execute_reply.started":"2022-12-30T05:27:57.134002Z","shell.execute_reply":"2022-12-30T05:27:57.13879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame(test_df[\"patient_id\"].copy())\nsub[\"CE\"] = y_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-30T05:27:57.141215Z","iopub.execute_input":"2022-12-30T05:27:57.141538Z","iopub.status.idle":"2022-12-30T05:27:57.17166Z","shell.execute_reply.started":"2022-12-30T05:27:57.141509Z","shell.execute_reply":"2022-12-30T05:27:57.170446Z"},"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-30T05:27:57.17451Z","iopub.execute_input":"2022-12-30T05:27:57.174867Z","iopub.status.idle":"2022-12-30T05:27:58.225316Z","shell.execute_reply.started":"2022-12-30T05:27:57.174835Z","shell.execute_reply":"2022-12-30T05:27:58.224284Z"},"trusted":true},"execution_count":null,"outputs":[]}]}