{"metadata":{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport math\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-19T09:57:03.078168Z","iopub.execute_input":"2022-03-19T09:57:03.078704Z","iopub.status.idle":"2022-03-19T09:57:03.084337Z","shell.execute_reply.started":"2022-03-19T09:57:03.078667Z","shell.execute_reply":"2022-03-19T09:57:03.083168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Files","metadata":{}},{"cell_type":"code","source":"!ls ../input/sorghum-id-fgvc-9","metadata":{"execution":{"iopub.status.busy":"2022-03-19T09:57:03.359965Z","iopub.execute_input":"2022-03-19T09:57:03.36032Z","iopub.status.idle":"2022-03-19T09:57:04.140856Z","shell.execute_reply.started":"2022-03-19T09:57:03.360279Z","shell.execute_reply":"2022-03-19T09:57:04.139885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_DIR = \"../input/sorghum-id-fgvc-9\"\nTRAIN_IMG_PATH = os.path.join(BASE_DIR, \"train_images\")\nTEST_IMG_PATH = os.path.join(BASE_DIR, \"test\")","metadata":{"execution":{"iopub.status.busy":"2022-03-19T09:57:04.143152Z","iopub.execute_input":"2022-03-19T09:57:04.143468Z","iopub.status.idle":"2022-03-19T09:57:04.149904Z","shell.execute_reply.started":"2022-03-19T09:57:04.143431Z","shell.execute_reply":"2022-03-19T09:57:04.149091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train data","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(os.path.join(BASE_DIR, \"train_cultivar_mapping.csv\"))\nprint(f\"Number of train images: {train_df.shape[0]}\")\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-19T09:57:04.151551Z","iopub.execute_input":"2022-03-19T09:57:04.15252Z","iopub.status.idle":"2022-03-19T09:57:04.203907Z","shell.execute_reply.started":"2022-03-19T09:57:04.152469Z","shell.execute_reply":"2022-03-19T09:57:04.202648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Size of training data\n### Check existence of images\n* Note that some images, listed in train_cultivar_mapping.csv, do not exist.","metadata":{}},{"cell_type":"code","source":"train_df[\"image_existence\"] = [os.path.exists(os.path.join(TRAIN_IMG_PATH, row.image)) for _, row in train_df.iterrows()]\nprint(f\"Number of non-existent images: {train_df[train_df.image_existence == False].size}\")","metadata":{"execution":{"iopub.status.busy":"2022-03-19T09:57:04.206497Z","iopub.execute_input":"2022-03-19T09:57:04.206945Z","iopub.status.idle":"2022-03-19T09:57:22.532795Z","shell.execute_reply.started":"2022-03-19T09:57:04.206893Z","shell.execute_reply":"2022-03-19T09:57:22.531903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plot number of images","metadata":{}},{"cell_type":"code","source":"train_counts = train_df.cultivar.value_counts()\nexisted_train_counts = train_df[train_df.image_existence == True].cultivar.value_counts()\nprint(f\"Number of cultivars: {train_counts.size}\")\n\nplt.figure(figsize=(18, 4))\nplt.bar(train_counts.index, train_counts.values, color=\"black\", alpha=0.5, label=\"All\")\nplt.bar(existed_train_counts.index, existed_train_counts.values, color=\"red\", alpha=0.5, label=\"Existed\")\nplt.xticks(rotation=90)\nplt.xlabel(\"Class label (cultivar)\")\nplt.ylabel(\"Frequency\")\nplt.title(\"Number of train images\")\nplt.ylim(0, 350)\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-19T09:57:22.536077Z","iopub.execute_input":"2022-03-19T09:57:22.536462Z","iopub.status.idle":"2022-03-19T09:57:24.904176Z","shell.execute_reply.started":"2022-03-19T09:57:22.536415Z","shell.execute_reply":"2022-03-19T09:57:24.903381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Show examples","metadata":{}},{"cell_type":"code","source":"n_cols = 5\nplt.figure(figsize=(20,100))\n\nshown_cultivars = set()\nplot_index = 1\n\nfor _, row in train_df[train_df.image_existence == True].iterrows():\n    if row.cultivar in shown_cultivars:\n        continue\n    shown_cultivars.add(row.cultivar)\n\n    img_path = os.path.join(TRAIN_IMG_PATH, row.image)\n    img = cv2.imread(img_path)\n    plt.subplot(math.ceil(train_df.cultivar.value_counts().size / n_cols), n_cols,  plot_index)\n    plot_index += 1\n    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\n    plt.title(f\"{row.cultivar}\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-19T09:57:24.905398Z","iopub.execute_input":"2022-03-19T09:57:24.906144Z","iopub.status.idle":"2022-03-19T09:57:56.453422Z","shell.execute_reply.started":"2022-03-19T09:57:24.906104Z","shell.execute_reply":"2022-03-19T09:57:56.451918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Testing data","metadata":{}},{"cell_type":"code","source":"test_df = pd.read_csv(os.path.join(BASE_DIR, \"sample_submission.csv\"))\nprint(f\"Number of testing data: {test_df.shape[0]}\")\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-19T09:57:56.455558Z","iopub.execute_input":"2022-03-19T09:57:56.455873Z","iopub.status.idle":"2022-03-19T09:57:56.496471Z","shell.execute_reply.started":"2022-03-19T09:57:56.455837Z","shell.execute_reply":"2022-03-19T09:57:56.495297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check existence of images\n* All test images exist.","metadata":{}},{"cell_type":"code","source":"test_df[\"image_existence\"] = [os.path.exists(os.path.join(TEST_IMG_PATH, row.filename)) for _, row in test_df.iterrows()]\nprint(f\"Number of non-existent images: {test_df[test_df.image_existence == False].size}\")","metadata":{"execution":{"iopub.status.busy":"2022-03-19T09:57:56.498072Z","iopub.execute_input":"2022-03-19T09:57:56.498696Z","iopub.status.idle":"2022-03-19T09:58:15.96098Z","shell.execute_reply.started":"2022-03-19T09:57:56.498644Z","shell.execute_reply":"2022-03-19T09:58:15.960309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Show examples","metadata":{}},{"cell_type":"code","source":"n_cols = 5\nplot_index = 1\nn_show_images = 100\n\nplt.figure(figsize=(20,100))\nfor _, row in test_df.iloc[:n_show_images,:].iterrows():\n    img_path = os.path.join(TEST_IMG_PATH, row.filename)\n    img = cv2.imread(img_path)\n    plt.subplot(math.ceil(n_show_images / n_cols), n_cols,  plot_index)\n    plot_index += 1\n    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\n    plt.title(f\"{row.filename}\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-19T09:58:15.962072Z","iopub.execute_input":"2022-03-19T09:58:15.962478Z","iopub.status.idle":"2022-03-19T09:58:48.490845Z","shell.execute_reply.started":"2022-03-19T09:58:15.962446Z","shell.execute_reply":"2022-03-19T09:58:48.489069Z"},"trusted":true},"execution_count":null,"outputs":[]}]}