{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"},{"sourceId":14092665,"sourceType":"datasetVersion","datasetId":8973258}],"dockerImageVersionId":31192,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nos.environ[\"PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION\"] = \"python\"\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom pathlib import Path\n\n# Test image directory\nTEST_DIR = Path(\"/kaggle/input/cassava-leaf-disease-classification/test_images\")\n\n# Load sample submission\nsubmission = pd.read_csv(\"/kaggle/input/cassava-leaf-disease-classification/sample_submission.csv\")\n\nsubmission.head()\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-12-10T16:44:10.849742Z","iopub.execute_input":"2025-12-10T16:44:10.850541Z","iopub.status.idle":"2025-12-10T16:44:10.864247Z","shell.execute_reply.started":"2025-12-10T16:44:10.850509Z","shell.execute_reply":"2025-12-10T16:44:10.863416Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\n\nmodel = load_model(\"/kaggle/input/ea-cassava-leaf-disease-classification-models/efficientnetb0_cassava.h5\")\nprint(\"Model loaded successfully!\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T16:46:02.325266Z","iopub.execute_input":"2025-12-10T16:46:02.325692Z","iopub.status.idle":"2025-12-10T16:46:06.266164Z","shell.execute_reply.started":"2025-12-10T16:46:02.325668Z","shell.execute_reply":"2025-12-10T16:46:06.265518Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications.efficientnet import preprocess_input\n\ntest_images = []\nimage_ids = []\n\nfor img_path in TEST_DIR.iterdir():\n    if img_path.suffix == \".jpg\":\n        image_ids.append(img_path.name)\n        img = load_img(img_path, target_size=(224, 224))\n        img = img_to_array(img)\n        img = preprocess_input(img)  # same as training\n        test_images.append(img)\n\ntest_images = np.array(test_images)\nprint(\"Loaded test images:\", test_images.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T16:46:48.659474Z","iopub.execute_input":"2025-12-10T16:46:48.659806Z","iopub.status.idle":"2025-12-10T16:46:48.724680Z","shell.execute_reply.started":"2025-12-10T16:46:48.659782Z","shell.execute_reply":"2025-12-10T16:46:48.723806Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.efficientnet import preprocess_input\nfrom pathlib import Path\nimport pandas as pd\nimport numpy as np\n\n# 1. Paths & image size (match training)\nIMG_SIZE = (224, 224)\nTEST_DIR = Path(\"/kaggle/input/cassava-leaf-disease-classification/test_images\")\n\n# 2. Build DataFrame of test images (sorted for stable order)\ntest_files = sorted(TEST_DIR.glob(\"*.jpg\"))\ntest_df = pd.DataFrame({\n    \"image_id\": [p.name for p in test_files],\n    \"filepath\": [str(p) for p in test_files],\n})\n\n# 3. Test generator – use SAME preprocessing as training\ntest_datagen = ImageDataGenerator(\n    preprocessing_function=preprocess_input\n)\n\ntest_gen = test_datagen.flow_from_dataframe(\n    test_df,\n    x_col=\"filepath\",\n    y_col=None,\n    target_size=IMG_SIZE,\n    class_mode=None,\n    batch_size=32,\n    shuffle=False\n)\n\n# 4. Predict\npreds = model.predict(test_gen, verbose=1)\nlabels = np.argmax(preds, axis=1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T16:50:12.979509Z","iopub.execute_input":"2025-12-10T16:50:12.979852Z","iopub.status.idle":"2025-12-10T16:50:21.551917Z","shell.execute_reply.started":"2025-12-10T16:50:12.979830Z","shell.execute_reply":"2025-12-10T16:50:21.551149Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub = pd.read_csv(\n    \"/kaggle/input/cassava-leaf-disease-classification/sample_submission.csv\"\n)\n\n# Make sure order matches (it should, since we sorted filenames)\nprint(sub.head())\nprint(test_df.head())\n\n# Fill label column\nsub[\"label\"] = labels\n\n# Save submission\nsub.to_csv(\"submission.csv\", index=False)\nprint(\"submission.csv saved!\")\nsub.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-10T16:51:24.055389Z","iopub.execute_input":"2025-12-10T16:51:24.056387Z","iopub.status.idle":"2025-12-10T16:51:24.075844Z","shell.execute_reply.started":"2025-12-10T16:51:24.056358Z","shell.execute_reply":"2025-12-10T16:51:24.075174Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}