{"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\nfrom tensorflow import keras \nfrom keras import layers\nfrom pathlib import Path\nimport pandas as pd\nimport numpy as np\nimport gc\nimport os\nfrom tqdm.auto import tqdm\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-11-26T19:45:41.888717Z","iopub.execute_input":"2022-11-26T19:45:41.889347Z","iopub.status.idle":"2022-11-26T19:45:41.900246Z","shell.execute_reply.started":"2022-11-26T19:45:41.889296Z","shell.execute_reply":"2022-11-26T19:45:41.897387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_base_path = Path(\"/kaggle/input/model-weights\")\npretrained_base = keras.models.load_model(\"/kaggle/input/cv-course-models/cv-course-models/resnet50\")\npretrained_base.trainable = False\nmodel = keras.Sequential([\n    pretrained_base, \n    layers.BatchNormalization(),\n    layers.Dense(128, activation=\"relu\"),\n    layers.Dropout(0.33), \n    layers.Dense(128, activation=\"relu\"), \n    layers.Dropout(0.33), \n    layers.Dense(2, activation=\"sigmoid\")\n])\nmodel.load_weights(model_base_path/\"Prediction_Model.ckpt\")","metadata":{"execution":{"iopub.status.busy":"2022-11-26T19:46:25.421927Z","iopub.execute_input":"2022-11-26T19:46:25.422532Z","iopub.status.idle":"2022-11-26T19:46:36.832401Z","shell.execute_reply.started":"2022-11-26T19:46:25.422467Z","shell.execute_reply":"2022-11-26T19:46:36.831264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from timeit import default_timer as timer\nimport pathlib\nfrom typing import List, Tuple, Dict\nfrom openslide import OpenSlide, deepzoom\nfrom openslide.deepzoom import DeepZoomGenerator\n\ndef convert_to_float_V2(image):\n    return tf.expand_dims((tf.convert_to_tensor(np.asarray(image), dtype=tf.float32)/127.5)-1, axis=0)\n\ndef tiled_img_pred(source_path: pathlib.Path , \n                     model:tf.keras.Model,\n                     TILE_SIZE: int = 512,\n                     resize_to: int = 224\n                     ):\n    img = OpenSlide(source_path)\n    tile_generator = DeepZoomGenerator(img, \n                                      tile_size=TILE_SIZE, \n                                      overlap= False, \n                                      limit_bounds=False)\n    nrows, ncols = tile_generator.level_tiles[-1]\n    ZOOM_LEVEL = tile_generator.level_count - 1\n    \n    avg_pred = tf.expand_dims(tf.constant((0.,0.), dtype= tf.float32), axis=0)\n    for x in range(nrows):\n        for y in range(ncols):\n            tile_img = tile_generator.get_tile(ZOOM_LEVEL,\n                                              (x, y))\n            ver_tile_img = tile_img.convert(\"L\")\n            std, avg = np.std(ver_tile_img), np.average(ver_tile_img)\n            if avg <= 240 and std >= 16.5:\n                tile_img = tile_img.resize((resize_to, resize_to))\n                tile_img = convert_to_float_V2(tile_img)\n                pred = model.predict(tile_img, verbose=0)\n                avg_pred = np.concatenate((avg_pred, pred), axis=0)\n    return np.mean(avg_pred, axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-11-26T19:46:41.925005Z","iopub.execute_input":"2022-11-26T19:46:41.92548Z","iopub.status.idle":"2022-11-26T19:46:41.939248Z","shell.execute_reply.started":"2022-11-26T19:46:41.925442Z","shell.execute_reply":"2022-11-26T19:46:41.938079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for _,_,test_data in os.walk(\"/kaggle/input/mayo-clinic-strip-ai/test\"):\n    pass\nprint(f\"Test image dataset:{test_data}\")","metadata":{"execution":{"iopub.status.busy":"2022-11-26T19:46:44.464682Z","iopub.execute_input":"2022-11-26T19:46:44.465456Z","iopub.status.idle":"2022-11-26T19:46:44.472087Z","shell.execute_reply.started":"2022-11-26T19:46:44.465414Z","shell.execute_reply":"2022-11-26T19:46:44.470952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_directory = Path(\"/kaggle/input/mayo-clinic-strip-ai/test\")\nresults = []\nfor img_id in test_data:\n    img_path = test_directory/img_id\n    ce_prob, laa_prob = tiled_img_pred(source_path = img_path, \n                                     model= model)\n    results.append((img_id[:-4], ce_prob, laa_prob))\nresults","metadata":{"execution":{"iopub.status.busy":"2022-11-26T19:46:46.891776Z","iopub.execute_input":"2022-11-26T19:46:46.892284Z","iopub.status.idle":"2022-11-26T20:02:47.863614Z","shell.execute_reply.started":"2022-11-26T19:46:46.892241Z","shell.execute_reply":"2022-11-26T20:02:47.862574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.DataFrame(results, columns=[\"image_id\", \"CE\", \"LAA\"]).sort_values(by=\"image_id\")\nsub_df","metadata":{"execution":{"iopub.status.busy":"2022-11-26T20:07:13.020157Z","iopub.execute_input":"2022-11-26T20:07:13.020678Z","iopub.status.idle":"2022-11-26T20:07:13.043749Z","shell.execute_reply.started":"2022-11-26T20:07:13.020634Z","shell.execute_reply":"2022-11-26T20:07:13.041967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.to_csv(\"submission.csv\", index=False)\n!head submission.csv","metadata":{"execution":{"iopub.status.busy":"2022-11-26T20:10:11.133563Z","iopub.execute_input":"2022-11-26T20:10:11.134104Z","iopub.status.idle":"2022-11-26T20:10:11.512099Z","shell.execute_reply.started":"2022-11-26T20:10:11.134059Z","shell.execute_reply":"2022-11-26T20:10:11.510996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}