{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":19596,"databundleVersionId":1292430,"sourceType":"competition"},{"sourceId":1262046,"sourceType":"datasetVersion","datasetId":726424},{"sourceId":1264575,"sourceType":"datasetVersion","datasetId":725893}],"dockerImageVersionId":29956,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import cv2\nimport audioread\nimport logging\nimport os\nimport random\nimport time\nimport warnings\nimport matplotlib.pyplot as plt\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\nimport statistics\nimport librosa.display\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom contextlib import contextmanager\nfrom pathlib import Path\nfrom typing import Optional\n\nfrom tqdm import tqdm\n\nfrom fastprogress import progress_bar\nfrom sklearn.metrics import f1_score\nfrom torchvision import models","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-11T18:26:42.621794Z","iopub.execute_input":"2024-01-11T18:26:42.622158Z","iopub.status.idle":"2024-01-11T18:26:42.631038Z","shell.execute_reply.started":"2024-01-11T18:26:42.622127Z","shell.execute_reply":"2024-01-11T18:26:42.630173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = pd.read_csv('/kaggle/input/birdsong-recognition/train.csv')\ntest_csv = pd.read_csv('/kaggle/input/birdsong-recognition/test.csv')\ntest_metadata = pd.read_csv('/kaggle/input/birdsong-recognition/example_test_audio_metadata.csv')\ntest_summary = pd.read_csv('/kaggle/input/birdsong-recognition/example_test_audio_summary.csv')\ntrain_audio = []\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        file = os.path.join(dirname, filename)\n        if file[:len(\"/kaggle/input/birdsong-recognition/train_audio/\")] == \"/kaggle/input/birdsong-recognition/train_audio/\":\n            train_audio.append(file)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:42.652756Z","iopub.execute_input":"2024-01-11T18:26:42.653126Z","iopub.status.idle":"2024-01-11T18:26:43.227841Z","shell.execute_reply.started":"2024-01-11T18:26:42.653083Z","shell.execute_reply":"2024-01-11T18:26:43.226861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:43.229749Z","iopub.execute_input":"2024-01-11T18:26:43.230056Z","iopub.status.idle":"2024-01-11T18:26:43.286242Z","shell.execute_reply.started":"2024-01-11T18:26:43.230027Z","shell.execute_reply":"2024-01-11T18:26:43.285299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:43.287894Z","iopub.execute_input":"2024-01-11T18:26:43.288302Z","iopub.status.idle":"2024-01-11T18:26:43.368858Z","shell.execute_reply.started":"2024-01-11T18:26:43.288259Z","shell.execute_reply":"2024-01-11T18:26:43.368037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv[\"playback_used\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:43.370160Z","iopub.execute_input":"2024-01-11T18:26:43.370423Z","iopub.status.idle":"2024-01-11T18:26:43.381837Z","shell.execute_reply.started":"2024-01-11T18:26:43.370397Z","shell.execute_reply":"2024-01-11T18:26:43.380996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv[\"playback_used\"].fillna(\"no\", inplace = True)\ntrain_csv[\"playback_used\"].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:43.386015Z","iopub.execute_input":"2024-01-11T18:26:43.386398Z","iopub.status.idle":"2024-01-11T18:26:43.396432Z","shell.execute_reply.started":"2024-01-11T18:26:43.386368Z","shell.execute_reply":"2024-01-11T18:26:43.395588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lpu = LabelEncoder()\nlpu.fit([\"no\", \"yes\"])\ntrain_csv[\"playback_used\"] = lpu.transform(train_csv[\"playback_used\"])\ntrain_csv[\"playback_used\"].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:43.399181Z","iopub.execute_input":"2024-01-11T18:26:43.399604Z","iopub.status.idle":"2024-01-11T18:26:43.422122Z","shell.execute_reply.started":"2024-01-11T18:26:43.399527Z","shell.execute_reply":"2024-01-11T18:26:43.421123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lbird = LabelEncoder()\nunique_birds = train_csv[\"ebird_code\"]\nlbird.fit(train_csv[\"ebird_code\"].unique())\ntrain_csv[\"ebird_code\"] = lbird.transform(train_csv[\"ebird_code\"])\ntrain_csv[\"ebird_code\"].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:43.423513Z","iopub.execute_input":"2024-01-11T18:26:43.423885Z","iopub.status.idle":"2024-01-11T18:26:43.452130Z","shell.execute_reply.started":"2024-01-11T18:26:43.423848Z","shell.execute_reply":"2024-01-11T18:26:43.451320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv[\"channels\"] = train_csv[\"channels\"].apply(lambda x : int(x[0]))","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:43.453172Z","iopub.execute_input":"2024-01-11T18:26:43.453436Z","iopub.status.idle":"2024-01-11T18:26:43.481319Z","shell.execute_reply.started":"2024-01-11T18:26:43.453410Z","shell.execute_reply":"2024-01-11T18:26:43.480450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"year = []\nmonth = []\nday = []\nfor i in range(0, len(train_csv[\"date\"])):\n    d = train_csv[\"date\"][i]\n    year.append(int(d[0:4]))\n    month.append(int(d[5:7]))\n    day.append(int(d[8:10]))\ntrain_csv[\"year\"] = year\ntrain_csv[\"month\"] = month\ntrain_csv[\"day\"] = day","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:43.482945Z","iopub.execute_input":"2024-01-11T18:26:43.483333Z","iopub.status.idle":"2024-01-11T18:26:44.029365Z","shell.execute_reply.started":"2024-01-11T18:26:43.483296Z","shell.execute_reply":"2024-01-11T18:26:44.028646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv[\"speed\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:44.030566Z","iopub.execute_input":"2024-01-11T18:26:44.030830Z","iopub.status.idle":"2024-01-11T18:26:44.042684Z","shell.execute_reply.started":"2024-01-11T18:26:44.030805Z","shell.execute_reply":"2024-01-11T18:26:44.041831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lsps = LabelEncoder()\nlsps.fit(train_csv[\"species\"].unique())\ntrain_csv[\"species\"] = lsps.transform(train_csv[\"species\"])\ntrain_csv[\"species\"].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:44.043865Z","iopub.execute_input":"2024-01-11T18:26:44.044164Z","iopub.status.idle":"2024-01-11T18:26:44.068818Z","shell.execute_reply.started":"2024-01-11T18:26:44.044136Z","shell.execute_reply":"2024-01-11T18:26:44.067892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv[\"bird_seen\"].fillna(\"yes\", inplace = True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:44.070000Z","iopub.execute_input":"2024-01-11T18:26:44.070253Z","iopub.status.idle":"2024-01-11T18:26:44.076757Z","shell.execute_reply.started":"2024-01-11T18:26:44.070229Z","shell.execute_reply":"2024-01-11T18:26:44.075825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lbs = LabelEncoder()\nlbs.fit([\"no\", \"yes\"])\ntrain_csv[\"bird_seen\"] = lbs.transform(train_csv[\"bird_seen\"])\ntrain_csv[\"bird_seen\"].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:44.077807Z","iopub.execute_input":"2024-01-11T18:26:44.078126Z","iopub.status.idle":"2024-01-11T18:26:44.103080Z","shell.execute_reply.started":"2024-01-11T18:26:44.078097Z","shell.execute_reply":"2024-01-11T18:26:44.102205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv[\"latitude\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:44.104351Z","iopub.execute_input":"2024-01-11T18:26:44.104721Z","iopub.status.idle":"2024-01-11T18:26:44.121763Z","shell.execute_reply.started":"2024-01-11T18:26:44.104692Z","shell.execute_reply":"2024-01-11T18:26:44.120939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"la = []\nmla = 0\nfor i in range(0, len(train_csv[\"latitude\"])):\n    if train_csv[\"latitude\"][i] != \"Not specified\":\n        la.append(float(train_csv[\"latitude\"][i]))\nmla = sum(la)/len(la)\ntrain_csv['latitude'].replace('Not specified', mla, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:44.124911Z","iopub.execute_input":"2024-01-11T18:26:44.125313Z","iopub.status.idle":"2024-01-11T18:26:45.063921Z","shell.execute_reply.started":"2024-01-11T18:26:44.125283Z","shell.execute_reply":"2024-01-11T18:26:45.062935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv[\"latitude\"] = train_csv[\"latitude\"].astype(np.float64)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:45.065259Z","iopub.execute_input":"2024-01-11T18:26:45.065562Z","iopub.status.idle":"2024-01-11T18:26:45.078403Z","shell.execute_reply.started":"2024-01-11T18:26:45.065532Z","shell.execute_reply":"2024-01-11T18:26:45.077549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lo = []\nmlo = 0\nfor i in range(0, len(train_csv[\"longitude\"])):\n    if train_csv[\"longitude\"][i] != \"Not specified\":\n        lo.append(float(train_csv[\"longitude\"][i]))\nmlo = sum(lo)/len(lo)\ntrain_csv['longitude'].replace('Not specified', mlo, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:45.079813Z","iopub.execute_input":"2024-01-11T18:26:45.080171Z","iopub.status.idle":"2024-01-11T18:26:46.051283Z","shell.execute_reply.started":"2024-01-11T18:26:45.080143Z","shell.execute_reply":"2024-01-11T18:26:46.050596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv[\"longitude\"] = train_csv[\"longitude\"].astype(np.float64)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:46.052515Z","iopub.execute_input":"2024-01-11T18:26:46.052777Z","iopub.status.idle":"2024-01-11T18:26:46.065623Z","shell.execute_reply.started":"2024-01-11T18:26:46.052752Z","shell.execute_reply":"2024-01-11T18:26:46.064848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv[\"sampling_rate\"] = train_csv[\"sampling_rate\"].apply(lambda x : int(x[: len(x) - 5]))","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:46.066740Z","iopub.execute_input":"2024-01-11T18:26:46.067058Z","iopub.status.idle":"2024-01-11T18:26:46.103276Z","shell.execute_reply.started":"2024-01-11T18:26:46.067028Z","shell.execute_reply":"2024-01-11T18:26:46.102564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv[\"elevation\"].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:46.104408Z","iopub.execute_input":"2024-01-11T18:26:46.104672Z","iopub.status.idle":"2024-01-11T18:26:46.111884Z","shell.execute_reply.started":"2024-01-11T18:26:46.104646Z","shell.execute_reply":"2024-01-11T18:26:46.111201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv['elevation'].replace('~950 m', '950 m', inplace=True)\ntrain_csv['elevation'].replace('1,650 m m', '1650 m', inplace=True)\ntrain_csv['elevation'].replace('1,650 m', '1650 m', inplace=True)\ntrain_csv['elevation'].replace('1,360 m m', '1360 m', inplace=True)\ntrain_csv['elevation'].replace('2,470 m', '2470 m', inplace=True)\ntrain_csv['elevation'].replace('930-990 m', '960 m', inplace=True)\ntrain_csv['elevation'].replace('2,335 m m', '2335 m', inplace=True)\ntrain_csv['elevation'].replace('1650-1900 m', '1775 m', inplace=True)\ntrain_csv['elevation'].replace('1,360 m m', '1360 m', inplace=True)\ntrain_csv['elevation'].replace('3,000 m', '3000 m', inplace=True)\ntrain_csv['elevation'].replace('2,470 m m', '2470 m', inplace=True)\ntrain_csv['elevation'].replace('1,105 m', '1105 m', inplace=True)\ntrain_csv['elevation'].replace('3.3 m m', '3.3 m', inplace=True)\ntrain_csv['elevation'].replace('1900m m', '1900 m', inplace=True)\ntrain_csv['elevation'].replace('1400m m', '1400 m', inplace=True)\ntrain_csv['elevation'].replace('? m', \"None\", inplace=True)\ntrain_csv['elevation'].replace('- m', \"None\", inplace=True)\ntrain_csv['elevation'].replace('?? m', \"None\", inplace=True)\ntrain_csv['elevation'].replace('Unknown m', \"None\", inplace=True)\ntrain_csv['elevation'].replace(' m', \"None\", inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:46.113368Z","iopub.execute_input":"2024-01-11T18:26:46.113687Z","iopub.status.idle":"2024-01-11T18:26:46.155848Z","shell.execute_reply.started":"2024-01-11T18:26:46.113644Z","shell.execute_reply":"2024-01-11T18:26:46.155004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"e = []\nem = 0\nfor i in range(0, len(train_csv[\"elevation\"])):\n    if train_csv[\"elevation\"][i] != \"None\":\n        ev = train_csv[\"elevation\"][i]\n    #    print(ev)\n        e.append(float(ev[:len(ev) - 1]))\nem = sum(e)/len(e)\ntrain_csv['elevation'].replace(\"None\", str(em) + \" m\", inplace=True)\ntrain_csv['elevation'] = train_csv['elevation'].apply(lambda x : float(x[:len(x) - 1]))","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:46.157192Z","iopub.execute_input":"2024-01-11T18:26:46.157469Z","iopub.status.idle":"2024-01-11T18:26:47.127004Z","shell.execute_reply.started":"2024-01-11T18:26:46.157443Z","shell.execute_reply":"2024-01-11T18:26:47.126304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.fillna(\"None\", inplace = True)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:47.128202Z","iopub.execute_input":"2024-01-11T18:26:47.128580Z","iopub.status.idle":"2024-01-11T18:26:47.185106Z","shell.execute_reply.started":"2024-01-11T18:26:47.128543Z","shell.execute_reply":"2024-01-11T18:26:47.184434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b = []\nbm = 0\nfor i in range(0, len(train_csv[\"bitrate_of_mp3\"])):\n    if train_csv[\"bitrate_of_mp3\"][i] != \"None\":\n        ev = train_csv[\"bitrate_of_mp3\"][i]\n #       print(ev)\n        b.append(float(ev[:len(ev) - 5]))\nbm = sum(b)/len(b)\ntrain_csv['bitrate_of_mp3'].replace(\"None\", str(bm) + \" (bps)\", inplace=True)\ntrain_csv[\"bitrate_of_mp3\"] = train_csv[\"bitrate_of_mp3\"].apply(lambda x : float(x[:len(x) - 5]))","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:47.186108Z","iopub.execute_input":"2024-01-11T18:26:47.186362Z","iopub.status.idle":"2024-01-11T18:26:48.153571Z","shell.execute_reply.started":"2024-01-11T18:26:47.186337Z","shell.execute_reply":"2024-01-11T18:26:48.152876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lc = LabelEncoder()\nlc.fit(train_csv[\"country\"].unique())\ntrain_csv[\"country\"] = lc.transform(train_csv[\"country\"])\ntrain_csv[\"country\"].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:48.154798Z","iopub.execute_input":"2024-01-11T18:26:48.155209Z","iopub.status.idle":"2024-01-11T18:26:48.171789Z","shell.execute_reply.started":"2024-01-11T18:26:48.155170Z","shell.execute_reply":"2024-01-11T18:26:48.170884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv[\"primary_label\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:48.173063Z","iopub.execute_input":"2024-01-11T18:26:48.173424Z","iopub.status.idle":"2024-01-11T18:26:48.186443Z","shell.execute_reply.started":"2024-01-11T18:26:48.173389Z","shell.execute_reply":"2024-01-11T18:26:48.185625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lpl = LabelEncoder()\nlpl.fit(train_csv[\"primary_label\"].unique())\ntrain_csv[\"primary_label\"] = lpl.transform(train_csv[\"primary_label\"])\ntrain_csv[\"primary_label\"].unique()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:48.187604Z","iopub.execute_input":"2024-01-11T18:26:48.187869Z","iopub.status.idle":"2024-01-11T18:26:48.206826Z","shell.execute_reply.started":"2024-01-11T18:26:48.187841Z","shell.execute_reply":"2024-01-11T18:26:48.206004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv[\"time\"]","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:48.207892Z","iopub.execute_input":"2024-01-11T18:26:48.208187Z","iopub.status.idle":"2024-01-11T18:26:48.215913Z","shell.execute_reply.started":"2024-01-11T18:26:48.208161Z","shell.execute_reply":"2024-01-11T18:26:48.215033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l = []\nsu = 0\nle = 0\ntrain_csv[\"time\"].replace(\"Dawn\", \"6:30\", inplace = True)\ntrain_csv[\"time\"].replace(\"Sunset\", \"18:00\", inplace = True)\nfor i in range(0, len(train_csv[\"time\"])):\n    t = train_csv[\"time\"][i]\n    if t[0] == \"?\":\n        l.append(\"None\")\n        continue\n    if t == \"pm\":\n        l.append(\"None\")\n        continue\n    if t == \"am\":\n        l.append(\"None\")\n        continue\n    if t == \".\":\n        l.append(\"None\")\n        continue\n    if t == \"Dawn (at dusk)\":\n        l.append(\"None\")\n        continue\n    if t[0] == \"x\":\n        l.append(\"None\")\n        continue\n    s = t.split(\":\")\n    if s[1][-2:] == \"am\":\n        s[1] = s[1][:-2]\n    if s[1][-2:] == \"pm\":\n        s[1] = s[1][:-2]\n        s[0] = int(s[0]) + 12\n    l.append(int(s[0]) + float(s[1])/60)\n    su += int(s[0]) + float(s[1])/60\n    le += 1\n        \nmean = su/le\nfor i in range(0, len(l)):\n    if l[i] == \"None\":\n        l[i] = mean   \ntrain_csv[\"time\"] = l","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:48.217330Z","iopub.execute_input":"2024-01-11T18:26:48.217686Z","iopub.status.idle":"2024-01-11T18:26:48.786793Z","shell.execute_reply.started":"2024-01-11T18:26:48.217650Z","shell.execute_reply":"2024-01-11T18:26:48.785806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ll = LabelEncoder()\nll.fit(train_csv[\"license\"].unique())\ntrain_csv[\"license\"] = ll.transform(train_csv[\"license\"])\ntrain_csv[\"license\"]","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:48.788242Z","iopub.execute_input":"2024-01-11T18:26:48.788626Z","iopub.status.idle":"2024-01-11T18:26:48.806449Z","shell.execute_reply.started":"2024-01-11T18:26:48.788585Z","shell.execute_reply":"2024-01-11T18:26:48.805698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(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)  # type: ignore\n    torch.backends.cudnn.deterministic = True  # type: ignore\n    torch.backends.cudnn.benchmark = True  # type: ignore\n    \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    \n@contextmanager\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\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-11T18:26:48.807537Z","iopub.execute_input":"2024-01-11T18:26:48.807795Z","iopub.status.idle":"2024-01-11T18:26:48.821020Z","shell.execute_reply.started":"2024-01-11T18:26:48.807770Z","shell.execute_reply":"2024-01-11T18:26:48.820216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logger = get_logger(\"main.log\")\nset_seed(1213)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:48.826444Z","iopub.execute_input":"2024-01-11T18:26:48.826719Z","iopub.status.idle":"2024-01-11T18:26:48.835455Z","shell.execute_reply.started":"2024-01-11T18:26:48.826694Z","shell.execute_reply":"2024-01-11T18:26:48.834728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TARGET_SR = 32000\nTEST = Path(\"../input/birdsong-recognition/test_audio\").exists()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:48.837134Z","iopub.execute_input":"2024-01-11T18:26:48.837500Z","iopub.status.idle":"2024-01-11T18:26:48.844905Z","shell.execute_reply.started":"2024-01-11T18:26:48.837471Z","shell.execute_reply":"2024-01-11T18:26:48.844193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if TEST:\n    DATA_DIR = Path(\"../input/birdsong-recognition/\")\nelse:\n    DATA_DIR = Path(\"../input/birdcall-check/\")\n    \n\ntest = pd.read_csv(DATA_DIR / \"test.csv\")\ntest_audio = DATA_DIR / \"test_audio\"\n\n\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:48.846318Z","iopub.execute_input":"2024-01-11T18:26:48.846600Z","iopub.status.idle":"2024-01-11T18:26:48.866948Z","shell.execute_reply.started":"2024-01-11T18:26:48.846573Z","shell.execute_reply":"2024-01-11T18:26:48.866248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ResNet(nn.Module):\n    def __init__(self, base_model_name: str, pretrained=False,\n                 num_classes=264):\n        super().__init__()\n        base_model = models.__getattribute__(base_model_name)(\n            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_proba = F.softmax(x, dim=1)\n        multilabel_proba = F.sigmoid(x)\n        return {\n            \"logits\": x,\n            \"multiclass_proba\": multiclass_proba,\n            \"multilabel_proba\": multilabel_proba\n        }","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:48.868150Z","iopub.execute_input":"2024-01-11T18:26:48.868421Z","iopub.status.idle":"2024-01-11T18:26:48.880135Z","shell.execute_reply.started":"2024-01-11T18:26:48.868395Z","shell.execute_reply":"2024-01-11T18:26:48.879144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feature_extractor(file_name):\n    audio, sample_rate = librosa.load(file_name)\n    mfccs_features = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)\n    mfccs_scaled_features = np.mean(mfccs_features.T,axis=0)\n\n    return mfccs_scaled_features","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:48.881117Z","iopub.execute_input":"2024-01-11T18:26:48.881426Z","iopub.status.idle":"2024-01-11T18:26:48.893080Z","shell.execute_reply.started":"2024-01-11T18:26:48.881398Z","shell.execute_reply":"2024-01-11T18:26:48.892341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fmin = []\nfmax = []\n\nfor i in tqdm(range(0, 50)):\n    y, sr = librosa.load(train_audio[i])\n    t = librosa.feature.melspectrogram(y=y, sr=sr, n_mels = 128)\n    fmin.append(np.min(t))\n    fmax.append(np.max(t))\n    ","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:26:48.894371Z","iopub.execute_input":"2024-01-11T18:26:48.894737Z","iopub.status.idle":"2024-01-11T18:29:06.728593Z","shell.execute_reply.started":"2024-01-11T18:26:48.894701Z","shell.execute_reply":"2024-01-11T18:29:06.727544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"min(fmin)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:29:06.730382Z","iopub.execute_input":"2024-01-11T18:29:06.730732Z","iopub.status.idle":"2024-01-11T18:29:06.737644Z","shell.execute_reply.started":"2024-01-11T18:29:06.730696Z","shell.execute_reply":"2024-01-11T18:29:06.736630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max(fmax)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:29:06.739197Z","iopub.execute_input":"2024-01-11T18:29:06.739530Z","iopub.status.idle":"2024-01-11T18:29:06.747151Z","shell.execute_reply.started":"2024-01-11T18:29:06.739499Z","shell.execute_reply":"2024-01-11T18:29:06.746222Z"},"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\": 10,\n    \"fmax\": 16000\n}\n\nweights_path = \"../input/birdcall-resnet50-init-weights/best.pth\"","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:29:06.748550Z","iopub.execute_input":"2024-01-11T18:29:06.748889Z","iopub.status.idle":"2024-01-11T18:29:06.755755Z","shell.execute_reply.started":"2024-01-11T18:29:06.748844Z","shell.execute_reply":"2024-01-11T18:29:06.754779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoded_labels = lbird.transform(unique_birds)\n\nBIRD_CODE = dict(zip(unique_birds, encoded_labels))\n\nINV_BIRD_CODE = {v : k for k, v in BIRD_CODE.items()}","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:29:06.757027Z","iopub.execute_input":"2024-01-11T18:29:06.757295Z","iopub.status.idle":"2024-01-11T18:29:06.779322Z","shell.execute_reply.started":"2024-01-11T18:29:06.757270Z","shell.execute_reply":"2024-01-11T18:29:06.778523Z"},"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    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.zeros_like(Xstd, dtype=np.uint8)\n    return V\n\n\nclass TestDataset(data.Dataset):\n    def __init__(self, df: pd.DataFrame, clip: np.ndarray,\n                 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                \n                melspec = librosa.feature.melspectrogram(y_batch,\n                                                         sr=SR,\n                                                         **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        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\n            melspec = librosa.feature.melspectrogram(y, sr=SR, **self.melspectrogram_parameters)\n            melspec = librosa.power_to_db(melspec).astype(np.float32)\n\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-11T18:29:06.782851Z","iopub.execute_input":"2024-01-11T18:29:06.783178Z","iopub.status.idle":"2024-01-11T18:29:06.813085Z","shell.execute_reply.started":"2024-01-11T18:29:06.783151Z","shell.execute_reply":"2024-01-11T18:29:06.812279Z"},"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)\n    model.load_state_dict(checkpoint[\"model_state_dict\"])\n    device = torch.device(\"cuda\")\n    model.to(device)\n    model.eval()\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:29:06.816290Z","iopub.execute_input":"2024-01-11T18:29:06.816576Z","iopub.status.idle":"2024-01-11T18:29:06.825967Z","shell.execute_reply.started":"2024-01-11T18:29:06.816535Z","shell.execute_reply":"2024-01-11T18:29:06.825012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\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_proba\"].detach().cpu().numpy().reshape(-1)\n\n            events = proba >= threshold\n            labels = np.argwhere(events).reshape(-1).tolist()\n\n        else:\n            # to avoid prediction on large batch\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            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_proba\"].detach().cpu().numpy()\n                    \n                events = proba >= threshold\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                        \n            labels = list(all_events)\n        if len(labels) == 0:\n            prediction_dict[row_id] = \"nocall\"\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-11T18:29:06.827400Z","iopub.execute_input":"2024-01-11T18:29:06.827799Z","iopub.status.idle":"2024-01-11T18:29:06.849765Z","shell.execute_reply.started":"2024-01-11T18:29:06.827763Z","shell.execute_reply":"2024-01-11T18:29:06.848966Z"},"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=TARGET_SR,\n                                   mono=True,\n                                   res_type=\"kaiser_fast\")\n        \n        test_df_for_audio_id = test_df.query(\n            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    \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-11T18:29:06.851236Z","iopub.execute_input":"2024-01-11T18:29:06.851532Z","iopub.status.idle":"2024-01-11T18:29:06.867341Z","shell.execute_reply.started":"2024-01-11T18:29:06.851505Z","shell.execute_reply":"2024-01-11T18:29:06.866583Z"},"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.65)\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:29:06.868686Z","iopub.execute_input":"2024-01-11T18:29:06.869001Z","iopub.status.idle":"2024-01-11T18:29:24.856702Z","shell.execute_reply.started":"2024-01-11T18:29:06.868954Z","shell.execute_reply":"2024-01-11T18:29:24.855834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:29:24.858344Z","iopub.execute_input":"2024-01-11T18:29:24.858642Z","iopub.status.idle":"2024-01-11T18:29:24.870709Z","shell.execute_reply.started":"2024-01-11T18:29:24.858611Z","shell.execute_reply":"2024-01-11T18:29:24.869961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_id_unique = test[\"audio_id\"].unique()\nout = []\nfor i in range(0, len(audio_id_unique)):\n    d = []\n    for j in range(0, len(submission[\"row_id\"])):\n        if audio_id_unique[i] in submission[\"row_id\"][j]:\n            d.append(submission[\"birds\"][j])\n    out.append(statistics.mode(d))\nD = {\"audio_id\" : audio_id_unique, \"bird\" : out}\nfinal_submission = pd.DataFrame(D)\nfinal_submission.to_csv(\"final_submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:29:24.872210Z","iopub.execute_input":"2024-01-11T18:29:24.872513Z","iopub.status.idle":"2024-01-11T18:29:24.913275Z","shell.execute_reply.started":"2024-01-11T18:29:24.872481Z","shell.execute_reply":"2024-01-11T18:29:24.912332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_submission","metadata":{"execution":{"iopub.status.busy":"2024-01-11T18:29:24.914346Z","iopub.execute_input":"2024-01-11T18:29:24.914617Z","iopub.status.idle":"2024-01-11T18:29:24.925310Z","shell.execute_reply.started":"2024-01-11T18:29:24.914590Z","shell.execute_reply":"2024-01-11T18:29:24.924298Z"},"trusted":true},"execution_count":null,"outputs":[]}]}