{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.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":70203,"databundleVersionId":8068726,"sourceType":"competition"}],"dockerImageVersionId":30162,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Inference for 🦜BirdCLEF with Random data\n\n**Clarification about the submission format: https://www.kaggle.com/c/birdclef-2022/discussion/308009**","metadata":{}},{"cell_type":"code","source":"!ls -l /kaggle/input/birdclef-2024\n\nPATH_DATASET = \"/kaggle/input/birdclef-2024\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-08T08:49:14.404509Z","iopub.execute_input":"2024-04-08T08:49:14.405601Z","iopub.status.idle":"2024-04-08T08:49:15.522626Z","shell.execute_reply.started":"2024-04-08T08:49:14.405543Z","shell.execute_reply":"2024-04-08T08:49:15.521625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\ndf_train = pd.read_csv(os.path.join(PATH_DATASET, \"train_metadata.csv\"))\ndisplay(df_train.head())\nlabels = sorted(df_train['primary_label'].unique())\nprint(f\"labels: {len(labels)}\")\ndel df_train","metadata":{"execution":{"iopub.status.busy":"2024-04-08T08:49:15.524769Z","iopub.execute_input":"2024-04-08T08:49:15.525069Z","iopub.status.idle":"2024-04-08T08:49:15.679007Z","shell.execute_reply.started":"2024-04-08T08:49:15.525034Z","shell.execute_reply":"2024-04-08T08:49:15.678077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fake audio to dataset\n\nThe true conversion is in **https://www.kaggle.com/code/jirkaborovec/birdclef-convert-spectrograms-reduce-noise**","metadata":{}},{"cell_type":"code","source":"import os\nimport glob\nimport torch\nimport torchaudio\nfrom math import ceil\nfrom tqdm.auto import tqdm\nfrom functools import partial\nfrom joblib import Parallel, delayed\n\n\ndef convert_fake(fn, path_in, frame_length: int = 5) -> list:\n    path_audio = os.path.join(path_in, fn)\n    waveform, sample_rate = torchaudio.load(path_audio)\n    nb = int(frame_length * sample_rate)\n    counts = ceil(waveform.size()[-1] / nb)\n    records = [{\n        \"end_time\": (i + 1) * frame_length,\n        \"file_id\": os.path.splitext(fn)[0],\n    } for i in range(counts)]\n    return records\n\n_convert_fake = partial(convert_fake, path_in=os.path.join(PATH_DATASET, \"test_soundscapes\"))","metadata":{"execution":{"iopub.status.busy":"2024-04-08T08:49:15.680780Z","iopub.execute_input":"2024-04-08T08:49:15.681115Z","iopub.status.idle":"2024-04-08T08:49:15.690642Z","shell.execute_reply.started":"2024-04-08T08:49:15.681072Z","shell.execute_reply":"2024-04-08T08:49:15.689942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"soundscapes = glob.glob(os.path.join(PATH_DATASET, \"test_soundscapes\", \"*.ogg\"))\nsoundscapes = list(map(os.path.basename, soundscapes))\nconverted = []\nfor batch in Parallel(n_jobs=3)(delayed(_convert_fake)(fn) for fn in tqdm(soundscapes)):\n    converted += batch\n# _= list(map(_convert_and_export, tqdm(train_meta[\"filename\"])))\n\ndf_converted = pd.DataFrame(converted)\ndisplay(df_converted.head())","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-04-08T08:49:15.692422Z","iopub.execute_input":"2024-04-08T08:49:15.693195Z","iopub.status.idle":"2024-04-08T08:49:15.744332Z","shell.execute_reply.started":"2024-04-08T08:49:15.693146Z","shell.execute_reply":"2024-04-08T08:49:15.743420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# fake Inference with magic","metadata":{}},{"cell_type":"code","source":"import numpy as np\nfrom pprint import pprint\n\npredictions = []\nfor _ in tqdm(range(len(df_converted))):\n    # lbs = [torch.argmax(p[\"preds\"].float()).item() for p in preds]\n    predictions += [np.random.random(len(labels))]\n\npprint(predictions[:2])","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-04-08T08:49:15.745604Z","iopub.execute_input":"2024-04-08T08:49:15.745820Z","iopub.status.idle":"2024-04-08T08:49:15.787073Z","shell.execute_reply.started":"2024-04-08T08:49:15.745793Z","shell.execute_reply":"2024-04-08T08:49:15.786387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Format submission","metadata":{}},{"cell_type":"code","source":"! cat /kaggle/input/birdclef-2023/sample_submission.csv","metadata":{"execution":{"iopub.status.busy":"2024-04-08T08:49:15.788959Z","iopub.execute_input":"2024-04-08T08:49:15.789203Z","iopub.status.idle":"2024-04-08T08:49:16.887284Z","shell.execute_reply.started":"2024-04-08T08:49:15.789161Z","shell.execute_reply":"2024-04-08T08:49:16.886511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = []\nfor i, row in tqdm(df_converted.iterrows(), total=len(df_converted)):\n    pred = dict(zip(labels, predictions[i].round(3)))\n    pred[\"row_id\"] = f\"{row['file_id']}_{row['end_time']}\"\n    submission.append(pred)","metadata":{"execution":{"iopub.status.busy":"2024-04-08T08:49:16.888688Z","iopub.execute_input":"2024-04-08T08:49:16.888955Z","iopub.status.idle":"2024-04-08T08:49:16.933018Z","shell.execute_reply.started":"2024-04-08T08:49:16.888918Z","shell.execute_reply":"2024-04-08T08:49:16.931632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission = pd.DataFrame(submission)\nif submission:  # for the dryrun\n    df_submission.set_index(\"row_id\", inplace=True)\ndf_submission.to_csv(\"submission.csv\")\n\n! head submission.csv","metadata":{"execution":{"iopub.status.busy":"2024-04-08T08:49:16.934765Z","iopub.execute_input":"2024-04-08T08:49:16.935276Z","iopub.status.idle":"2024-04-08T08:49:18.049337Z","shell.execute_reply.started":"2024-04-08T08:49:16.935229Z","shell.execute_reply":"2024-04-08T08:49:18.048169Z"},"trusted":true},"execution_count":null,"outputs":[]}]}