{"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":"none","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-09-03T20:08:40.825277Z","iopub.execute_input":"2025-09-03T20:08:40.825517Z","iopub.status.idle":"2025-09-03T20:09:06.295452Z","shell.execute_reply.started":"2025-09-03T20:08:40.825496Z","shell.execute_reply":"2025-09-03T20:09:06.293789Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Explaratory data analysis","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import os\nimport json\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2\nfrom PIL import Image\nimport hashlib\nimport os\nimport cv2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T20:27:50.559542Z","iopub.execute_input":"2025-09-03T20:27:50.560604Z","iopub.status.idle":"2025-09-03T20:27:50.565572Z","shell.execute_reply.started":"2025-09-03T20:27:50.560567Z","shell.execute_reply":"2025-09-03T20:27:50.564524Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\n# /kaggle/input/cassava-leaf-disease-classification/sample_submission.csv\n# /kaggle/input/cassava-leaf-disease-classification/label_num_to_disease_map.json\n# /kaggle/input/cassava-leaf-disease-classification/train.csv\n\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Base_dir = \"/kaggle/input/cassava-leaf-disease-classification/train.csv\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T20:28:39.603335Z","iopub.execute_input":"2025-09-03T20:28:39.603687Z","iopub.status.idle":"2025-09-03T20:28:39.607893Z","shell.execute_reply.started":"2025-09-03T20:28:39.603662Z","shell.execute_reply":"2025-09-03T20:28:39.60684Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with open(\"/kaggle/input/cassava-leaf-disease-classification/label_num_to_disease_map.json\") as file:\n    print(\"yes\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T20:30:54.063455Z","iopub.execute_input":"2025-09-03T20:30:54.063803Z","iopub.status.idle":"2025-09-03T20:30:54.071997Z","shell.execute_reply.started":"2025-09-03T20:30:54.063778Z","shell.execute_reply":"2025-09-03T20:30:54.071127Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#.step-1... Load and inspect label map (mapping from numerical labels to disease names)\nwith open(os.path.join(Base_dir,\"/kaggle/input/cassava-leaf-disease-classification/label_num_to_disease_map.json\")) as file:\n    map_classes = json.loads(file.read())\n    map_classes = {int(k): v for k, v in map_classes.items()}\n\n# display\nprint(\"Class Mapping: \")\nprint(json.dumps(map_classes,indent=4))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T20:37:15.30477Z","iopub.execute_input":"2025-09-03T20:37:15.305152Z","iopub.status.idle":"2025-09-03T20:37:15.31709Z","shell.execute_reply.started":"2025-09-03T20:37:15.305125Z","shell.execute_reply":"2025-09-03T20:37:15.315791Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir(os.path.join(base_dir, \"train_images\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T20:41:45.478605Z","iopub.execute_input":"2025-09-03T20:41:45.478969Z","iopub.status.idle":"2025-09-03T20:41:45.501982Z","shell.execute_reply.started":"2025-09-03T20:41:45.478945Z","shell.execute_reply":"2025-09-03T20:41:45.499608Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# step-2 : - Load training image filenames and display the count\ninput_files = os.listdir(os.path.join(base_dir, \"train_images\"))\nprint(f\"Number of train images:  {len(input_files)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T20:45:04.412136Z","iopub.execute_input":"2025-09-03T20:45:04.413021Z","iopub.status.idle":"2025-09-03T20:45:04.425356Z","shell.execute_reply.started":"2025-09-03T20:45:04.412989Z","shell.execute_reply":"2025-09-03T20:45:04.424348Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#.Load train csv\ndf_train = pd.read_csv(os.path.join(base_dir, \"train.csv\"))\ndf_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T20:46:09.604822Z","iopub.execute_input":"2025-09-03T20:46:09.605608Z","iopub.status.idle":"2025-09-03T20:46:09.666623Z","shell.execute_reply.started":"2025-09-03T20:46:09.605575Z","shell.execute_reply":"2025-09-03T20:46:09.665711Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train[\"class_name\"] = df_train[\"label\"].map(map_classes)\ndf_train                                              ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T20:49:21.011523Z","iopub.execute_input":"2025-09-03T20:49:21.011866Z","iopub.status.idle":"2025-09-03T20:49:21.033697Z","shell.execute_reply.started":"2025-09-03T20:49:21.011844Z","shell.execute_reply":"2025-09-03T20:49:21.032796Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train['class_name'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T20:50:22.285234Z","iopub.execute_input":"2025-09-03T20:50:22.285521Z","iopub.status.idle":"2025-09-03T20:50:22.296154Z","shell.execute_reply.started":"2025-09-03T20:50:22.285501Z","shell.execute_reply":"2025-09-03T20:50:22.295295Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#.check classs distribution\nclass_distribution = df_train['class_name'].value_counts()\n# plot the class dist.\nplt.figure(figsize=(10,6))\nclass_distribution.plot(kind='bar')\nplt.title(\"Class dist of Cassava Leaf Disease\")\nplt.ylabel('Number of Images')\nplt.xlabel('Disease Classes')\nplt.xticks(rotation=45)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T20:53:27.348843Z","iopub.execute_input":"2025-09-03T20:53:27.349508Z","iopub.status.idle":"2025-09-03T20:53:27.883328Z","shell.execute_reply.started":"2025-09-03T20:53:27.349478Z","shell.execute_reply":"2025-09-03T20:53:27.882243Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#.counts plot\nplt.figure(figsize=(8,4))\nsns.countplot(y='class_name', data=df_train)\nplt.title('class Distribution (Seaborn)')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T20:56:30.776405Z","iopub.execute_input":"2025-09-03T20:56:30.776771Z","iopub.status.idle":"2025-09-03T20:56:30.955312Z","shell.execute_reply.started":"2025-09-03T20:56:30.776745Z","shell.execute_reply":"2025-09-03T20:56:30.954122Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# step:-5 Basic datset exploration\nprint(\"Dataset Info:\")\nprint(df_train.info())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T20:58:27.700425Z","iopub.execute_input":"2025-09-03T20:58:27.700768Z","iopub.status.idle":"2025-09-03T20:58:27.719566Z","shell.execute_reply.started":"2025-09-03T20:58:27.700738Z","shell.execute_reply":"2025-09-03T20:58:27.718629Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\nDataset Summary Statistics:\")\nprint(df_train.describe())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T20:59:19.506497Z","iopub.execute_input":"2025-09-03T20:59:19.50686Z","iopub.status.idle":"2025-09-03T20:59:19.520603Z","shell.execute_reply.started":"2025-09-03T20:59:19.506834Z","shell.execute_reply":"2025-09-03T20:59:19.519774Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" # check missing values\nprint(f\"\\nMissing values in each column:\\n{df_train.isnull().sum()}\")\nprint(f\"Number of duplicate rows: {df_train.duplicated().sum()}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T21:02:32.554823Z","iopub.execute_input":"2025-09-03T21:02:32.55547Z","iopub.status.idle":"2025-09-03T21:02:32.571188Z","shell.execute_reply.started":"2025-09-03T21:02:32.555444Z","shell.execute_reply":"2025-09-03T21:02:32.570118Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path = \"/kaggle/input/cassava-leaf-disease-classification/train_images/4025247063.jpg\"\npath2 = \"4025247063.jpg\"\n\ncv2.imread(path2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T21:06:12.867862Z","iopub.execute_input":"2025-09-03T21:06:12.868147Z","iopub.status.idle":"2025-09-03T21:06:12.884223Z","shell.execute_reply.started":"2025-09-03T21:06:12.868126Z","shell.execute_reply":"2025-09-03T21:06:12.883196Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# step-7\n#.Dictionary to store image\nimg_shapes = {}\nfor image_name in os.listdir(os.path.join(base_dir,\"train_images\"))[:500]:\n    image = cv2.imread(os.path.join(base_dir,\"train_images\", image_name))\n    img_shapes[image.shape] = img_shapes.get(image.shape,0) + 1\n\nprint(\"\\nSample Image shapes and their Frequencies (from 100 images):\")\nprint(img_shapes)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T21:10:53.455152Z","iopub.execute_input":"2025-09-03T21:10:53.455442Z","iopub.status.idle":"2025-09-03T21:11:01.340568Z","shell.execute_reply.started":"2025-09-03T21:10:53.455423Z","shell.execute_reply":"2025-09-03T21:11:01.33979Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot the image size dist..\nimg_shapes_df = pd.DataFrame(list(img_shapes.items()), columns=['Image Shape','Count'])\nplt.figure(figsize=(10,6))\nimg_shapes_df.sort_values(by='Count',ascending=False).plot(kind='bar',x='Image Shape')\nplt.title('Image Shape Dist (sample of 300 Images')\nplt.xlabel('Image Shape')\nplt.ylabel('Number of Images')\nplt.xticks(rotation=45)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T21:19:09.240505Z","iopub.execute_input":"2025-09-03T21:19:09.24084Z","iopub.status.idle":"2025-09-03T21:19:09.398812Z","shell.execute_reply.started":"2025-09-03T21:19:09.240818Z","shell.execute_reply":"2025-09-03T21:19:09.397985Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T21:20:26.35393Z","iopub.execute_input":"2025-09-03T21:20:26.35422Z","iopub.status.idle":"2025-09-03T21:20:26.363759Z","shell.execute_reply.started":"2025-09-03T21:20:26.3542Z","shell.execute_reply":"2025-09-03T21:20:26.362787Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# define func\nimport os\nimport matplotlib.pyplot as plt\nfrom PIL import Image\n\ndef plot_images_from_class(class_id, num_images=9):\n    # filter rows for this class\n    class_images = df_train[df_train['label'] == class_id]\n    \n    # pick limited number of images\n    num_images = min(len(class_images),num_images)\n    \n    # plot images in 3x3 grid\n    plt.figure(figsize=(15, 15))\n    images = class_images.sample(num_images)\n    for i, (_, row) in enumerate(class_images.iterrows()):\n        img_path = os.path.join(base_dir, \"train_images\", row['image_id'])\n        img = Image.open(img_path)\n        \n        plt.subplot(3, 3, i+1)\n        plt.imshow(img)\n        plt.title(map_classes[class_id])\n        plt.axis('off')\n    \n    plt.tight_layout()\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T21:38:00.657586Z","iopub.execute_input":"2025-09-03T21:38:00.658113Z","iopub.status.idle":"2025-09-03T21:38:00.666772Z","shell.execute_reply.started":"2025-09-03T21:38:00.658088Z","shell.execute_reply":"2025-09-03T21:38:00.665421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_images_from_class(0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T21:42:19.133984Z","iopub.execute_input":"2025-09-03T21:42:19.134295Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_images_from_class(class_id, num_images=9):\n    class_images = df_train[df_train['label'] == class_id].sample(num_images)\n    \n    plt.figure(figsize=(10, 10))\n    for i, (_, row) in enumerate(class_images.iterrows()):\n        img_path = os.path.join(base_dir, \"train_images\", row['image_id'])\n        img = Image.open(img_path)\n        \n        plt.subplot(3, 3, i+1)   # safe, since num_images ≤ 9\n        plt.imshow(img)\n        plt.title(map_classes[class_id])\n        plt.axis('off')\n    \n    plt.tight_layout()\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T21:41:57.893095Z","iopub.execute_input":"2025-09-03T21:41:57.893377Z","iopub.status.idle":"2025-09-03T21:41:57.899653Z","shell.execute_reply.started":"2025-09-03T21:41:57.893358Z","shell.execute_reply":"2025-09-03T21:41:57.898594Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#.visualize 0 to 4\nfor i in range(5):\n    print(f\"Displaying sample image for class: {map_classes[i]}\")\n    plot_images_from_class(i)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-03T21:42:37.124537Z","iopub.execute_input":"2025-09-03T21:42:37.124899Z","iopub.status.idle":"2025-09-03T21:42:45.709108Z","shell.execute_reply.started":"2025-09-03T21:42:37.124875Z","shell.execute_reply":"2025-09-03T21:42:45.707777Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}