{"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 sys\nsys.path.append('../input/timm-pytorch-image-models/pytorch-image-models-master')\nimport timm","metadata":{"execution":{"iopub.status.busy":"2022-04-14T12:53:02.415769Z","iopub.execute_input":"2022-04-14T12:53:02.416032Z","iopub.status.idle":"2022-04-14T12:53:02.419991Z","shell.execute_reply.started":"2022-04-14T12:53:02.416002Z","shell.execute_reply":"2022-04-14T12:53:02.418974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-04-14T12:53:02.482494Z","iopub.execute_input":"2022-04-14T12:53:02.482990Z","iopub.status.idle":"2022-04-14T12:53:02.487072Z","shell.execute_reply.started":"2022-04-14T12:53:02.482952Z","shell.execute_reply":"2022-04-14T12:53:02.486315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport torch\nimport os\nimport torch.nn as nn\nimport albumentations as albu\nfrom albumentations.pytorch import ToTensorV2\nimport soundfile as sf\nimport librosa","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-14T12:53:02.527765Z","iopub.execute_input":"2022-04-14T12:53:02.528343Z","iopub.status.idle":"2022-04-14T12:53:02.532687Z","shell.execute_reply.started":"2022-04-14T12:53:02.528295Z","shell.execute_reply":"2022-04-14T12:53:02.531958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_audio_dir = '../input/birdclef-2022/test_soundscapes/'\nfile_list = [f.split('.')[0] for f in sorted(os.listdir(test_audio_dir))]\n\nprint('Number of test soundscapes:', len(file_list))","metadata":{"execution":{"iopub.status.busy":"2022-04-14T12:53:02.614049Z","iopub.execute_input":"2022-04-14T12:53:02.614509Z","iopub.status.idle":"2022-04-14T12:53:02.623381Z","shell.execute_reply.started":"2022-04-14T12:53:02.614468Z","shell.execute_reply":"2022-04-14T12:53:02.622679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_list","metadata":{"execution":{"iopub.status.busy":"2022-04-14T12:53:02.692662Z","iopub.execute_input":"2022-04-14T12:53:02.693308Z","iopub.status.idle":"2022-04-14T12:53:02.698702Z","shell.execute_reply.started":"2022-04-14T12:53:02.693272Z","shell.execute_reply":"2022-04-14T12:53:02.697893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scored_bird = [\"akiapo\", \"aniani\", \"apapan\", \"barpet\", \"crehon\", \"elepai\", \n               \"ercfra\", \"hawama\", \"hawcre\", \"hawgoo\", \"hawhaw\", \"hawpet1\",\n               \"houfin\", \"iiwi\", \"jabwar\", \"maupar\", \"omao\", \"puaioh\", \"skylar\",\n               \"warwhe1\", \"yefcan\"]\nlen(scored_bird)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T12:53:02.793591Z","iopub.execute_input":"2022-04-14T12:53:02.794199Z","iopub.status.idle":"2022-04-14T12:53:02.801694Z","shell.execute_reply.started":"2022-04-14T12:53:02.794160Z","shell.execute_reply":"2022-04-14T12:53:02.800803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.backbone = timm.create_model('tf_efficientnet_b0', in_chans=1, pretrained=False)\n        in_features = self.backbone.classifier.in_features\n        self.backbone.classifier = nn.Identity()\n        self.fc = nn.Sequential(\n            nn.Dropout(0.2), \n            nn.Linear(in_features, 21)\n        )\n    def forward(self, x):\n        feature = self.backbone(x)\n        logits = self.fc(feature)\n        return logits","metadata":{"execution":{"iopub.status.busy":"2022-04-14T12:53:02.862293Z","iopub.execute_input":"2022-04-14T12:53:02.862775Z","iopub.status.idle":"2022-04-14T12:53:02.869304Z","shell.execute_reply.started":"2022-04-14T12:53:02.862740Z","shell.execute_reply":"2022-04-14T12:53:02.868446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelTanks = [\n    '../input/exp-b0-5s-5folds/exp001_tf_efficientnet_b0_5clip_fold0.ckpt',\n    '../input/exp-b0-5s-5folds/exp001_tf_efficientnet_b0_5clip_fold1.ckpt',\n    '../input/exp-b0-5s-5folds/exp001_tf_efficientnet_b0_5clip_fold2.ckpt',\n    '../input/exp-b0-5s-5folds/exp001_tf_efficientnet_b0_5clip_fold3.ckpt',\n    '../input/exp-b0-5s-5folds/exp001_tf_efficientnet_b0_5clip_fold4.ckpt'\n]","metadata":{"execution":{"iopub.status.busy":"2022-04-14T12:53:02.962971Z","iopub.execute_input":"2022-04-14T12:53:02.963462Z","iopub.status.idle":"2022-04-14T12:53:02.967288Z","shell.execute_reply.started":"2022-04-14T12:53:02.963427Z","shell.execute_reply":"2022-04-14T12:53:02.966601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_model(pth):\n    static = torch.load(pth)\n    new_static = {}\n    for k, v in static['state_dict'].items():\n        new_static[k[4:]] = v\n    net = Model()\n    net.load_state_dict(new_static)\n    return net","metadata":{"execution":{"iopub.status.busy":"2022-04-14T12:53:03.028192Z","iopub.execute_input":"2022-04-14T12:53:03.028890Z","iopub.status.idle":"2022-04-14T12:53:03.033882Z","shell.execute_reply.started":"2022-04-14T12:53:03.028840Z","shell.execute_reply":"2022-04-14T12:53:03.033062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transforms = albu.Compose([\n    ToTensorV2()\n])","metadata":{"execution":{"iopub.status.busy":"2022-04-14T12:53:03.073730Z","iopub.execute_input":"2022-04-14T12:53:03.075378Z","iopub.status.idle":"2022-04-14T12:53:03.079044Z","shell.execute_reply.started":"2022-04-14T12:53:03.075347Z","shell.execute_reply":"2022-04-14T12:53:03.078300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def readAudio(pth):\n    y, sr = sf.read(pth, always_2d=True)\n    if sr != 32000:\n        y = librosa.resample(y, sr, 32000)\n    y = y[:, 0]\n    return y, 32000","metadata":{"execution":{"iopub.status.busy":"2022-04-14T12:53:03.122273Z","iopub.execute_input":"2022-04-14T12:53:03.122475Z","iopub.status.idle":"2022-04-14T12:53:03.127382Z","shell.execute_reply.started":"2022-04-14T12:53:03.122451Z","shell.execute_reply":"2022-04-14T12:53:03.126611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def scale_minmax(X, min=0.0, max=1.0):\n    X_std = (X - X.min()) / (X.max() - X.min())\n    X_scaled = X_std * (max - min) + min\n    return X_scaled","metadata":{"execution":{"iopub.status.busy":"2022-04-14T12:53:03.176051Z","iopub.execute_input":"2022-04-14T12:53:03.176797Z","iopub.status.idle":"2022-04-14T12:53:03.181819Z","shell.execute_reply.started":"2022-04-14T12:53:03.176755Z","shell.execute_reply":"2022-04-14T12:53:03.181144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_mel(y, sr):\n    melspec = librosa.feature.melspectrogram(y, sr=sr, n_mels=256, n_fft=2048, hop_length=512, pad_mode='reflect', power=2.0)\n    image = librosa.power_to_db(melspec).astype(np.float32)\n    image = scale_minmax(image, 0, 255)\n    image = transforms(image=image)['image']\n    return image / 255.","metadata":{"execution":{"iopub.status.busy":"2022-04-14T12:53:03.236646Z","iopub.execute_input":"2022-04-14T12:53:03.237071Z","iopub.status.idle":"2022-04-14T12:53:03.242327Z","shell.execute_reply.started":"2022-04-14T12:53:03.237037Z","shell.execute_reply":"2022-04-14T12:53:03.241695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_clips(path):\n    y, sr = readAudio(path)\n    #y = y[:-300]\n    #print(len(y))\n    mels = []\n    for i in range(12):\n        clip = np.zeros(32000*5)\n        start = i * 32000 * 5\n        end = (i+1) * 32000 * 5\n        if end > len(y):\n            clip[:(len(y)-start)] = y[start:]\n        else:\n            clip = y[start:end]\n        #clip = y[start:end]\n        #print(len(clip))\n        mel = make_mel(clip, sr)\n        mels.append(mel)\n    mels = torch.stack(mels)\n    #print(mels.shape)\n    return mels","metadata":{"execution":{"iopub.status.busy":"2022-04-14T12:53:03.291209Z","iopub.execute_input":"2022-04-14T12:53:03.291893Z","iopub.status.idle":"2022-04-14T12:53:03.298748Z","shell.execute_reply.started":"2022-04-14T12:53:03.291851Z","shell.execute_reply":"2022-04-14T12:53:03.298093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(models, mels):\n    preds = []\n    for model in models:\n        model.eval()\n        model.cuda()\n        with torch.no_grad():\n            logits = model(mels.cuda())\n            preds.append(logits)\n        del model\n        torch.cuda.empty_cache()\n    preds = sum(preds) / len(preds)\n    preds = preds.sigmoid().cpu().detach().data.numpy()\n    return preds","metadata":{"execution":{"iopub.status.busy":"2022-04-14T12:53:03.333749Z","iopub.execute_input":"2022-04-14T12:53:03.334050Z","iopub.status.idle":"2022-04-14T12:53:03.339798Z","shell.execute_reply.started":"2022-04-14T12:53:03.334021Z","shell.execute_reply":"2022-04-14T12:53:03.339075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = {'row_id': [], 'target': []}\nbinary_th = 0.50\n\nmodels = [load_model(pth) for pth in modelTanks]\nfor afile in file_list: \n    path = test_audio_dir + afile + '.ogg'\n\n    # y, sr = readAudio(path)\n    # mels = make_clips(y, sr)\n    mels = make_clips(path)\n    outputs = predict(models, mels) # 12x21\n    # print(outputs.shape)\n    for idx, i in enumerate(range(outputs.shape[0])):        \n        chunk_end_time = (i + 1) * 5\n        p = (outputs[i] > binary_th).astype(bool)\n        for bird, t in zip(scored_bird, p):\n            row_id = afile + '_' + bird + '_' + str(chunk_end_time)\n            pred['row_id'].append(row_id)\n            pred['target'].append(t)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T12:53:03.368252Z","iopub.execute_input":"2022-04-14T12:53:03.368571Z","iopub.status.idle":"2022-04-14T12:53:05.105635Z","shell.execute_reply.started":"2022-04-14T12:53:03.368533Z","shell.execute_reply":"2022-04-14T12:53:05.104877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = pd.DataFrame(pred, columns = ['row_id', 'target'])\nresults.to_csv(\"submission.csv\", index=False)  ","metadata":{"execution":{"iopub.status.busy":"2022-04-14T12:53:05.109456Z","iopub.execute_input":"2022-04-14T12:53:05.109667Z","iopub.status.idle":"2022-04-14T12:53:05.117446Z","shell.execute_reply.started":"2022-04-14T12:53:05.109643Z","shell.execute_reply":"2022-04-14T12:53:05.116825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-14T12:53:05.118784Z","iopub.execute_input":"2022-04-14T12:53:05.119206Z","iopub.status.idle":"2022-04-14T12:53:05.133001Z","shell.execute_reply.started":"2022-04-14T12:53:05.119169Z","shell.execute_reply":"2022-04-14T12:53:05.131964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}