{"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":"markdown","source":"# Notes","metadata":{"papermill":{"duration":0.04953,"end_time":"2021-02-16T15:59:39.560117","exception":false,"start_time":"2021-02-16T15:59:39.510587","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"In this kenel, I'm going to use a classical **ResneSt50** for bird identification.","metadata":{"papermill":{"duration":0.04754,"end_time":"2021-02-16T15:59:39.092567","exception":false,"start_time":"2021-02-16T15:59:39.045027","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"* The inference is based on these [resnest50 weights](https://www.kaggle.com/kneroma/kkiller-birdclef-models-public). Please, don't forget upvoting the dataset to make it more visible for others\n* The inference pipeline is optimized as much as I can in order to reduce execution time","metadata":{"papermill":{"duration":0.04461,"end_time":"2021-02-16T15:59:39.651121","exception":false,"start_time":"2021-02-16T15:59:39.606511","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!nvidia-smi","metadata":{"id":"ikMF-nQSdTbF","outputId":"041c6767-86ab-46e4-9b22-a4840121ecca","papermill":{"duration":1.301859,"end_time":"2021-02-16T15:59:41.363841","exception":false,"start_time":"2021-02-16T15:59:40.061982","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T08:20:14.828613Z","iopub.execute_input":"2021-05-24T08:20:14.828998Z","iopub.status.idle":"2021-05-24T08:20:15.733946Z","shell.execute_reply.started":"2021-05-24T08:20:14.828966Z","shell.execute_reply":"2021-05-24T08:20:15.732427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# try:\n#     import resnest\n# except ModuleNotFoundError:\n#     !pip install -q \"../input/resnest50-fast-package/resnest-0.0.6b20200701/resnest\"","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:20:15.739813Z","iopub.execute_input":"2021-05-24T08:20:15.740269Z","iopub.status.idle":"2021-05-24T08:20:15.749753Z","shell.execute_reply.started":"2021-05-24T08:20:15.740222Z","shell.execute_reply":"2021-05-24T08:20:15.748523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    import efficientnet_pytorch\nexcept ModuleNotFoundError:\n    !pip install -q \"../input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master\"","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:20:15.754430Z","iopub.execute_input":"2021-05-24T08:20:15.756737Z","iopub.status.idle":"2021-05-24T08:20:15.767183Z","shell.execute_reply.started":"2021-05-24T08:20:15.756693Z","shell.execute_reply":"2021-05-24T08:20:15.765767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport librosa as lb\nimport soundfile as sf\nimport pandas as pd\nimport cv2\nfrom pathlib import Path\nimport re\n\nimport torch\nfrom torch import nn\nfrom  torch.utils.data import Dataset, DataLoader\n\nfrom tqdm.notebook import tqdm\n\nimport time\n#from resnest.torch import resnest50\nfrom efficientnet_pytorch import EfficientNet","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","id":"kSCcqxf0c_O7","papermill":{"duration":4.153186,"end_time":"2021-02-16T15:59:57.894303","exception":false,"start_time":"2021-02-16T15:59:53.741117","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T08:20:15.770281Z","iopub.execute_input":"2021-05-24T08:20:15.772063Z","iopub.status.idle":"2021-05-24T08:20:15.786184Z","shell.execute_reply.started":"2021-05-24T08:20:15.772020Z","shell.execute_reply":"2021-05-24T08:20:15.784935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.046502,"end_time":"2021-02-16T15:59:59.284533","exception":false,"start_time":"2021-02-16T15:59:59.238031","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Configs","metadata":{"papermill":{"duration":0.046996,"end_time":"2021-02-16T15:59:59.377834","exception":false,"start_time":"2021-02-16T15:59:59.330838","status":"completed"},"tags":[]}},{"cell_type":"code","source":"NUM_CLASSES = 273\nSR = 32_000\nDURATION = 5\n#THRESH = 0.25\nTHRESH = 0.80\n\nDEVICE = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(\"DEVICE:\", DEVICE)\n","metadata":{"id":"rJhYZVIDc_O9","papermill":{"duration":0.440614,"end_time":"2021-02-16T15:59:59.86408","exception":false,"start_time":"2021-02-16T15:59:59.423466","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T08:20:15.796957Z","iopub.execute_input":"2021-05-24T08:20:15.799315Z","iopub.status.idle":"2021-05-24T08:20:15.808399Z","shell.execute_reply.started":"2021-05-24T08:20:15.799226Z","shell.execute_reply":"2021-05-24T08:20:15.806976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# First, get a list of soundscape files to process.\n# We'll use the test_soundscape directory if it contains \"ogg\" files\n# (which it only does when submitting the notebook), \n# otherwise we'll use the train_soundscape folder to make predictions.\n\nTEST_AUDIO_ROOT = Path(\"../input/birdclef-2021/test_soundscapes\")\nSAMPLE_SUB_PATH = \"../input/birdclef-2021/sample_submission.csv\"\nTARGET_PATH = None","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:20:15.813684Z","iopub.execute_input":"2021-05-24T08:20:15.814599Z","iopub.status.idle":"2021-05-24T08:20:15.822151Z","shell.execute_reply.started":"2021-05-24T08:20:15.814557Z","shell.execute_reply":"2021-05-24T08:20:15.819082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(list(TEST_AUDIO_ROOT.glob(\"*.ogg\")))","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:20:15.823989Z","iopub.execute_input":"2021-05-24T08:20:15.826586Z","iopub.status.idle":"2021-05-24T08:20:15.843948Z","shell.execute_reply.started":"2021-05-24T08:20:15.826541Z","shell.execute_reply":"2021-05-24T08:20:15.842813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not len(list(TEST_AUDIO_ROOT.glob(\"*.ogg\"))):\n    TEST_AUDIO_ROOT = Path(\"../input/birdclef-2021/train_soundscapes\")\n    SAMPLE_SUB_PATH = None\n    # SAMPLE_SUB_PATH = \"../input/birdclef-2021/sample_submission.csv\"\n    TARGET_PATH = Path(\"../input/birdclef-2021/train_soundscape_labels.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:20:15.845686Z","iopub.execute_input":"2021-05-24T08:20:15.848005Z","iopub.status.idle":"2021-05-24T08:20:15.857931Z","shell.execute_reply.started":"2021-05-24T08:20:15.847925Z","shell.execute_reply":"2021-05-24T08:20:15.855912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data","metadata":{"papermill":{"duration":0.046026,"end_time":"2021-02-16T16:00:00.054107","exception":false,"start_time":"2021-02-16T16:00:00.008081","status":"completed"},"tags":[]}},{"cell_type":"code","source":"class MelSpecComputer:\n    def __init__(self, sr, n_mels, fmin, fmax, **kwargs):\n        self.sr = sr\n        self.n_mels = n_mels\n        self.fmin = fmin\n        self.fmax = fmax\n        kwargs[\"n_fft\"] = kwargs.get(\"n_fft\", self.sr//10)\n        kwargs[\"hop_length\"] = kwargs.get(\"hop_length\", self.sr//(10*4))\n        self.kwargs = kwargs\n\n    def __call__(self, y):\n\n        melspec = lb.feature.melspectrogram(\n            y, sr=self.sr, n_mels=self.n_mels, fmin=self.fmin, fmax=self.fmax, **self.kwargs,\n        )\n\n        melspec = lb.power_to_db(melspec).astype(np.float32)\n        return melspec","metadata":{"id":"4fwNhdbJc_O-","papermill":{"duration":0.056975,"end_time":"2021-02-16T16:00:00.157168","exception":false,"start_time":"2021-02-16T16:00:00.100193","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T08:20:15.862082Z","iopub.execute_input":"2021-05-24T08:20:15.863748Z","iopub.status.idle":"2021-05-24T08:20:15.885063Z","shell.execute_reply.started":"2021-05-24T08:20:15.863704Z","shell.execute_reply":"2021-05-24T08:20:15.883410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mono_to_color(X, eps=1e-6, mean=None, std=None):\n    mean = mean or X.mean()\n    std = std or X.std()\n    X = (X - mean) / (std + eps)\n    \n    _min, _max = X.min(), X.max()\n\n    if (_max - _min) > eps:\n        V = np.clip(X, _min, _max)\n        V = 255 * (V - _min) / (_max - _min)\n        V = V.astype(np.uint8)\n    else:\n        V = np.zeros_like(X, dtype=np.uint8)\n\n    return V\n\ndef crop_or_pad(y, length):\n    if len(y) < length:\n        y = np.concatenate([y, length - np.zeros(len(y))])\n    elif len(y) > length:\n        y = y[:length]\n    return y","metadata":{"id":"Nk0haSTIc_O_","papermill":{"duration":0.065406,"end_time":"2021-02-16T16:00:00.271104","exception":false,"start_time":"2021-02-16T16:00:00.205698","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T08:20:15.888366Z","iopub.execute_input":"2021-05-24T08:20:15.889954Z","iopub.status.idle":"2021-05-24T08:20:15.909378Z","shell.execute_reply.started":"2021-05-24T08:20:15.889909Z","shell.execute_reply":"2021-05-24T08:20:15.908093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class BirdCLEFDataset(Dataset):\n    def __init__(self, data, sr=SR, n_mels=128, fmin=0, fmax=None, duration=DURATION, step=None, res_type=\"kaiser_fast\", resample=True):\n        \n        self.data = data\n        \n        self.sr = sr\n        self.n_mels = n_mels\n        self.fmin = fmin\n        self.fmax = fmax or self.sr//2\n\n        self.duration = duration\n        self.audio_length = self.duration*self.sr\n        self.step = step or self.audio_length\n        \n        self.res_type = res_type\n        self.resample = resample\n\n        self.mel_spec_computer = MelSpecComputer(sr=self.sr, n_mels=self.n_mels, fmin=self.fmin,\n                                                 fmax=self.fmax)\n    def __len__(self):\n        return len(self.data)\n    \n    @staticmethod\n    def normalize(image):\n        image = image.astype(\"float32\", copy=False) / 255.0\n        image = np.stack([image, image, image])\n        return image\n    \n    def audio_to_image(self, audio):\n        melspec = self.mel_spec_computer(audio) \n        image = mono_to_color(melspec)\n        image = self.normalize(image)\n        return image\n\n    def read_file(self, filepath):\n        audio, orig_sr = sf.read(filepath, dtype=\"float32\")\n\n        if self.resample and orig_sr != self.sr:\n            audio = lb.resample(audio, orig_sr, self.sr, res_type=self.res_type)\n          \n        audios = []\n        for i in range(self.audio_length, len(audio) + self.step, self.step):\n            start = max(0, i - self.audio_length)\n            end = start + self.audio_length\n            audios.append(audio[start:end])\n            \n        if len(audios[-1]) < self.audio_length:\n            audios = audios[:-1]\n            \n        images = [self.audio_to_image(audio) for audio in audios]\n        images = np.stack(images)\n        \n        return images\n    \n        \n    def __getitem__(self, idx):\n        return self.read_file(self.data.loc[idx, \"filepath\"])","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:20:15.915411Z","iopub.execute_input":"2021-05-24T08:20:15.918730Z","iopub.status.idle":"2021-05-24T08:20:15.942517Z","shell.execute_reply.started":"2021-05-24T08:20:15.918673Z","shell.execute_reply":"2021-05-24T08:20:15.941290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.DataFrame(\n     [(path.stem, *path.stem.split(\"_\"), path) for path in Path(TEST_AUDIO_ROOT).glob(\"*.ogg\")],\n    columns = [\"filename\", \"id\", \"site\", \"date\", \"filepath\"]\n)\nprint(data.shape)\ndata.head()","metadata":{"id":"rVwlOrbxc_PD","outputId":"9b53ec99-634a-4b30-f9b5-75036184482b","papermill":{"duration":0.162256,"end_time":"2021-02-16T16:00:01.102152","exception":false,"start_time":"2021-02-16T16:00:00.939896","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T08:20:15.948761Z","iopub.execute_input":"2021-05-24T08:20:15.951942Z","iopub.status.idle":"2021-05-24T08:20:15.988841Z","shell.execute_reply.started":"2021-05-24T08:20:15.951881Z","shell.execute_reply":"2021-05-24T08:20:15.987642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Use new rich metadata from private dataset\ndf_train = pd.read_csv(\"../input/birdcled2021train-metadata-limited-birds-rich-set/train_metadata_filtered_rich.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:20:15.993592Z","iopub.execute_input":"2021-05-24T08:20:15.996372Z","iopub.status.idle":"2021-05-24T08:20:16.355406Z","shell.execute_reply.started":"2021-05-24T08:20:15.996330Z","shell.execute_reply":"2021-05-24T08:20:16.354139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LABEL_IDS = {label: label_id for label_id,label in enumerate(sorted(df_train[\"primary_label\"].unique()))}\nINV_LABEL_IDS = {val: key for key,val in LABEL_IDS.items()}","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:20:16.358057Z","iopub.execute_input":"2021-05-24T08:20:16.358679Z","iopub.status.idle":"2021-05-24T08:20:16.370751Z","shell.execute_reply.started":"2021-05-24T08:20:16.358617Z","shell.execute_reply":"2021-05-24T08:20:16.369181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#LABEL_IDS","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:20:16.372698Z","iopub.execute_input":"2021-05-24T08:20:16.373314Z","iopub.status.idle":"2021-05-24T08:20:16.381096Z","shell.execute_reply.started":"2021-05-24T08:20:16.373239Z","shell.execute_reply":"2021-05-24T08:20:16.379271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"label_id\"].min(), df_train[\"label_id\"].max()","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:20:16.383537Z","iopub.execute_input":"2021-05-24T08:20:16.384149Z","iopub.status.idle":"2021-05-24T08:20:16.395201Z","shell.execute_reply.started":"2021-05-24T08:20:16.384050Z","shell.execute_reply":"2021-05-24T08:20:16.393387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference","metadata":{"id":"BNsivZZZc_PG","papermill":{"duration":0.048675,"end_time":"2021-02-16T16:00:08.424781","exception":false,"start_time":"2021-02-16T16:00:08.376106","status":"completed"},"tags":[]}},{"cell_type":"code","source":"test_data = BirdCLEFDataset(data=data)\nlen(test_data), test_data[0].shape","metadata":{"id":"AzlOErOmc_PH","papermill":{"duration":0.058312,"end_time":"2021-02-16T16:00:08.531199","exception":false,"start_time":"2021-02-16T16:00:08.472887","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T08:20:16.397393Z","iopub.execute_input":"2021-05-24T08:20:16.398093Z","iopub.status.idle":"2021-05-24T08:20:20.594946Z","shell.execute_reply.started":"2021-05-24T08:20:16.398047Z","shell.execute_reply":"2021-05-24T08:20:20.593723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.048931,"end_time":"2021-02-16T16:00:14.351476","exception":false,"start_time":"2021-02-16T16:00:14.302545","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def load_net(checkpoint_path, num_classes=NUM_CLASSES):\n#     net = resnest50(pretrained=False)\n#     net.fc = nn.Linear(net.fc.in_features, num_classes)\n#     dummy_device = torch.device(\"cpu\")\n#     d = torch.load(checkpoint_path, map_location=dummy_device)\n#     for key in list(d.keys()):\n#         d[key.replace(\"model.\", \"\")] = d.pop(key)\n#     net.load_state_dict(d)\n#     net = net.to(DEVICE)\n#     net = net.eval()\n#     return net","metadata":{"papermill":{"duration":0.05945,"end_time":"2021-02-16T16:00:14.565804","exception":false,"start_time":"2021-02-16T16:00:14.506354","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T08:20:20.596650Z","iopub.execute_input":"2021-05-24T08:20:20.597575Z","iopub.status.idle":"2021-05-24T08:20:20.602312Z","shell.execute_reply.started":"2021-05-24T08:20:20.597481Z","shell.execute_reply":"2021-05-24T08:20:20.600935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_efficient_net(checkpoint_path, num_classes=NUM_CLASSES):\n    net = EfficientNet.from_name('efficientnet-b5')\n    net._fc = nn.Linear(net._fc.in_features, num_classes)\n    #model._fc = nn.Linear(in_features=model._fc.in_features, out_features=6)\n    dummy_device = torch.device(\"cpu\")\n    d = torch.load(checkpoint_path, map_location=dummy_device)\n    for key in list(d.keys()):\n        d[key.replace(\"model.\", \"\")] = d.pop(key)\n    #model.load_state_dict(torch.load('../input/pytorch-efficientnet/best_model.pth'))\n    net.load_state_dict(d)\n    net = net.to(DEVICE)\n    net = net.eval()\n    return net","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:20:20.604547Z","iopub.execute_input":"2021-05-24T08:20:20.605108Z","iopub.status.idle":"2021-05-24T08:20:20.618068Z","shell.execute_reply.started":"2021-05-24T08:20:20.605064Z","shell.execute_reply":"2021-05-24T08:20:20.616837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# checkpoint_paths = [\n#     Path(\"../input/kkiller-birdclef-models-public/birdclef_resnest50_fold0_epoch_10_f1_val_06471_20210417161101.pth\"),\n# ]\n\n\n# nets = [\n#         load_net(checkpoint_path.as_posix()) for checkpoint_path in checkpoint_paths\n# ]","metadata":{"id":"ayVLRTzLc_PI","papermill":{"duration":14.506299,"end_time":"2021-02-16T16:00:30.268614","exception":false,"start_time":"2021-02-16T16:00:15.762315","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T08:20:20.620070Z","iopub.execute_input":"2021-05-24T08:20:20.621109Z","iopub.status.idle":"2021-05-24T08:20:20.629392Z","shell.execute_reply.started":"2021-05-24T08:20:20.621046Z","shell.execute_reply":"2021-05-24T08:20:20.628128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ncheckpoint_efficient_paths = [\n    Path(\"../input/birdclef-enb5-fold0-epoch-17-f1-val-07796/birdclef_efficientnet-b5_fold0_epoch_17_f1_val_07796_20210524050547.pth\"),\n]\n\n\nefficient_nets = [\n        load_efficient_net(checkpoint_efficient_path.as_posix()) for checkpoint_efficient_path in checkpoint_efficient_paths\n]","metadata":{"papermill":{"duration":0.051509,"end_time":"2021-02-16T16:00:30.373444","exception":false,"start_time":"2021-02-16T16:00:30.321935","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T08:20:20.631392Z","iopub.execute_input":"2021-05-24T08:20:20.633499Z","iopub.status.idle":"2021-05-24T08:20:21.332508Z","shell.execute_reply.started":"2021-05-24T08:20:20.633452Z","shell.execute_reply":"2021-05-24T08:20:21.331255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@torch.no_grad()\ndef get_thresh_preds(out, thresh=None):\n    thresh = thresh or THRES\n    o = (-out).argsort(1)\n    npreds = (out > thresh).sum(1)\n    preds = []\n    for oo, npred in zip(o, npreds):\n        preds.append(oo[:npred].cpu().numpy().tolist())\n    return preds","metadata":{"papermill":{"duration":0.059925,"end_time":"2021-02-16T16:00:30.912547","exception":false,"start_time":"2021-02-16T16:00:30.852622","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T08:20:21.334206Z","iopub.execute_input":"2021-05-24T08:20:21.334611Z","iopub.status.idle":"2021-05-24T08:20:21.342923Z","shell.execute_reply.started":"2021-05-24T08:20:21.334569Z","shell.execute_reply":"2021-05-24T08:20:21.341358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_bird_names(preds):\n    bird_names = []\n    for pred in preds:\n        if not pred:\n            bird_names.append(\"nocall\")\n        else:\n            bird_names.append(\" \".join([INV_LABEL_IDS[bird_id] for bird_id in pred]))\n    return bird_names","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:20:21.345128Z","iopub.execute_input":"2021-05-24T08:20:21.345638Z","iopub.status.idle":"2021-05-24T08:20:21.355530Z","shell.execute_reply.started":"2021-05-24T08:20:21.345595Z","shell.execute_reply":"2021-05-24T08:20:21.353701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(nets, test_data, names=True):\n    preds = []\n    with torch.no_grad():\n        for idx in  tqdm(list(range(len(test_data)))):\n            xb = torch.from_numpy(test_data[idx]).to(DEVICE)\n            pred = 0.\n            for net in nets:\n                o = net(xb)\n                o = torch.sigmoid(o)\n\n                pred += o\n\n            pred /= len(nets)\n            \n            if names:\n                pred = get_bird_names(get_thresh_preds(pred))\n\n            preds.append(pred)\n    return preds","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:20:21.357944Z","iopub.execute_input":"2021-05-24T08:20:21.358412Z","iopub.status.idle":"2021-05-24T08:20:21.368186Z","shell.execute_reply.started":"2021-05-24T08:20:21.358369Z","shell.execute_reply":"2021-05-24T08:20:21.366875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pred_probas = predict(nets, test_data, names=False)\n# print(len(pred_probas))","metadata":{"papermill":{"duration":7466.932385,"end_time":"2021-02-16T18:04:58.116349","exception":false,"start_time":"2021-02-16T16:00:31.183964","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-24T08:20:21.370420Z","iopub.execute_input":"2021-05-24T08:20:21.371487Z","iopub.status.idle":"2021-05-24T08:20:21.378670Z","shell.execute_reply.started":"2021-05-24T08:20:21.371442Z","shell.execute_reply":"2021-05-24T08:20:21.377536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_probas_efficient = predict(efficient_nets, test_data, names=False)\nprint(len(pred_probas_efficient))","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:20:21.380786Z","iopub.execute_input":"2021-05-24T08:20:21.381289Z","iopub.status.idle":"2021-05-24T08:21:53.177750Z","shell.execute_reply.started":"2021-05-24T08:20:21.381245Z","shell.execute_reply":"2021-05-24T08:21:53.176342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#len(pred_probas)\nlen(pred_probas_efficient)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.179959Z","iopub.execute_input":"2021-05-24T08:21:53.180875Z","iopub.status.idle":"2021-05-24T08:21:53.188539Z","shell.execute_reply.started":"2021-05-24T08:21:53.180831Z","shell.execute_reply":"2021-05-24T08:21:53.187214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pred_probas[0]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.190629Z","iopub.execute_input":"2021-05-24T08:21:53.191216Z","iopub.status.idle":"2021-05-24T08:21:53.202366Z","shell.execute_reply.started":"2021-05-24T08:21:53.191160Z","shell.execute_reply":"2021-05-24T08:21:53.201098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_probas_efficient[0]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.203793Z","iopub.execute_input":"2021-05-24T08:21:53.204066Z","iopub.status.idle":"2021-05-24T08:21:53.353351Z","shell.execute_reply.started":"2021-05-24T08:21:53.204039Z","shell.execute_reply":"2021-05-24T08:21:53.352059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pred_probas[0]+pred_probas_efficient[0]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.355513Z","iopub.execute_input":"2021-05-24T08:21:53.356096Z","iopub.status.idle":"2021-05-24T08:21:53.361582Z","shell.execute_reply.started":"2021-05-24T08:21:53.356053Z","shell.execute_reply":"2021-05-24T08:21:53.360293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pred_probas_avg =[(pred_probas[i]+pred_probas_efficient[i])/2 for i in range(0,len(pred_probas))]\n#pred_probas_avg =[(pred_probas[i]*3+pred_probas_efficient[i]*1)/4 for i in range(0,len(pred_probas))]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.363554Z","iopub.execute_input":"2021-05-24T08:21:53.364564Z","iopub.status.idle":"2021-05-24T08:21:53.373872Z","shell.execute_reply.started":"2021-05-24T08:21:53.364519Z","shell.execute_reply":"2021-05-24T08:21:53.372447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#len(pred_probas_avg)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.383565Z","iopub.execute_input":"2021-05-24T08:21:53.384022Z","iopub.status.idle":"2021-05-24T08:21:53.388475Z","shell.execute_reply.started":"2021-05-24T08:21:53.383989Z","shell.execute_reply":"2021-05-24T08:21:53.387045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pred_probas_avg[0]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.390357Z","iopub.execute_input":"2021-05-24T08:21:53.391281Z","iopub.status.idle":"2021-05-24T08:21:53.401276Z","shell.execute_reply.started":"2021-05-24T08:21:53.391222Z","shell.execute_reply":"2021-05-24T08:21:53.399944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#preds_avg = [get_bird_names(get_thresh_preds(pred, thresh=THRESH)) for pred in pred_probas_avg]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.404443Z","iopub.execute_input":"2021-05-24T08:21:53.405232Z","iopub.status.idle":"2021-05-24T08:21:53.416825Z","shell.execute_reply.started":"2021-05-24T08:21:53.405180Z","shell.execute_reply":"2021-05-24T08:21:53.415290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#preds_avg[:2]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.419134Z","iopub.execute_input":"2021-05-24T08:21:53.419982Z","iopub.status.idle":"2021-05-24T08:21:53.431607Z","shell.execute_reply.started":"2021-05-24T08:21:53.419935Z","shell.execute_reply":"2021-05-24T08:21:53.430149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#preds = [get_bird_names(get_thresh_preds(pred, thresh=THRESH)) for pred in pred_probas]\n# preds[:2]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.433482Z","iopub.execute_input":"2021-05-24T08:21:53.434575Z","iopub.status.idle":"2021-05-24T08:21:53.442875Z","shell.execute_reply.started":"2021-05-24T08:21:53.434519Z","shell.execute_reply":"2021-05-24T08:21:53.441888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#preds[:2]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.445171Z","iopub.execute_input":"2021-05-24T08:21:53.445867Z","iopub.status.idle":"2021-05-24T08:21:53.453922Z","shell.execute_reply.started":"2021-05-24T08:21:53.445824Z","shell.execute_reply":"2021-05-24T08:21:53.452956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_efficient = [get_bird_names(get_thresh_preds(pred, thresh=THRESH)) for pred in pred_probas_efficient]\n# preds[:2]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.456179Z","iopub.execute_input":"2021-05-24T08:21:53.456907Z","iopub.status.idle":"2021-05-24T08:21:53.576086Z","shell.execute_reply.started":"2021-05-24T08:21:53.456847Z","shell.execute_reply":"2021-05-24T08:21:53.574876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#preds_efficient[:2]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.578182Z","iopub.execute_input":"2021-05-24T08:21:53.578676Z","iopub.status.idle":"2021-05-24T08:21:53.584537Z","shell.execute_reply.started":"2021-05-24T08:21:53.578611Z","shell.execute_reply":"2021-05-24T08:21:53.583050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preds_as_df(data, preds):\n    sub = {\n        \"row_id\": [],\n        \"birds\": [],\n    }\n    \n    for row, pred in zip(data.itertuples(False), preds):\n        row_id = [f\"{row.id}_{row.site}_{5*i}\" for i in range(1, len(pred)+1)]\n        sub[\"birds\"] += pred\n        sub[\"row_id\"] += row_id\n        \n    sub = pd.DataFrame(sub)\n    \n    if SAMPLE_SUB_PATH:\n        sample_sub = pd.read_csv(SAMPLE_SUB_PATH, usecols=[\"row_id\"])\n        sub = sample_sub.merge(sub, on=\"row_id\", how=\"left\")\n        sub[\"birds\"] = sub[\"birds\"].fillna(\"nocall\")\n    return sub","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.586583Z","iopub.execute_input":"2021-05-24T08:21:53.587063Z","iopub.status.idle":"2021-05-24T08:21:53.599449Z","shell.execute_reply.started":"2021-05-24T08:21:53.587018Z","shell.execute_reply":"2021-05-24T08:21:53.598164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sub = preds_as_df(data, preds)\n# print(sub.shape)\n# sub","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.602904Z","iopub.execute_input":"2021-05-24T08:21:53.603791Z","iopub.status.idle":"2021-05-24T08:21:53.610707Z","shell.execute_reply.started":"2021-05-24T08:21:53.603745Z","shell.execute_reply":"2021-05-24T08:21:53.609265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_efficient = preds_as_df(data, preds_efficient)\nprint(sub_efficient.shape)\nsub_efficient","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.613659Z","iopub.execute_input":"2021-05-24T08:21:53.614462Z","iopub.status.idle":"2021-05-24T08:21:53.637809Z","shell.execute_reply.started":"2021-05-24T08:21:53.614419Z","shell.execute_reply":"2021-05-24T08:21:53.636172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sub_avg = preds_as_df(data, preds_avg)\n# print(sub_avg.shape)\n# sub_avg","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.641461Z","iopub.execute_input":"2021-05-24T08:21:53.641968Z","iopub.status.idle":"2021-05-24T08:21:53.647693Z","shell.execute_reply.started":"2021-05-24T08:21:53.641939Z","shell.execute_reply":"2021-05-24T08:21:53.646160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_efficient.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.649844Z","iopub.execute_input":"2021-05-24T08:21:53.650393Z","iopub.status.idle":"2021-05-24T08:21:53.666241Z","shell.execute_reply.started":"2021-05-24T08:21:53.650330Z","shell.execute_reply":"2021-05-24T08:21:53.665022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#CPMP's code\ndef fast_f1_score(predictions, target):\n    tp = (predictions * target).sum(1)\n    fp = (predictions * (1 - target)).sum(1)\n    fn = ((1 - predictions) * target).sum(1)\n    f1 = tp / (tp + (fp + fn) / 2)\n    precision = tp / (tp + fp)\n    recall = tp / (tp + fn)\n    return f1.mean(), precision.mean(), recall.mean()\n    #return {\"f1\": f1, \"prec\": precision, \"rec\": recall}","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.667462Z","iopub.execute_input":"2021-05-24T08:21:53.667763Z","iopub.status.idle":"2021-05-24T08:21:53.675913Z","shell.execute_reply.started":"2021-05-24T08:21:53.667735Z","shell.execute_reply":"2021-05-24T08:21:53.674505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Small validation","metadata":{}},{"cell_type":"code","source":"def get_metrics(s_true, s_pred):\n    s_true = set(s_true.split())\n    s_pred = set(s_pred.split())\n    n, n_true, n_pred = len(s_true.intersection(s_pred)), len(s_true), len(s_pred)\n    \n    prec = n/n_pred\n    rec = n/n_true\n    f1 = 2*prec*rec/(prec + rec) if prec + rec else 0\n    \n    return {\"f1\": f1, \"prec\": prec, \"rec\": rec, \"n_true\": n_true, \"n_pred\": n_pred, \"n\": n}","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.678366Z","iopub.execute_input":"2021-05-24T08:21:53.678967Z","iopub.status.idle":"2021-05-24T08:21:53.689626Z","shell.execute_reply.started":"2021-05-24T08:21:53.678923Z","shell.execute_reply":"2021-05-24T08:21:53.688376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if TARGET_PATH:\n#     sub_target = pd.read_csv(TARGET_PATH)\n#     sub_target = sub_target.merge(sub, how=\"left\", on=\"row_id\")\n    \n#     print(sub_target[\"birds_x\"].notnull().sum(), sub_target[\"birds_x\"].notnull().sum())\n#     assert sub_target[\"birds_x\"].notnull().all()\n#     assert sub_target[\"birds_y\"].notnull().all()\n    \n#     df_metrics = pd.DataFrame([get_metrics(s_true, s_pred) for s_true, s_pred in zip(sub_target.birds_x, sub_target.birds_y)])\n#     #import pdb;pdb.set_trace()\n#     #cpmp_f1_mean, cpmp_precision_mean, cpmp_recall_mean = fast_f1_score(sub_target.birds_y, sub_target.birds_x)\n    \n#     #print (f\"f1_mean = {cpmp_f1_mean}, precision_mean = {cpmp_precision_mean}, recall_mean = {cpmp_recall_mean}\")\n#     print(df_metrics.mean())","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.691809Z","iopub.execute_input":"2021-05-24T08:21:53.692564Z","iopub.status.idle":"2021-05-24T08:21:53.700567Z","shell.execute_reply.started":"2021-05-24T08:21:53.692516Z","shell.execute_reply":"2021-05-24T08:21:53.699481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sub_target[sub_target.birds_y != \"nocall\"]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.702893Z","iopub.execute_input":"2021-05-24T08:21:53.703647Z","iopub.status.idle":"2021-05-24T08:21:53.714301Z","shell.execute_reply.started":"2021-05-24T08:21:53.703602Z","shell.execute_reply":"2021-05-24T08:21:53.712891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sub_target[sub_target.birds_x != \"nocall\"]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.718163Z","iopub.execute_input":"2021-05-24T08:21:53.718735Z","iopub.status.idle":"2021-05-24T08:21:53.725054Z","shell.execute_reply.started":"2021-05-24T08:21:53.718695Z","shell.execute_reply":"2021-05-24T08:21:53.723430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if TARGET_PATH:\n    sub_efficient_target = pd.read_csv(TARGET_PATH)\n    sub_efficient_target = sub_efficient_target.merge(sub_efficient, how=\"left\", on=\"row_id\")\n    \n    print(sub_efficient_target[\"birds_x\"].notnull().sum(), sub_efficient_target[\"birds_x\"].notnull().sum())\n    \n    assert sub_efficient_target[\"birds_x\"].notnull().all()\n    assert sub_efficient_target[\"birds_y\"].notnull().all()\n    \n    df_sufficient_metrics = pd.DataFrame([get_metrics(s_true, s_pred) for s_true, s_pred in zip(sub_efficient_target.birds_x, sub_efficient_target.birds_y)])\n \n    #cpmp_f1_mean, cpmp_precision_mean, cpmp_recall_mean = fast_f1_score(sub_efficient_target.birds_y, sub_efficient_target.birds_x)\n    #print (f\"f1_mean = {cpmp_f1_mean}, precision_mean = {cpmp_precision_mean}, recall_mean = {cpmp_recall_mean}\")\n\n    print(df_sufficient_metrics.mean())","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.727700Z","iopub.execute_input":"2021-05-24T08:21:53.728337Z","iopub.status.idle":"2021-05-24T08:21:53.776404Z","shell.execute_reply.started":"2021-05-24T08:21:53.728291Z","shell.execute_reply":"2021-05-24T08:21:53.775081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_efficient_target[sub_efficient_target.birds_y != \"nocall\"]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.778742Z","iopub.execute_input":"2021-05-24T08:21:53.779208Z","iopub.status.idle":"2021-05-24T08:21:53.804482Z","shell.execute_reply.started":"2021-05-24T08:21:53.779148Z","shell.execute_reply":"2021-05-24T08:21:53.803426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_efficient_target[sub_efficient_target.birds_x != \"nocall\"]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.806315Z","iopub.execute_input":"2021-05-24T08:21:53.806850Z","iopub.status.idle":"2021-05-24T08:21:53.830700Z","shell.execute_reply.started":"2021-05-24T08:21:53.806803Z","shell.execute_reply":"2021-05-24T08:21:53.829247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if TARGET_PATH:\n#     sub_avg_target = pd.read_csv(TARGET_PATH)\n#     sub_avg_target = sub_avg_target.merge(sub_avg, how=\"left\", on=\"row_id\")\n    \n#     print(sub_avg_target[\"birds_x\"].notnull().sum(), sub_avg_target[\"birds_x\"].notnull().sum())\n    \n#     assert sub_avg_target[\"birds_x\"].notnull().all()\n#     assert sub_avg_target[\"birds_y\"].notnull().all()\n    \n#     df_avg_metrics = pd.DataFrame([get_metrics(s_true, s_pred) for s_true, s_pred in zip(sub_avg_target.birds_x, sub_avg_target.birds_y)])\n \n#     #cpmp_f1_mean, cpmp_precision_mean, cpmp_recall_mean = fast_f1_score(sub_efficient_target.birds_y, sub_efficient_target.birds_x)\n#     #print (f\"f1_mean = {cpmp_f1_mean}, precision_mean = {cpmp_precision_mean}, recall_mean = {cpmp_recall_mean}\")\n\n#     print(df_avg_metrics.mean())","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.832774Z","iopub.execute_input":"2021-05-24T08:21:53.833203Z","iopub.status.idle":"2021-05-24T08:21:53.839302Z","shell.execute_reply.started":"2021-05-24T08:21:53.833161Z","shell.execute_reply":"2021-05-24T08:21:53.837327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sub_avg_target[sub_avg_target.birds_y != \"nocall\"]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.841372Z","iopub.execute_input":"2021-05-24T08:21:53.842047Z","iopub.status.idle":"2021-05-24T08:21:53.852086Z","shell.execute_reply.started":"2021-05-24T08:21:53.841955Z","shell.execute_reply":"2021-05-24T08:21:53.851034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sub_avg_target[sub_avg_target.birds_x != \"nocall\"]","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:21:53.854283Z","iopub.execute_input":"2021-05-24T08:21:53.855978Z","iopub.status.idle":"2021-05-24T08:21:53.862499Z","shell.execute_reply.started":"2021-05-24T08:21:53.855849Z","shell.execute_reply":"2021-05-24T08:21:53.860985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}