{"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":"# Importing Trained Model","metadata":{}},{"cell_type":"code","source":"!rm -r /kaggle/working/*","metadata":{"execution":{"iopub.status.busy":"2023-07-26T15:32:42.808025Z","iopub.execute_input":"2023-07-26T15:32:42.808608Z","iopub.status.idle":"2023-07-26T15:32:43.945313Z","shell.execute_reply.started":"2023-07-26T15:32:42.808557Z","shell.execute_reply":"2023-07-26T15:32:43.943231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config_yml_path = \"/kaggle/input/reu2023models/eca_nfnet_l0-20230722-1300_db_z_sigmoid_config.yml\"\nmodel_path = \"/kaggle/input/reu2023models/eca_nfnet_l0-20230724-1752.pt\"\ncheckpoint_path = '/kaggle/input/ecanfnet-l0/ecanfnet_l0_ra2-e3e9ac50.pth'","metadata":{"execution":{"iopub.status.busy":"2023-07-26T15:32:43.948946Z","iopub.execute_input":"2023-07-26T15:32:43.949519Z","iopub.status.idle":"2023-07-26T15:32:43.958810Z","shell.execute_reply.started":"2023-07-26T15:32:43.949433Z","shell.execute_reply":"2023-07-26T15:32:43.956550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# copy files into write directory so config can be loaded in\nimport shutil\nsrc = \"/kaggle/input/pyha-analyzer\"\ndst = \"/kaggle/working/pyha-analyzer\"\nshutil.copytree(src, dst)\n!cp {config_yml_path} /kaggle/working/pyha-analyzer/config.yml","metadata":{"execution":{"iopub.status.busy":"2023-07-26T15:32:43.960664Z","iopub.execute_input":"2023-07-26T15:32:43.961104Z","iopub.status.idle":"2023-07-26T15:32:45.162194Z","shell.execute_reply.started":"2023-07-26T15:32:43.961063Z","shell.execute_reply":"2023-07-26T15:32:45.159809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pyha_analyzer for model dependency\nimport sys\nsys.path.append('/kaggle/working/pyha-analyzer/')\nsys.argv = [\"\"]\nfrom pyha_analyzer import config\nfrom pyha_analyzer.models.timm_model import TimmModel\nfrom pyha_analyzer.utils import set_seed","metadata":{"execution":{"iopub.status.busy":"2023-07-26T15:32:45.166761Z","iopub.execute_input":"2023-07-26T15:32:45.167269Z","iopub.status.idle":"2023-07-26T15:32:45.176702Z","shell.execute_reply.started":"2023-07-26T15:32:45.167214Z","shell.execute_reply":"2023-07-26T15:32:45.174634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pytorch requirements\nimport timm\nimport torch\nfrom torch import nn\nimport torch.nn.functional as F\nfrom torch.amp.autocast_mode import autocast\nfrom torch.optim import Adam\nfrom torch.utils.data import DataLoader, WeightedRandomSampler\nfrom torchmetrics.classification import MultilabelAveragePrecision\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-07-26T15:32:45.178177Z","iopub.execute_input":"2023-07-26T15:32:45.178621Z","iopub.status.idle":"2023-07-26T15:32:45.193771Z","shell.execute_reply.started":"2023-07-26T15:32:45.178580Z","shell.execute_reply":"2023-07-26T15:32:45.192447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg = config.cfg\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'\nmodel = timm.create_model(model_name = cfg.model,\n                          drop_rate=0.5,\n                          checkpoint_path=checkpoint_path).to(device)\nmodel.head.fc = nn.Linear(2304, 262)\nmodel.loss_fn = nn.CrossEntropyLoss()\n# load trained model checkpoint\nmodel.load_state_dict(torch.load(model_path, map_location=torch.device(device)), strict=False)\nmodel.eval()\nprint(\"loaded model!\")","metadata":{"execution":{"iopub.status.busy":"2023-07-26T15:32:45.195834Z","iopub.execute_input":"2023-07-26T15:32:45.196383Z","iopub.status.idle":"2023-07-26T15:32:46.732272Z","shell.execute_reply.started":"2023-07-26T15:32:45.196322Z","shell.execute_reply":"2023-07-26T15:32:46.730519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pretrained = timm.create_model(model_name = cfg.model,\n                          drop_rate=0.5,\n                          checkpoint_path=checkpoint_path).to(device)\npretrained.head.fc = nn.Linear(2304, 262)\npretrained","metadata":{"execution":{"iopub.status.busy":"2023-07-26T15:32:46.734031Z","iopub.execute_input":"2023-07-26T15:32:46.734438Z","iopub.status.idle":"2023-07-26T15:32:47.385929Z","shell.execute_reply.started":"2023-07-26T15:32:46.734399Z","shell.execute_reply":"2023-07-26T15:32:47.384444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Make inference","metadata":{}},{"cell_type":"code","source":"import os\nfrom pathlib import Path\nfrom joblib import Parallel, delayed\nfrom tqdm import tqdm\nimport glob\nimport pandas as pd\nimport torch\nimport torch.nn as nn\nimport torchaudio\nimport timm","metadata":{"execution":{"iopub.status.busy":"2023-07-26T15:32:47.387755Z","iopub.execute_input":"2023-07-26T15:32:47.388195Z","iopub.status.idle":"2023-07-26T15:32:47.395823Z","shell.execute_reply.started":"2023-07-26T15:32:47.388143Z","shell.execute_reply":"2023-07-26T15:32:47.394316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/birdclef-2023/train_metadata.csv')\ncompetition_classes = sorted(df.primary_label.unique())\nfilepaths = glob.glob('/kaggle/input/birdclef-2023/test_soundscapes/*.ogg')\n# remove classes without examples\ncompetition_classes.remove(\"afpkin1\")\ncompetition_classes.remove(\"golher1\")","metadata":{"execution":{"iopub.status.busy":"2023-07-26T15:32:47.397970Z","iopub.execute_input":"2023-07-26T15:32:47.398565Z","iopub.status.idle":"2023-07-26T15:32:47.509115Z","shell.execute_reply.started":"2023-07-26T15:32:47.398501Z","shell.execute_reply":"2023-07-26T15:32:47.507408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def to_image(audio):\n        \"\"\"\n        Convert audio clip to 3-channel spectrogram image\n        \"\"\"\n        convert_to_mel = audtr.MelSpectrogram(\n                sample_rate=cfg.sample_rate,\n                n_mels=cfg.n_mels,\n                n_fft=cfg.n_fft)\n        mel = convert_to_mel(audio)\n        # Convert to Image\n        image = torch.stack([mel, mel, mel])\n        \n        # Convert to decibels\n        # Log scale the power\n        decibel_convert = audtr.AmplitudeToDB(stype=\"power\")\n        image = decibel_convert(image)\n        \n        # Normalize Image\n        # Inspired by\n        # https://medium.com/@hasithsura/audio-classification-d37a82d6715\n        mean = image.mean()\n        std = image.std()\n        image = (image - mean) / (std + 1e-6)\n        \n        # Sigmoid to get 0 to 1 scaling (0.5 becomes mean)\n        image = torch.sigmoid(image)\n        return image","metadata":{"execution":{"iopub.status.busy":"2023-07-26T15:32:47.514268Z","iopub.execute_input":"2023-07-26T15:32:47.514810Z","iopub.status.idle":"2023-07-26T15:32:47.526289Z","shell.execute_reply.started":"2023-07-26T15:32:47.514762Z","shell.execute_reply":"2023-07-26T15:32:47.524770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# make predictions\nMIN_WINDOW = 32_000 * 5\nfrom torchaudio import transforms as audtr\nimport torch.nn.functional as F\n\ndef process(filepath):\n    all_predictions = []\n    name = Path(filepath).stem\n    audio = torchaudio.load(filepath)[0][0]\n    for i in range(0, 120):\n        crop = audio[i*MIN_WINDOW:(i+1)*MIN_WINDOW]\n        image = to_image(crop)\n        \n        with torch.no_grad():\n            #pred = F.softmax(model(image[None]))\n            pred = torch.sigmoid(model(image[None]))\n        t = (i + 1) * 5\n        \n        all_predictions.append({\"row_id\": f'{name}_{t}',\"predictions\": pred})\n    return all_predictions\nall_predictions = [process(f) for f in filepaths]\n# all_predictions = Parallel(n_jobs=os.cpu_count())(\n#     delayed(process)(filepath) \n#     for filepath in tqdm(filepaths, 'Processing files')\n# )\nall_predictions = [p2 for p in all_predictions for p2 in p] # flatten","metadata":{"execution":{"iopub.status.busy":"2023-07-26T15:35:23.275817Z","iopub.execute_input":"2023-07-26T15:35:23.276295Z","iopub.status.idle":"2023-07-26T15:35:51.368108Z","shell.execute_reply.started":"2023-07-26T15:35:23.276250Z","shell.execute_reply":"2023-07-26T15:35:51.367046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert predictions into a dataframe\ndf = pd.concat([\n    pd.DataFrame({'row_id': [p['row_id'] for p in all_predictions]}), \n    pd.DataFrame(torch.stack([p['predictions'][0] for p in all_predictions]).numpy(), columns=competition_classes)\n], axis=1)\ndf","metadata":{"execution":{"iopub.status.busy":"2023-07-26T15:35:51.370222Z","iopub.execute_input":"2023-07-26T15:35:51.370862Z","iopub.status.idle":"2023-07-26T15:35:51.423224Z","shell.execute_reply.started":"2023-07-26T15:35:51.370821Z","shell.execute_reply":"2023-07-26T15:35:51.421748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# add removed birds\ndf[\"afpkin1\"] = 0\ndf[\"golher1\"] = 0\ndf = pd.concat([df['row_id'],\n          df[df.columns.difference(['row_id'])]\\\n         .sort_index(axis=1)], ignore_index=False, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-07-26T15:35:51.425049Z","iopub.execute_input":"2023-07-26T15:35:51.425466Z","iopub.status.idle":"2023-07-26T15:35:51.441562Z","shell.execute_reply.started":"2023-07-26T15:35:51.425424Z","shell.execute_reply":"2023-07-26T15:35:51.439508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# what were the top birds predicted?\ndf.iloc[:, 2:].idxmax(axis=1).value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-07-26T15:35:51.444335Z","iopub.execute_input":"2023-07-26T15:35:51.444776Z","iopub.status.idle":"2023-07-26T15:35:51.464114Z","shell.execute_reply.started":"2023-07-26T15:35:51.444735Z","shell.execute_reply":"2023-07-26T15:35:51.462462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"rebfir2\"].describe()","metadata":{"execution":{"iopub.status.busy":"2023-07-26T15:35:54.135821Z","iopub.execute_input":"2023-07-26T15:35:54.136289Z","iopub.status.idle":"2023-07-26T15:35:54.153239Z","shell.execute_reply.started":"2023-07-26T15:35:54.136249Z","shell.execute_reply":"2023-07-26T15:35:54.151256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2023-07-26T15:34:55.974278Z","iopub.execute_input":"2023-07-26T15:34:55.975723Z","iopub.status.idle":"2023-07-26T15:34:56.604837Z","shell.execute_reply.started":"2023-07-26T15:34:55.975652Z","shell.execute_reply":"2023-07-26T15:34:56.602942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-26T15:33:15.075443Z","iopub.execute_input":"2023-07-26T15:33:15.075871Z","iopub.status.idle":"2023-07-26T15:33:15.135600Z","shell.execute_reply.started":"2023-07-26T15:33:15.075830Z","shell.execute_reply":"2023-07-26T15:33:15.134211Z"},"trusted":true},"execution_count":null,"outputs":[]}]}