{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport tifffile as tiff\nimport matplotlib.pyplot as plt\nimport glob\nimport numpy as np\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:38.607200Z","iopub.execute_input":"2026-07-28T21:07:38.607511Z","iopub.status.idle":"2026-07-28T21:07:38.613100Z","shell.execute_reply.started":"2026-07-28T21:07:38.607482Z","shell.execute_reply":"2026-07-28T21:07:38.611899Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(os.listdir(\"/kaggle/input\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:38.625220Z","iopub.execute_input":"2026-07-28T21:07:38.625546Z","iopub.status.idle":"2026-07-28T21:07:38.638297Z","shell.execute_reply.started":"2026-07-28T21:07:38.625517Z","shell.execute_reply":"2026-07-28T21:07:38.637066Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(os.listdir(\"/kaggle/input/competitions\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:40.829385Z","iopub.execute_input":"2026-07-28T21:07:40.829684Z","iopub.status.idle":"2026-07-28T21:07:40.835492Z","shell.execute_reply.started":"2026-07-28T21:07:40.829645Z","shell.execute_reply":"2026-07-28T21:07:40.834344Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"comp_path = \"/kaggle/input/competitions/beyond-visible-spectrum-ai-for-agriculture-2024\"\nprint(os.listdir(comp_path))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:41.373714Z","iopub.execute_input":"2026-07-28T21:07:41.374078Z","iopub.status.idle":"2026-07-28T21:07:41.380785Z","shell.execute_reply.started":"2026-07-28T21:07:41.374046Z","shell.execute_reply":"2026-07-28T21:07:41.379352Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"arch_path = os.path.join(comp_path, \"archive\")\nprint(os.listdir(arch_path))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:41.382704Z","iopub.execute_input":"2026-07-28T21:07:41.383046Z","iopub.status.idle":"2026-07-28T21:07:41.405412Z","shell.execute_reply.started":"2026-07-28T21:07:41.383018Z","shell.execute_reply":"2026-07-28T21:07:41.404241Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tr_path = os.path.join(arch_path, \"train\")\nprint(os.listdir(tr_path))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:41.429383Z","iopub.execute_input":"2026-07-28T21:07:41.429700Z","iopub.status.idle":"2026-07-28T21:07:41.435061Z","shell.execute_reply.started":"2026-07-28T21:07:41.429675Z","shell.execute_reply":"2026-07-28T21:07:41.433904Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"health_path = os.path.join(tr_path, \"Health\")\nprint(os.listdir(health_path)[:20])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:41.436126Z","iopub.execute_input":"2026-07-28T21:07:41.436380Z","iopub.status.idle":"2026-07-28T21:07:41.461857Z","shell.execute_reply.started":"2026-07-28T21:07:41.436356Z","shell.execute_reply":"2026-07-28T21:07:41.460829Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"health_files = glob.glob(os.path.join(health_path, \"*.tif\"))\n\nprint(\"Number of Health images:\", len(health_files))\nprint(health_files[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:41.706014Z","iopub.execute_input":"2026-07-28T21:07:41.706331Z","iopub.status.idle":"2026-07-28T21:07:41.712997Z","shell.execute_reply.started":"2026-07-28T21:07:41.706306Z","shell.execute_reply":"2026-07-28T21:07:41.712324Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_path = health_files[0]\n\nimage = tiff.imread(image_path)\n\nprint(\"Image loaded successfully!\")\nprint(\"Image shape:\", image.shape)\nprint(\"Data type:\", image.dtype)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:41.819551Z","iopub.execute_input":"2026-07-28T21:07:41.819940Z","iopub.status.idle":"2026-07-28T21:07:41.831652Z","shell.execute_reply.started":"2026-07-28T21:07:41.819907Z","shell.execute_reply":"2026-07-28T21:07:41.830420Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from collections import Counter\n\nimage_shapes = Counter()\n\nfor file_path in health_files:\n    img = tiff.imread(file_path)\n    image_shapes[img.shape] += 1\n\nprint(image_shapes)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:41.937170Z","iopub.execute_input":"2026-07-28T21:07:41.937433Z","iopub.status.idle":"2026-07-28T21:07:42.395004Z","shell.execute_reply.started":"2026-07-28T21:07:41.937415Z","shell.execute_reply":"2026-07-28T21:07:42.393920Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_125_path = None\nimage_126_path = None\n\nfor file_path in health_files:\n    img = tiff.imread(file_path)\n\n    if img.shape == (64, 64, 125) and image_125_path is None:\n        image_125_path = file_path\n\n    elif img.shape == (64, 64, 126) and image_126_path is None:\n        image_126_path = file_path\n\n    if image_125_path is not None and image_126_path is not None:\n        break\n\nprint(\"125-band image:\", os.path.basename(image_125_path))\nprint(\"126-band image:\", os.path.basename(image_126_path))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:42.396165Z","iopub.execute_input":"2026-07-28T21:07:42.396373Z","iopub.status.idle":"2026-07-28T21:07:42.408699Z","shell.execute_reply.started":"2026-07-28T21:07:42.396353Z","shell.execute_reply":"2026-07-28T21:07:42.407866Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_125 = tiff.imread(image_125_path)\nimage_126 = tiff.imread(image_126_path)\n\nprint(\"125-band image shape:\", image_125.shape)\nprint(\"126-band image shape:\", image_126.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:42.446113Z","iopub.execute_input":"2026-07-28T21:07:42.446467Z","iopub.status.idle":"2026-07-28T21:07:42.456130Z","shell.execute_reply.started":"2026-07-28T21:07:42.446437Z","shell.execute_reply":"2026-07-28T21:07:42.455287Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"band_summary = pd.DataFrame({\n    \"Image\": [\"125-band\", \"126-band\"],\n    \"First Band Mean\": [\n        image_125[:, :, 0].mean(),\n        image_126[:, :, 0].mean()\n    ],\n    \"Last Band Mean\": [\n        image_125[:, :, -1].mean(),\n        image_126[:, :, -1].mean()\n    ]\n})\nband_summary.index = [1, 2]\nprint(band_summary)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:42.467684Z","iopub.execute_input":"2026-07-28T21:07:42.468946Z","iopub.status.idle":"2026-07-28T21:07:42.478991Z","shell.execute_reply.started":"2026-07-28T21:07:42.468894Z","shell.execute_reply":"2026-07-28T21:07:42.477692Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"band0 = image_125[:, :, 0]\n\nprint(\"Mean:\", band0.mean())\nprint(\"Min:\", band0.min())\nprint(\"Max:\", band0.max())\nprint(\"Standard Deviation:\", band0.std())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:42.616182Z","iopub.execute_input":"2026-07-28T21:07:42.616495Z","iopub.status.idle":"2026-07-28T21:07:42.622709Z","shell.execute_reply.started":"2026-07-28T21:07:42.616473Z","shell.execute_reply":"2026-07-28T21:07:42.621761Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = []\n\ntotal_pixels = image_126.shape[0] * image_126.shape[1]\n\nfor band in range(image_126.shape[2]):\n    band_data = image_126[:, :, band]\n\n    zero_count = np.sum(band_data == 0)\n    max_count = np.sum(band_data == 65535)\n\n    results.append([\n        band + 1,          # Display bands as 1 to 126\n        zero_count,\n        max_count\n    ])\n\nsummary_df = pd.DataFrame(\n    results,\n    columns=[\"Band\", \"Zero Pixels\", \"65535 Pixels\"]\n)\n\ndisplay(summary_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:42.724333Z","iopub.execute_input":"2026-07-28T21:07:42.724689Z","iopub.status.idle":"2026-07-28T21:07:42.742442Z","shell.execute_reply.started":"2026-07-28T21:07:42.724661Z","shell.execute_reply":"2026-07-28T21:07:42.741295Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"band0_125 = image_125[:, :, 0].flatten()\nband0_126 = image_126[:, :, 0].flatten()\n\nplt.figure(figsize=(15,5))\n\nplt.plot(band0_125, label=\"125-band Image\")\nplt.plot(band0_126, label=\"126-band Image\", alpha=0.7)\n\nplt.title(\"Comparison of Band 0 Pixel Values\")\nplt.xlabel(\"Pixel Index (0 to 4095)\")\nplt.ylabel(\"Pixel Value\")\nplt.legend()\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:42.781922Z","iopub.execute_input":"2026-07-28T21:07:42.782269Z","iopub.status.idle":"2026-07-28T21:07:42.934333Z","shell.execute_reply.started":"2026-07-28T21:07:42.782242Z","shell.execute_reply":"2026-07-28T21:07:42.933376Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"band0_125 = image_125[:, :, -1].flatten()\nband0_126 = image_126[:, :, -1].flatten()\n\nplt.figure(figsize=(15,5))\n\nplt.plot(band0_125, label=\"125-band Image\")\nplt.plot(band0_126, label=\"126-band Image\", alpha=0.7)\n\nplt.title(\"Comparison of Last Band Pixel Values\")\nplt.xlabel(\"Pixel Index (0 to 4095)\")\nplt.ylabel(\"Pixel Value\")\nplt.legend()\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:45.053926Z","iopub.execute_input":"2026-07-28T21:07:45.054271Z","iopub.status.idle":"2026-07-28T21:07:45.216719Z","shell.execute_reply.started":"2026-07-28T21:07:45.054244Z","shell.execute_reply":"2026-07-28T21:07:45.215494Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pixel_125 = image_125[32, 32, :]\npixel_126 = image_126[32, 32, :]\n\nplt.figure(figsize=(10, 5))\n\nplt.plot(pixel_125, label=\"125-band image\")\nplt.plot(pixel_126, label=\"126-band image\")\n\nplt.xlabel(\"Spectral Band\")\nplt.ylabel(\"Pixel Value\")\nplt.title(\"Spectral Profile Comparison\")\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:45.469566Z","iopub.execute_input":"2026-07-28T21:07:45.469940Z","iopub.status.idle":"2026-07-28T21:07:45.604127Z","shell.execute_reply.started":"2026-07-28T21:07:45.469906Z","shell.execute_reply":"2026-07-28T21:07:45.603214Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"last_band_means = []\n\nfor file_path in health_files:\n    img = tiff.imread(file_path)\n\n    if img.shape == (64, 64, 126):\n        last_band_mean = img[:, :, -1].mean()\n        last_band_means.append(last_band_mean)\n\nprint(\"Number of 126-band images:\", len(last_band_means))\nprint(\"Last-band means:\")\nprint(last_band_means)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:45.605505Z","iopub.execute_input":"2026-07-28T21:07:45.605746Z","iopub.status.idle":"2026-07-28T21:07:46.057786Z","shell.execute_reply.started":"2026-07-28T21:07:45.605718Z","shell.execute_reply":"2026-07-28T21:07:46.056764Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for file_path in health_files:\n    \n    img = tiff.imread(file_path)\n\n    if img.shape == (64, 64, 126):\n        \n        last_band = img[:, :, -1]\n\n        print(\n            os.path.basename(file_path),\n            \"min =\", last_band.min(),\n            \"max =\", last_band.max(),\n            \"unique values =\", len(np.unique(last_band))\n        )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:46.059446Z","iopub.execute_input":"2026-07-28T21:07:46.059754Z","iopub.status.idle":"2026-07-28T21:07:46.495427Z","shell.execute_reply.started":"2026-07-28T21:07:46.059723Z","shell.execute_reply":"2026-07-28T21:07:46.494305Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = []\ntest_image = image_126\n\nfor band in range(test_image.shape[2]):\n    \n    band_data = test_image[:, :, band]\n    \n    results.append({\n        \"Band\": band + 1,\n        \"Min\": band_data.min(),\n        \"Max\": band_data.max(),\n        \"Mean\": band_data.mean(),\n        \"Unique Values\": len(np.unique(band_data))\n    })\n\nband_stats = pd.DataFrame(results)\n\ndisplay(band_stats)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:46.496891Z","iopub.execute_input":"2026-07-28T21:07:46.497135Z","iopub.status.idle":"2026-07-28T21:07:46.536544Z","shell.execute_reply.started":"2026-07-28T21:07:46.497112Z","shell.execute_reply":"2026-07-28T21:07:46.535714Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for file_path in health_files:\n\n    img = tiff.imread(file_path)\n\n    if img.shape == (64, 64, 126):\n\n        print(\"\\n\", os.path.basename(file_path))\n\n        for band in range(126):\n\n            unique_count = len(np.unique(img[:, :, band]))\n\n            if unique_count <= 2:\n                print(\n                    \"Suspicious Band:\",\n                    band + 1,\n                    \"| Unique values:\",\n                    unique_count\n                )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:46.537434Z","iopub.execute_input":"2026-07-28T21:07:46.537676Z","iopub.status.idle":"2026-07-28T21:07:47.749677Z","shell.execute_reply.started":"2026-07-28T21:07:46.537654Z","shell.execute_reply":"2026-07-28T21:07:47.748466Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bad_img = tiff.imread(\"/kaggle/input/competitions/beyond-visible-spectrum-ai-for-agriculture-2024/archive/train/Health/hyper (23).tif\")\n\nplt.imshow(bad_img[:, :, 50], cmap=\"gray\")\nplt.title(\"Band 51 of hyper (23)\")\nplt.colorbar()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:47.751523Z","iopub.execute_input":"2026-07-28T21:07:47.751856Z","iopub.status.idle":"2026-07-28T21:07:47.911329Z","shell.execute_reply.started":"2026-07-28T21:07:47.751824Z","shell.execute_reply":"2026-07-28T21:07:47.910141Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for file_path in health_files:\n\n    img = tiff.imread(file_path)\n\n    # Check only 125-band images\n    if img.shape == (64, 64, 125):\n\n        suspicious_bands = 0\n\n        for band in range(125):\n\n            unique_count = len(np.unique(img[:, :, band]))\n\n            if unique_count <= 2:\n                suspicious_bands += 1\n\n        print(\n            os.path.basename(file_path),\n            \"-> Suspicious bands:\",\n            suspicious_bands\n        )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:47.912766Z","iopub.execute_input":"2026-07-28T21:07:47.913102Z","iopub.status.idle":"2026-07-28T21:07:51.825672Z","shell.execute_reply.started":"2026-07-28T21:07:47.913076Z","shell.execute_reply":"2026-07-28T21:07:51.824083Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bad_img = tiff.imread(\"/kaggle/input/competitions/beyond-visible-spectrum-ai-for-agriculture-2024/archive/train/Health/hyper (167).tif\")\n\nplt.imshow(bad_img[:, :, 50], cmap=\"gray\")\nplt.title(\"Band 51 of hyper (167)\")\nplt.colorbar()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:51.826752Z","iopub.execute_input":"2026-07-28T21:07:51.827077Z","iopub.status.idle":"2026-07-28T21:07:51.991251Z","shell.execute_reply.started":"2026-07-28T21:07:51.827050Z","shell.execute_reply":"2026-07-28T21:07:51.989970Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def analyze_folder(folder_path):\n\n    empty_images = []\n    bad126_images = []\n\n    files = [os.path.join(folder_path, f)\n             for f in os.listdir(folder_path)\n             if f.endswith(\".tif\")]\n\n    print(\"Total images:\", len(files))\n\n    for file in files:\n\n        img = tiff.imread(file)\n\n        # 125-band images\n        if img.shape == (64, 64, 125):\n\n            suspicious = 0\n\n            for band in range(125):\n\n                if len(np.unique(img[:, :, band])) <= 2:\n                    suspicious += 1\n\n            if suspicious == 125:\n                empty_images.append(os.path.basename(file))\n\n        # 126-band images\n        elif img.shape == (64, 64, 126):\n\n            suspicious = 0\n\n            for band in range(126):\n\n                if len(np.unique(img[:, :, band])) <= 2:\n                    suspicious += 1\n\n            if suspicious == 126:\n                empty_images.append(os.path.basename(file))\n            else:\n                bad126_images.append(os.path.basename(file))\n\n    print(\"\\nEmpty images:\", len(empty_images))\n    print(empty_images)\n\n    print(\"\\n126-band images:\", len(bad126_images))\n    print(bad126_images)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:51.992326Z","iopub.execute_input":"2026-07-28T21:07:51.992583Z","iopub.status.idle":"2026-07-28T21:07:52.001297Z","shell.execute_reply.started":"2026-07-28T21:07:51.992552Z","shell.execute_reply":"2026-07-28T21:07:51.999505Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"analyze_folder(health_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:52.004172Z","iopub.execute_input":"2026-07-28T21:07:52.004444Z","iopub.status.idle":"2026-07-28T21:07:56.720249Z","shell.execute_reply.started":"2026-07-28T21:07:52.004420Z","shell.execute_reply":"2026-07-28T21:07:56.718767Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rust_path = \"/kaggle/input/competitions/beyond-visible-spectrum-ai-for-agriculture-2024/archive/train/Rust\"\nanalyze_folder(rust_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:07:56.721274Z","iopub.execute_input":"2026-07-28T21:07:56.721498Z","iopub.status.idle":"2026-07-28T21:08:01.819068Z","shell.execute_reply.started":"2026-07-28T21:07:56.721478Z","shell.execute_reply":"2026-07-28T21:08:01.817853Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"health_img = tiff.imread(os.path.join(health_path, \"hyper (23).tif\"))\nrust_img = tiff.imread(os.path.join(rust_path, \"hyper (23).tif\"))\n\nprint(\"Health shape:\", health_img.shape)\nprint(\"Rust shape:\", rust_img.shape)\n\nif health_img.shape == rust_img.shape:\n    if np.array_equal(health_img, rust_img):\n        print(\"The two images are exactly the same.\")\n    else:\n        print(\"The two images are different.\")\nelse:\n    print(\"The images have different shapes, so they cannot be identical.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:01.820452Z","iopub.execute_input":"2026-07-28T21:08:01.820695Z","iopub.status.idle":"2026-07-28T21:08:01.832655Z","shell.execute_reply.started":"2026-07-28T21:08:01.820669Z","shell.execute_reply":"2026-07-28T21:08:01.831457Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"difference = health_img[:, :, band].astype(float) - rust_img[:, :, band].astype(float)\n\nplt.figure(figsize=(5,5))\nplt.imshow(difference, cmap='bwr')\nplt.colorbar()\nplt.title(\"Difference (Health - Rust) : Band 1\")\nplt.axis('off')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:01.834283Z","iopub.execute_input":"2026-07-28T21:08:01.834777Z","iopub.status.idle":"2026-07-28T21:08:01.952878Z","shell.execute_reply.started":"2026-07-28T21:08:01.834747Z","shell.execute_reply":"2026-07-28T21:08:01.951107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rust_files = glob.glob(os.path.join(rust_path, \"*.tif\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:01.953932Z","iopub.execute_input":"2026-07-28T21:08:01.954126Z","iopub.status.idle":"2026-07-28T21:08:01.960480Z","shell.execute_reply.started":"2026-07-28T21:08:01.954109Z","shell.execute_reply":"2026-07-28T21:08:01.958999Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for file_path in rust_files:\n\n    img = tiff.imread(file_path)\n\n    # Check only 125-band images\n    if img.shape == (64, 64, 125):\n\n        suspicious_bands = 0\n\n        for band in range(125):\n\n            unique_count = len(np.unique(img[:, :, band]))\n\n            if unique_count <= 2:\n                suspicious_bands += 1\n\n        print(\n            os.path.basename(file_path),\n            \"-> Suspicious bands:\",\n            suspicious_bands\n        )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:01.962322Z","iopub.execute_input":"2026-07-28T21:08:01.962668Z","iopub.status.idle":"2026-07-28T21:08:06.000550Z","shell.execute_reply.started":"2026-07-28T21:08:01.962627Z","shell.execute_reply":"2026-07-28T21:08:05.998984Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for file_path in rust_files:\n\n    img = tiff.imread(file_path)\n\n    # Check only 125-band images\n    if img.shape == (64, 64, 126):\n\n        suspicious_bands = 0\n\n        for band in range(126):\n\n            unique_count = len(np.unique(img[:, :, band]))\n\n            if unique_count <= 2:\n                suspicious_bands += 1\n\n        print(\n            os.path.basename(file_path),\n            \"-> Suspicious bands:\",\n            suspicious_bands\n        )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:06.001522Z","iopub.execute_input":"2026-07-28T21:08:06.001776Z","iopub.status.idle":"2026-07-28T21:08:07.262306Z","shell.execute_reply.started":"2026-07-28T21:08:06.001753Z","shell.execute_reply":"2026-07-28T21:08:07.261002Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    print(dirname)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:07.263316Z","iopub.execute_input":"2026-07-28T21:08:07.263579Z","iopub.status.idle":"2026-07-28T21:08:08.590667Z","shell.execute_reply.started":"2026-07-28T21:08:07.263553Z","shell.execute_reply":"2026-07-28T21:08:08.589543Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Original Rust folder\ninput_folder = \"/kaggle/input/competitions/beyond-visible-spectrum-ai-for-agriculture-2024/archive/train/Rust\"\n\n# New cleaned folder\noutput_folder = \"/kaggle/working/Rust_Clean\"\n\nos.makedirs(output_folder, exist_ok=True)\n\ncount = 0\n\nfor file in os.listdir(input_folder):\n\n    if file.endswith(\".tif\"):\n\n        path = os.path.join(input_folder, file)\n\n        img = tiff.imread(path)\n\n        # If image has 126 bands\n        if img.shape[2] == 126:\n\n            # Remove the last band\n            img = img[:, :, :125]\n            count += 1\n\n        # Save image\n        tiff.imwrite(os.path.join(output_folder, file), img)\n\nprint(f\"Done! Corrected {count} images.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:08.592117Z","iopub.execute_input":"2026-07-28T21:08:08.592424Z","iopub.status.idle":"2026-07-28T21:08:09.363551Z","shell.execute_reply.started":"2026-07-28T21:08:08.592397Z","shell.execute_reply":"2026-07-28T21:08:09.362251Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"folder = \"/kaggle/working/Rust_Clean\"\n\nfor file in os.listdir(folder):\n    if file.endswith(\".tif\"):\n        img = tiff.imread(os.path.join(folder, file))\n\n        if img.shape != (64, 64, 125):\n            print(file, img.shape)\n\nprint(\"Verification Complete!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:09.364785Z","iopub.execute_input":"2026-07-28T21:08:09.365023Z","iopub.status.idle":"2026-07-28T21:08:09.468466Z","shell.execute_reply.started":"2026-07-28T21:08:09.365003Z","shell.execute_reply":"2026-07-28T21:08:09.467536Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Path to the original Healthy folder\ninput_folder = \"/kaggle/input/competitions/beyond-visible-spectrum-ai-for-agriculture-2024/archive/train/Health\"\n\n# Folder where cleaned images will be saved\noutput_folder = \"/kaggle/working/Healthy_Clean\"\n\nos.makedirs(output_folder, exist_ok=True)\n\ncount = 0\nempty_images = ['hyper (12).tif', 'hyper (153).tif', 'hyper (67).tif', 'hyper (76).tif', 'hyper (23).tif', 'hyper (38).tif', 'hyper (26).tif', 'hyper (34).tif', 'hyper (167).tif']\n\nfor file in os.listdir(input_folder):\n\n    if file in empty_images:\n        continue  # Skip empty image\n\n    img = tiff.imread(os.path.join(input_folder, file))\n\n    if img.shape[2] == 126:\n        img = img[:, :, :125]\n        count+=1\n\n    tiff.imwrite(os.path.join(output_folder, file), img)\nprint(f\"Done! Corrected {count} images.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:09.469597Z","iopub.execute_input":"2026-07-28T21:08:09.469894Z","iopub.status.idle":"2026-07-28T21:08:10.512958Z","shell.execute_reply.started":"2026-07-28T21:08:09.469864Z","shell.execute_reply":"2026-07-28T21:08:10.512124Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"folder = \"/kaggle/working/Healthy_Clean\"\n\nfor file in os.listdir(folder):\n    if file.endswith(\".tif\"):\n        img = tiff.imread(os.path.join(folder, file))\n\n        if img.shape != (64, 64, 125):\n            print(file, img.shape)\n\nprint(\"Verification Complete!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:10.514207Z","iopub.execute_input":"2026-07-28T21:08:10.514498Z","iopub.status.idle":"2026-07-28T21:08:10.619546Z","shell.execute_reply.started":"2026-07-28T21:08:10.514474Z","shell.execute_reply":"2026-07-28T21:08:10.618397Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rust_folder = \"/kaggle/working/Rust_Clean\"\nhealthy_folder = \"/kaggle/working/Healthy_Clean\"\n\nrust_count = len([f for f in os.listdir(rust_folder) if f.endswith(\".tif\")])\nhealthy_count = len([f for f in os.listdir(healthy_folder) if f.endswith(\".tif\")])\n\nprint(\"Rust images   :\", rust_count)\nprint(\"Healthy images:\", healthy_count)\nprint(\"Total images  :\", rust_count + healthy_count)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:10.622486Z","iopub.execute_input":"2026-07-28T21:08:10.622738Z","iopub.status.idle":"2026-07-28T21:08:10.630983Z","shell.execute_reply.started":"2026-07-28T21:08:10.622717Z","shell.execute_reply":"2026-07-28T21:08:10.629937Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img = tiff.TiffFile(\"/kaggle/working/Rust_Clean/hyper (1).tif\")\n\nprint(img.pages[0].tags)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:10.631924Z","iopub.execute_input":"2026-07-28T21:08:10.632173Z","iopub.status.idle":"2026-07-28T21:08:10.655355Z","shell.execute_reply.started":"2026-07-28T21:08:10.632143Z","shell.execute_reply":"2026-07-28T21:08:10.654214Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Input folders\nhealthy_input = \"/kaggle/working/Healthy_Clean\"\nrust_input = \"/kaggle/working/Rust_Clean\"\n\n# Output folders\nhealthy_output = \"/kaggle/working/Healthy_Normalized\"\nrust_output = \"/kaggle/working/Rust_Normalized\"\n\nos.makedirs(healthy_output, exist_ok=True)\nos.makedirs(rust_output, exist_ok=True)\n\n# Find global maximum pixel value\nglobal_max = 0\n\nfor folder in [healthy_input, rust_input]:\n    for file in os.listdir(folder):\n        if file.endswith(\".tif\"):\n            img = tiff.imread(os.path.join(folder, file))\n            global_max = max(global_max, img.max())\n\nprint(\"Global Maximum =\", global_max)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:10.656432Z","iopub.execute_input":"2026-07-28T21:08:10.656607Z","iopub.status.idle":"2026-07-28T21:08:10.926338Z","shell.execute_reply.started":"2026-07-28T21:08:10.656590Z","shell.execute_reply":"2026-07-28T21:08:10.925219Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"global_max = 10176.0\n\n# Normalize Healthy images\nfor file in os.listdir(healthy_input):\n    if file.endswith(\".tif\"):\n        img = tiff.imread(os.path.join(healthy_input, file))\n        img = img.astype(np.float32) / global_max\n        tiff.imwrite(os.path.join(healthy_output, file), img)\n\n# Normalize Rust images\nfor file in os.listdir(rust_input):\n    if file.endswith(\".tif\"):\n        img = tiff.imread(os.path.join(rust_input, file))\n        img = img.astype(np.float32) / global_max\n        tiff.imwrite(os.path.join(rust_output, file), img)\n\nprint(\"Normalization completed successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:10.927403Z","iopub.execute_input":"2026-07-28T21:08:10.927710Z","iopub.status.idle":"2026-07-28T21:08:12.394016Z","shell.execute_reply.started":"2026-07-28T21:08:10.927680Z","shell.execute_reply":"2026-07-28T21:08:12.393014Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"healthy_folder = \"/kaggle/working/Healthy_Normalized\"\n\nresults = []\n\nfor file in sorted(os.listdir(healthy_folder))[:5]:\n    if file.endswith(\".tif\"):\n        img = tiff.imread(os.path.join(healthy_folder, file))\n\n        results.append({\n            \"File Name\": file,\n            \"Shape\": str(img.shape),\n            \"Min\": img.min(),\n            \"Max\": img.max(),\n            \"Mean\": img.mean()\n        })\n\nhealthy_df = pd.DataFrame(results)\n\nhealthy_df.index = range(1, len(healthy_df) + 1)\n\ndisplay(healthy_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:12.395033Z","iopub.execute_input":"2026-07-28T21:08:12.395252Z","iopub.status.idle":"2026-07-28T21:08:12.415593Z","shell.execute_reply.started":"2026-07-28T21:08:12.395231Z","shell.execute_reply":"2026-07-28T21:08:12.414552Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"healthy_folder = \"/kaggle/working/Healthy_Normalized\"\nrust_folder = \"/kaggle/working/Rust_Normalized\"\n\nX = []   # Images\ny = []   # Labels\n\n# Healthy = 0\nfor file in sorted(os.listdir(healthy_folder)):\n    if file.endswith(\".tif\"):\n        img = tiff.imread(os.path.join(healthy_folder, file))\n        X.append(img)\n        y.append(0)\n\n# Rust = 1\nfor file in sorted(os.listdir(rust_folder)):\n    if file.endswith(\".tif\"):\n        img = tiff.imread(os.path.join(rust_folder, file))\n        X.append(img)\n        y.append(1)\n\n# Convert to NumPy arrays\nX = np.array(X)\ny = np.array(y)\n\nprint(\"Images Shape :\", X.shape)\nprint(\"Labels Shape :\", y.shape)\n\nprint(\"\\nHealthy Images :\", np.sum(y == 0))\nprint(\"Rust Images    :\", np.sum(y == 1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:12.416704Z","iopub.execute_input":"2026-07-28T21:08:12.416961Z","iopub.status.idle":"2026-07-28T21:08:12.942953Z","shell.execute_reply.started":"2026-07-28T21:08:12.416939Z","shell.execute_reply":"2026-07-28T21:08:12.942222Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.save(\"/kaggle/working/X.npy\", X)\nnp.save(\"/kaggle/working/y.npy\", y)\n\nprint(\"Dataset saved successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:12.943892Z","iopub.execute_input":"2026-07-28T21:08:12.944128Z","iopub.status.idle":"2026-07-28T21:08:13.649062Z","shell.execute_reply.started":"2026-07-28T21:08:12.944101Z","shell.execute_reply":"2026-07-28T21:08:13.647927Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# First split: Train (80%) and Temp (20%)\nX_train, X_temp, y_train, y_temp = train_test_split(\n    X,\n    y,\n    test_size=0.20,\n    random_state=42,\n    stratify=y\n)\n\n# Second split: Temp -> Validation (10%) + Test (10%)\nX_val, X_test, y_val, y_test = train_test_split(\n    X_temp,\n    y_temp,\n    test_size=0.50,\n    random_state=42,\n    stratify=y_temp\n)\n\nprint(\"Training Set :\", X_train.shape)\nprint(\"Validation Set:\", X_val.shape)\nprint(\"Test Set :\", X_test.shape)\n\nprint(\"\\nTraining Labels\")\nprint(\"Healthy:\", (y_train == 0).sum())\nprint(\"Rust   :\", (y_train == 1).sum())\n\nprint(\"\\nValidation Labels\")\nprint(\"Healthy:\", (y_val == 0).sum())\nprint(\"Rust   :\", (y_val == 1).sum())\n\nprint(\"\\nTest Labels\")\nprint(\"Healthy:\", (y_test == 0).sum())\nprint(\"Rust   :\", (y_test == 1).sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:13.650276Z","iopub.execute_input":"2026-07-28T21:08:13.650571Z","iopub.status.idle":"2026-07-28T21:08:13.831318Z","shell.execute_reply.started":"2026-07-28T21:08:13.650538Z","shell.execute_reply":"2026-07-28T21:08:13.829497Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def augment_image(img):\n\n    augmented = []\n\n    # Original image\n    augmented.append(img)\n\n    # Horizontal Flip\n    augmented.append(np.flip(img, axis=1))\n\n    # Vertical Flip\n    augmented.append(np.flip(img, axis=0))\n\n    # Rotate 90 degrees\n    augmented.append(np.rot90(img, k=1, axes=(0, 1)))\n\n    return augmented","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:13.832454Z","iopub.execute_input":"2026-07-28T21:08:13.832825Z","iopub.status.idle":"2026-07-28T21:08:17.588669Z","shell.execute_reply.started":"2026-07-28T21:08:13.832770Z","shell.execute_reply":"2026-07-28T21:08:17.587571Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_aug = []\ny_train_aug = []\n\nfor img, label in zip(X_train, y_train):\n\n    augmented_images = augment_image(img)\n\n    for aug_img in augmented_images:\n        X_train_aug.append(aug_img)\n        y_train_aug.append(label)\n\nX_train_aug = np.array(X_train_aug)\ny_train_aug = np.array(y_train_aug)\n\nprint(\"Original Training Images :\", len(X_train))\nprint(\"Augmented Training Images:\", len(X_train_aug))\nprint(\"Training Labels :\", len(y_train_aug))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:17.589675Z","iopub.execute_input":"2026-07-28T21:08:17.589956Z","iopub.status.idle":"2026-07-28T21:08:18.061316Z","shell.execute_reply.started":"2026-07-28T21:08:17.589932Z","shell.execute_reply":"2026-07-28T21:08:18.060651Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.save(\"X_train_aug.npy\", X_train_aug)\nnp.save(\"y_train_aug.npy\", y_train_aug)\n\nnp.save(\"X_val.npy\", X_val)\nnp.save(\"y_val.npy\", y_val)\n\nnp.save(\"X_test.npy\", X_test)\nnp.save(\"y_test.npy\", y_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:18.062191Z","iopub.execute_input":"2026-07-28T21:08:18.062423Z","iopub.status.idle":"2026-07-28T21:08:21.091679Z","shell.execute_reply.started":"2026-07-28T21:08:18.062397Z","shell.execute_reply":"2026-07-28T21:08:21.090291Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(os.listdir())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:21.092868Z","iopub.execute_input":"2026-07-28T21:08:21.093142Z","iopub.status.idle":"2026-07-28T21:08:21.098742Z","shell.execute_reply.started":"2026-07-28T21:08:21.093117Z","shell.execute_reply":"2026-07-28T21:08:21.097578Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import shutil\n\n# Create a new folder\nfolder_name = \"/kaggle/working/HSI_Final_Dataset\"\nos.makedirs(folder_name, exist_ok=True)\n\n# Files you want to share\nfiles = [\n    \"X_train_aug.npy\",\n    \"y_train_aug.npy\",\n    \"X_val.npy\",\n    \"y_val.npy\",\n    \"X_test.npy\",\n    \"y_test.npy\"\n]\n\n# Copy only selected files\nfor file in files:\n    shutil.copy(\n        f\"/kaggle/working/{file}\",\n        f\"{folder_name}/{file}\"\n    )\n\nprint(\"Selected files copied successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-28T21:08:21.099770Z","iopub.execute_input":"2026-07-28T21:08:21.100058Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import shutil\n\nshutil.make_archive(\n    \"/kaggle/working/HSI_Final_Dataset\",\n    \"zip\",\n    \"/kaggle/working/HSI_Final_Dataset\"\n)\n\nprint(\"ZIP created!\")","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}