{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"},{"sourceId":11848,"databundleVersionId":862157,"sourceType":"competition"},{"sourceId":6954891,"sourceType":"datasetVersion","datasetId":3994600},{"sourceId":6992989,"sourceType":"datasetVersion","datasetId":4019372}],"dockerImageVersionId":30579,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-18T04:02:11.233882Z","iopub.execute_input":"2023-11-18T04:02:11.234180Z","iopub.status.idle":"2023-11-18T04:02:14.904060Z","shell.execute_reply.started":"2023-11-18T04:02:11.234156Z","shell.execute_reply":"2023-11-18T04:02:14.902573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport matplotlib.image as mpimg","metadata":{"execution":{"iopub.status.busy":"2023-11-18T04:02:14.916761Z","iopub.execute_input":"2023-11-18T04:02:14.917681Z","iopub.status.idle":"2023-11-18T04:02:14.924675Z","shell.execute_reply.started":"2023-11-18T04:02:14.917632Z","shell.execute_reply":"2023-11-18T04:02:14.923695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load trained model","metadata":{}},{"cell_type":"code","source":"model = keras.models.load_model(\"/kaggle/input/model2/model2.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-11-18T04:02:14.926379Z","iopub.execute_input":"2023-11-18T04:02:14.927118Z","iopub.status.idle":"2023-11-18T04:02:16.364180Z","shell.execute_reply.started":"2023-11-18T04:02:14.927080Z","shell.execute_reply":"2023-11-18T04:02:16.363364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Test Data","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/histopathologic-cancer-detection/sample_submission.csv\")\nprint(df.columns)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T04:02:16.366696Z","iopub.execute_input":"2023-11-18T04:02:16.366983Z","iopub.status.idle":"2023-11-18T04:02:16.423226Z","shell.execute_reply.started":"2023-11-18T04:02:16.366958Z","shell.execute_reply":"2023-11-18T04:02:16.422276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Define crop function","metadata":{}},{"cell_type":"code","source":"def crop_center(img):\n    center_x, center_y = img.shape[0] // 2, img.shape[1] // 2\n    half_width, half_height = 16, 16  # Half of 32x32 is 16x16\n    cropped_img = img[center_x - half_width:center_x + half_width, center_y - half_height:center_y + half_height]\n    return cropped_img","metadata":{"execution":{"iopub.status.busy":"2023-11-18T04:02:16.424737Z","iopub.execute_input":"2023-11-18T04:02:16.424995Z","iopub.status.idle":"2023-11-18T04:02:16.430704Z","shell.execute_reply.started":"2023-11-18T04:02:16.424972Z","shell.execute_reply":"2023-11-18T04:02:16.429565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['id'] = df['id'] + '.tif'","metadata":{"execution":{"iopub.status.busy":"2023-11-18T04:02:16.431766Z","iopub.execute_input":"2023-11-18T04:02:16.432032Z","iopub.status.idle":"2023-11-18T04:02:16.452652Z","shell.execute_reply.started":"2023-11-18T04:02:16.432008Z","shell.execute_reply":"2023-11-18T04:02:16.451790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Print first few filenames from the DataFrame\nprint(df['id'].head())","metadata":{"execution":{"iopub.status.busy":"2023-11-18T04:02:16.453811Z","iopub.execute_input":"2023-11-18T04:02:16.454661Z","iopub.status.idle":"2023-11-18T04:02:16.461135Z","shell.execute_reply.started":"2023-11-18T04:02:16.454635Z","shell.execute_reply":"2023-11-18T04:02:16.460184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create the test data generator","metadata":{"execution":{"iopub.status.busy":"2023-11-18T03:59:10.497718Z","iopub.execute_input":"2023-11-18T03:59:10.498316Z","iopub.status.idle":"2023-11-18T03:59:10.502280Z","shell.execute_reply.started":"2023-11-18T03:59:10.498287Z","shell.execute_reply":"2023-11-18T03:59:10.501272Z"}}},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(\n    rescale = 1 / 255,\n    preprocessing_function=crop_center)\n\ntest_generator = test_datagen.flow_from_dataframe(\n    dataframe=df,\n    directory=\"/kaggle/input/histopathologic-cancer-detection/test\",\n    x_col=\"id\",\n    batch_size=32,\n    shuffle=False,\n    class_mode=None,\n    target_size=(32,32)\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T04:04:04.461892Z","iopub.execute_input":"2023-11-18T04:04:04.462623Z","iopub.status.idle":"2023-11-18T04:04:29.916410Z","shell.execute_reply.started":"2023-11-18T04:04:04.462587Z","shell.execute_reply":"2023-11-18T04:04:29.915599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Use model to predict","metadata":{}},{"cell_type":"code","source":"test_prob = model.predict(test_generator)\nprint(test_prob.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T04:04:33.866042Z","iopub.execute_input":"2023-11-18T04:04:33.867123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"/kaggle/input/histopathologic-cancer-detection/sample_submission.csv\")\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-18T04:02:54.860637Z","iopub.status.idle":"2023-11-18T04:02:54.861004Z","shell.execute_reply.started":"2023-11-18T04:02:54.860827Z","shell.execute_reply":"2023-11-18T04:02:54.860843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"label\"] = test_prob.argmax(axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T04:02:54.861868Z","iopub.status.idle":"2023-11-18T04:02:54.862206Z","shell.execute_reply.started":"2023-11-18T04:02:54.862039Z","shell.execute_reply":"2023-11-18T04:02:54.862054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission2.csv\", header = True, index = False)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T04:02:54.863642Z","iopub.status.idle":"2023-11-18T04:02:54.863953Z","shell.execute_reply.started":"2023-11-18T04:02:54.863797Z","shell.execute_reply":"2023-11-18T04:02:54.863811Z"},"trusted":true},"execution_count":null,"outputs":[]}]}