{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install /kaggle/input/fastai2-wheels/fastscript-0.1.4-py3-none-any.whl\n!pip install /kaggle/input/fastai2-wheels/nbdev-0.2.12-py3-none-any.whl\n!pip install /kaggle/input/fastai2-wheels/fastprogress-0.2.2-py3-none-any.whl\n!pip install /kaggle/input/fastai2-wheels/fastcore-0.1.10-py3-none-any.whl\n!pip install /kaggle/input/fastai2-wheels/fastai2-0.0.10-py3-none-any.whl","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport torch\nfrom fastai2.vision.all import *\nimport soundfile as sf\nfrom pathlib import Path\nimport librosa\nimport multiprocessing\nimport warnings\nwarnings.filterwarnings(\"ignore\", category=UserWarning)\n\nfrom torch.utils.data import Dataset\nfrom torch.utils.data import DataLoader","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"mean, std = (-6.132126808166504e-05, 0.04304003225515465)\nclasses = pd.read_pickle('../input/birdcall-first-model/classes.pkl')\n\nget_arch = lambda: nn.Sequential(*[\n    Lambda(lambda x: x.unsqueeze(1)),\n    ConvLayer(1, 16, ks=64, stride=2, ndim=1),\n    ConvLayer(16, 16, ks=8, stride=8, ndim=1),\n    ConvLayer(16, 32, ks=32, stride=2, ndim=1),\n    ConvLayer(32, 32, ks=8, stride=8, ndim=1),\n    ConvLayer(32, 64, ks=16, stride=2, ndim=1),\n    ConvLayer(64, 128, ks=8, stride=2, ndim=1),\n    ConvLayer(128, 256, ks=4, stride=2, ndim=1),\n    ConvLayer(256, 256, ks=4, stride=4, ndim=1),\n    Flatten(),\n    LinBnDrop(5120, 512, p=0.25, act=nn.ReLU()),\n    LinBnDrop(512, 512, p=0.25, act=nn.ReLU()),\n    LinBnDrop(512, 256, p=0.25, act=nn.ReLU()),\n    LinBnDrop(256, len(classes)),\n    nn.Sigmoid()\n])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = get_arch()\nmodel.load_state_dict(torch.load('../input/birdcall-first-model/first_model.pth'))\nmodel.cuda()\nmodel.eval();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"SAMPLE_RATE = 32_000\n\nTEST_PATH = Path('../input/birdsong-recognition') if os.path.exists('../input/birdsong-recognition/test_audio') else Path('../input/birdcall-check')\n\nTEST_AUDIO_PATH = TEST_PATH/'test_audio'\ntest_df = pd.read_csv(TEST_PATH/'test.csv')\ntest_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class AudioDataset(Dataset):\n    def __init__(self, items, classes, rec, mean=None, std=None):\n        self.items = items\n        self.vocab = classes\n        self.do_norm = (mean and std)\n        self.mean = mean\n        self.std = std\n        self.rec = rec\n    def __getitem__(self, idx):\n        _, rec_fn, start = self.items[idx]\n        x = self.rec[start*SAMPLE_RATE:(start+5)*SAMPLE_RATE]\n        if self.do_norm: x = self.normalize(x)\n        return x.astype(np.float32)\n    def normalize(self, x):\n        return (x - self.mean) / self.std    \n    def __len__(self):\n        return len(self.items)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\n\nrow_ids = []\nresults = []\n\nfor audio_id in test_df[test_df.site.isin(['site_1', 'site_2'])].audio_id.unique():\n    items = [(row.row_id, row.audio_id, int(row.seconds)-5) for idx, row in test_df[test_df.audio_id == audio_id].iterrows()]\n    rec = librosa.load(TEST_AUDIO_PATH/f'{audio_id}.mp3', sr=SAMPLE_RATE, res_type='kaiser_fast')[0]\n    test_ds = AudioDataset(items, classes, rec, mean=mean, std=std)\n    dl = DataLoader(test_ds, batch_size=128)\n    for batch in dl:\n        with torch.no_grad():\n            preds = model(batch.cuda()).cpu().detach()\n            for row in preds:\n                birds = []\n                for idx in np.where(row > 0.5)[0]:\n                    birds.append(classes[idx])\n                if not birds: birds = ['nocall']\n                results.append(' '.join(birds)) \n    row_ids += [item[0] for item in items]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predicted = pd.DataFrame(data={'row_id': row_ids, 'birds': results})\n\nsub = pd.DataFrame(data={'row_id': test_df.row_id})\nsub = sub.merge(predicted, 'left', 'row_id')\nsub.fillna('nocall', inplace=True)\nsub.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}