{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":19596,"databundleVersionId":1292430,"sourceType":"competition"},{"sourceId":1262046,"sourceType":"datasetVersion","datasetId":726424},{"sourceId":1264575,"sourceType":"datasetVersion","datasetId":725893}],"dockerImageVersionId":30635,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-14T14:09:30.157333Z","iopub.execute_input":"2024-01-14T14:09:30.157827Z","iopub.status.idle":"2024-01-14T14:09:30.703578Z","shell.execute_reply.started":"2024-01-14T14:09:30.157764Z","shell.execute_reply":"2024-01-14T14:09:30.699302Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**DOING EDA**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport librosa\nfrom sklearn.preprocessing import LabelEncoder\nimport librosa.display\nimport soundfile as sf \nimport IPython.display as ipd \nfrom matplotlib.animation import FuncAnimation","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:18:09.010860Z","iopub.execute_input":"2024-01-14T14:18:09.012400Z","iopub.status.idle":"2024-01-14T14:18:09.021530Z","shell.execute_reply.started":"2024-01-14T14:18:09.012290Z","shell.execute_reply":"2024-01-14T14:18:09.020013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/birdsong-recognition/train.csv')\npd.set_option('display.max_column' , None)\npd.set_option('display.max_rows', None)  \npd.set_option('display.max_columns', None)\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:18:11.984050Z","iopub.execute_input":"2024-01-14T14:18:11.984525Z","iopub.status.idle":"2024-01-14T14:18:12.432679Z","shell.execute_reply.started":"2024-01-14T14:18:11.984490Z","shell.execute_reply":"2024-01-14T14:18:12.431085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:18:16.427949Z","iopub.execute_input":"2024-01-14T14:18:16.428409Z","iopub.status.idle":"2024-01-14T14:18:16.532799Z","shell.execute_reply.started":"2024-01-14T14:18:16.428374Z","shell.execute_reply":"2024-01-14T14:18:16.531547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null_columns = data.columns[data.isnull().any()]\nnull_columns","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:18:20.366674Z","iopub.execute_input":"2024-01-14T14:18:20.367854Z","iopub.status.idle":"2024-01-14T14:18:20.450738Z","shell.execute_reply.started":"2024-01-14T14:18:20.367798Z","shell.execute_reply":"2024-01-14T14:18:20.449518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['rating'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:18:23.643886Z","iopub.execute_input":"2024-01-14T14:18:23.644466Z","iopub.status.idle":"2024-01-14T14:18:23.653599Z","shell.execute_reply.started":"2024-01-14T14:18:23.644417Z","shell.execute_reply":"2024-01-14T14:18:23.652600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['playback_used'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:18:26.058800Z","iopub.execute_input":"2024-01-14T14:18:26.059567Z","iopub.status.idle":"2024-01-14T14:18:26.070281Z","shell.execute_reply.started":"2024-01-14T14:18:26.059527Z","shell.execute_reply":"2024-01-14T14:18:26.068504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['playback_used'].value_counts(dropna=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:18:36.071706Z","iopub.execute_input":"2024-01-14T14:18:36.072207Z","iopub.status.idle":"2024-01-14T14:18:36.085291Z","shell.execute_reply.started":"2024-01-14T14:18:36.072172Z","shell.execute_reply":"2024-01-14T14:18:36.083750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['playback_used'].fillna('no' , inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:19:10.355434Z","iopub.execute_input":"2024-01-14T14:19:10.356009Z","iopub.status.idle":"2024-01-14T14:19:10.365984Z","shell.execute_reply.started":"2024-01-14T14:19:10.355955Z","shell.execute_reply":"2024-01-14T14:19:10.364588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['ebird_code'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:19:19.261562Z","iopub.execute_input":"2024-01-14T14:19:19.262065Z","iopub.status.idle":"2024-01-14T14:19:19.272557Z","shell.execute_reply.started":"2024-01-14T14:19:19.262029Z","shell.execute_reply":"2024-01-14T14:19:19.271209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['channels'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:19:22.209354Z","iopub.execute_input":"2024-01-14T14:19:22.210027Z","iopub.status.idle":"2024-01-14T14:19:22.221291Z","shell.execute_reply.started":"2024-01-14T14:19:22.209983Z","shell.execute_reply":"2024-01-14T14:19:22.219739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"channel_counts = data['channels'].value_counts()\n# Plot a bar graph\nplt.bar(channel_counts.index, channel_counts.values)\n\n# Set plot labels and title\nplt.xlabel('Channels')\nplt.ylabel('Count')\nplt.title('Channel Distribution')\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:19:24.943109Z","iopub.execute_input":"2024-01-14T14:19:24.944077Z","iopub.status.idle":"2024-01-14T14:19:25.243562Z","shell.execute_reply.started":"2024-01-14T14:19:24.944033Z","shell.execute_reply":"2024-01-14T14:19:25.242570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(style=\"whitegrid\")  # Set the background style\n\n# Define a custom color palette\ncustom_palette = sns.color_palette(\"viridis\", n_colors=len(data['rating'].unique()))\n\nsns.countplot(x='rating', data=data, order=data['rating'].value_counts().index, palette=custom_palette)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:19:46.123389Z","iopub.execute_input":"2024-01-14T14:19:46.123996Z","iopub.status.idle":"2024-01-14T14:19:46.590231Z","shell.execute_reply.started":"2024-01-14T14:19:46.123953Z","shell.execute_reply":"2024-01-14T14:19:46.588675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['channels'] = data['channels'].astype(str).str[0].astype(int)\ndata['channels'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:19:42.969553Z","iopub.execute_input":"2024-01-14T14:19:42.970081Z","iopub.status.idle":"2024-01-14T14:19:43.008473Z","shell.execute_reply.started":"2024-01-14T14:19:42.970041Z","shell.execute_reply":"2024-01-14T14:19:43.007123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['date'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:19:53.824949Z","iopub.execute_input":"2024-01-14T14:19:53.825585Z","iopub.status.idle":"2024-01-14T14:19:53.838198Z","shell.execute_reply.started":"2024-01-14T14:19:53.825533Z","shell.execute_reply":"2024-01-14T14:19:53.836889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['year'] = data['date'].apply(lambda x : x.split('-')[0]).astype(int)\ndata['month'] = data['date'].apply(lambda x : x.split('-')[1]).astype(int)\ndata['day'] = data['date'].apply(lambda x : x.split('-')[2]).astype(int)\ndata = data.drop(['date'] , axis=1)\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:19:56.603017Z","iopub.execute_input":"2024-01-14T14:19:56.603517Z","iopub.status.idle":"2024-01-14T14:19:56.714188Z","shell.execute_reply.started":"2024-01-14T14:19:56.603476Z","shell.execute_reply":"2024-01-14T14:19:56.712692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6,6))\nsns.countplot(x='day', data=data)\n\nplt.xticks(rotation =90)\nplt.title('Count Plot of months')\nplt.xlabel('Year')\nplt.ylabel('Count')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:20:01.037262Z","iopub.execute_input":"2024-01-14T14:20:01.037724Z","iopub.status.idle":"2024-01-14T14:20:02.052936Z","shell.execute_reply.started":"2024-01-14T14:20:01.037690Z","shell.execute_reply":"2024-01-14T14:20:02.050936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nplt.figure(figsize=(6,6))\nsns.countplot(x='pitch', data=data)\n\nplt.xticks(rotation =90)\nplt.title('Plot of Pitch')\nplt.xlabel('pitch')\nplt.ylabel('Count')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:20:06.612908Z","iopub.execute_input":"2024-01-14T14:20:06.613419Z","iopub.status.idle":"2024-01-14T14:20:07.146425Z","shell.execute_reply.started":"2024-01-14T14:20:06.613380Z","shell.execute_reply":"2024-01-14T14:20:07.144958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n","metadata":{}},{"cell_type":"code","source":"train = data.iloc[[0 ,1 , 1147 , 1148 , 4754 , 4755 , 7221 , 7222 , 21088 , 21089]].reset_index(drop=True)\ntrain.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:20:20.691683Z","iopub.execute_input":"2024-01-14T14:20:20.693056Z","iopub.status.idle":"2024-01-14T14:20:20.750437Z","shell.execute_reply.started":"2024-01-14T14:20:20.693004Z","shell.execute_reply":"2024-01-14T14:20:20.748759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Feature Engineering**","metadata":{}},{"cell_type":"code","source":"base_directory = '/kaggle/input/birdsong-recognition/train_audio/'\n\n# Create a new column 'file_location' by combining 'category' and 'file_name'\naudio = base_directory + train['ebird_code'] + '/' + train['filename']\naudio","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:20:25.060141Z","iopub.execute_input":"2024-01-14T14:20:25.060617Z","iopub.status.idle":"2024-01-14T14:20:25.074517Z","shell.execute_reply.started":"2024-01-14T14:20:25.060584Z","shell.execute_reply":"2024-01-14T14:20:25.072840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_list = []\nsr_list = []\n\n# Loop through the 'audio' column and load each audio file\nfor audio_file in audio:\n    y_, sr_ = librosa.load(audio_file)\n    y_list.append(y_)\n    sr_list.append(sr_)\n\n# Add 'y' and 'sr' columns to the DataFrame\ntrain['y'] = y_list\ntrain['sr'] = sr_list","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:20:28.421941Z","iopub.execute_input":"2024-01-14T14:20:28.422458Z","iopub.status.idle":"2024-01-14T14:20:29.520230Z","shell.execute_reply.started":"2024-01-14T14:20:28.422418Z","shell.execute_reply":"2024-01-14T14:20:29.519031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(10):\n    print(f\"Audio {i}: {train.loc[i, 'ebird_code']}\")\n    ipd.display(ipd.Audio(train.loc[i, 'y'], rate=train.loc[i, 'sr']))","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:20:31.915584Z","iopub.execute_input":"2024-01-14T14:20:31.916078Z","iopub.status.idle":"2024-01-14T14:20:32.561174Z","shell.execute_reply.started":"2024-01-14T14:20:31.916042Z","shell.execute_reply":"2024-01-14T14:20:32.560079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['zcr'] = None\n\n# Loop through the DataFrame and calculate zero-crossing rates for each audio file\nfor i in range(len(train)):\n    y = train.loc[i, 'y']\n    zcr = librosa.feature.zero_crossing_rate(y)\n    train.at[i, 'zcr'] = zcr\n\n# Plot Zero Crossing Rate for all audio files\nnum_files_to_plot = 10  # Change this based on the number of files you want to plot\nplt.figure(figsize=(15, 10))\n\nfor i in range(num_files_to_plot):\n    zcr_values = train.loc[i, 'zcr'][0]\n    plt.subplot(4, 3, i+1)  # Assuming you want a 2x5 grid for the first 10 files\n    plt.plot(zcr_values, label='ZCR')\n    plt.title(f'ZCR - {train.loc[i, \"ebird_code\"]}')\n    plt.legend()\n    plt.xlabel('Frame Index')\n    plt.ylabel('Zero Crossing Rate')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:20:48.451234Z","iopub.execute_input":"2024-01-14T14:20:48.451730Z","iopub.status.idle":"2024-01-14T14:20:52.711974Z","shell.execute_reply.started":"2024-01-14T14:20:48.451694Z","shell.execute_reply":"2024-01-14T14:20:52.710848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['rms_energy'] = None\n\nfor i in range(len(train)):\n    y = train.loc[i, 'y']\n    rms_energy = librosa.feature.rms(y=y)\n    train.at[i, 'rms_energy'] = rms_energy\n\n# Plot RMS Energy for all audio files\nnum_files_to_plot = 10  # Change this based on the number of files you want to plot\nplt.figure(figsize=(15, 10))\n\nfor i in range(num_files_to_plot):\n    rms_energy_values = train.loc[i, 'rms_energy'][0]\n    plt.subplot(4, 3, i+1)  # Assuming you want a 2x5 grid for the first 10 files\n    plt.plot(rms_energy_values, label='RMS Energy')\n    plt.title(f'RMS Energy - {train.loc[i, \"ebird_code\"]}')\n    plt.legend()\n    plt.xlabel('Frame Index')\n    plt.ylabel('RMS Energy')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:21:03.809111Z","iopub.execute_input":"2024-01-14T14:21:03.809583Z","iopub.status.idle":"2024-01-14T14:21:08.735458Z","shell.execute_reply.started":"2024-01-14T14:21:03.809549Z","shell.execute_reply":"2024-01-14T14:21:08.733825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_spectral_rolloff(y, sr, title):\n    S, phase = librosa.magphase(librosa.stft(y))\n    rolloff = librosa.feature.spectral_rolloff(S=S, sr=sr, roll_percent=0.99)\n\n    # Plot Spectral Rolloff\n    plt.plot(rolloff[0], label='Spectral Rolloff')\n    plt.title(f'{title}')\n    plt.legend()\n\n# Plot Spectral Rolloff for all audio files\nnum_files_to_plot = 10  # Change this based on the number of files you want to plot\nplt.figure(figsize=(15, 10))\n\nfor i in range(num_files_to_plot):\n    plt.subplot(4, 3, i+1)  # Assuming you want a 2x5 grid for the first 10 files\n    plot_spectral_rolloff(train.loc[i, 'y'], train.loc[i, 'sr'], f'{train.loc[i, \"ebird_code\"]}')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:21:13.970360Z","iopub.execute_input":"2024-01-14T14:21:13.970838Z","iopub.status.idle":"2024-01-14T14:21:19.280929Z","shell.execute_reply.started":"2024-01-14T14:21:13.970803Z","shell.execute_reply":"2024-01-14T14:21:19.279153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Model Building**","metadata":{}},{"cell_type":"code","source":"!pip install resampy","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:52:02.392268Z","iopub.execute_input":"2024-01-14T14:52:02.392917Z","iopub.status.idle":"2024-01-14T14:52:17.431087Z","shell.execute_reply.started":"2024-01-14T14:52:02.392871Z","shell.execute_reply":"2024-01-14T14:52:17.429077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport audioread\nimport logging\nimport os\nimport random\nimport time\nimport warnings\n\nimport resampy\nimport librosa\nimport numpy as np\nimport pandas as pd\nimport soundfile as sf\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.utils.data as data\n\nfrom contextlib import contextmanager\nfrom pathlib import Path\nfrom typing import Optional\n\nfrom fastprogress import progress_bar\nfrom sklearn.metrics import f1_score\nfrom torchvision import models","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:52:24.529685Z","iopub.execute_input":"2024-01-14T14:52:24.530287Z","iopub.status.idle":"2024-01-14T14:52:24.540578Z","shell.execute_reply.started":"2024-01-14T14:52:24.530239Z","shell.execute_reply":"2024-01-14T14:52:24.538641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def setseed(seed: int=42):\n    random.seed(seed)\n    np.random.seed(seed)\n    os.environ[\"PYTHONHASHSEED\"]= str(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic=True\n    torch.backends.cudnn.benchmark=True\n    \ndef get_logger(out_file=None):\n    logger=logging.getLogger()\n    formatter=logging.Formatter(\"%(asctime)s-%(levelname)s-%(message)s\")\n    logger.handlers=[]\n    logger.setLevel=(logging.INFO)\n    \n    handler=logging.StreamHandler()\n    handler.setFormatter(formatter)\n    handler.setLevel(logging.INFO)\n    logger.addHandler(handler)\n    \n    if out_file is not None:\n        fh=logging.FileHandler(out_file)\n        fh.setFormatter(formatter)\n        fh.setLevel(logging.INFO)\n        logger.addHandler(fh)\n    logger.info(\"logger set up\")\n    return logger\n\n@contextmanager\n\ndef timer(name: str,logger: Optional[logging.Logger]=None):\n    t0=time.time()\n    msg=f\"[{name}]start\"\n    if logger is None:\n        print(msg)\n    else:\n        logger.info(msg)\n    yield\n    msg=f\"[{name}] done in {time.time()-t0:.2f}s\"\n    if logger is None:\n        print(msg)\n    else:\n        logger.info(msg)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:52:30.911087Z","iopub.execute_input":"2024-01-14T14:52:30.911569Z","iopub.status.idle":"2024-01-14T14:52:30.928963Z","shell.execute_reply.started":"2024-01-14T14:52:30.911533Z","shell.execute_reply":"2024-01-14T14:52:30.927461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logger=get_logger(\"main.log\")\nsetseed(1213)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:52:38.471388Z","iopub.execute_input":"2024-01-14T14:52:38.471875Z","iopub.status.idle":"2024-01-14T14:52:38.483599Z","shell.execute_reply.started":"2024-01-14T14:52:38.471838Z","shell.execute_reply":"2024-01-14T14:52:38.482145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test= pd.read_csv(\"/kaggle/input/birdcall-check/test.csv\")\ntest_audio=\"/kaggle/input/birdcall-check/test_audio\"\n\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:52:44.188378Z","iopub.execute_input":"2024-01-14T14:52:44.188873Z","iopub.status.idle":"2024-01-14T14:52:44.213647Z","shell.execute_reply.started":"2024-01-14T14:52:44.188839Z","shell.execute_reply":"2024-01-14T14:52:44.212703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ResNet(nn.Module):\n    def __init__(self, base_model_name:str, pretrained=False,num_classes=264):\n        super().__init__()\n        base_model=models.__getattribute__(base_model_name)(pretrained=pretrained)\n        layers=list(base_model.children())[:-2]\n        layers.append(nn.AdaptiveMaxPool2d(1))\n        self.encoder=nn.Sequential(*layers)\n        \n        in_features=base_model.fc.in_features\n        \n        self.classifier=nn.Sequential(\n             nn.Linear(in_features,1024),nn.ReLU(),nn.Dropout(p=0.2),\n             nn.Linear(1024,1024),nn.ReLU(),nn.Dropout(p=0.2),\n             nn.Linear(1024,num_classes))\n        \n    def forward(self,x):\n        batch_size=x.size(0)\n        x=self.encoder(x).view(batch_size,-1)\n        x=self.classifier(x)\n        multiclass_p=F.softmax(x,dim=1)\n        multilabel_p=F.sigmoid(x)\n        return{\n            \"logits\":x,\n            \"multiclass_p\":multiclass_p,\n            \"multilabel_p\":multilabel_p\n        }","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:52:48.182207Z","iopub.execute_input":"2024-01-14T14:52:48.183891Z","iopub.status.idle":"2024-01-14T14:52:48.198609Z","shell.execute_reply.started":"2024-01-14T14:52:48.183813Z","shell.execute_reply":"2024-01-14T14:52:48.197059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_config={\n    \"base_model_name\":\"resnet50\",\n    \"pretrained\":False,\n    \"num_classes\":264\n}\n\nmelspectrogram_parameters={\n    \"n_mels\":128,\n    \"fmin\":20,\n    \"fmax\":16000\n}\n\nweights_path=\"/kaggle/input/birdcall-resnet50-init-weights/best.pth\"","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:52:54.258135Z","iopub.execute_input":"2024-01-14T14:52:54.258604Z","iopub.status.idle":"2024-01-14T14:52:54.266047Z","shell.execute_reply.started":"2024-01-14T14:52:54.258567Z","shell.execute_reply":"2024-01-14T14:52:54.264768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\ndf=pd.read_csv(\"/kaggle/input/birdsong-recognition/train.csv\")\nunique_bird_names=df.ebird_code.unique()\nlabelencoder=LabelEncoder()\nencoded_labels=labelencoder.fit_transform(unique_bird_names)\n\nBIRD_CODE=dict(zip(unique_bird_names,encoded_labels))\nINV_BIRD_CODE={v:k for k,v in BIRD_CODE.items()}\n\nfor bird_name,label in INV_BIRD_CODE.items():\n    print(f\"{bird_name}:{label}\")","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:53:01.162503Z","iopub.execute_input":"2024-01-14T14:53:01.163909Z","iopub.status.idle":"2024-01-14T14:53:01.718290Z","shell.execute_reply.started":"2024-01-14T14:53:01.163847Z","shell.execute_reply":"2024-01-14T14:53:01.717025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mono_to_color(X:np.ndarray,\n                  mean=None,\n                  std=None,\n                  norm_max=None,\n                  norm_min=None,\n                  eps=1e-6):\n    X=np.stack([X,X,X],axis=-1)\n    \n    mean=mean or X.mean()\n    X=X-mean\n    std=std or X.std()\n    Xstd=X/(std+eps)\n    _min,_max=Xstd.min(),Xstd.max()\n    norm_max=norm_max or _max\n    norm_min=norm_min or _min\n    \n    if(_max-_min)>eps:\n        V=Xstd\n        V[V<norm_min]=norm_min\n        V[V>norm_max]=norm_max\n        V=255*(V-norm_min)/(norm_max-norm_min)\n        V=V.astype(np.uint8)\n    else:\n        V=np.zeroes_like(Xstd,dtype=np.uint8)\n    return V","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:53:11.415807Z","iopub.execute_input":"2024-01-14T14:53:11.416272Z","iopub.status.idle":"2024-01-14T14:53:11.427705Z","shell.execute_reply.started":"2024-01-14T14:53:11.416236Z","shell.execute_reply":"2024-01-14T14:53:11.426232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TestDataset(data.Dataset):\n    def __init__(self, df:pd.DataFrame, clip:np.ndarray,img_size=224, melspectrogram_parameters={}):\n        self.df=df\n        self.clip=clip\n        self.img_size=img_size\n        self.melspectrogram_parameters=melspectrogram_parameters\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx:int):\n        SR=32000\n        sample=self.df.loc[idx,:]\n        site=sample.site\n        row_id=sample.row_id\n        \n        if site==\"site_3\":\n            y=self.clip.astype(np.float32)\n            len_y=len(y)\n            start=0\n            end=SR*5\n            images=[]\n            while len_y>start:\n                y_batch=y[start:end].astype(np.float32)\n                if len(y_batch)!=(SR*5):\n                    break\n                start=end\n                end=end+SR*5\n                melspec=librosa.feature.melspectrogram(y=y_batch,sr=32000,**self.melspectrogram_parameters)\n                melspec=librosa.power_to_db(melspec).astype(np.float32)\n                image=mono_to_color(melspec)\n                height,width,_=image.shape\n                image=cv2.resize(image,(int(width*self.img_size/height),self.img_size))\n                image=np.moveaxis(image,2,0)\n                image=(image/255.0).astype(np.float32)\n                images.append(image)\n            images=np.asarray(images)\n            return images,row_id,site\n        \n        else:\n            end_seconds=int(sample.seconds)\n            start_seconds=int(end_seconds-5)\n            \n            start_index=SR*start_seconds\n            end_index=SR*end_seconds\n            \n            y=self.clip[start_index:end_index].astype(np.float32)\n            melspec=librosa.feature.melspectrogram(y=y,sr=32000,**self.melspectrogram_parameters)\n            melspec=librosa.power_to_db(melspec).astype(np.float32)\n            image=mono_to_color(melspec)\n            height,width,_=image.shape\n            image=cv2.resize(image,(int(width*self.img_size/height),self.img_size))\n            image=np.moveaxis(image,2,0)\n            image=(image/255.0).astype(np.float32)\n            \n            return image,row_id,site","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:53:17.380840Z","iopub.execute_input":"2024-01-14T14:53:17.381335Z","iopub.status.idle":"2024-01-14T14:53:17.403163Z","shell.execute_reply.started":"2024-01-14T14:53:17.381299Z","shell.execute_reply":"2024-01-14T14:53:17.401515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model(config: dict, weights_path: str):\n    model = ResNet(**config)\n    checkpoint = torch.load(weights_path, map_location=torch.device('cpu'))\n    model.load_state_dict(checkpoint[\"model_state_dict\"])\n    model.eval()\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:53:34.874432Z","iopub.execute_input":"2024-01-14T14:53:34.874929Z","iopub.status.idle":"2024-01-14T14:53:34.882548Z","shell.execute_reply.started":"2024-01-14T14:53:34.874890Z","shell.execute_reply":"2024-01-14T14:53:34.880988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Prediction and analysis\n","metadata":{}},{"cell_type":"code","source":"def prediction_for_clip(test_df: pd.DataFrame,\n                        clip:np.ndarray,\n                        model:ResNet,\n                        mel_params:dict,\n                        threshold=0.5):\n    dataset=TestDataset(df=test_df,\n                       clip=clip,\n                       img_size=224,\n                       melspectrogram_parameters=mel_params)\n    loader=data.DataLoader(dataset,batch_size=1,shuffle=False)\n    device=torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    \n    model.eval()\n    prediction_dict={}\n    for image,row_id,site in progress_bar(loader):\n        site=site[0]\n        row_id=row_id[0]\n        if site in {\"site_1\",\"site_2\"}:\n            image=image.to(device)\n            \n            with torch.no_grad():\n                prediction=model(image)\n                proba=prediction[\"multilabel_p\"].detach().cpu().numpy().reshape(-1)\n                \n            events=proba>=threshold\n            labels=np.argwhere(events).reshape(-1).tolist()\n            \n        else:\n            image=image.squeeze(0)\n            batch_size=16\n            whole_size=image.size(0)\n            if whole_size % batch_size==0:\n                n_iter=whole_size//batch_size\n            else:\n                n_iter=whole_size//batch_size+1\n                \n                \n            all_events=set()\n            for batch_i in range(n_iter):\n                batch=image[batch_i*batch_size:(batch_i+1)*batch_size]\n                if batch.ndim==3:\n                    batch=batch.unsqueeze(0)\n                    \n                batch=batch.to(device)\n                with torch.no_grad():\n                    prediction=model(batch)\n                    proba=prediction[\"multilabel_p\"].detach().cpu().numpy()\n                events=proba>=threshold\n                \n                for i in range(len(events)):\n                    event=events[i,:]\n                    labels=np.argwhere(event).reshape(-1).tolist()\n                    for label in labels:\n                        all_events.add(label)\n            labels=list(all_events)\n        \n        if len(labels)==0:\n            prediction_dict[row_id]=\"nocall\"\n            \n        else:\n            labels_str_list=list(map(lambda x: INV_BIRD_CODE[x],labels))\n            label_string=\" \".join(labels_str_list)\n            prediction_dict[row_id]=label_string\n    return prediction_dict","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:53:47.948960Z","iopub.execute_input":"2024-01-14T14:53:47.949600Z","iopub.status.idle":"2024-01-14T14:53:47.970620Z","shell.execute_reply.started":"2024-01-14T14:53:47.949558Z","shell.execute_reply":"2024-01-14T14:53:47.969258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prediction(test_df:pd.DataFrame,\n              test_audio:Path,\n              model_config:dict,\n              mel_params:dict,\n              weights_path:str,\n              threshold=0.5):\n    model=get_model(model_config, weights_path)\n    unique_audio_id=test_df.audio_id.unique()\n    \n    warnings.filterwarnings(\"ignore\")\n    prediction_dfs=[]\n    for audio_id in unique_audio_id:\n        with timer(f\"Loading {audio_id}\", logger):\n            clip,_=librosa.load(test_audio+\"/\"+(audio_id+\".mp3\"),\n                                sr=32000,\n                                mono=True,\n                                res_type=\"scipy\")\n        test_df_for_audio_id=test_df.query(f\"audio_id=='{audio_id}'\").reset_index(drop=True)\n        with timer(f\"Prediction on {audio_id}\", logger):\n            prediction_dict=prediction_for_clip(test_df_for_audio_id,\n                                               clip=clip,\n                                               model=model,\n                                               mel_params=mel_params,\n                                               threshold=threshold)\n        row_id=list(prediction_dict.keys())\n        birds=list(prediction_dict.values())\n        prediction_df=pd.DataFrame({\n            \"row_id\":row_id,\n            \"birds\":birds\n        })\n        prediction_dfs.append(prediction_df)\n    prediction_df=pd.concat(prediction_dfs, axis=0,sort=False).reset_index(drop=True)\n    return prediction_df","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:53:55.215414Z","iopub.execute_input":"2024-01-14T14:53:55.215914Z","iopub.status.idle":"2024-01-14T14:53:55.228551Z","shell.execute_reply.started":"2024-01-14T14:53:55.215878Z","shell.execute_reply":"2024-01-14T14:53:55.226974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=prediction(test_df=test,\n                     test_audio=test_audio,\n                     model_config=model_config,\n                     mel_params= melspectrogram_parameters,\n                     weights_path=weights_path,\n                     threshold=0.8)\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-14T14:54:02.815213Z","iopub.execute_input":"2024-01-14T14:54:02.815718Z","iopub.status.idle":"2024-01-14T14:55:01.472810Z","shell.execute_reply.started":"2024-01-14T14:54:02.815679Z","shell.execute_reply":"2024-01-14T14:55:01.471698Z"},"trusted":true},"execution_count":null,"outputs":[]}]}