{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":11848,"databundleVersionId":862157,"sourceType":"competition"},{"sourceId":8986640,"sourceType":"datasetVersion","datasetId":5412272}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import Packages","metadata":{}},{"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":{"execution":{"iopub.status.busy":"2024-07-19T17:30:28.360671Z","iopub.execute_input":"2024-07-19T17:30:28.361266Z","iopub.status.idle":"2024-07-19T17:30:40.017802Z","shell.execute_reply.started":"2024-07-19T17:30:28.361233Z","shell.execute_reply":"2024-07-19T17:30:40.016817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load DataFrame","metadata":{}},{"cell_type":"code","source":"test_directory = '/kaggle/input/histopathologic-cancer-detection/test'\nprint('Test:', len(os.listdir(test_directory)))","metadata":{"execution":{"iopub.status.busy":"2024-07-19T17:30:56.305371Z","iopub.execute_input":"2024-07-19T17:30:56.306493Z","iopub.status.idle":"2024-07-19T17:31:00.691962Z","shell.execute_reply.started":"2024-07-19T17:30:56.306460Z","shell.execute_reply":"2024-07-19T17:31:00.690915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Generator","metadata":{}},{"cell_type":"code","source":"BATCH_SIZE = 64\n\ntest_datagen = ImageDataGenerator(rescale=1/255)\n\ntest = os.listdir(test_directory)\ntest_df = pd.DataFrame(test, columns=['id'])\n\ntest_loader = test_datagen.flow_from_dataframe(\n    dataframe = test_df,\n    directory = test_directory,\n    x_col = 'id', \n    batch_size = BATCH_SIZE, \n    shuffle = False,\n    class_mode = None,\n    target_size = (32,32)\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-19T17:31:06.464376Z","iopub.execute_input":"2024-07-19T17:31:06.464740Z","iopub.status.idle":"2024-07-19T17:34:11.391910Z","shell.execute_reply.started":"2024-07-19T17:31:06.464711Z","shell.execute_reply":"2024-07-19T17:34:11.390959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn = keras.models.load_model('/kaggle/input/week3model/Histopath_model_v01.h5')\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-19T17:35:21.288684Z","iopub.execute_input":"2024-07-19T17:35:21.289056Z","iopub.status.idle":"2024-07-19T17:35:22.614102Z","shell.execute_reply.started":"2024-07-19T17:35:21.289025Z","shell.execute_reply":"2024-07-19T17:35:22.613201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test Predictions","metadata":{}},{"cell_type":"code","source":"test_probs = cnn.predict(test_loader)","metadata":{"execution":{"iopub.status.busy":"2024-07-19T17:36:13.397567Z","iopub.execute_input":"2024-07-19T17:36:13.398030Z","iopub.status.idle":"2024-07-19T17:43:09.300297Z","shell.execute_reply.started":"2024-07-19T17:36:13.397991Z","shell.execute_reply":"2024-07-19T17:43:09.299447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = np.argmax(test_probs, axis=1)\nprint(test_pred[:10])","metadata":{"execution":{"iopub.status.busy":"2024-07-19T17:48:23.757292Z","iopub.execute_input":"2024-07-19T17:48:23.758196Z","iopub.status.idle":"2024-07-19T17:48:23.763617Z","shell.execute_reply.started":"2024-07-19T17:48:23.758141Z","shell.execute_reply":"2024-07-19T17:48:23.762770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/sample_submission.csv')\nsubmission.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-07-19T17:48:29.458855Z","iopub.execute_input":"2024-07-19T17:48:29.459224Z","iopub.status.idle":"2024-07-19T17:48:29.580456Z","shell.execute_reply.started":"2024-07-19T17:48:29.459195Z","shell.execute_reply":"2024-07-19T17:48:29.579585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.label = test_pred\nsubmission.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-07-19T17:57:13.021868Z","iopub.execute_input":"2024-07-19T17:57:13.022285Z","iopub.status.idle":"2024-07-19T17:57:13.032765Z","shell.execute_reply.started":"2024-07-19T17:57:13.022251Z","shell.execute_reply":"2024-07-19T17:57:13.031809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False, header=True)","metadata":{"execution":{"iopub.status.busy":"2024-07-19T17:57:46.239605Z","iopub.execute_input":"2024-07-19T17:57:46.240439Z","iopub.status.idle":"2024-07-19T17:57:46.390076Z","shell.execute_reply.started":"2024-07-19T17:57:46.240410Z","shell.execute_reply":"2024-07-19T17:57:46.389348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}