{"cells":[{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"!pip uninstall fastai torch torchaudio fastcore torchvision -y ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"!pip install ../input/packages/packages/packages/colorednoise-1.1.1/colorednoise-1.1.1 --find-links ../input/packages/packages --no-index --use-feature=2020-resolver","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"!pip install ../input/packages/fastaudio-0.0.post0.dev143gc7a2b85.dirty-py2.py3-none-any.whl --find-links ../input/packages/packages/packages --no-index --verbose --upgrade --use-feature=2020-resolver","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from fastai.vision.all import *","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from fastaudio.core.all import *\nfrom fastaudio.augment.all import *","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"path = Path('/kaggle/input/learner-no-noise')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset_mapping = {\n    'a': Path('../input/birdsong-resampled-train-audio-00'),\n    'b': Path('../input/birdsong-resampled-train-audio-00'),\n    'c': Path('../input/birdsong-resampled-train-audio-01'),\n    'd': Path('../input/birdsong-resampled-train-audio-01'),\n    'e': Path('../input/birdsong-resampled-train-audio-01'),\n    'f': Path('../input/birdsong-resampled-train-audio-01'),\n    'g': Path('../input/birdsong-resampled-train-audio-02'),\n    'h': Path('../input/birdsong-resampled-train-audio-02'),\n    'i': Path('../input/birdsong-resampled-train-audio-02'),\n    'j': Path('../input/birdsong-resampled-train-audio-02'),\n    'k': Path('../input/birdsong-resampled-train-audio-02'),\n    'l': Path('../input/birdsong-resampled-train-audio-02'),\n    'm': Path('../input/birdsong-resampled-train-audio-02'),\n    'n': Path('../input/birdsong-resampled-train-audio-03'),\n    'o': Path('../input/birdsong-resampled-train-audio-03'),\n    'p': Path('../input/birdsong-resampled-train-audio-03'),\n    'q': Path('../input/birdsong-resampled-train-audio-03'),\n    'r': Path('../input/birdsong-resampled-train-audio-03'),\n    's': Path('../input/birdsong-resampled-train-audio-04'),\n    't': Path('../input/birdsong-resampled-train-audio-04'),\n    'u': Path('../input/birdsong-resampled-train-audio-04'),\n    'v': Path('../input/birdsong-resampled-train-audio-04'),\n    'w': Path('../input/birdsong-resampled-train-audio-04'),\n    'x': Path('../input/birdsong-resampled-train-audio-04'),\n    'y': Path('../input/birdsong-resampled-train-audio-04'),\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"BIRD_CODE = {\n    'aldfly': 0, 'ameavo': 1, 'amebit': 2, 'amecro': 3, 'amegfi': 4,\n    'amekes': 5, 'amepip': 6, 'amered': 7, 'amerob': 8, 'amewig': 9,\n    'amewoo': 10, 'amtspa': 11, 'annhum': 12, 'astfly': 13, 'baisan': 14,\n    'baleag': 15, 'balori': 16, 'banswa': 17, 'barswa': 18, 'bawwar': 19,\n    'belkin1': 20, 'belspa2': 21, 'bewwre': 22, 'bkbcuc': 23, 'bkbmag1': 24,\n    'bkbwar': 25, 'bkcchi': 26, 'bkchum': 27, 'bkhgro': 28, 'bkpwar': 29,\n    'bktspa': 30, 'blkpho': 31, 'blugrb1': 32, 'blujay': 33, 'bnhcow': 34,\n    'boboli': 35, 'bongul': 36, 'brdowl': 37, 'brebla': 38, 'brespa': 39,\n    'brncre': 40, 'brnthr': 41, 'brthum': 42, 'brwhaw': 43, 'btbwar': 44,\n    'btnwar': 45, 'btywar': 46, 'buffle': 47, 'buggna': 48, 'buhvir': 49,\n    'bulori': 50, 'bushti': 51, 'buwtea': 52, 'buwwar': 53, 'cacwre': 54,\n    'calgul': 55, 'calqua': 56, 'camwar': 57, 'cangoo': 58, 'canwar': 59,\n    'canwre': 60, 'carwre': 61, 'casfin': 62, 'caster1': 63, 'casvir': 64,\n    'cedwax': 65, 'chispa': 66, 'chiswi': 67, 'chswar': 68, 'chukar': 69,\n    'clanut': 70, 'cliswa': 71, 'comgol': 72, 'comgra': 73, 'comloo': 74,\n    'commer': 75, 'comnig': 76, 'comrav': 77, 'comred': 78, 'comter': 79,\n    'comyel': 80, 'coohaw': 81, 'coshum': 82, 'cowscj1': 83, 'daejun': 84,\n    'doccor': 85, 'dowwoo': 86, 'dusfly': 87, 'eargre': 88, 'easblu': 89,\n    'easkin': 90, 'easmea': 91, 'easpho': 92, 'eastow': 93, 'eawpew': 94,\n    'eucdov': 95, 'eursta': 96, 'evegro': 97, 'fiespa': 98, 'fiscro': 99,\n    'foxspa': 100, 'gadwal': 101, 'gcrfin': 102, 'gnttow': 103, 'gnwtea': 104,\n    'gockin': 105, 'gocspa': 106, 'goleag': 107, 'grbher3': 108, 'grcfly': 109,\n    'greegr': 110, 'greroa': 111, 'greyel': 112, 'grhowl': 113, 'grnher': 114,\n    'grtgra': 115, 'grycat': 116, 'gryfly': 117, 'haiwoo': 118, 'hamfly': 119,\n    'hergul': 120, 'herthr': 121, 'hoomer': 122, 'hoowar': 123, 'horgre': 124,\n    'horlar': 125, 'houfin': 126, 'houspa': 127, 'houwre': 128, 'indbun': 129,\n    'juntit1': 130, 'killde': 131, 'labwoo': 132, 'larspa': 133, 'lazbun': 134,\n    'leabit': 135, 'leafly': 136, 'leasan': 137, 'lecthr': 138, 'lesgol': 139,\n    'lesnig': 140, 'lesyel': 141, 'lewwoo': 142, 'linspa': 143, 'lobcur': 144,\n    'lobdow': 145, 'logshr': 146, 'lotduc': 147, 'louwat': 148, 'macwar': 149,\n    'magwar': 150, 'mallar3': 151, 'marwre': 152, 'merlin': 153, 'moublu': 154,\n    'mouchi': 155, 'moudov': 156, 'norcar': 157, 'norfli': 158, 'norhar2': 159,\n    'normoc': 160, 'norpar': 161, 'norpin': 162, 'norsho': 163, 'norwat': 164,\n    'nrwswa': 165, 'nutwoo': 166, 'olsfly': 167, 'orcwar': 168, 'osprey': 169,\n    'ovenbi1': 170, 'palwar': 171, 'pasfly': 172, 'pecsan': 173, 'perfal': 174,\n    'phaino': 175, 'pibgre': 176, 'pilwoo': 177, 'pingro': 178, 'pinjay': 179,\n    'pinsis': 180, 'pinwar': 181, 'plsvir': 182, 'prawar': 183, 'purfin': 184,\n    'pygnut': 185, 'rebmer': 186, 'rebnut': 187, 'rebsap': 188, 'rebwoo': 189,\n    'redcro': 190, 'redhea': 191, 'reevir1': 192, 'renpha': 193, 'reshaw': 194,\n    'rethaw': 195, 'rewbla': 196, 'ribgul': 197, 'rinduc': 198, 'robgro': 199,\n    'rocpig': 200, 'rocwre': 201, 'rthhum': 202, 'ruckin': 203, 'rudduc': 204,\n    'rufgro': 205, 'rufhum': 206, 'rusbla': 207, 'sagspa1': 208, 'sagthr': 209,\n    'savspa': 210, 'saypho': 211, 'scatan': 212, 'scoori': 213, 'semplo': 214,\n    'semsan': 215, 'sheowl': 216, 'shshaw': 217, 'snobun': 218, 'snogoo': 219,\n    'solsan': 220, 'sonspa': 221, 'sora': 222, 'sposan': 223, 'spotow': 224,\n    'stejay': 225, 'swahaw': 226, 'swaspa': 227, 'swathr': 228, 'treswa': 229,\n    'truswa': 230, 'tuftit': 231, 'tunswa': 232, 'veery': 233, 'vesspa': 234,\n    'vigswa': 235, 'warvir': 236, 'wesblu': 237, 'wesgre': 238, 'weskin': 239,\n    'wesmea': 240, 'wessan': 241, 'westan': 242, 'wewpew': 243, 'whbnut': 244,\n    'whcspa': 245, 'whfibi': 246, 'whtspa': 247, 'whtswi': 248, 'wilfly': 249,\n    'wilsni1': 250, 'wiltur': 251, 'winwre3': 252, 'wlswar': 253, 'wooduc': 254,\n    'wooscj2': 255, 'woothr': 256, 'y00475': 257, 'yebfly': 258, 'yebsap': 259,\n    'yehbla': 260, 'yelwar': 261, 'yerwar': 262, 'yetvir': 263\n}\n\nINV_BIRD_CODE = {v: k for k, v in BIRD_CODE.items()}\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_x(x):\n    fp = x['file_path']\n    path = dataset_mapping[fp[0]]\n    return path/fp\n\ndef get_y(x): \n    return [x['ebird_code']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = load_learner(path/'export.pkl',cpu=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.save('model.pth', with_opt=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path_bird = Path('/kaggle/input/birdsong-recognition/')\npath_bird.ls()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if not (path_bird/'test_audio').exists():\n    path_bird = Path('/kaggle/input/birdcall-check')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path_bird","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(path_bird/'test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"group = df.groupby(['site'])['audio_id'].value_counts()\ngroup.name = 'counts'\ngdf = pd.DataFrame(group).reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from pydub import AudioSegment as AS\nfrom pydub.effects import normalize","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TSR = 32000","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def normalize(x):\n    return librosa.util.normalize(x)\n\ndef read(file, norm=True):\n    try:\n        a = AS.from_mp3(file)\n        a = a.set_frame_rate(TSR)\n    except:\n        return TSR, np.zeros(MAXLEN)\n\n    y = np.array(a.get_array_of_samples())\n    if a.channels == 2: y = y.reshape((-1, 2)).mean(axis=1)\n    if norm: return a.frame_rate, normalize(y)\n    if not norm: return a.frame_rate, np.float32(y)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"file = path_bird/'test_audio'/'41e6fe6504a34bf6846938ba78d13df1.mp3'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sr, x = read(file)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class Site3Dataset:\n    def __init__(self, df, path):\n        self.df = df\n        self.path = path\n        self.transforms = [torchaudio.transforms.MelSpectrogram(sample_rate=32000, n_fft=512),\n                           torchaudio.transforms.AmplitudeToDB()]\n        self.clips = None\n    def __len__(self):\n        return len(self.df)*2\n    \n    def __getitem__(self, idx):\n        row = self.df.iloc[idx//2]\n        if self.clips is not None:\n            clips = self.clips\n            self.clips = None\n            return {\n            'audio_id': row['audio_id'],\n            'spec': clips,\n            }\n        row = self.df.iloc[idx//2]\n        filename= row['audio_id']+'.mp3'\n        site = row['site']\n        sr,x  = read(self.path/'test_audio'/filename)\n        x = torch.from_numpy(x)\n        start_idxs = np.arange(0,len(x))[::sr*5]\n        end_idxs = np.arange(0,len(x))[sr*5::sr*5]\n        sub_count = len(end_idxs)\n        clips = []\n        for lo,hi in zip(start_idxs[:sub_count], end_idxs[:sub_count]):\n            clip = x[lo:hi]\n            clips.append(clip)\n        clips = torch.stack(clips).float()\n        for tfm in self.transforms:\n            clips = tfm(clips)\n        self.clips = clips[len(clips)//2:,...]\n        \n        return {\n            'audio_id': row['audio_id'],\n            'spec': clips,\n        }\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"site_3_df = df.loc[df.loc[:,'site']== 'site_3']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class Dataset:\n    def __init__(self, df, path):\n        self.df = df\n        self.path = path\n        self.transforms = [torchaudio.transforms.MelSpectrogram(sample_rate=32000, n_fft=512),\n                           torchaudio.transforms.AmplitudeToDB()]\n\n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        row = self.df.iloc[idx]\n        seconds = np.int(row['seconds'].item())\n        filename = row['audio_id']+'.mp3'\n        site = row['site']\n        sr,x  = read(self.path/'test_audio'/filename)\n        x = torch.from_numpy(x).float()\n        start_idxs = sr*(seconds-5)\n        end_idxs = sr*(seconds)\n        clip = x[start_idxs:end_idxs]\n        for tfm in self.transforms:\n            clip = tfm(clip)\n                        \n        return {\n            'audio_id': row['audio_id'],\n            'spec': clip,\n        }","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_s = df.loc[df.loc[:,'site'] != 'site_3']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ds = Dataset(df_s, path_bird)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dl = torch.utils.data.DataLoader(ds, batch_size=4, shuffle=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"site3_ds = Site3Dataset(site_3_df,path_bird)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dl_s3 = torch.utils.data.DataLoader(site3_ds, batch_size=1, shuffle=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import gc","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['birds'] = ''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\nlearn.model.eval()\npredictions = []\nfor x in dl:\n    pred = 0\n    x['spec']=x['spec'].cuda()\n    if len(x['spec'].squeeze().shape)==3:\n        print(x['spec'].squeeze()[:,None].shape)\n        pred = learn.model(x['spec'].squeeze()[:,None])\n    else:\n        pred = learn.model(x['spec'])\n    predictions.append(pred.detach().cpu().numpy())\n    x['spec']=x['spec'].cpu()\n\ndef get_batch(x, bs=4):\n    for i in range(0,x.shape[1], bs):\n        yield x[:,i:i+bs,...].cuda()\n\n# for x in dl_s3:\n#     pred = []\n#     for batch in get_batch(x['spec']):\n#         if len(batch.squeeze().shape)==3:\n#             print(batch.squeeze()[:,None].shape)\n#             pred.append(learn.model(batch.squeeze()[:,None]))\n#         else:\n#             pred.append(learn.model(batch))\n#     predictions.append(torch.cat(pred).detach().cpu().numpy())\n#     gc.collect()\n\nproc_preds = []\nthresh = 0.2\nfor prediction in predictions:\n    pred = (torch.from_numpy(prediction).sigmoid() > thresh).float()\n    proc_preds.append(pred)\n\ndf['birds']=df['birds'].map(lambda x: list())\n\nj = 0\nfor i, pred in enumerate(proc_preds):\n    num_preds = len(pred)\n    start_idx = i*num_preds\n    end_idx = (i+1)*num_preds\n    pred_idx, pred_y = torch.where(pred==1)\n    for idx, bird_code in zip(pred_idx, pred_y):\n        if df.loc[j,'site'] == 'site_3':\n            num_preds = 1\n            df.iloc[j,-1].append(INV_BIRD_CODE[bird_code.item()])\n            continue\n\n        df.iloc[j+idx.item(),-1].append(INV_BIRD_CODE[bird_code.item()])\n    j += num_preds\ndf['birds'] = df['birds'].map(lambda x: set(x))\ndf['birds'] = df['birds'].map(lambda x: ' '.join(x) if len(x)>0 else 'nocall')\n\ndf[['row_id','birds']].to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['birds']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}