{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Full analysis can be found here:\n\nhttps://github.com/flaviabarcellos/Histopathologic-Cancer-Detection/blob/main/Week_3_CNN_Cancer_Detection_Kaggle_Mini-Project_Histopathologic_Cancer_Detection_V2.ipynb","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-13T19:23:29.735564Z","iopub.execute_input":"2023-09-13T19:23:29.736217Z","iopub.status.idle":"2023-09-13T19:23:29.769955Z","shell.execute_reply.started":"2023-09-13T19:23:29.736182Z","shell.execute_reply":"2023-09-13T19:23:29.768493Z"}}},{"cell_type":"code","source":"from keras.models import load_model\nimport pandas as pd\nimport os\nfrom tensorflow.keras.preprocessing import image\nimport numpy as np\n","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:48:55.829097Z","iopub.execute_input":"2023-09-13T19:48:55.829545Z","iopub.status.idle":"2023-09-13T19:48:56.198381Z","shell.execute_reply.started":"2023-09-13T19:48:55.829511Z","shell.execute_reply":"2023-09-13T19:48:56.197204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = '/kaggle/input/histopathologic-cancer-detection/test/'\ntest_image_names = os.listdir(test_path)\n\ndef load_and_preprocess_image(img_path, target_size=(96, 96)):\n    img = image.load_img(img_path, target_size=target_size)\n    img_array = image.img_to_array(img)\n    img_array = np.expand_dims(img_array, axis=0)\n\n    return img_array\n\ntest_images = np.vstack([load_and_preprocess_image(os.path.join(test_path, fname)) for fname in test_image_names])\n","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:49:16.172300Z","iopub.execute_input":"2023-09-13T19:49:16.172727Z","iopub.status.idle":"2023-09-13T19:59:59.736013Z","shell.execute_reply.started":"2023-09-13T19:49:16.172695Z","shell.execute_reply":"2023-09-13T19:59:59.734359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model('/kaggle/input/cnn-cancer-detection-kaggle-mini-project/kaggle_model.h5')\npredictions = model.predict(test_images)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:59:59.741666Z","iopub.execute_input":"2023-09-13T19:59:59.742167Z","iopub.status.idle":"2023-09-13T20:03:41.082976Z","shell.execute_reply.started":"2023-09-13T19:59:59.742126Z","shell.execute_reply":"2023-09-13T20:03:41.081756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"binary_predictions = np.where(predictions > 0.5, 1, 0)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-13T20:08:00.166286Z","iopub.execute_input":"2023-09-13T20:08:00.166710Z","iopub.status.idle":"2023-09-13T20:08:00.172986Z","shell.execute_reply.started":"2023-09-13T20:08:00.166675Z","shell.execute_reply":"2023-09-13T20:08:00.171735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({\n    'id': [name.split('.')[0] for name in test_image_names],\n    'label': binary_predictions.squeeze()\n})\n\n#submission_df.to_csv('submission.csv', index=False)\nsubmission_df.to_csv('/kaggle/working/submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-13T20:34:13.649567Z","iopub.execute_input":"2023-09-13T20:34:13.650126Z","iopub.status.idle":"2023-09-13T20:34:13.931761Z","shell.execute_reply.started":"2023-09-13T20:34:13.650086Z","shell.execute_reply":"2023-09-13T20:34:13.929875Z"},"trusted":true},"execution_count":null,"outputs":[]}],"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"}}