{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.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"}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Preparation","metadata":{}},{"cell_type":"code","source":"# Import functions first\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os, cv2, json\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2024-04-19T07:28:16.017307Z","iopub.execute_input":"2024-04-19T07:28:16.017715Z","iopub.status.idle":"2024-04-19T07:28:19.873718Z","shell.execute_reply.started":"2024-04-19T07:28:16.017680Z","shell.execute_reply":"2024-04-19T07:28:19.872370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the file directory location\nbase_dir = \"../input/cassava-leaf-disease-classification\"","metadata":{"execution":{"iopub.status.busy":"2024-04-19T07:28:24.573938Z","iopub.execute_input":"2024-04-19T07:28:24.575285Z","iopub.status.idle":"2024-04-19T07:28:24.580959Z","shell.execute_reply.started":"2024-04-19T07:28:24.575242Z","shell.execute_reply":"2024-04-19T07:28:24.579613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Acquire explanatory data analysis (EDA) features of the mapping\nwith open(os.path.join(base_dir, \"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\nprint(json.dumps(map_classes, indent=4))","metadata":{"execution":{"iopub.status.busy":"2024-04-19T07:28:27.836622Z","iopub.execute_input":"2024-04-19T07:28:27.837060Z","iopub.status.idle":"2024-04-19T07:28:27.847945Z","shell.execute_reply.started":"2024-04-19T07:28:27.837027Z","shell.execute_reply":"2024-04-19T07:28:27.846530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show number of train images\ninput_files = os.listdir(os.path.join(base_dir, \"train_images\"))\nprint(f\"Number of train images: {len(input_files)}\")","metadata":{"execution":{"iopub.status.busy":"2024-04-19T07:28:32.585898Z","iopub.execute_input":"2024-04-19T07:28:32.586881Z","iopub.status.idle":"2024-04-19T07:28:33.034387Z","shell.execute_reply.started":"2024-04-19T07:28:32.586843Z","shell.execute_reply":"2024-04-19T07:28:33.033186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show the image resolution\nimg_shapes = {}\n\nfor image_name in os.listdir(os.path.join(base_dir, \"train_images\"))[:300]:\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(img_shapes)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T07:28:37.852555Z","iopub.execute_input":"2024-04-19T07:28:37.852979Z","iopub.status.idle":"2024-04-19T07:28:42.592589Z","shell.execute_reply.started":"2024-04-19T07:28:37.852950Z","shell.execute_reply":"2024-04-19T07:28:42.590816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image classification","metadata":{}},{"cell_type":"code","source":"# Load the training DataFrame\ndf_train = pd.read_csv(os.path.join(base_dir, \"train.csv\"))\ndf_train[\"class_name\"] = df_train[\"label\"].map(map_classes)\n\ndf_train","metadata":{"execution":{"iopub.status.busy":"2024-04-19T07:28:46.951569Z","iopub.execute_input":"2024-04-19T07:28:46.951990Z","iopub.status.idle":"2024-04-19T07:28:47.014911Z","shell.execute_reply.started":"2024-04-19T07:28:46.951956Z","shell.execute_reply":"2024-04-19T07:28:47.013667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show class counts along with images\nplt.figure(figsize=(8, 4))\nsns.countplot(y=\"class_name\", data=df_train)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T07:29:02.510758Z","iopub.execute_input":"2024-04-19T07:29:02.511689Z","iopub.status.idle":"2024-04-19T07:29:02.838013Z","shell.execute_reply.started":"2024-04-19T07:29:02.511652Z","shell.execute_reply":"2024-04-19T07:29:02.836718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create a submission","metadata":{}},{"cell_type":"code","source":"# Generate the submission template\ndf_sub = pd.read_csv(\"../input/cassava-leaf-disease-classification/sample_submission.csv\", index_col=0)\n\ndf_sub","metadata":{"execution":{"iopub.status.busy":"2024-04-19T07:29:16.029431Z","iopub.execute_input":"2024-04-19T07:29:16.030348Z","iopub.status.idle":"2024-04-19T07:29:16.049453Z","shell.execute_reply.started":"2024-04-19T07:29:16.030308Z","shell.execute_reply":"2024-04-19T07:29:16.048050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show one of the test images file name\nos.listdir(os.path.join(base_dir, \"test_images\"))","metadata":{"execution":{"iopub.status.busy":"2024-04-19T07:29:18.756182Z","iopub.execute_input":"2024-04-19T07:29:18.756614Z","iopub.status.idle":"2024-04-19T07:29:18.766352Z","shell.execute_reply.started":"2024-04-19T07:29:18.756581Z","shell.execute_reply":"2024-04-19T07:29:18.765075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compute the accuracy rating\nfor pred_class in range(0, 5):\n    y_true = df_train[\"label\"].values\n    y_pred = np.full_like(y_true, pred_class)\n    print(f\"Accuracy score (predict {pred_class}): {accuracy_score(y_true, y_pred):.3f}\")","metadata":{"execution":{"iopub.status.busy":"2024-04-19T07:29:21.418429Z","iopub.execute_input":"2024-04-19T07:29:21.418841Z","iopub.status.idle":"2024-04-19T07:29:21.436841Z","shell.execute_reply.started":"2024-04-19T07:29:21.418809Z","shell.execute_reply":"2024-04-19T07:29:21.435829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assign label\ndf_sub[\"label\"] = 3\n\n# Save as CSV submission file\ndf_sub.to_csv(\"submission.csv\")\nprint(\"Successfully saved as CSV file\")","metadata":{"execution":{"iopub.status.busy":"2024-04-19T07:29:24.737466Z","iopub.execute_input":"2024-04-19T07:29:24.737863Z","iopub.status.idle":"2024-04-19T07:29:24.748156Z","shell.execute_reply.started":"2024-04-19T07:29:24.737834Z","shell.execute_reply":"2024-04-19T07:29:24.746920Z"},"trusted":true},"execution_count":null,"outputs":[]}]}