{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"},{"sourceId":8566023,"sourceType":"datasetVersion","datasetId":5121097},{"sourceId":8585987,"sourceType":"datasetVersion","datasetId":5135350}],"dockerImageVersionId":30715,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from glob import glob\nimport pickle\nimport os\n\nimport numpy as np\nimport pandas as pd\nimport torch\nimport torchaudio\nfrom tqdm import tqdm\n\ntqdm.pandas()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-02T14:05:36.884439Z","iopub.execute_input":"2024-06-02T14:05:36.884797Z","iopub.status.idle":"2024-06-02T14:05:41.589205Z","shell.execute_reply.started":"2024-06-02T14:05:36.884770Z","shell.execute_reply":"2024-06-02T14:05:41.588067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SPECIES = [\n    'asbfly', 'ashdro1', 'ashpri1', 'ashwoo2', 'asikoe2', 'asiope1', 'aspfly1', 'aspswi1', 'barfly1', 'barswa',\n    'bcnher', 'bkcbul1', 'bkrfla1', 'bkskit1', 'bkwsti', 'bladro1', 'blaeag1', 'blakit1', 'blhori1', 'blnmon1',\n    'blrwar1', 'bncwoo3', 'brakit1', 'brasta1', 'brcful1', 'brfowl1', 'brnhao1', 'brnshr', 'brodro1', 'brwjac1',\n    'brwowl1', 'btbeat1', 'bwfshr1', 'categr', 'chbeat1', 'cohcuc1', 'comfla1', 'comgre', 'comior1', 'comkin1',\n    'commoo3', 'commyn', 'compea', 'comros', 'comsan', 'comtai1', 'copbar1', 'crbsun2', 'cregos1', 'crfbar1',\n    'crseag1', 'dafbab1', 'darter2', 'eaywag1', 'emedov2', 'eucdov', 'eurbla2', 'eurcoo', 'forwag1', 'gargan',\n    'gloibi', 'goflea1', 'graher1', 'grbeat1', 'grecou1', 'greegr', 'grefla1', 'grehor1', 'grejun2', 'grenig1',\n    'grewar3', 'grnsan', 'grnwar1', 'grtdro1', 'gryfra', 'grynig2', 'grywag', 'gybpri1', 'gyhcaf1', 'heswoo1',\n    'hoopoe', 'houcro1', 'houspa', 'inbrob1', 'indpit1', 'indrob1', 'indrol2', 'indtit1', 'ingori1', 'inpher1',\n    'insbab1', 'insowl1', 'integr', 'isbduc1', 'jerbus2', 'junbab2', 'junmyn1', 'junowl1', 'kenplo1', 'kerlau2',\n    'labcro1', 'laudov1', 'lblwar1', 'lesyel1', 'lewduc1', 'lirplo', 'litegr', 'litgre1', 'litspi1', 'litswi1',\n    'lobsun2', 'maghor2', 'malpar1', 'maltro1', 'malwoo1', 'marsan', 'mawthr1', 'moipig1', 'nilfly2', 'niwpig1',\n    'nutman', 'orihob2', 'oripip1', 'pabflo1', 'paisto1', 'piebus1', 'piekin1', 'placuc3', 'plaflo1', 'plapri1',\n    'plhpar1', 'pomgrp2', 'purher1', 'pursun3', 'pursun4', 'purswa3', 'putbab1', 'redspu1', 'rerswa1', 'revbul',\n    'rewbul', 'rewlap1', 'rocpig', 'rorpar', 'rossta2', 'rufbab3', 'ruftre2', 'rufwoo2', 'rutfly6', 'sbeowl1',\n    'scamin3', 'shikra1', 'smamin1', 'sohmyn1', 'spepic1', 'spodov', 'spoowl1', 'sqtbul1', 'stbkin1', 'sttwoo1',\n    'thbwar1', 'tibfly3', 'tilwar1', 'vefnut1', 'vehpar1', 'wbbfly1', 'wemhar1', 'whbbul2', 'whbsho3', 'whbtre1',\n    'whbwag1', 'whbwat1', 'whbwoo2', 'whcbar1', 'whiter2', 'whrmun', 'whtkin2', 'woosan', 'wynlau1', 'yebbab1',\n    'yebbul3', 'zitcis1'\n]","metadata":{"execution":{"iopub.status.busy":"2024-06-02T14:07:54.506394Z","iopub.execute_input":"2024-06-02T14:07:54.506969Z","iopub.status.idle":"2024-06-02T14:07:54.517744Z","shell.execute_reply.started":"2024-06-02T14:07:54.506936Z","shell.execute_reply":"2024-06-02T14:07:54.516486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## load data files list","metadata":{}},{"cell_type":"code","source":"TEST_AUDIO_DIR = '/kaggle/input/birdclef-2024/test_soundscapes'\nUNLABELED_AUDIO_DIR = '/kaggle/input/birdclef-2024/unlabeled_soundscapes'","metadata":{"execution":{"iopub.status.busy":"2024-06-02T14:07:58.196100Z","iopub.execute_input":"2024-06-02T14:07:58.196478Z","iopub.status.idle":"2024-06-02T14:07:58.201506Z","shell.execute_reply.started":"2024-06-02T14:07:58.196451Z","shell.execute_reply":"2024-06-02T14:07:58.200418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_files = glob(os.path.join(TEST_AUDIO_DIR, '**/*.ogg'), recursive=True)\nif len(test_files) == 0:\n    print(f'loading from {UNLABELED_AUDIO_DIR}')\n    test_files = glob(os.path.join(UNLABELED_AUDIO_DIR, '**/*.ogg'), recursive=True)[:50]\n\n# test_files = test_files[:10]\ndf_test_files = pd.DataFrame(test_files, columns=['file_path'])","metadata":{"execution":{"iopub.status.busy":"2024-06-02T14:07:58.917331Z","iopub.execute_input":"2024-06-02T14:07:58.917700Z","iopub.status.idle":"2024-06-02T14:08:03.987682Z","shell.execute_reply.started":"2024-06-02T14:07:58.917675Z","shell.execute_reply":"2024-06-02T14:08:03.986811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test_files","metadata":{"execution":{"iopub.status.busy":"2024-06-02T14:08:03.989454Z","iopub.execute_input":"2024-06-02T14:08:03.989874Z","iopub.status.idle":"2024-06-02T14:08:04.012626Z","shell.execute_reply.started":"2024-06-02T14:08:03.989839Z","shell.execute_reply":"2024-06-02T14:08:04.011474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## load model","metadata":{}},{"cell_type":"code","source":"with open('/kaggle/input/birdclef2024-sample-sklearn-model-3/model3.pkl', 'rb') as f:\n    model = pickle.load(f)","metadata":{"execution":{"iopub.status.busy":"2024-06-02T14:08:13.903061Z","iopub.execute_input":"2024-06-02T14:08:13.903673Z","iopub.status.idle":"2024-06-02T14:08:19.389313Z","shell.execute_reply.started":"2024-06-02T14:08:13.903633Z","shell.execute_reply":"2024-06-02T14:08:19.388296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## inference","metadata":{}},{"cell_type":"code","source":"def extract_feat(\n    waveform: torch.tensor,\n    sampling_rate: int,\n) -> np.ndarray:\n    mfcc_transform = torchaudio.transforms.MFCC(\n        sample_rate=sampling_rate,\n        melkwargs={\"n_fft\": 512, \"hop_length\": 512//2, \"n_mels\": 40},\n    )\n    mfcc = mfcc_transform(waveform).mean(dim=-1)\n    return mfcc.squeeze().numpy()\n\n\ndef inference_single(filepath: str) -> pd.DataFrame:\n    waveform, sr = torchaudio.load(filepath, normalize=True)\n    duration_sec = len(waveform.squeeze()) / sr\n    end_secs = list(range(5, int(duration_sec) + 1, 5))\n    filename_prefix_id = filepath.split('/')[-1].replace('.ogg', '')  # xxxxxx for unlabeled_soundscapes. soundscape_xxxxxx for test_saundscapes\n    df_row_ids = pd.Series([f'{filename_prefix_id}_{end_time}' for end_time in end_secs]).to_frame('row_id')\n    feats = np.vstack([\n        extract_feat(\n            waveform=waveform[:, int((end_sec-5)*sr):int(end_sec*sr)],\n            sampling_rate=sr,\n        )\n        for end_sec in end_secs\n    ])\n    df_preds_proba = pd.DataFrame(data=model.predict_proba(feats), columns=SPECIES)\n    return pd.concat([df_row_ids, df_preds_proba], axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-02T14:08:19.391240Z","iopub.execute_input":"2024-06-02T14:08:19.391650Z","iopub.status.idle":"2024-06-02T14:08:19.402591Z","shell.execute_reply.started":"2024-06-02T14:08:19.391616Z","shell.execute_reply":"2024-06-02T14:08:19.401090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_pred_list = []\nfor filepath in tqdm(df_test_files['file_path']):\n    df_pred_list.append(inference_single(filepath))\n\ndf_pred = pd.concat(df_pred_list, axis=0).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-02T14:08:24.309632Z","iopub.execute_input":"2024-06-02T14:08:24.310072Z","iopub.status.idle":"2024-06-02T14:08:42.037553Z","shell.execute_reply.started":"2024-06-02T14:08:24.310039Z","shell.execute_reply":"2024-06-02T14:08:42.036484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_pred","metadata":{"execution":{"iopub.status.busy":"2024-06-02T14:08:43.262461Z","iopub.execute_input":"2024-06-02T14:08:43.262865Z","iopub.status.idle":"2024-06-02T14:08:43.302500Z","shell.execute_reply.started":"2024-06-02T14:08:43.262834Z","shell.execute_reply":"2024-06-02T14:08:43.301367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_pred.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-01T04:33:15.642344Z","iopub.execute_input":"2024-06-01T04:33:15.642759Z","iopub.status.idle":"2024-06-01T04:33:16.470478Z","shell.execute_reply.started":"2024-06-01T04:33:15.642728Z","shell.execute_reply":"2024-06-01T04:33:16.468946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -lh","metadata":{"execution":{"iopub.status.busy":"2024-06-01T04:33:16.472454Z","iopub.execute_input":"2024-06-01T04:33:16.472824Z","iopub.status.idle":"2024-06-01T04:33:17.527590Z","shell.execute_reply.started":"2024-06-01T04:33:16.472794Z","shell.execute_reply":"2024-06-01T04:33:17.526145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"END","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"df_pred[SPECIES].to_numpy().argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-06-01T04:33:31.501119Z","iopub.execute_input":"2024-06-01T04:33:31.501537Z","iopub.status.idle":"2024-06-01T04:33:31.521220Z","shell.execute_reply.started":"2024-06-01T04:33:31.501505Z","shell.execute_reply":"2024-06-01T04:33:31.519773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}