{"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":"code","source":"import os\nfrom pathlib import Path\nfrom joblib import Parallel, delayed\nfrom tqdm import tqdm\nimport glob\nimport pandas as pd\nimport torch\nimport torch.nn as nn\nimport torchaudio\nimport timm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-14T04:39:29.129299Z","iopub.execute_input":"2023-04-14T04:39:29.129922Z","iopub.status.idle":"2023-04-14T04:39:34.525986Z","shell.execute_reply.started":"2023-04-14T04:39:29.129883Z","shell.execute_reply":"2023-04-14T04:39:34.525043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Paste from [Data] notebook","metadata":{}},{"cell_type":"code","source":"MIN_WINDOW = 32_000 * 5\n\ndef maybe_pad(audio):\n    if len(audio) < MIN_WINDOW:\n        return torch.concat([audio, torch.zeros(MIN_WINDOW - len(audio))])\n    return audio\n\ncompute_melspec = torchaudio.transforms.MelSpectrogram(\n    sample_rate=32_000,\n    n_mels=128,\n    n_fft=2048, \n    hop_length=512,\n    f_min=0,\n    f_max=32_000 // 2,\n)\n\npower_to_db = torchaudio.transforms.AmplitudeToDB(\n    stype=\"power\",\n    top_db=80.0,\n)\n\ndef normalize_to_uint8(X):\n    mean, std = X.mean(), X.std()\n    if std < 1e-6:\n        raise Exception(\"std is too low\")\n    X = (X - mean) / std\n    _min, _max = X.min(), X.max()\n    return (255 * (X - _min) / (_max - _min)).type(torch.uint8)","metadata":{"execution":{"iopub.status.busy":"2023-04-14T04:39:34.527943Z","iopub.execute_input":"2023-04-14T04:39:34.528475Z","iopub.status.idle":"2023-04-14T04:39:34.641455Z","shell.execute_reply.started":"2023-04-14T04:39:34.528441Z","shell.execute_reply":"2023-04-14T04:39:34.639832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Paste from [Train] notebook","metadata":{}},{"cell_type":"code","source":"def make_model(out_features, pretrained):\n    model = timm.create_model('tf_efficientnet_b0_ns', pretrained=pretrained)\n    model.classifier = nn.Linear(in_features=model.classifier.in_features, out_features=out_features)\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-04-14T04:39:34.644046Z","iopub.execute_input":"2023-04-14T04:39:34.644597Z","iopub.status.idle":"2023-04-14T04:39:34.652912Z","shell.execute_reply.started":"2023-04-14T04:39:34.644547Z","shell.execute_reply":"2023-04-14T04:39:34.651163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load stuff","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/birdclef-2023/train_metadata.csv')\nindex_to_label = sorted(df.primary_label.unique())\n\nmodel = make_model(len(index_to_label), pretrained=False)\nmodel.load_state_dict(torch.load('/kaggle/input/bc23-train-melspecs/models/model__0__13__tensor(1_3455).pt', map_location='cpu')['model_state_dict'])\nmodel.eval()\n\nfilepaths = glob.glob('/kaggle/input/birdclef-2023/test_soundscapes/*.ogg')\n#filepaths = [filepaths[0] for i in range(200)] # simulate submission","metadata":{"execution":{"iopub.status.busy":"2023-04-14T04:39:34.656262Z","iopub.execute_input":"2023-04-14T04:39:34.656673Z","iopub.status.idle":"2023-04-14T04:39:35.382555Z","shell.execute_reply.started":"2023-04-14T04:39:34.656634Z","shell.execute_reply":"2023-04-14T04:39:35.380999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference","metadata":{}},{"cell_type":"code","source":"def process(filepath):\n    all_predictions = []\n    name = Path(filepath).stem\n    audio = torchaudio.load(filepath)[0][0]\n    for i in range(0, 120):\n        crop = audio[i*MIN_WINDOW:(i+1)*MIN_WINDOW]\n        data = normalize_to_uint8(power_to_db(compute_melspec(maybe_pad(crop))))\n        data = data / 127 - 1 # normalize to float\n        data = torch.stack([data, data, data])\n        with torch.no_grad():\n            pred = torch.sigmoid(model(data[None])[0])\n        t = (i + 1) * 5\n        all_predictions.append({\"row_id\": f'{name}_{t}',\"predictions\": pred})\n    return all_predictions\n\nall_predictions = Parallel(n_jobs=os.cpu_count())(\n    delayed(process)(filepath) \n    for filepath in tqdm(filepaths, 'Processing files')\n)\nall_predictions = [p2 for p in all_predictions for p2 in p] # flatten","metadata":{"execution":{"iopub.status.busy":"2023-04-14T04:39:35.384463Z","iopub.execute_input":"2023-04-14T04:39:35.385641Z","iopub.status.idle":"2023-04-14T04:40:12.151705Z","shell.execute_reply.started":"2023-04-14T04:39:35.385591Z","shell.execute_reply":"2023-04-14T04:40:12.149470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.concat([\n    pd.DataFrame({'row_id': [p['row_id'] for p in all_predictions]}), \n    pd.DataFrame(torch.stack([p['predictions'] for p in all_predictions]).numpy(), columns=index_to_label)\n], axis=1)\n\ndf","metadata":{"execution":{"iopub.status.busy":"2023-04-14T04:40:12.152601Z","iopub.status.idle":"2023-04-14T04:40:12.153015Z","shell.execute_reply.started":"2023-04-14T04:40:12.152799Z","shell.execute_reply":"2023-04-14T04:40:12.152818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-14T04:40:12.155470Z","iopub.status.idle":"2023-04-14T04:40:12.155977Z","shell.execute_reply.started":"2023-04-14T04:40:12.155743Z","shell.execute_reply":"2023-04-14T04:40:12.155764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head -2 submission.csv","metadata":{"execution":{"iopub.status.busy":"2023-04-14T04:40:12.158101Z","iopub.status.idle":"2023-04-14T04:40:12.158943Z","shell.execute_reply.started":"2023-04-14T04:40:12.158565Z","shell.execute_reply":"2023-04-14T04:40:12.158611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}