{"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":"markdown","source":"# Intro\nWelcome to the [Herbarium 2022 - FGVC9](https://www.kaggle.com/c/herbarium-2022-fgvc9//) compedtition.\n\n![](https://storage.googleapis.com/kaggle-competitions/kaggle/33679/logos/header.png)\n\n<font size=\"4\"><span style=\"color: royalblue;\">Please vote the notebook up if it helps you. Feel free to leave a comment above the notebook. Thank you. </span></font>","metadata":{}},{"cell_type":"markdown","source":"# Libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport json","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:08:12.062009Z","iopub.execute_input":"2022-02-15T07:08:12.062373Z","iopub.status.idle":"2022-02-15T07:08:12.425416Z","shell.execute_reply.started":"2022-02-15T07:08:12.062282Z","shell.execute_reply":"2022-02-15T07:08:12.424552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Path","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:08:23.431547Z","iopub.execute_input":"2022-02-15T07:08:23.431856Z","iopub.status.idle":"2022-02-15T07:08:23.436297Z"}}},{"cell_type":"code","source":"path = '/kaggle/input/herbarium-2022-fgvc9/'\nos.listdir(path)","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:08:51.677608Z","iopub.execute_input":"2022-02-15T07:08:51.678031Z","iopub.status.idle":"2022-02-15T07:08:51.685963Z","shell.execute_reply.started":"2022-02-15T07:08:51.678Z","shell.execute_reply":"2022-02-15T07:08:51.685146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"code","source":"samp_subm = pd.read_csv(path+'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:09:26.616228Z","iopub.execute_input":"2022-02-15T07:09:26.616483Z","iopub.status.idle":"2022-02-15T07:09:26.713914Z","shell.execute_reply.started":"2022-02-15T07:09:26.616458Z","shell.execute_reply":"2022-02-15T07:09:26.713162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(path+'train_metadata.json') as f:\n    train_data = json.load(f)\n\nwith open(path+'test_metadata.json') as f:\n    test_data = json.load(f)","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:10:43.361726Z","iopub.execute_input":"2022-02-15T07:10:43.362285Z","iopub.status.idle":"2022-02-15T07:10:59.294596Z","shell.execute_reply.started":"2022-02-15T07:10:43.362251Z","shell.execute_reply":"2022-02-15T07:10:59.293726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Overview","metadata":{}},{"cell_type":"code","source":"print('Number of train samples:', len(train_data['images']))\nprint('Number of submission samples:', len(samp_subm))","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:14:52.650748Z","iopub.execute_input":"2022-02-15T07:14:52.651279Z","iopub.status.idle":"2022-02-15T07:14:52.656421Z","shell.execute_reply.started":"2022-02-15T07:14:52.651243Z","shell.execute_reply":"2022-02-15T07:14:52.655442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For each image Id, we should predict the corresponding image label (category_id) in the Predicted column.","metadata":{}},{"cell_type":"code","source":"samp_subm.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:31:19.710607Z","iopub.execute_input":"2022-02-15T07:31:19.710894Z","iopub.status.idle":"2022-02-15T07:31:19.719433Z","shell.execute_reply.started":"2022-02-15T07:31:19.710864Z","shell.execute_reply":"2022-02-15T07:31:19.718844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"markdown","source":"## Focus Train Data Structure\nThere are some metadata:","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:15:39.155777Z","iopub.execute_input":"2022-02-15T07:15:39.156239Z"}}},{"cell_type":"code","source":"train_data.keys()","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:17:03.534282Z","iopub.execute_input":"2022-02-15T07:17:03.534694Z","iopub.status.idle":"2022-02-15T07:17:03.539403Z","shell.execute_reply.started":"2022-02-15T07:17:03.534666Z","shell.execute_reply":"2022-02-15T07:17:03.53875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['annotations'][0]","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:15:50.085214Z","iopub.execute_input":"2022-02-15T07:15:50.085639Z","iopub.status.idle":"2022-02-15T07:15:50.091349Z","shell.execute_reply.started":"2022-02-15T07:15:50.08561Z","shell.execute_reply":"2022-02-15T07:15:50.090742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['images'][0]","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:21:30.413772Z","iopub.execute_input":"2022-02-15T07:21:30.414078Z","iopub.status.idle":"2022-02-15T07:21:30.419564Z","shell.execute_reply.started":"2022-02-15T07:21:30.414046Z","shell.execute_reply":"2022-02-15T07:21:30.418933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['categories'][0]","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:16:00.217858Z","iopub.execute_input":"2022-02-15T07:16:00.218317Z","iopub.status.idle":"2022-02-15T07:16:00.223342Z","shell.execute_reply.started":"2022-02-15T07:16:00.218285Z","shell.execute_reply":"2022-02-15T07:16:00.222795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['genera'][0]","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:17:23.102123Z","iopub.execute_input":"2022-02-15T07:17:23.102441Z","iopub.status.idle":"2022-02-15T07:17:23.108105Z","shell.execute_reply.started":"2022-02-15T07:17:23.102401Z","shell.execute_reply":"2022-02-15T07:17:23.107221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['institutions'][0]","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:17:38.488496Z","iopub.execute_input":"2022-02-15T07:17:38.488824Z","iopub.status.idle":"2022-02-15T07:17:38.494917Z","shell.execute_reply.started":"2022-02-15T07:17:38.488776Z","shell.execute_reply":"2022-02-15T07:17:38.494018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['distances'][0]","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:17:50.314778Z","iopub.execute_input":"2022-02-15T07:17:50.315063Z","iopub.status.idle":"2022-02-15T07:17:50.320702Z","shell.execute_reply.started":"2022-02-15T07:17:50.315037Z","shell.execute_reply":"2022-02-15T07:17:50.319838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['license'][0]","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:18:12.218157Z","iopub.execute_input":"2022-02-15T07:18:12.218448Z","iopub.status.idle":"2022-02-15T07:18:12.224644Z","shell.execute_reply.started":"2022-02-15T07:18:12.218416Z","shell.execute_reply":"2022-02-15T07:18:12.223667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Focus Test Data Structure\nThere are some metadata: \"image_id\", \"file_name\", \"licenses\".","metadata":{}},{"cell_type":"code","source":"test_data[0]","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:20:31.865087Z","iopub.execute_input":"2022-02-15T07:20:31.865415Z","iopub.status.idle":"2022-02-15T07:20:31.871374Z","shell.execute_reply.started":"2022-02-15T07:20:31.865382Z","shell.execute_reply":"2022-02-15T07:20:31.870643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plot An Examples\nWe focus on the first example of the train data set:","metadata":{}},{"cell_type":"code","source":"row = 0\nprint('image:', train_data['images'][row])\nprint('category:', train_data['categories'][row])","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:24:45.503124Z","iopub.execute_input":"2022-02-15T07:24:45.503431Z","iopub.status.idle":"2022-02-15T07:24:45.50924Z","shell.execute_reply.started":"2022-02-15T07:24:45.503403Z","shell.execute_reply":"2022-02-15T07:24:45.508233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_name = train_data['images'][row]['file_name']\nspecies = train_data['categories'][row]['species']","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:25:42.977891Z","iopub.execute_input":"2022-02-15T07:25:42.978202Z","iopub.status.idle":"2022-02-15T07:25:42.983348Z","shell.execute_reply.started":"2022-02-15T07:25:42.978162Z","shell.execute_reply":"2022-02-15T07:25:42.982123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread(path+'train_images/'+file_name)\nprint('Shape:', img.shape)","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:26:14.284557Z","iopub.execute_input":"2022-02-15T07:26:14.285052Z","iopub.status.idle":"2022-02-15T07:26:14.338321Z","shell.execute_reply.started":"2022-02-15T07:26:14.285001Z","shell.execute_reply":"2022-02-15T07:26:14.337606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(7, 7))\nax.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\nax.set_xticklabels([])\nax.set_yticklabels([])\nax.set_title(species)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:26:29.266872Z","iopub.execute_input":"2022-02-15T07:26:29.267739Z","iopub.status.idle":"2022-02-15T07:26:29.645892Z","shell.execute_reply.started":"2022-02-15T07:26:29.267701Z","shell.execute_reply":"2022-02-15T07:26:29.645181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plot Some Examples\nWe define a simple plot function to plot some examples of the train images:","metadata":{}},{"cell_type":"code","source":"def plot_examples():\n    fig, axs = plt.subplots(4, 4, figsize=(20, 20))\n    fig.subplots_adjust(hspace = .1, wspace=.1)\n    \n    axs = axs.ravel()\n    for i in range(16):\n        img = cv2.imread(path+'train_images/'+train_data['images'][i]['file_name'])\n        axs[i].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\n        axs[i].set_title(train_data['categories'][i]['species'])\n        axs[i].set_xticklabels([])\n        axs[i].set_yticklabels([])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:27:32.769351Z","iopub.execute_input":"2022-02-15T07:27:32.77013Z","iopub.status.idle":"2022-02-15T07:27:32.776971Z","shell.execute_reply.started":"2022-02-15T07:27:32.770086Z","shell.execute_reply":"2022-02-15T07:27:32.776081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_examples()","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:27:37.925255Z","iopub.execute_input":"2022-02-15T07:27:37.925554Z","iopub.status.idle":"2022-02-15T07:27:41.33479Z","shell.execute_reply.started":"2022-02-15T07:27:37.925519Z","shell.execute_reply":"2022-02-15T07:27:41.333857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Categories","metadata":{}},{"cell_type":"code","source":"df_train_image = pd.json_normalize(train_data['images'])\ndf_train_anno = pd.json_normalize(train_data['annotations'])","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:35:02.663195Z","iopub.execute_input":"2022-02-15T07:35:02.663526Z","iopub.status.idle":"2022-02-15T07:35:09.666998Z","shell.execute_reply.started":"2022-02-15T07:35:02.663491Z","shell.execute_reply":"2022-02-15T07:35:09.666117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_anno['category_id'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:35:29.558108Z","iopub.execute_input":"2022-02-15T07:35:29.558399Z","iopub.status.idle":"2022-02-15T07:35:29.581083Z","shell.execute_reply.started":"2022-02-15T07:35:29.558368Z","shell.execute_reply":"2022-02-15T07:35:29.580248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Export Data","metadata":{}},{"cell_type":"code","source":"samp_subm['Predicted'] = 2774","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:36:27.398322Z","iopub.execute_input":"2022-02-15T07:36:27.398901Z","iopub.status.idle":"2022-02-15T07:36:27.405743Z","shell.execute_reply.started":"2022-02-15T07:36:27.398865Z","shell.execute_reply":"2022-02-15T07:36:27.404785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samp_subm.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-15T07:09:42.690642Z","iopub.execute_input":"2022-02-15T07:09:42.690932Z","iopub.status.idle":"2022-02-15T07:09:42.910341Z","shell.execute_reply.started":"2022-02-15T07:09:42.690904Z","shell.execute_reply":"2022-02-15T07:09:42.909393Z"},"trusted":true},"execution_count":null,"outputs":[]}]}