{"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":"markdown","source":"This notebook is discussed in the below discussion:\n\n- https://www.kaggle.com/competitions/birdclef-2022/discussion/323582","metadata":{}},{"cell_type":"code","source":"!pip install nb-black >/dev/null","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-07T02:04:36.001992Z","iopub.execute_input":"2022-05-07T02:04:36.002435Z","iopub.status.idle":"2022-05-07T02:04:51.497421Z","shell.execute_reply.started":"2022-05-07T02:04:36.002299Z","shell.execute_reply":"2022-05-07T02:04:51.496278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nfrom types import SimpleNamespace\n\nimport pandas as pd\nimport numpy as np\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport librosa\nimport librosa.display\nimport IPython.display as ipd\n\nwarnings.filterwarnings(\"ignore\")\nplt.style.use(\"ggplot\")\n\n%load_ext lab_black","metadata":{"execution":{"iopub.status.busy":"2022-05-07T03:16:51.24045Z","iopub.execute_input":"2022-05-07T03:16:51.240768Z","iopub.status.idle":"2022-05-07T03:16:51.25922Z","shell.execute_reply.started":"2022-05-07T03:16:51.240738Z","shell.execute_reply":"2022-05-07T03:16:51.258253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg = SimpleNamespace(\n    sample_rate=32_000,\n    window_size=2048,\n    hop_length=512,\n    mel_bins=128,\n    fmin=0,\n    fmax=16_000,\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T02:38:59.928242Z","iopub.execute_input":"2022-05-07T02:38:59.929308Z","iopub.status.idle":"2022-05-07T02:38:59.9441Z","shell.execute_reply.started":"2022-05-07T02:38:59.929245Z","shell.execute_reply":"2022-05-07T02:38:59.943003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_call_meta_df(\n    call_df, meta_df, train_meta_df, unit_frame_sec=5, sample_rate=32_000\n):\n    call_df = call_df.copy()\n    meta_df = meta_df.copy()\n    train_meta_df = train_meta_df.copy()\n    meta_df[\"num_5_sec_frames\"] = meta_df.length // (unit_frame_sec * sample_rate) + 1\n    meta_df[\"last_end_time\"] = meta_df[\"num_5_sec_frames\"] * unit_frame_sec\n\n    call_meta_df = pd.merge(call_df, meta_df, on=\"filename\", how=\"left\")\n    call_meta_df = call_meta_df.query(\"end_time <= last_end_time\")\n\n    train_meta_df.rename({\"filename\": \"original_filename\"}, axis=1, inplace=True)\n    call_meta_df = pd.merge(\n        call_meta_df,\n        train_meta_df,\n        on=\"original_filename\",\n        how=\"left\",\n    )\n    return call_meta_df\n\n\ndef split_pos_neg(input_df):\n    input_df = input_df.copy()\n    pos, neg = input_df.query(\"target > 0.5\"), input_df.query(\"target <= 0.5\")\n    return pos, neg\n\n\ndef visualize(rel_path, input_df, title):\n    input_df = input_df.copy()\n    path = f\"../input/birdclef-2022-subclip-60-sec/train_audio/{rel_path}\"\n    display(ipd.Audio(path))\n\n    # show mel spec\n    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(24, 6), sharex=True)\n    audio, _ = librosa.core.load(path, sr=cfg.sample_rate, mono=True)\n    melspec = librosa.feature.melspectrogram(\n        audio,\n        sr=cfg.sample_rate,\n        n_fft=cfg.window_size,\n        hop_length=cfg.hop_length,\n        n_mels=cfg.mel_bins,\n        power=1.0,\n        fmin=cfg.fmin,\n        fmax=cfg.fmax,\n    )\n    spec = librosa.pcen(\n        melspec * (2**31),\n        time_constant=0.06,\n        eps=1e-6,\n        gain=0.8,\n        power=0.25,\n        bias=10,\n        sr=cfg.sample_rate,\n        hop_length=cfg.hop_length,\n    )\n    colormesh = librosa.display.specshow(\n        spec,\n        hop_length=cfg.hop_length,\n        sr=cfg.sample_rate,\n        fmin=cfg.fmin,\n        fmax=cfg.fmax,\n        x_axis=\"time\",\n        y_axis=\"mel\",\n        ax=ax1,\n    )\n    ax1.set_title(\n        title,\n        fontsize=15,\n    )\n\n    ax2 = sns.lineplot(\n        x=\"time_sec\",\n        y=\"target\",\n        data=input_df.query(\"filename == @rel_path\"),\n        marker=\"o\",\n    )\n    ax2.set(ylim=(0, 1))","metadata":{"execution":{"iopub.status.busy":"2022-05-07T03:03:47.890238Z","iopub.execute_input":"2022-05-07T03:03:47.890609Z","iopub.status.idle":"2022-05-07T03:03:47.961799Z","shell.execute_reply.started":"2022-05-07T03:03:47.890575Z","shell.execute_reply":"2022-05-07T03:03:47.960975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sub clip 60 sec","metadata":{}},{"cell_type":"code","source":"call_df = pd.read_csv(\n    \"../input/birdclef22-add-binary-classifier-label-60sec/birdclef_2022_call_meta.csv\"\n)\nmeta_df = pd.read_csv(\"../input/birdclef-2022-subclip-60-sec/subclip_meta.csv\")\ntrain_meta_df = pd.read_csv(\"../input/birdclef-2022/train_metadata.csv\")\nscored_birds = pd.read_json(\"../input/birdclef-2022/scored_birds.json\")[0].tolist()","metadata":{"execution":{"iopub.status.busy":"2022-05-07T03:04:42.495161Z","iopub.execute_input":"2022-05-07T03:04:42.496023Z","iopub.status.idle":"2022-05-07T03:04:43.094753Z","shell.execute_reply.started":"2022-05-07T03:04:42.495981Z","shell.execute_reply":"2022-05-07T03:04:43.093719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"call_meta_df = create_call_meta_df(call_df, meta_df, train_meta_df)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T03:04:43.096334Z","iopub.execute_input":"2022-05-07T03:04:43.096619Z","iopub.status.idle":"2022-05-07T03:04:43.465669Z","shell.execute_reply.started":"2022-05-07T03:04:43.096585Z","shell.execute_reply":"2022-05-07T03:04:43.464652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(call_meta_df.query(\"target > 0.5\")) / len(call_meta_df)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T02:51:17.877894Z","iopub.execute_input":"2022-05-07T02:51:17.878421Z","iopub.status.idle":"2022-05-07T02:51:17.909988Z","shell.execute_reply.started":"2022-05-07T02:51:17.878365Z","shell.execute_reply":"2022-05-07T02:51:17.909075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_, ax = plt.subplots()\nsns.histplot(call_meta_df.target, ax=ax)\nax.set(title=\"Distribution of call probability\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-07T02:51:18.152761Z","iopub.execute_input":"2022-05-07T02:51:18.153346Z","iopub.status.idle":"2022-05-07T02:51:18.604279Z","shell.execute_reply.started":"2022-05-07T02:51:18.153282Z","shell.execute_reply":"2022-05-07T02:51:18.603526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scored_df = train_meta_df.query(\"primary_label in @scored_birds\")\nsorted_scored_birds = scored_df.value_counts(\"primary_label\").keys().tolist()","metadata":{"execution":{"iopub.status.busy":"2022-05-07T03:22:35.171512Z","iopub.execute_input":"2022-05-07T03:22:35.17211Z","iopub.status.idle":"2022-05-07T03:22:35.185987Z","shell.execute_reply.started":"2022-05-07T03:22:35.172071Z","shell.execute_reply":"2022-05-07T03:22:35.185236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pos_paths = []\nfor bird in sorted_scored_birds:\n    pos_path = (\n        call_meta_df.query(\"primary_label == @bird and target > 0.5\")\n        .sample(1)\n        .filename.item()\n    )\n    pos_paths.append(pos_path)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T03:22:43.183981Z","iopub.execute_input":"2022-05-07T03:22:43.184545Z","iopub.status.idle":"2022-05-07T03:22:43.413311Z","shell.execute_reply.started":"2022-05-07T03:22:43.184493Z","shell.execute_reply":"2022-05-07T03:22:43.412311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, p_path in enumerate(pos_paths):\n    print(f\"{i+1}: {p_path}\")\n    visualize(p_path, call_meta_df, title=p_path)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-07T03:31:18.17776Z","iopub.execute_input":"2022-05-07T03:31:18.178044Z","iopub.status.idle":"2022-05-07T03:31:39.257701Z","shell.execute_reply.started":"2022-05-07T03:31:18.178013Z","shell.execute_reply":"2022-05-07T03:31:39.256662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# subclip 5 sec","metadata":{}},{"cell_type":"code","source":"call_df_5sec = pd.read_csv(\n    \"../input/birdclef22-add-binary-classifier-label-5sec/birdclef_2022_call_meta_5_sec.csv\"\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T03:28:52.166578Z","iopub.execute_input":"2022-05-07T03:28:52.166883Z","iopub.status.idle":"2022-05-07T03:28:52.646858Z","shell.execute_reply.started":"2022-05-07T03:28:52.166852Z","shell.execute_reply":"2022-05-07T03:28:52.645684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"call_meta_df_5sec = create_call_meta_df(call_df_5sec, meta_df, train_meta_df)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T03:28:52.649585Z","iopub.execute_input":"2022-05-07T03:28:52.650121Z","iopub.status.idle":"2022-05-07T03:28:52.982142Z","shell.execute_reply.started":"2022-05-07T03:28:52.650081Z","shell.execute_reply":"2022-05-07T03:28:52.981411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(call_meta_df_5sec.query(\"target > 0.5\")) / len(call_meta_df_5sec)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T03:28:52.984119Z","iopub.execute_input":"2022-05-07T03:28:52.98464Z","iopub.status.idle":"2022-05-07T03:28:53.022213Z","shell.execute_reply.started":"2022-05-07T03:28:52.984593Z","shell.execute_reply":"2022-05-07T03:28:53.021202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_, ax = plt.subplots()\nsns.histplot(call_meta_df_5sec.target, ax=ax)\nax.set(title=\"Distribution of call probability\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-07T03:28:53.357531Z","iopub.execute_input":"2022-05-07T03:28:53.35788Z","iopub.status.idle":"2022-05-07T03:28:53.654568Z","shell.execute_reply.started":"2022-05-07T03:28:53.357848Z","shell.execute_reply":"2022-05-07T03:28:53.653437Z"},"trusted":true},"execution_count":null,"outputs":[]}]}