{"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":"# BirdCLEF 2022\n## Identify bird calls in soundscapes","metadata":{}},{"cell_type":"markdown","source":"![](https://nas-national-prod.s3.amazonaws.com/styles/article_hero_inline/s3/web_aud_apa-2018_iiwi_a1-7039-1_ts_photo-justin-peter.jpg?itok=0CUwtcxL)\n#### > Challenge in this competition is to identify which birds are calling in long recordings given quite limited training data. This is the exact challenge faced by scientists trying to monitor rare birds in Hawaii. For example, there are only a few thousand individual Nene geese left in the world, which makes it difficult to acquire recordings of their calls.","metadata":{}},{"cell_type":"markdown","source":"# Files\n\n#### train_metadata.csv - A wide range of metadata is provided for the training data. The most directly relevant fields are:\n \n* primary_label - a code for the bird species. You can review detailed information about the bird codes by appending the code to https://ebird.org/species/, such as https://ebird.org/species/amecro for the American Crow.\n* secondary_labels: Background species as annotated by the recordist. An empty list does not mean that no background birds are audible.\n* author - the eBird user who provided the recording.\n* filename: the associated audio file.\n* rating: Float value between 0.0 and 5.0 as an indicator of the quality rating on Xeno-canto and the number of background species, where 5.0 is the highest and 1.0 is the lowest. 0.0 means that this recording has no user rating yet.\n* train_audio/ - The bulk of the training data consists of short recordings of individual bird calls generously uploaded by users of xenocanto.org. These files have been downsampled to 32 kHz where applicable to match the test set audio and converted to the ogg format.\n\n#### test_soundscapes/ - When you submit a notebook, the test_soundscapes directory will be populated with approximately 5,500 recordings to be used for scoring. These are each within a few milliseconds of 1 minute long and in the ogg audio format. Only one soundscape is available for download.\n\n\n#### test.csv - Metadata for the test set. Only the first three rows are available for download; the full test.csv is provided in the hidden test set.\n\n* row_id - A unique identifier for the row.\n* file_id - A unique identifier for the audio file.\n* bird - The ebird code for the row. There is one row for each of the scored species per 5 second window per audio file.\n* end_time - The last second of the 5 second time window (5, 10, 15, etc).\n\n\n#### sample_submission.csv - A valid sample submission. Only the first three rows are available for download; the full submission.csv is provided in the hidden test set.\n\n* row_id - A unique identifier for the row.\n* target - True/False for whether or not the bird in question called during the 5 second window.\n* scored_birds.json - The subset of the species in the dataset that are scored.\n\n\n####  eBird_Taxonomy_v2021.csv - Data on the relationships between different species.","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport matplotlib.pylab as plt\nimport seaborn as sns\nimport plotly.express as px\n\n# For exploring audio files\nimport librosa\nimport librosa.display\nimport IPython.display as ipd\n\nsns.set_theme(style=\"white\", palette=None)\ncolor_pal = plt.rcParams[\"axes.prop_cycle\"].by_key()[\"color\"]\n\nfrom itertools import cycle\n\ncolor_cycle = cycle(plt.rcParams[\"axes.prop_cycle\"].by_key()[\"color\"])","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:15.466554Z","iopub.execute_input":"2022-02-16T11:02:15.467838Z","iopub.status.idle":"2022-02-16T11:02:17.711332Z","shell.execute_reply.started":"2022-02-16T11:02:15.467522Z","shell.execute_reply":"2022-02-16T11:02:17.710008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read in the CSV files.\nBASE_DIR = '../input/birdclef-2022/'\ntrain = pd.read_csv(f'{BASE_DIR}/train_metadata.csv')\ntest = pd.read_csv(f'{BASE_DIR}/test.csv')\nebird = pd.read_csv(f'{BASE_DIR}/eBird_Taxonomy_v2021.csv')\nss = pd.read_csv(f'{BASE_DIR}/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:17.714242Z","iopub.execute_input":"2022-02-16T11:02:17.715224Z","iopub.status.idle":"2022-02-16T11:02:17.889360Z","shell.execute_reply.started":"2022-02-16T11:02:17.715159Z","shell.execute_reply":"2022-02-16T11:02:17.888408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Exploration","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:17.891112Z","iopub.execute_input":"2022-02-16T11:02:17.891826Z","iopub.status.idle":"2022-02-16T11:02:17.916501Z","shell.execute_reply.started":"2022-02-16T11:02:17.891782Z","shell.execute_reply":"2022-02-16T11:02:17.915623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:17.918965Z","iopub.execute_input":"2022-02-16T11:02:17.919750Z","iopub.status.idle":"2022-02-16T11:02:17.956715Z","shell.execute_reply.started":"2022-02-16T11:02:17.919690Z","shell.execute_reply":"2022-02-16T11:02:17.955745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:17.958158Z","iopub.execute_input":"2022-02-16T11:02:17.958621Z","iopub.status.idle":"2022-02-16T11:02:17.969322Z","shell.execute_reply.started":"2022-02-16T11:02:17.958586Z","shell.execute_reply":"2022-02-16T11:02:17.968719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:17.970282Z","iopub.execute_input":"2022-02-16T11:02:17.970939Z","iopub.status.idle":"2022-02-16T11:02:17.988347Z","shell.execute_reply.started":"2022-02-16T11:02:17.970902Z","shell.execute_reply":"2022-02-16T11:02:17.987689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ebird.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:17.989592Z","iopub.execute_input":"2022-02-16T11:02:17.990201Z","iopub.status.idle":"2022-02-16T11:02:18.005623Z","shell.execute_reply.started":"2022-02-16T11:02:17.990169Z","shell.execute_reply":"2022-02-16T11:02:18.004615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ebird.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:18.007321Z","iopub.execute_input":"2022-02-16T11:02:18.007639Z","iopub.status.idle":"2022-02-16T11:02:18.037528Z","shell.execute_reply.started":"2022-02-16T11:02:18.007597Z","shell.execute_reply":"2022-02-16T11:02:18.036661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:18.038844Z","iopub.execute_input":"2022-02-16T11:02:18.039264Z","iopub.status.idle":"2022-02-16T11:02:18.049373Z","shell.execute_reply.started":"2022-02-16T11:02:18.039231Z","shell.execute_reply":"2022-02-16T11:02:18.048348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'There are {train.primary_label.nunique()} bird species:\\n{train.primary_label.unique()}')","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:18.052021Z","iopub.execute_input":"2022-02-16T11:02:18.052474Z","iopub.status.idle":"2022-02-16T11:02:18.065264Z","shell.execute_reply.started":"2022-02-16T11:02:18.052392Z","shell.execute_reply":"2022-02-16T11:02:18.064631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"fig, axs = plt.subplots(1, 2, figsize=(16, 6))\n# See the frequency of labels in the training dataset\ntrain[\"common_name\"].value_counts().head(20).plot(\n    kind=\"bar\", ax=axs[0], width=1, color=color_pal[0]\n)\n\naxs[0].set_title(\"Top 20 Birds with Labels\", fontsize=20)\n\n# See the frequency of labels in the training dataset\nax = (\n    train[\"common_name\"]\n    .value_counts()\n    .tail(20)\n    .plot(kind=\"bar\", ax=axs[1], width=1, color=color_pal[1])\n)\naxs[1].set_title(\"Bottom 20 Birds with Labels\", fontsize=20)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:18.066473Z","iopub.execute_input":"2022-02-16T11:02:18.067095Z","iopub.status.idle":"2022-02-16T11:02:19.054886Z","shell.execute_reply.started":"2022-02-16T11:02:18.067063Z","shell.execute_reply":"2022-02-16T11:02:19.053931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* We see that there are varying counts of examples for each bird type.\n* Some birds have 500 labels while others have less than 10","metadata":{}},{"cell_type":"code","source":"# Top 50 birds with labels\npie, ax = plt.subplots(figsize=[20,15])\ntrain[\"common_name\"].value_counts().head(50).plot(kind='pie',\n#                                                     autopct='%.2f',\n                                                    ax=ax,\n                                                    title='Spacies distibution',\n                                                    rotatelabels =True,\n                                                    cmap = 'hot')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:19.056303Z","iopub.execute_input":"2022-02-16T11:02:19.057121Z","iopub.status.idle":"2022-02-16T11:02:19.817668Z","shell.execute_reply.started":"2022-02-16T11:02:19.057072Z","shell.execute_reply":"2022-02-16T11:02:19.813012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.scatter_geo(\n    train,\n    lat=\"latitude\",\n    lon=\"longitude\",\n    color=\"common_name\",\n    width=1_000,\n    height=500,\n    title=\"BirdCLEF 2022 Training Data\",\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:19.819060Z","iopub.execute_input":"2022-02-16T11:02:19.819560Z","iopub.status.idle":"2022-02-16T11:02:20.887646Z","shell.execute_reply.started":"2022-02-16T11:02:19.819517Z","shell.execute_reply":"2022-02-16T11:02:20.886879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(10, 5))\n# See the frequency of labels in the training dataset\ntrain[\"author\"].value_counts().head(50).plot(\n    kind=\"bar\", ax=ax, width=1, color=color_pal[2]\n)\n\nax.set_title(\"Top 50 Authors\", fontsize=20)","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:20.889044Z","iopub.execute_input":"2022-02-16T11:02:20.889811Z","iopub.status.idle":"2022-02-16T11:02:23.054310Z","shell.execute_reply.started":"2022-02-16T11:02:20.889762Z","shell.execute_reply":"2022-02-16T11:02:23.053429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Listen to the audio for the first training example\nfn = train[\"filename\"].values[1]\nipd.Audio(f\"{BASE_DIR}train_audio/{fn}\")","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:23.056044Z","iopub.execute_input":"2022-02-16T11:02:23.056576Z","iopub.status.idle":"2022-02-16T11:02:23.074803Z","shell.execute_reply.started":"2022-02-16T11:02:23.056532Z","shell.execute_reply":"2022-02-16T11:02:23.073974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Load in the audio file as a numpy array","metadata":{}},{"cell_type":"code","source":"y, sr = librosa.load(f\"{BASE_DIR}train_audio/{fn}\")\nprint(f\"Numpy array of the audio loaded of shape {y.shape} and sample rate {sr}\")","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:23.076111Z","iopub.execute_input":"2022-02-16T11:02:23.077112Z","iopub.status.idle":"2022-02-16T11:02:25.406265Z","shell.execute_reply.started":"2022-02-16T11:02:23.077068Z","shell.execute_reply":"2022-02-16T11:02:25.405182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Plotting 10 Random Audio Files from the training dataset\n","metadata":{}},{"cell_type":"code","source":"# Plot The Audio File\ndef plot_raw_audio(filename, birdtype, color):\n    y, sr = librosa.load(f\"{BASE_DIR}train_audio/{filename}\")\n    ax = pd.DataFrame(y).plot(\n        figsize=(10, 3), title=f\"{birdtype} Raw Audio\", lw=0.1, color=color\n    )\n    plt.legend().remove()\n    plt.show()\n\n\nfor i, d in train.sample(10, random_state=529).iterrows():\n    plot_raw_audio(d[\"filename\"], d[\"common_name\"], next(color_cycle))","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:25.407849Z","iopub.execute_input":"2022-02-16T11:02:25.408350Z","iopub.status.idle":"2022-02-16T11:02:35.132109Z","shell.execute_reply.started":"2022-02-16T11:02:25.408304Z","shell.execute_reply":"2022-02-16T11:02:35.131165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Creating Spectograms of Birds","metadata":{}},{"cell_type":"code","source":"def plot_audio_melspec(filename, birdtype):\n    y, sr = librosa.load(f\"{BASE_DIR}train_audio/{filename}\")\n    S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128, fmax=8000)\n\n    fig, ax = plt.subplots(figsize=(10, 3))\n    S_dB = librosa.power_to_db(S, ref=np.max)\n    img = librosa.display.specshow(\n        S_dB, x_axis=\"time\", y_axis=\"mel\", sr=sr, fmax=8000, ax=ax\n    )\n    fig.colorbar(img, ax=ax, format=\"%+2.0f dB\")\n    ax.set(title=f\"Mel-frequency for bird {birdtype}\")\n    plt.show()\n\n\nfor i, d in train.sample(10, random_state=529).iterrows():\n    plot_audio_melspec(d[\"filename\"], d[\"common_name\"])","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:35.133682Z","iopub.execute_input":"2022-02-16T11:02:35.134113Z","iopub.status.idle":"2022-02-16T11:02:45.653606Z","shell.execute_reply.started":"2022-02-16T11:02:35.134065Z","shell.execute_reply":"2022-02-16T11:02:45.652627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv('../input/birdclef-2022/sample_submission.csv')\nsubmission['target'] = True\nsubmission.to_csv('submission.csv', index=False)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-16T11:02:45.654793Z","iopub.execute_input":"2022-02-16T11:02:45.655030Z","iopub.status.idle":"2022-02-16T11:02:45.672373Z","shell.execute_reply.started":"2022-02-16T11:02:45.655001Z","shell.execute_reply":"2022-02-16T11:02:45.671510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> Work in progress on More EDAs,Data Exploration and Better Submission","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}