{"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":"none","dataSources":[{"sourceId":11848,"databundleVersionId":862157,"sourceType":"competition"},{"sourceId":8986640,"sourceType":"datasetVersion","datasetId":5412272},{"sourceId":9034444,"sourceType":"datasetVersion","datasetId":5445754},{"sourceId":9035506,"sourceType":"datasetVersion","datasetId":5446514}],"dockerImageVersionId":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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-26T16:14:54.996777Z","iopub.execute_input":"2024-07-26T16:14:54.997607Z","iopub.status.idle":"2024-07-26T16:15:07.361771Z","shell.execute_reply.started":"2024-07-26T16:14:54.997565Z","shell.execute_reply":"2024-07-26T16:15:07.360755Z"},"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-26T16:15:07.363453Z","iopub.execute_input":"2024-07-26T16:15:07.363981Z","iopub.status.idle":"2024-07-26T16:15:12.342448Z","shell.execute_reply.started":"2024-07-26T16:15:07.363955Z","shell.execute_reply":"2024-07-26T16:15:12.341499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Generator","metadata":{}},{"cell_type":"code","source":"BATCH_SIZE = 128\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 = (64,64)\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T16:15:12.343671Z","iopub.execute_input":"2024-07-26T16:15:12.344036Z","iopub.status.idle":"2024-07-26T16:18:04.770891Z","shell.execute_reply.started":"2024-07-26T16:15:12.344004Z","shell.execute_reply":"2024-07-26T16:18:04.770121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn = keras.models.load_model('/kaggle/input/week3v2-1/Histopath_model_v2.h5')\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-26T16:18:04.771911Z","iopub.execute_input":"2024-07-26T16:18:04.772175Z","iopub.status.idle":"2024-07-26T16:18:06.141813Z","shell.execute_reply.started":"2024-07-26T16:18:04.772152Z","shell.execute_reply":"2024-07-26T16:18:06.140940Z"},"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-26T16:18:06.143913Z","iopub.execute_input":"2024-07-26T16:18:06.144208Z","iopub.status.idle":"2024-07-26T16:23:57.581591Z","shell.execute_reply.started":"2024-07-26T16:18:06.144181Z","shell.execute_reply":"2024-07-26T16:23:57.580244Z"},"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-26T16:23:57.583188Z","iopub.execute_input":"2024-07-26T16:23:57.583841Z","iopub.status.idle":"2024-07-26T16:23:57.589930Z","shell.execute_reply.started":"2024-07-26T16:23:57.583804Z","shell.execute_reply":"2024-07-26T16:23:57.589071Z"},"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-26T16:23:57.590905Z","iopub.execute_input":"2024-07-26T16:23:57.591174Z","iopub.status.idle":"2024-07-26T16:23:57.691983Z","shell.execute_reply.started":"2024-07-26T16:23:57.591151Z","shell.execute_reply":"2024-07-26T16:23:57.691138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.label = test_pred\nsubmission.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T16:23:57.693317Z","iopub.execute_input":"2024-07-26T16:23:57.694106Z","iopub.status.idle":"2024-07-26T16:23:57.705113Z","shell.execute_reply.started":"2024-07-26T16:23:57.694070Z","shell.execute_reply":"2024-07-26T16:23:57.704001Z"},"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-26T16:23:57.706433Z","iopub.execute_input":"2024-07-26T16:23:57.706784Z","iopub.status.idle":"2024-07-26T16:23:57.861202Z","shell.execute_reply.started":"2024-07-26T16:23:57.706749Z","shell.execute_reply":"2024-07-26T16:23:57.860402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}