{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.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":13589276,"sourceType":"datasetVersion","datasetId":8634017}],"dockerImageVersionId":31153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split\nimport tifffile as tiff\nimport pickle\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras.models import Sequential, load_model\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout, BatchNormalization\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-11-02T17:11:22.868937Z","iopub.execute_input":"2025-11-02T17:11:22.869136Z","iopub.status.idle":"2025-11-02T17:11:38.435366Z","shell.execute_reply.started":"2025-11-02T17:11:22.869114Z","shell.execute_reply":"2025-11-02T17:11:38.434708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with open('/kaggle/input/histocancerdetect-best-with-densenet121/Cancer_Detection_model_denbest.pk1', 'rb') as file: \n    history = pickle.load(file)\n    \ncnn = load_model('/kaggle/input/histocancerdetect-best-with-densenet121/Cancer_Detection_model_denbest.keras')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-02T17:11:38.436025Z","iopub.execute_input":"2025-11-02T17:11:38.436440Z","iopub.status.idle":"2025-11-02T17:11:45.746142Z","shell.execute_reply.started":"2025-11-02T17:11:38.436424Z","shell.execute_reply":"2025-11-02T17:11:45.745576Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1/255)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-02T17:11:45.748211Z","iopub.execute_input":"2025-11-02T17:11:45.748532Z","iopub.status.idle":"2025-11-02T17:11:45.752133Z","shell.execute_reply.started":"2025-11-02T17:11:45.748502Z","shell.execute_reply":"2025-11-02T17:11:45.751315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = pd.read_csv(\"/kaggle/input/histopathologic-cancer-detection/sample_submission.csv\")\ntest['id'] = test['id'].apply(lambda x:f'{x}.tif')\n\ntest_generator = test_datagen.flow_from_dataframe(\n    dataframe = test,\n    directory = '/kaggle/input/histopathologic-cancer-detection/test',\n    x_col = \"id\",\n    y_col = None,\n    batch_size = 100,\n    seed = 1,\n    shuffle = False,\n    class_mode = None,\n    target_size = (64,64)\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-02T17:11:45.755320Z","iopub.execute_input":"2025-11-02T17:11:45.756086Z","iopub.status.idle":"2025-11-02T17:14:37.636896Z","shell.execute_reply.started":"2025-11-02T17:11:45.756068Z","shell.execute_reply":"2025-11-02T17:14:37.636306Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_pred = np.argmax(cnn.predict(test_generator), axis=-1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-02T17:14:37.637555Z","iopub.execute_input":"2025-11-02T17:14:37.637755Z","iopub.status.idle":"2025-11-02T17:21:58.142924Z","shell.execute_reply.started":"2025-11-02T17:14:37.637739Z","shell.execute_reply":"2025-11-02T17:21:58.142325Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Submission = test\nSubmission['label'] = test_pred\nSubmission['id'] = Submission['id'].str.replace(r'.tif$', '',regex=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-02T17:21:58.143716Z","iopub.execute_input":"2025-11-02T17:21:58.143923Z","iopub.status.idle":"2025-11-02T17:21:58.202055Z","shell.execute_reply.started":"2025-11-02T17:21:58.143907Z","shell.execute_reply":"2025-11-02T17:21:58.201502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Submission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-02T17:21:58.202734Z","iopub.execute_input":"2025-11-02T17:21:58.203008Z","iopub.status.idle":"2025-11-02T17:21:58.223168Z","shell.execute_reply.started":"2025-11-02T17:21:58.202979Z","shell.execute_reply":"2025-11-02T17:21:58.222545Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Submission.to_csv('bestsubmission.csv',index=False,header=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-02T17:21:58.224923Z","iopub.execute_input":"2025-11-02T17:21:58.225176Z","iopub.status.idle":"2025-11-02T17:21:58.319526Z","shell.execute_reply.started":"2025-11-02T17:21:58.225160Z","shell.execute_reply":"2025-11-02T17:21:58.318956Z"}},"outputs":[],"execution_count":null}]}