{"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 pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport librosa\nimport librosa.display\nfrom IPython.display import Audio\nfrom itertools import cycle\nfrom glob import glob\nimport math\nimport soundfile as sf\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-01T12:33:43.268444Z","iopub.execute_input":"2023-04-01T12:33:43.269235Z","iopub.status.idle":"2023-04-01T12:33:44.759943Z","shell.execute_reply.started":"2023-04-01T12:33:43.269191Z","shell.execute_reply":"2023-04-01T12:33:44.757974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:33:44.762953Z","iopub.execute_input":"2023-04-01T12:33:44.763460Z","iopub.status.idle":"2023-04-01T12:33:55.553634Z","shell.execute_reply.started":"2023-04-01T12:33:44.763400Z","shell.execute_reply":"2023-04-01T12:33:55.552176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## For visualizations\nsns.set_theme(style=\"white\", palette=None)\ncolor_pal = plt.rcParams[\"axes.prop_cycle\"].by_key()[\"color\"]\ncolor_cycle = cycle(plt.rcParams[\"axes.prop_cycle\"].by_key()[\"color\"])","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:33:55.555037Z","iopub.execute_input":"2023-04-01T12:33:55.555788Z","iopub.status.idle":"2023-04-01T12:33:55.563411Z","shell.execute_reply.started":"2023-04-01T12:33:55.555736Z","shell.execute_reply":"2023-04-01T12:33:55.562173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Helper Functions","metadata":{}},{"cell_type":"code","source":"def audio_read(a_f):\n    audio , sr = librosa.load(train_audio_dir +'/'+ a_f , sr = 32000)\n    return Audio(audio , rate = sr)\n\ndef audio_load(a_f):\n    audio , sr = librosa.load(train_audio_dir +'/'+ a_f , sr = 32000)\n    return (audio , sr)\n\ndef audio_display(a_f):\n    \n    y, sr = audio_load(a_f)\n    pd.Series(y).plot(figsize=(15, 5),\n                  lw=1,\n                  title=a_f,\n                  color=color_pal[0],\n                  alpha = 0.5)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:33:55.565906Z","iopub.execute_input":"2023-04-01T12:33:55.566351Z","iopub.status.idle":"2023-04-01T12:33:55.578780Z","shell.execute_reply.started":"2023-04-01T12:33:55.566308Z","shell.execute_reply":"2023-04-01T12:33:55.577611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Overlook","metadata":{}},{"cell_type":"code","source":"train_meta = pd.read_csv('/kaggle/input/birdclef-2023/train_metadata.csv')\nbird_taxo = pd.read_csv('/kaggle/input/birdclef-2023/eBird_Taxonomy_v2021.csv')\ntrain_audio_dir = '/kaggle/input/birdclef-2023/train_audio'","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:33:55.579961Z","iopub.execute_input":"2023-04-01T12:33:55.580989Z","iopub.status.idle":"2023-04-01T12:33:55.824355Z","shell.execute_reply.started":"2023-04-01T12:33:55.580943Z","shell.execute_reply":"2023-04-01T12:33:55.823126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:33:55.826188Z","iopub.execute_input":"2023-04-01T12:33:55.826571Z","iopub.status.idle":"2023-04-01T12:33:55.867391Z","shell.execute_reply.started":"2023-04-01T12:33:55.826533Z","shell.execute_reply":"2023-04-01T12:33:55.866192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bird_taxo.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:33:55.868875Z","iopub.execute_input":"2023-04-01T12:33:55.869665Z","iopub.status.idle":"2023-04-01T12:33:55.886475Z","shell.execute_reply.started":"2023-04-01T12:33:55.869620Z","shell.execute_reply":"2023-04-01T12:33:55.884906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta.info()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:33:55.887995Z","iopub.execute_input":"2023-04-01T12:33:55.888352Z","iopub.status.idle":"2023-04-01T12:33:55.926867Z","shell.execute_reply.started":"2023-04-01T12:33:55.888319Z","shell.execute_reply":"2023-04-01T12:33:55.925439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bird_taxo.info()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:33:55.928432Z","iopub.execute_input":"2023-04-01T12:33:55.928800Z","iopub.status.idle":"2023-04-01T12:33:55.951367Z","shell.execute_reply.started":"2023-04-01T12:33:55.928763Z","shell.execute_reply":"2023-04-01T12:33:55.950342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"for i in train_meta.columns:\n    print(i , train_meta[i].nunique())\n    ","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:33:55.955820Z","iopub.execute_input":"2023-04-01T12:33:55.956203Z","iopub.status.idle":"2023-04-01T12:33:55.986457Z","shell.execute_reply.started":"2023-04-01T12:33:55.956168Z","shell.execute_reply":"2023-04-01T12:33:55.985006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in bird_taxo.columns:\n    print(i , bird_taxo[i].nunique())","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:33:55.987860Z","iopub.execute_input":"2023-04-01T12:33:55.988327Z","iopub.status.idle":"2023-04-01T12:33:56.018814Z","shell.execute_reply.started":"2023-04-01T12:33:55.988279Z","shell.execute_reply":"2023-04-01T12:33:56.017493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We only need the classification type of the bird that is the label in the train_meta and the filename to be able to train our model so we will simply extract them out as the train_df.","metadata":{}},{"cell_type":"code","source":"train_df = train_meta[['primary_label' , 'filename']]\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:33:56.020222Z","iopub.execute_input":"2023-04-01T12:33:56.020621Z","iopub.status.idle":"2023-04-01T12:33:56.032634Z","shell.execute_reply.started":"2023-04-01T12:33:56.020587Z","shell.execute_reply":"2023-04-01T12:33:56.031219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Extract the length of each file","metadata":{}},{"cell_type":"code","source":"train_df['length'] = 0\nfor i in range(len(train_df.filename)):\n    length = len(audio_load(train_df.filename[i])[0]) // 32000\n    train_df['length'][i] = length\n    \ntrain_df.to_csv(\"train_df_clip_time.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:33:56.034325Z","iopub.execute_input":"2023-04-01T12:33:56.034684Z","iopub.status.idle":"2023-04-01T12:49:35.792441Z","shell.execute_reply.started":"2023-04-01T12:33:56.034641Z","shell.execute_reply":"2023-04-01T12:49:35.790763Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df = pd.read_csv('/kaggle/input/train-df-clip-time/train_df_clip_time.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:49:35.794782Z","iopub.execute_input":"2023-04-01T12:49:35.795217Z","iopub.status.idle":"2023-04-01T12:49:35.808664Z","shell.execute_reply.started":"2023-04-01T12:49:35.795157Z","shell.execute_reply":"2023-04-01T12:49:35.807329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.sort_values( by='length',ascending = False)[:5].plot(x = 'primary_label',y='length',kind = 'barh')","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:49:35.810325Z","iopub.execute_input":"2023-04-01T12:49:35.810674Z","iopub.status.idle":"2023-04-01T12:49:36.198681Z","shell.execute_reply.started":"2023-04-01T12:49:35.810642Z","shell.execute_reply":"2023-04-01T12:49:36.197033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.sort_values( by='length',ascending = True)[:10].plot(x = 'primary_label',y='length',kind = 'barh')","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:49:36.201823Z","iopub.execute_input":"2023-04-01T12:49:36.202410Z","iopub.status.idle":"2023-04-01T12:49:36.532604Z","shell.execute_reply.started":"2023-04-01T12:49:36.202358Z","shell.execute_reply":"2023-04-01T12:49:36.531192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The interesting things that we can find is that some of the clips have even less than a sec length making them more a form of noise rather than a form of training data. Additionally, some of the clips are quite large upto around 2000sec which would translate to around 33 mins.","metadata":{}},{"cell_type":"markdown","source":"## Number of clips available for different birds\n\nWe can see that we have a highly imbalanced dataset so data augmentation of some of the birds might be a good idea. Another imporatant aspect to keep in mind is that some birds only have 1 instance recorded so they must be kept in the training fold only.","metadata":{}},{"cell_type":"code","source":"train_df['primary_label'].value_counts().sort_values(ascending = False)[:10].plot(kind = 'barh' , figsize = (10,15))","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:49:36.535132Z","iopub.execute_input":"2023-04-01T12:49:36.536300Z","iopub.status.idle":"2023-04-01T12:49:36.905691Z","shell.execute_reply.started":"2023-04-01T12:49:36.536237Z","shell.execute_reply":"2023-04-01T12:49:36.904344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Extracting a few samples from the training df","metadata":{}},{"cell_type":"code","source":"## extracting the first row of each group of birds available to see some of the trend in the frq \n## and amplitude\ntrain_compact = train_df.groupby('primary_label').first().reset_index()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:49:36.907115Z","iopub.execute_input":"2023-04-01T12:49:36.907467Z","iopub.status.idle":"2023-04-01T12:49:36.922420Z","shell.execute_reply.started":"2023-04-01T12:49:36.907432Z","shell.execute_reply":"2023-04-01T12:49:36.921086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(train_compact['filename'][:2])):\n  ## y is a 2-D numpy array of audio signal and sr is the sample rate i.e. the samples taken per second which is virtually the sound quality\n  y, sr = librosa.load(train_audio_dir +'/'+ train_compact.filename[i] , sr = 32000)\n  pd.Series(y).plot(figsize=(15, 5),\n                  lw=1,\n                  title=f'Bird Name:{train_compact.primary_label[i]}',\n                  color=color_pal[0],\n                  alpha = 0.5)\n  plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T12:49:36.924578Z","iopub.execute_input":"2023-04-01T12:49:36.925555Z","iopub.status.idle":"2023-04-01T12:49:38.290081Z","shell.execute_reply.started":"2023-04-01T12:49:36.925504Z","shell.execute_reply":"2023-04-01T12:49:38.288888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can see from the above graphs that a lot of variation is there in the pattern of the bird sounds that we have. Moreover, there are multiple calls that are being made by any singular bird. We can also see that the length of the various clips are different so we need to cater for that as well when we design the model and cut it into clips before diving to model design..\n","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}