{"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":"!pip install ../input/birdclef22source/noisereduce-2.0.1-py3-none-any.whl --no-index --no-deps\n!pip install ../input/birdclef22source/colorednoise-2.1.0-py3-none-any.whl --no-index --no-deps\n!pip install ../input/birdclef22source/timm-0.4.5-py3-none-any.whl --no-index --no-deps","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-12-14T15:29:40.975063Z","iopub.execute_input":"2022-12-14T15:29:40.976046Z","iopub.status.idle":"2022-12-14T15:29:49.955724Z","shell.execute_reply.started":"2022-12-14T15:29:40.975941Z","shell.execute_reply":"2022-12-14T15:29:49.954590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset, DataLoader\nimport pandas as pd\nimport numpy as np\nimport sys\nimport librosa\nsys.path.append('/kaggle/input/birdclef22source')","metadata":{"execution":{"iopub.status.busy":"2022-12-14T15:29:49.959398Z","iopub.execute_input":"2022-12-14T15:29:49.959718Z","iopub.status.idle":"2022-12-14T15:29:53.198397Z","shell.execute_reply.started":"2022-12-14T15:29:49.959687Z","shell.execute_reply":"2022-12-14T15:29:53.197347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os, torch\nimport models, config, helpers, inference\nfrom dataset import normalize_0_1\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2022-12-14T15:29:53.200039Z","iopub.execute_input":"2022-12-14T15:29:53.200651Z","iopub.status.idle":"2022-12-14T15:29:56.142793Z","shell.execute_reply.started":"2022-12-14T15:29:53.200610Z","shell.execute_reply":"2022-12-14T15:29:56.141619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc_path = '/kaggle/input/birdclef-2022/test_soundscapes'\nfile_list = [f.split('.')[0] for f in sorted(os.listdir(sc_path))]\nmapping = pd.read_csv('/kaggle/input/birdclef22source/mapping.csv')","metadata":{"execution":{"iopub.status.busy":"2022-12-14T15:29:56.145958Z","iopub.execute_input":"2022-12-14T15:29:56.146357Z","iopub.status.idle":"2022-12-14T15:29:56.165172Z","shell.execute_reply.started":"2022-12-14T15:29:56.146318Z","shell.execute_reply":"2022-12-14T15:29:56.164314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load the Models","metadata":{}},{"cell_type":"code","source":"mel_length = int(config.signal_conf['len_segment'] * config.signal_conf['sr']  / config.signal_conf['hop_length'] + 1)","metadata":{"execution":{"iopub.status.busy":"2022-12-14T15:29:56.166461Z","iopub.execute_input":"2022-12-14T15:29:56.166907Z","iopub.status.idle":"2022-12-14T15:29:56.172167Z","shell.execute_reply.started":"2022-12-14T15:29:56.166872Z","shell.execute_reply":"2022-12-14T15:29:56.171237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_list = []\nmodel_path = '/kaggle/input/birdclef22source/weigths/weigths/ast'\nfor item in os.listdir(model_path):\n    #model = models.CNNModel(in_dim=1)\n    model = models.ASTModel()\n    model.load_state_dict(torch.load(os.path.join(model_path, item), map_location=torch.device('cpu')))\n    model.eval()\n    model_list.append(model.to('cuda' if torch.cuda.is_available() else 'cpu'))\n    del model","metadata":{"execution":{"iopub.status.busy":"2022-12-14T15:29:56.173395Z","iopub.execute_input":"2022-12-14T15:29:56.175440Z","iopub.status.idle":"2022-12-14T15:30:04.048996Z","shell.execute_reply.started":"2022-12-14T15:29:56.175412Z","shell.execute_reply":"2022-12-14T15:30:04.047916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Do Inference","metadata":{}},{"cell_type":"code","source":"def repeat_spec(spec, length):\n    div = length / spec.shape[1]\n    div_int = int(div)\n    div_rest = div - div_int\n    specs = []\n    for _ in range(div_int):\n        specs.append(spec)\n    if div_rest != 0:\n        specs.append(spec[:,:int(div_rest*spec.shape[1])+1])\n    return np.hstack(specs)","metadata":{"execution":{"iopub.status.busy":"2022-12-14T15:30:04.050516Z","iopub.execute_input":"2022-12-14T15:30:04.050915Z","iopub.status.idle":"2022-12-14T15:30:04.058278Z","shell.execute_reply.started":"2022-12-14T15:30:04.050878Z","shell.execute_reply":"2022-12-14T15:30:04.056998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"do_padding = False","metadata":{"execution":{"iopub.status.busy":"2022-12-14T15:30:04.059932Z","iopub.execute_input":"2022-12-14T15:30:04.060633Z","iopub.status.idle":"2022-12-14T15:30:04.069529Z","shell.execute_reply.started":"2022-12-14T15:30:04.060589Z","shell.execute_reply":"2022-12-14T15:30:04.068579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nwith open('/kaggle/input/birdclef-2022/scored_birds.json') as sbfile:\n    scored_birds = json.load(sbfile)","metadata":{"execution":{"iopub.status.busy":"2022-12-14T15:43:48.901081Z","iopub.execute_input":"2022-12-14T15:43:48.901682Z","iopub.status.idle":"2022-12-14T15:43:48.911276Z","shell.execute_reply.started":"2022-12-14T15:43:48.901637Z","shell.execute_reply":"2022-12-14T15:43:48.910321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = {'row_id': [], 'target': []}","metadata":{"execution":{"iopub.status.busy":"2022-12-14T15:43:49.183552Z","iopub.execute_input":"2022-12-14T15:43:49.183999Z","iopub.status.idle":"2022-12-14T15:43:49.190037Z","shell.execute_reply.started":"2022-12-14T15:43:49.183963Z","shell.execute_reply":"2022-12-14T15:43:49.188987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for afile in file_list:\n    \n    path = os.path.join(sc_path, afile + '.ogg')\n    n_chunks = 12\n    \n    data, sr= librosa.load(path, sr=32000,duration=60, res_type='kaiser_fast')\n    mel = inference.get_mel(data, True, True)\n    mel = normalize_0_1(mel)\n    \n    for i in range(n_chunks):\n        start_time = 5*i\n        end_time = start_time + 5\n        start_time_samples = int(start_time * 32_000 / 512)\n        chunk = mel[:,start_time_samples:start_time_samples+313]\n        # pad or repeat until 30 seconds reached\n        if do_padding:\n            pad_len = mel_length - chunk.shape[1]\n            padding = np.zeros((chunk.shape[0], pad_len))\n            chunk = np.hstack((chunk, padding))\n        else:\n            chunk = repeat_spec(chunk, mel_length)\n            \n    \n        mel_tensor = torch.Tensor(chunk).reshape((1,chunk.shape[0], chunk.shape[1])).to('cuda' if torch.cuda.is_available() else 'cpu')\n\n        # do inference for each bird\n        for bird in scored_birds:\n            # predict with the models\n            logits = torch.zeros(len(mapping))\n            for model in model_list:\n                with torch.no_grad():\n                    out = model(mel_tensor).squeeze()\n                    logits += out.detach().cpu()\n            # take the maximum\n            logits = logits.unsqueeze(0)\n            sigmo = torch.sigmoid(logits)\n            _, idxs = np.where(sigmo>0.5)\n            pred_name = mapping.iloc[idxs,:].iloc[:,1].to_numpy()\n            # generate submission\n            row_id = afile + '_' + bird + '_' + str(end_time)\n            #print(bird, pred_name, bird==pred_name)\n            pred['row_id'].append(row_id)\n            pred['target'].append(True if bird in pred_name else False)","metadata":{"execution":{"iopub.status.busy":"2022-12-14T15:44:18.411206Z","iopub.execute_input":"2022-12-14T15:44:18.411593Z","iopub.status.idle":"2022-12-14T15:44:30.723857Z","shell.execute_reply.started":"2022-12-14T15:44:18.411563Z","shell.execute_reply":"2022-12-14T15:44:30.722789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = pd.DataFrame(pred, columns = ['row_id', 'target'])\nprint(results.sample(20))","metadata":{"execution":{"iopub.status.busy":"2022-12-14T15:44:43.264368Z","iopub.execute_input":"2022-12-14T15:44:43.264735Z","iopub.status.idle":"2022-12-14T15:44:43.274958Z","shell.execute_reply.started":"2022-12-14T15:44:43.264704Z","shell.execute_reply":"2022-12-14T15:44:43.273772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-14T15:30:23.937244Z","iopub.execute_input":"2022-12-14T15:30:23.937615Z","iopub.status.idle":"2022-12-14T15:30:23.955287Z","shell.execute_reply.started":"2022-12-14T15:30:23.937579Z","shell.execute_reply":"2022-12-14T15:30:23.954270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}