{"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":"# Inference using 21Classes","metadata":{}},{"cell_type":"markdown","source":"#### Code copied with minimal changes from this Notebook:\nhttps://www.kaggle.com/code/myso1987/birdclef2022-pytorch-resnet34-starter-lb-0-50","metadata":{}},{"cell_type":"markdown","source":"# Install required packages","metadata":{}},{"cell_type":"code","source":"!cp -r ../input/timm-pytorch-image-models .","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:42:23.752908Z","iopub.execute_input":"2023-11-14T12:42:23.753148Z","iopub.status.idle":"2023-11-14T12:42:26.355062Z","shell.execute_reply.started":"2023-11-14T12:42:23.753083Z","shell.execute_reply":"2023-11-14T12:42:26.353608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install timm-pytorch-image-models/pytorch-image-models-master/","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:42:26.36029Z","iopub.execute_input":"2023-11-14T12:42:26.36087Z","iopub.status.idle":"2023-11-14T12:42:58.341025Z","shell.execute_reply.started":"2023-11-14T12:42:26.360824Z","shell.execute_reply":"2023-11-14T12:42:58.340201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r ../input/torchlibrosa .","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:42:58.342565Z","iopub.execute_input":"2023-11-14T12:42:58.342884Z","iopub.status.idle":"2023-11-14T12:42:59.302684Z","shell.execute_reply.started":"2023-11-14T12:42:58.342844Z","shell.execute_reply":"2023-11-14T12:42:59.301755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install torchlibrosa/torchlibrosa-0.0.5-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:42:59.304717Z","iopub.execute_input":"2023-11-14T12:42:59.304977Z","iopub.status.idle":"2023-11-14T12:43:28.136701Z","shell.execute_reply.started":"2023-11-14T12:42:59.304947Z","shell.execute_reply":"2023-11-14T12:43:28.135973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install catalyst==20.12","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:28.138063Z","iopub.execute_input":"2023-11-14T12:43:28.138271Z","iopub.status.idle":"2023-11-14T12:43:28.142467Z","shell.execute_reply.started":"2023-11-14T12:43:28.138247Z","shell.execute_reply":"2023-11-14T12:43:28.141732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Library","metadata":{}},{"cell_type":"code","source":"import os\nimport json\nimport tqdm\nimport random\nimport shutil\nimport pandas as pd\nimport numpy as np\nimport torch\nimport torch.optim as optim\nimport torch.nn as nn\nfrom torch.utils.data import DataLoader, Dataset\nimport torchaudio\nimport torchaudio.transforms as T\nfrom torchvision.models.resnet import ResNet, BasicBlock\n\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import f1_score ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-14T12:43:28.143514Z","iopub.execute_input":"2023-11-14T12:43:28.143738Z","iopub.status.idle":"2023-11-14T12:43:30.708295Z","shell.execute_reply.started":"2023-11-14T12:43:28.143712Z","shell.execute_reply":"2023-11-14T12:43:30.707339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\nimport os\nimport math\nimport random\nimport warnings\n\nimport albumentations as A\nimport cv2\nimport librosa\nimport numpy as np\nimport pandas as pd\nimport soundfile as sf\nimport timm\nimport torch\nimport torch.optim as optim\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.utils.data as torchdata\n\nfrom pathlib import Path\nfrom typing import List\n\nfrom albumentations.pytorch import ToTensorV2\nfrom albumentations.core.transforms_interface import ImageOnlyTransform\nfrom sklearn import model_selection\nfrom sklearn import metrics\nfrom timm.models.layers import SelectAdaptivePool2d\nfrom torch.optim.optimizer import Optimizer\nfrom torchlibrosa.stft import LogmelFilterBank, Spectrogram\nfrom torchlibrosa.augmentation import SpecAugmentation\nimport logging\nfrom logging import getLogger, INFO, FileHandler,  Formatter,  StreamHandler\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:30.70992Z","iopub.execute_input":"2023-11-14T12:43:30.710586Z","iopub.status.idle":"2023-11-14T12:43:33.739664Z","shell.execute_reply.started":"2023-11-14T12:43:30.710543Z","shell.execute_reply":"2023-11-14T12:43:33.738813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed: int = 42):\n    random.seed(seed)\n    np.random.seed(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)  # type: ignore\n    torch.backends.cudnn.deterministic = True  # type: ignore\n    torch.backends.cudnn.benchmark = True  # type: ignore\n\ndef init_logger(log_file='train.log'):\n    \n    logger = getLogger(__name__)\n    logger.setLevel(INFO)\n    handler1 = StreamHandler()\n    handler1.setFormatter(Formatter(\"%(message)s\"))\n    handler2 = FileHandler(filename=log_file)\n    handler2.setFormatter(Formatter(\"%(message)s\"))\n    logger.addHandler(handler1)\n    logger.addHandler(handler2)\n    return logger\n    \ndef get_logger(out_file=None):\n    logger = logging.getLogger()\n    formatter = logging.Formatter(\"%(asctime)s - %(levelname)s - %(message)s\")\n    logger.handlers = []\n    logger.setLevel(logging.INFO)\n\n    handler = logging.StreamHandler()\n    handler.setFormatter(formatter)\n    handler.setLevel(logging.INFO)\n    logger.addHandler(handler)\n\n    if out_file is not None:\n        fh = logging.FileHandler(out_file)\n        fh.setFormatter(formatter)\n        fh.setLevel(logging.INFO)\n        logger.addHandler(fh)\n    logger.info(\"logger set up\")\n    return logger\n\ndef get_device() -> torch.device:\n    return torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:33.741279Z","iopub.execute_input":"2023-11-14T12:43:33.741565Z","iopub.status.idle":"2023-11-14T12:43:33.753968Z","shell.execute_reply.started":"2023-11-14T12:43:33.741522Z","shell.execute_reply":"2023-11-14T12:43:33.753158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logger = get_logger(\"main.log\")\nset_seed(1213)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:33.755407Z","iopub.execute_input":"2023-11-14T12:43:33.755934Z","iopub.status.idle":"2023-11-14T12:43:33.775244Z","shell.execute_reply.started":"2023-11-14T12:43:33.755895Z","shell.execute_reply":"2023-11-14T12:43:33.774628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = get_device()","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:33.776139Z","iopub.execute_input":"2023-11-14T12:43:33.77634Z","iopub.status.idle":"2023-11-14T12:43:33.849208Z","shell.execute_reply.started":"2023-11-14T12:43:33.776315Z","shell.execute_reply":"2023-11-14T12:43:33.848459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Loading","metadata":{}},{"cell_type":"code","source":"root_path = \"../input/birdclef-2022/\"\ninput_path = root_path + '/train_audio/'\nout_path = \"./train/\"\n\ntry:\n    os.mkdir(out_path)\nexcept FileExistsError:\n    pass\n\n\ntrain_meta = pd.read_csv(root_path + 'train_metadata.csv')\n\nwith open(root_path + '/scored_birds.json') as sbfile:\n    scored_birds = json.load(sbfile)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:33.852689Z","iopub.execute_input":"2023-11-14T12:43:33.85295Z","iopub.status.idle":"2023-11-14T12:43:34.011787Z","shell.execute_reply.started":"2023-11-14T12:43:33.852917Z","shell.execute_reply":"2023-11-14T12:43:34.011007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(scored_birds)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.012971Z","iopub.execute_input":"2023-11-14T12:43:34.013605Z","iopub.status.idle":"2023-11-14T12:43:34.021212Z","shell.execute_reply.started":"2023-11-14T12:43:34.013562Z","shell.execute_reply":"2023-11-14T12:43:34.020496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scored_birds","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.022404Z","iopub.execute_input":"2023-11-14T12:43:34.022613Z","iopub.status.idle":"2023-11-14T12:43:34.033335Z","shell.execute_reply.started":"2023-11-14T12:43:34.022587Z","shell.execute_reply":"2023-11-14T12:43:34.032603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### only 21 classes","metadata":{}},{"cell_type":"code","source":"train_meta_21classes = train_meta[train_meta['primary_label'].isin(scored_birds)]\nbird_label_21classes = sorted(train_meta_21classes[\"primary_label\"].unique())\nbird_label_total = sorted(train_meta[\"primary_label\"].unique())\nprint(bird_label_21classes,\"\\n\", bird_label_total)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.034323Z","iopub.execute_input":"2023-11-14T12:43:34.034542Z","iopub.status.idle":"2023-11-14T12:43:34.056959Z","shell.execute_reply.started":"2023-11-14T12:43:34.034515Z","shell.execute_reply":"2023-11-14T12:43:34.05622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from ast import literal_eval\n#train_meta['secondary_labels'] = train_meta['secondary_labels'].map(lambda x: literal_eval(x))","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.057926Z","iopub.execute_input":"2023-11-14T12:43:34.05813Z","iopub.status.idle":"2023-11-14T12:43:34.068196Z","shell.execute_reply.started":"2023-11-14T12:43:34.058104Z","shell.execute_reply":"2023-11-14T12:43:34.06752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#total = []\n#second_labels =  train_meta['secondary_labels'].tolist()\n#for s in second_labels:\n#    total.extend(s)\n#secondary_labels = list(set(total))","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.069125Z","iopub.execute_input":"2023-11-14T12:43:34.069334Z","iopub.status.idle":"2023-11-14T12:43:34.078387Z","shell.execute_reply.started":"2023-11-14T12:43:34.069302Z","shell.execute_reply":"2023-11-14T12:43:34.077791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    ######################\n    # Globals #\n    ######################\n    seed = 1213\n    epochs = 35\n    train = True\n    folds = [0]\n    img_size = 224\n    main_metric = \"epoch_f1_at_05\"\n    minimize_metric = False\n\n    ######################\n    # Data #\n    ######################\n    train_datadir = Path(\"../input/birdclef-2022/train_audio\")\n    train_csv = train_meta\n    #train_soundscape = \"../input/birdclef-2021/train_soundscape_labels.csv\"\n\n    ######################\n    # Dataset #\n    ######################\n    transforms = {\n        \"train\": [{\"name\": \"Normalize\"}],\n        \"valid\": [{\"name\": \"Normalize\"}],\n        \"test\": [{\"name\": \"Normalize\"}]\n    }\n    period = 20\n    n_mels = 256\n    fmin = 20\n    fmax = 16000\n    n_fft = 2048\n    hop_length = 512\n    sample_rate = 32000\n    melspectrogram_parameters = {\n        \"n_mels\": 256,\n        \"fmin\": 20,\n        \"fmax\": 16000\n    }\n\n    target_columns = scored_birds\n\n    ######################\n    # Loaders #\n    ######################\n    loader_params = {\n        \"train\": {\n            \"batch_size\": 32,\n            \"num_workers\": 20,\n            \"shuffle\": True\n        },\n        \"valid\": {\n            \"batch_size\": 64,\n            \"num_workers\": 20,\n            \"shuffle\": False\n        },\n        \"test\": {\n            \"batch_size\": 64,\n            \"num_workers\": 20,\n            \"shuffle\": False\n        }\n    }\n\n    ######################\n    # Split #\n    ######################\n    split = \"StratifiedKFold\"\n    split_params = {\n        \"n_splits\": 5,\n        \"shuffle\": True,\n        \"random_state\": 1213\n    }\n\n    ######################\n    # Model #\n    ######################\n    base_model_name = \"tf_efficientnet_b0_ns\"\n    pooling = \"max\"\n    pretrained = True\n    num_classes = 21\n    n_pretrain_classes = 131\n    in_channels = 1\n\n    ######################\n    # Criterion #\n    ######################\n    loss_name = \"BCEFocal2WayLoss\"\n    loss_params: dict = {}\n\n    ######################\n    # Optimizer #\n    ######################\n    optimizer_name = \"Adam\"\n    base_optimizer = \"Adam\"\n    optimizer_params = {\n        \"lr\": 0.001\n    }\n    # For SAM optimizer\n    base_optimizer = \"Adam\"\n\n    ######################\n    # Scheduler #\n    ######################\n    scheduler_name = \"CosineAnnealingLR\"\n    scheduler_params = {\n        \"T_max\": 10\n    }","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.07957Z","iopub.execute_input":"2023-11-14T12:43:34.079842Z","iopub.status.idle":"2023-11-14T12:43:34.092983Z","shell.execute_reply.started":"2023-11-14T12:43:34.079804Z","shell.execute_reply":"2023-11-14T12:43:34.092139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG_NFNETMIXUP:\n    ######################\n    # Globals #\n    ######################\n        fold = 4\n        DEBUG = False\n        mixed_precision = False\n        period = 5\n        hop_length = 320\n        seed = 1213\n        epochs = 100\n        train = True\n        folds = [0]\n        img_size = 224\n        main_metric = \"epoch_f1_at_02\"\n        minimize_metric = False\n\n        ######################\n        # Data #\n        ######################\n        train_datadir = Path(\"../input/birdclef-2022/train_audio\")\n        train_csv = train_meta\n        #train_soundscape = \"../input/birdclef-2021/train_soundscape_labels.csv\"\n\n        ######################\n        # Dataset #\n        ######################\n        transforms = {\n            \"train\": [{\"name\": \"Normalize\"}],\n            \"valid\": [{\"name\": \"Normalize\"}],\n            \"test\": [{\"name\": \"Normalize\"}]\n        }\n        \n        n_mels = 256\n        #256 change 비교 필요\n        fmin = 10\n        fmax = 16000\n        n_fft = 2048\n        \n        sample_rate = 32000\n        melspectrogram_parameters = {\n            \"n_mels\": 256,\n            \"fmin\": 20,\n            \"fmax\": 16000\n        }\n\n        target_columns = bird_label_total\n\n        ######################\n        # Loaders #\n        ######################\n        loader_params = {\n            \"train\": {\n                \"batch_size\": 32,\n                \"num_workers\": 20,\n                \"shuffle\": True\n            },\n            \"valid\": {\n                \"batch_size\": 64,\n                \"num_workers\": 20,\n                \"shuffle\": False\n            },\n            \"test\": {\n                \"batch_size\": 64,\n                \"num_workers\": 20,\n                \"shuffle\": False\n            }\n        }\n\n        ######################\n        # Split #\n        ######################\n        split = \"StratifiedKFold\"\n        split_params = {\n            \"n_splits\": 5,\n            \"shuffle\": True,\n            \"random_state\": 1213\n        }\n\n        ######################\n        # Model #\n        ######################\n        base_model_name = \"eca_nfnet_l0\"\n        pooling = \"max\"\n        pretrained = False\n        num_classes = len(bird_label_total)\n        in_channels = 1\n\n        ######################\n        # Criterion #\n        ######################\n        loss_name = \"BCEFocal2WayLoss\"\n        loss_params: dict = {}\n\n        ######################\n        # Optimizer #\n        ######################\n        optimizer_name = \"Adam\"\n        base_optimizer = \"Adam\"\n        optimizer_params = {\n            \"lr\": 0.0005,\n            #\"weight_decay\":1e-2,\n        }\n\n        ######################\n        # Scheduler #\n        ######################\n        scheduler_name = \"CosineAnnealingLR\"","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.093915Z","iopub.execute_input":"2023-11-14T12:43:34.094095Z","iopub.status.idle":"2023-11-14T12:43:34.107918Z","shell.execute_reply.started":"2023-11-14T12:43:34.094072Z","shell.execute_reply":"2023-11-14T12:43:34.107218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG_EFFV2DOUBLEMIXUP:\n    ######################\n    # Globals #\n    ######################\n        fold = 4\n        DEBUG = False\n        mixed_precision = False\n        period = 5\n        hop_length = 320\n        seed = 1213\n        epochs = 100\n        train = True\n        folds = [0]\n        img_size = 224\n        main_metric = \"epoch_f1_at_02\"\n        minimize_metric = False\n\n        ######################\n        # Data #\n        ######################\n        train_datadir = Path(\"../input/birdclef-2022/train_audio\")\n        train_csv = train_meta\n        #train_soundscape = \"../input/birdclef-2021/train_soundscape_labels.csv\"\n\n        ######################\n        # Dataset #\n        ######################\n        transforms = {\n            \"train\": [{\"name\": \"Normalize\"}],\n            \"valid\": [{\"name\": \"Normalize\"}],\n            \"test\": [{\"name\": \"Normalize\"}]\n        }\n        \n        n_mels = 128\n        #256 change 비교 필요\n        fmin = 10\n        fmax = 16000\n        n_fft = 1024\n        \n        sample_rate = 32000\n        melspectrogram_parameters = {\n            \"n_mels\": 128,\n            \"fmin\": 20,\n            \"fmax\": 16000\n        }\n\n        target_columns = bird_label_total\n\n        ######################\n        # Loaders #\n        ######################\n        loader_params = {\n            \"train\": {\n                \"batch_size\": 32,\n                \"num_workers\": 20,\n                \"shuffle\": True\n            },\n            \"valid\": {\n                \"batch_size\": 64,\n                \"num_workers\": 20,\n                \"shuffle\": False\n            },\n            \"test\": {\n                \"batch_size\": 64,\n                \"num_workers\": 20,\n                \"shuffle\": False\n            }\n        }\n\n        ######################\n        # Split #\n        ######################\n        split = \"StratifiedKFold\"\n        split_params = {\n            \"n_splits\": 4,\n            \"shuffle\": True,\n            \"random_state\": 1213\n        }\n\n        ######################\n        # Model #\n        ######################\n        base_model_name = \"tf_efficientnetv2_s_in21k\"\n        pooling = \"max\"\n        pretrained = False\n        num_classes = len(bird_label_total)\n        in_channels = 1\n\n        ######################\n        # Criterion #\n        ######################\n        loss_name = \"BCEFocal2WayLoss\"\n        loss_params: dict = {}\n\n        ######################\n        # Optimizer #\n        ######################\n        optimizer_name = \"Adam\"\n        base_optimizer = \"Adam\"\n        optimizer_params = {\n            \"lr\": 0.0005,\n            #\"weight_decay\":1e-2,\n        }\n\n        ######################\n        # Scheduler #\n        ######################\n        scheduler_name = \"CosineAnnealingLR\"","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.108973Z","iopub.execute_input":"2023-11-14T12:43:34.109174Z","iopub.status.idle":"2023-11-14T12:43:34.123269Z","shell.execute_reply.started":"2023-11-14T12:43:34.109149Z","shell.execute_reply":"2023-11-14T12:43:34.122528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG_EFFV2DOUBLEMIXUP_64MEL:\n    ######################\n    # Globals #\n    ######################\n        fold = 4\n        DEBUG = False\n        mixed_precision = False\n        period = 5\n        hop_length = 512\n        seed = 1213\n        epochs = 100\n        train = True\n        folds = [0]\n        img_size = 224\n        main_metric = \"epoch_f1_at_02\"\n        minimize_metric = False\n\n        ######################\n        # Data #\n        ######################\n        train_datadir = Path(\"../input/birdclef-2022/train_audio\")\n        train_csv = train_meta\n        #train_soundscape = \"../input/birdclef-2021/train_soundscape_labels.csv\"\n\n        ######################\n        # Dataset #\n        ######################\n        transforms = {\n            \"train\": [{\"name\": \"Normalize\"}],\n            \"valid\": [{\"name\": \"Normalize\"}],\n            \"test\": [{\"name\": \"Normalize\"}]\n        }\n        \n        n_mels = 64\n        #256 change 비교 필요\n        fmin = 10\n        fmax = 16000\n        n_fft = 2048\n        \n        sample_rate = 32000\n        melspectrogram_parameters = {\n            \"n_mels\": 64,\n            \"fmin\": 20,\n            \"fmax\": 16000\n        }\n\n        target_columns = bird_label_total\n\n        ######################\n        # Loaders #\n        ######################\n        loader_params = {\n            \"train\": {\n                \"batch_size\": 32,\n                \"num_workers\": 20,\n                \"shuffle\": True\n            },\n            \"valid\": {\n                \"batch_size\": 64,\n                \"num_workers\": 20,\n                \"shuffle\": False\n            },\n            \"test\": {\n                \"batch_size\": 64,\n                \"num_workers\": 20,\n                \"shuffle\": False\n            }\n        }\n\n        ######################\n        # Split #\n        ######################\n        split = \"StratifiedKFold\"\n        split_params = {\n            \"n_splits\": 4,\n            \"shuffle\": True,\n            \"random_state\": 1213\n        }\n\n        ######################\n        # Model #\n        ######################\n        base_model_name = \"tf_efficientnetv2_s_in21k\"\n        pooling = \"max\"\n        pretrained = False\n        num_classes = len(bird_label_total)\n        in_channels = 1\n\n        ######################\n        # Criterion #\n        ######################\n        loss_name = \"BCEFocal2WayLoss\"\n        loss_params: dict = {}\n\n        ######################\n        # Optimizer #\n        ######################\n        optimizer_name = \"Adam\"\n        base_optimizer = \"Adam\"\n        optimizer_params = {\n            \"lr\": 0.0005,\n            #\"weight_decay\":1e-2,\n        }\n\n        ######################\n        # Scheduler #\n        ######################\n        scheduler_name = \"CosineAnnealingLR\"","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.124505Z","iopub.execute_input":"2023-11-14T12:43:34.124951Z","iopub.status.idle":"2023-11-14T12:43:34.138079Z","shell.execute_reply.started":"2023-11-14T12:43:34.124913Z","shell.execute_reply":"2023-11-14T12:43:34.137485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG_EFFV2MDOUBLEMIXUP:\n    ######################\n    # Globals #\n    ######################\n        fold = 4\n        DEBUG = False\n        mixed_precision = False\n        period = 5\n        hop_length = 320\n        seed = 1213\n        epochs = 100\n        train = True\n        folds = [0]\n        img_size = 224\n        main_metric = \"epoch_f1_at_02\"\n        minimize_metric = False\n\n        ######################\n        # Data #\n        ######################\n        train_datadir = Path(\"../input/birdclef-2022/train_audio\")\n        train_csv = train_meta\n        #train_soundscape = \"../input/birdclef-2021/train_soundscape_labels.csv\"\n\n        ######################\n        # Dataset #\n        ######################\n        transforms = {\n            \"train\": [{\"name\": \"Normalize\"}],\n            \"valid\": [{\"name\": \"Normalize\"}],\n            \"test\": [{\"name\": \"Normalize\"}]\n        }\n        \n        n_mels = 128\n        #256 change 비교 필요\n        fmin = 10\n        fmax = 16000\n        n_fft = 1024\n        \n        sample_rate = 32000\n        melspectrogram_parameters = {\n            \"n_mels\": 128,\n            \"fmin\": 20,\n            \"fmax\": 16000\n        }\n\n        target_columns = bird_label_total\n\n        ######################\n        # Loaders #\n        ######################\n        loader_params = {\n            \"train\": {\n                \"batch_size\": 32,\n                \"num_workers\": 20,\n                \"shuffle\": True\n            },\n            \"valid\": {\n                \"batch_size\": 64,\n                \"num_workers\": 20,\n                \"shuffle\": False\n            },\n            \"test\": {\n                \"batch_size\": 64,\n                \"num_workers\": 20,\n                \"shuffle\": False\n            }\n        }\n\n        ######################\n        # Split #\n        ######################\n        split = \"StratifiedKFold\"\n        split_params = {\n            \"n_splits\": 4,\n            \"shuffle\": True,\n            \"random_state\": 1213\n        }\n\n        ######################\n        # Model #\n        ######################\n        base_model_name = \"tf_efficientnetv2_m_in21k\"\n        pooling = \"max\"\n        pretrained = False\n        num_classes = len(bird_label_total)\n        in_channels = 1\n\n        ######################\n        # Criterion #\n        ######################\n        loss_name = \"BCEFocal2WayLoss\"\n        loss_params: dict = {}\n\n        ######################\n        # Optimizer #\n        ######################\n        optimizer_name = \"Adam\"\n        base_optimizer = \"Adam\"\n        optimizer_params = {\n            \"lr\": 0.0005,\n            #\"weight_decay\":1e-2,\n        }\n\n        ######################\n        # Scheduler #\n        ######################\n        scheduler_name = \"CosineAnnealingLR\"","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.139065Z","iopub.execute_input":"2023-11-14T12:43:34.139287Z","iopub.status.idle":"2023-11-14T12:43:34.153625Z","shell.execute_reply.started":"2023-11-14T12:43:34.139255Z","shell.execute_reply":"2023-11-14T12:43:34.152901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG_NFNET0_SPEC_64MEL_15SEC:\n    ######################\n    # Globals #\n    ######################\n        fold = 0\n        DEBUG = False\n        mixed_precision = False\n        savename = f\"totalclass-doublemixup-5sec-length2048-64mel-512hop-weightmixed-nfnet-pretrain-fold-{fold}\"\n        period = 15\n        hop_length = 512\n        seed = 888\n        epochs = 100\n        train = True\n        folds = [0]\n        img_size = 224\n        main_metric = \"epoch_f1_at_02\"\n        minimize_metric = False\n\n        ######################\n        # Data #\n        ######################\n        train_datadir = Path(\"train\")\n        train_csv = train_meta\n        #train_soundscape = \"../input/birdclef-2021/train_soundscape_labels.csv\"\n\n        ######################\n        # Dataset #\n        ######################\n        transforms = {\n            #\"train\": [{\"name\":\"PitchShift\"},{\"name\":\"PinkNoise\"},{\"name\":\"RandomVolume\"},{\"name\": \"Normalize\"}],\n            \"train\": [{\"name\":\"GaussianNoise\"},{\"name\":\"PinkNoise\"},{\"name\":\"RandomVolume\"},{\"name\": \"Normalize\"}],\n            \"valid\": [{\"name\": \"Normalize\"}],\n            \"test\": [{\"name\": \"Normalize\"}]\n        }\n        \n        n_mels = 64\n        #256 change 비교 필요\n        fmin = 10\n        fmax = 16000\n        n_fft = 2048\n        \n        sample_rate = 32000\n        melspectrogram_parameters = {\n            \"n_mels\": n_mels,\n            \"fmin\": 20,\n            \"fmax\": 16000\n        }\n\n        target_columns = bird_label_total\n\n        ######################\n        # Loaders #\n        ######################\n        loader_params = {\n            \"train\": {\n                \"batch_size\": 32,\n                \"num_workers\": 20,\n                \"shuffle\": True\n            },\n            \"valid\": {\n                \"batch_size\": 64,\n                \"num_workers\": 20,\n                \"shuffle\": False\n            },\n            \"test\": {\n                \"batch_size\": 64,\n                \"num_workers\": 20,\n                \"shuffle\": False\n            }\n        }\n\n        ######################\n        # Split #\n        ######################\n        split = \"StratifiedKFold\"\n        split_params = {\n            \"n_splits\": 4,\n            \"shuffle\": True,\n            \"random_state\": 888\n        }\n\n        ######################\n        # Model #\n        ######################\n        base_model_name = \"eca_nfnet_l0\"\n        pooling = \"max\"\n        pretrained = False\n        num_classes = len(bird_label_total)\n        in_channels = 1\n\n        ######################\n        # Criterion #\n        ######################\n        loss_name = \"BCEFocal2WayLoss\"\n        loss_params: dict = {}\n\n        ######################\n        # Optimizer #\n        ######################\n        optimizer_name = \"Adam\"\n        base_optimizer = \"Adam\"\n        optimizer_params = {\n            \"lr\": 0.0005,\n            #\"weight_decay\":1e-2,\n        }\n\n        ######################\n        # Scheduler #\n        ######################\n        scheduler_name = \"CosineAnnealingLR\"\n        #scheduler_params = {\n        #    #\"T_max\": 25,\n        #    \"eta_min\": 1e-5,\n        #}","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.154953Z","iopub.execute_input":"2023-11-14T12:43:34.155215Z","iopub.status.idle":"2023-11-14T12:43:34.170317Z","shell.execute_reply.started":"2023-11-14T12:43:34.155178Z","shell.execute_reply":"2023-11-14T12:43:34.169539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#MixupTimmNFNETSPECSED\n#CFG_NFNET0_SPEC_64MEL_15SEC","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.171337Z","iopub.execute_input":"2023-11-14T12:43:34.171547Z","iopub.status.idle":"2023-11-14T12:43:34.183194Z","shell.execute_reply.started":"2023-11-14T12:43:34.171521Z","shell.execute_reply":"2023-11-14T12:43:34.182417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MixupTimmNFNETSPECSED(nn.Module):\n    def __init__(self, base_model_name: str, pretrained=False, num_classes=24, in_channels=1):\n        super().__init__()\n        # Spectrogram extractor\n        self.spectrogram_extractor = Spectrogram(n_fft=CFG_NFNET0_SPEC_64MEL_15SEC.n_fft, hop_length=CFG_NFNET0_SPEC_64MEL_15SEC.hop_length,\n                                                 win_length=CFG_NFNET0_SPEC_64MEL_15SEC.n_fft, window=\"hann\", center=True, pad_mode=\"reflect\",\n                                                 freeze_parameters=True)\n\n        # Logmel feature extractor\n        self.logmel_extractor = LogmelFilterBank(sr=CFG_NFNET0_SPEC_64MEL_15SEC.sample_rate, n_fft=CFG_NFNET0_SPEC_64MEL_15SEC.n_fft,\n                                                 n_mels=CFG_NFNET0_SPEC_64MEL_15SEC.n_mels, fmin=CFG_NFNET0_SPEC_64MEL_15SEC.fmin, fmax=CFG_NFNET0_SPEC_64MEL_15SEC.fmax, ref=1.0, amin=1e-10, top_db=None,\n                                                 freeze_parameters=True)\n\n        \n        self.spec_augmenter = SpecAugmentation(time_drop_width=64, time_stripes_num=2,\n                                               freq_drop_width=8, freq_stripes_num=2)\n\n        self.mixup = Mixup()\n        self.bn0 = nn.BatchNorm2d(CFG_NFNET0_SPEC_64MEL_15SEC.n_mels)\n\n        base_model = timm.create_model(\n            base_model_name, pretrained=pretrained, in_chans=in_channels)\n        layers = list(base_model.children())[:-2]\n        self.encoder = nn.Sequential(*layers)\n        if hasattr(base_model, \"head\"):\n            in_features = base_model.head.fc.in_features\n        else:\n            in_features = base_model.classifier.in_features\n\n        self.fc1 = nn.Linear(in_features, in_features, bias=True)\n        self.att_block = AttBlockV2(\n            in_features, num_classes, activation=\"sigmoid\")\n\n        self.init_weight()\n\n    def init_weight(self):\n        init_layer(self.fc1)\n        init_bn(self.bn0)\n\n    def forward(self, batch):\n        # batch -> image, target for mixup\n        input, target = batch\n\n\n        # (batch_size, 1, time_steps, freq_bins)\n        x = self.spectrogram_extractor(input)\n        x = self.logmel_extractor(x)    # (batch_size, 1, time_steps, mel_bins)\n        frames_num = x.shape[2]\n\n        if self.training:\n            \n            x, mixup_target = self.mixup(x, target)\n            if np.random.uniform() < 0.5:\n                x, mixup_target = self.mixup(x, mixup_target)\n\n            #mixup_target = mixup_target * weight.view(-1,1)\n            #print(\"mixuped :\", mixup_target, \"before : \", target)\n        \n\n        x = x.transpose(1, 3)\n        x = self.bn0(x)\n        x = x.transpose(1, 3)\n        if self.training:\n            x = self.spec_augmenter(x)\n        #if self.training:\n        #    x = self.spec_augmenter(x)\n\n        x = x.transpose(2, 3)\n        x = self.encoder(x)\n\n        # (batch_size, channels, frames)\n        x = torch.mean(x, dim=2)\n\n        # channel smoothing\n        x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1)\n        x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1)\n        # (batch_size, channels, frames)\n        x = x1 + x2\n        \n        x = F.dropout(x, p=0.5, training=self.training)\n        x = x.transpose(1, 2)\n        #(x, _) = self.gru(x)\n        # (batch_size, channels*some, frames)\n        x = F.relu_(self.fc1(x))\n        x = x.transpose(1, 2)\n        x = F.dropout(x, p=0.5, training=self.training)\n        # x shape : (batch_size, channels*some, frames)\n        # clipwise_output shape : (batch_size, channels*some*some2)\n        # norm_att shape : (batch_size,channels*some*some2, frames)\n        # segmentwise_output shape : (batch_size, channels*some*some2, frames) ->sigmoid(self.cla(x))\n        (clipwise_output, norm_att, segmentwise_output) = self.att_block(x)\n        logit = torch.sum(norm_att * self.att_block.cla(x), dim=2)\n        segmentwise_logit = self.att_block.cla(x).transpose(1, 2)\n        segmentwise_output = segmentwise_output.transpose(1, 2)\n        #print(\"frames_num : \", frames_num)\n        interpolate_ratio = frames_num // segmentwise_output.size(1)\n\n        # Get framewise output\n        framewise_output = interpolate(segmentwise_output,\n                                       interpolate_ratio)\n        framewise_output = pad_framewise_output(framewise_output, frames_num)\n\n        framewise_logit = interpolate(segmentwise_logit, interpolate_ratio)\n        framewise_logit = pad_framewise_output(framewise_logit, frames_num)\n        if self.training:\n            output_dict = {\n                \"framewise_output\": framewise_output, # applied interpolation at segmentwise_output\n                \"segmentwise_output\": segmentwise_output,\n                \"logit\": logit,\n                \"framewise_logit\": framewise_logit,\n                \"clipwise_output\": clipwise_output,\n                \"target\":mixup_target\n            }\n        else:\n            output_dict = {\n                \"framewise_output\": framewise_output, # applied interpolation at segmentwise_output\n                \"segmentwise_output\": segmentwise_output,\n                \"logit\": logit,\n                \"framewise_logit\": framewise_logit,\n                \"clipwise_output\": clipwise_output,\n                \"target\":target\n            }\n\n        return output_dict","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.184258Z","iopub.execute_input":"2023-11-14T12:43:34.184494Z","iopub.status.idle":"2023-11-14T12:43:34.209822Z","shell.execute_reply.started":"2023-11-14T12:43:34.184467Z","shell.execute_reply":"2023-11-14T12:43:34.209156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MixupTimmEFFV2SED(nn.Module):\n    def __init__(self, base_model_name: str, pretrained=False, num_classes=24, in_channels=1):\n        super().__init__()\n        # Spectrogram extractor\n        self.spectrogram_extractor = Spectrogram(n_fft=CFG_EFFV2DOUBLEMIXUP.n_fft, hop_length=CFG_EFFV2DOUBLEMIXUP.hop_length,\n                                                 win_length=CFG_EFFV2DOUBLEMIXUP.n_fft, window=\"hann\", center=True, pad_mode=\"reflect\",\n                                                 freeze_parameters=True)\n\n        # Logmel feature extractor\n        self.logmel_extractor = LogmelFilterBank(sr=CFG_EFFV2DOUBLEMIXUP.sample_rate, n_fft=CFG_EFFV2DOUBLEMIXUP.n_fft,\n                                                 n_mels=CFG_EFFV2DOUBLEMIXUP.n_mels, fmin=CFG_EFFV2DOUBLEMIXUP.fmin, fmax=CFG_EFFV2DOUBLEMIXUP.fmax, ref=1.0, amin=1e-10, top_db=None,\n                                                 freeze_parameters=True)\n\n        \n        self.mixup = Mixup()\n        self.bn0 = nn.BatchNorm2d(CFG_EFFV2DOUBLEMIXUP.n_mels)\n\n        base_model = timm.create_model(\n            base_model_name, pretrained=pretrained, in_chans=in_channels)\n        layers = list(base_model.children())[:-2]\n        self.encoder = nn.Sequential(*layers)\n        if hasattr(base_model, \"head\"):\n            in_features = base_model.head.fc.in_features\n        else:\n            in_features = base_model.classifier.in_features\n\n        self.fc1 = nn.Linear(in_features, in_features, bias=True)\n        self.att_block = AttBlockV2(\n            in_features, num_classes, activation=\"sigmoid\")\n\n        self.init_weight()\n\n    def init_weight(self):\n        init_layer(self.fc1)\n        init_bn(self.bn0)\n\n    def forward(self, batch):\n        # batch -> image, target for mixup\n        input, target = batch\n        if CFG_EFFV2DOUBLEMIXUP.DEBUG:\n            print(\"mixup before image shape: \", input.shape, \"target before mixup shape: \", target.shape)\n\n        # (batch_size, 1, time_steps, freq_bins)\n        x = self.spectrogram_extractor(input)\n        x = self.logmel_extractor(x)    # (batch_size, 1, time_steps, mel_bins)\n        frames_num = x.shape[2]\n\n        if self.training:\n            \n            x, mixup_target, weight = self.mixup(x, target)\n            if np.random.uniform() < 0.5:\n                x, mixup_target, weight = self.mixup(x, mixup_target)\n\n            #mixup_target = mixup_target * weight.view(-1,1)\n            #print(\"mixuped :\", mixup_target, \"before : \", target)\n        \n\n        x = x.transpose(1, 3)\n        x = self.bn0(x)\n        x = x.transpose(1, 3)\n\n        #if self.training:\n        #    x = self.spec_augmenter(x)\n\n        x = x.transpose(2, 3)\n        x = self.encoder(x)\n\n        # (batch_size, channels, frames)\n        x = torch.mean(x, dim=2)\n\n        # channel smoothing\n        x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1)\n        x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1)\n        # (batch_size, channels, frames)\n        x = x1 + x2\n        \n        x = F.dropout(x, p=0.5, training=self.training)\n        x = x.transpose(1, 2)\n        #(x, _) = self.gru(x)\n        # (batch_size, channels*some, frames)\n        x = F.relu_(self.fc1(x))\n        x = x.transpose(1, 2)\n        x = F.dropout(x, p=0.5, training=self.training)\n        # x shape : (batch_size, channels*some, frames)\n        # clipwise_output shape : (batch_size, channels*some*some2)\n        # norm_att shape : (batch_size,channels*some*some2, frames)\n        # segmentwise_output shape : (batch_size, channels*some*some2, frames) ->sigmoid(self.cla(x))\n        (clipwise_output, norm_att, segmentwise_output) = self.att_block(x)\n        logit = torch.sum(norm_att * self.att_block.cla(x), dim=2)\n        segmentwise_logit = self.att_block.cla(x).transpose(1, 2)\n        segmentwise_output = segmentwise_output.transpose(1, 2)\n        #print(\"frames_num : \", frames_num)\n        interpolate_ratio = frames_num // segmentwise_output.size(1)\n\n        # Get framewise output\n        framewise_output = interpolate(segmentwise_output,\n                                       interpolate_ratio)\n        framewise_output = pad_framewise_output(framewise_output, frames_num)\n\n        framewise_logit = interpolate(segmentwise_logit, interpolate_ratio)\n        framewise_logit = pad_framewise_output(framewise_logit, frames_num)\n        if self.training:\n            output_dict = {\n                \"framewise_output\": framewise_output, # applied interpolation at segmentwise_output\n                \"segmentwise_output\": segmentwise_output,\n                \"logit\": logit,\n                \"framewise_logit\": framewise_logit,\n                \"clipwise_output\": clipwise_output,\n                \"target\":mixup_target\n            }\n        else:\n            output_dict = {\n                \"framewise_output\": framewise_output, # applied interpolation at segmentwise_output\n                \"segmentwise_output\": segmentwise_output,\n                \"logit\": logit,\n                \"framewise_logit\": framewise_logit,\n                \"clipwise_output\": clipwise_output,\n                \"target\":target\n            }\n\n        return output_dict","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.210869Z","iopub.execute_input":"2023-11-14T12:43:34.211079Z","iopub.status.idle":"2023-11-14T12:43:34.236616Z","shell.execute_reply.started":"2023-11-14T12:43:34.211054Z","shell.execute_reply":"2023-11-14T12:43:34.235919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# this notebook is by default run on debug mode (only train one epoch).\n# If you'd like to get the results on par with that of inference notebook, you'll need to train the model around 30 epochs\nDEBUG = False\nif DEBUG:\n    CFG.epochs = 1","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.23758Z","iopub.execute_input":"2023-11-14T12:43:34.237788Z","iopub.status.idle":"2023-11-14T12:43:34.250164Z","shell.execute_reply.started":"2023-11-14T12:43:34.237762Z","shell.execute_reply":"2023-11-14T12:43:34.249429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Utils","metadata":{}},{"cell_type":"code","source":"#sample_rate = 32000\n#n_fft = 4096\n#win_length = None\n#hop_length = 512\n#n_mels = 256\n#min_sec_proc = sample_rate*5\n\n#mel_spectrogram = T.MelSpectrogram(\n#    sample_rate=sample_rate,\n#    n_fft=n_fft,\n#    win_length=win_length,\n#    hop_length=hop_length,\n#    center=True,\n#    pad_mode=\"reflect\",\n#    power=2.0,\n#    norm='slaney',\n#    onesided=True,\n#    n_mels=n_mels,\n#    mel_scale=\"htk\",\n#)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.251216Z","iopub.execute_input":"2023-11-14T12:43:34.251477Z","iopub.status.idle":"2023-11-14T12:43:34.261498Z","shell.execute_reply.started":"2023-11-14T12:43:34.251447Z","shell.execute_reply":"2023-11-14T12:43:34.260795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class WaveformDataset(Dataset):\n    def __init__(self,\n                 df: pd.DataFrame,\n                 datadir: Path,\n                 img_size=224,\n                 waveform_transforms=None,\n                 period=5,\n                 validation=False):\n        self.df = df\n        self.datadir = datadir\n        self.img_size = img_size\n        self.waveform_transforms = waveform_transforms\n        self.period = period\n        self.validation = validation\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, idx: int):\n        sample = self.df.loc[idx, :]\n        wav_name = sample[\"filename\"]\n        ebird_code = sample[\"primary_label\"]\n        \n        y, sr = sf.read(self.datadir / wav_name)\n        len_wav_shape = len(y.shape)\n        if len_wav_shape == 1:\n            pass\n        else:\n            y = y[:,0]\n        \n       # print(\"shape y : \", y.shape)\n        len_y = len(y)\n        effective_length = sr * self.period\n        if len_y < effective_length:\n            new_y = np.zeros(effective_length, dtype=y.dtype)\n            if not self.validation:\n                start = np.random.randint(effective_length - len_y)\n            else:\n                start = 0\n            new_y[start:start + len_y] = y\n            y = new_y.astype(np.float32)\n        elif len_y > effective_length:\n            if not self.validation:\n                start = np.random.randint(len_y - effective_length)\n            else:\n                start = 0\n            y = y[start:start + effective_length].astype(np.float32)\n        else:\n            y = y.astype(np.float32)\n\n        y = np.nan_to_num(y)\n\n        if self.waveform_transforms:\n            y = self.waveform_transforms(y)\n\n        y = np.nan_to_num(y)\n\n        labels = np.zeros(len(CFG.target_columns), dtype=float)\n        labels[CFG.target_columns.index(ebird_code)] = 1.0\n\n        return {\n            \"image\": y,\n            \"targets\": labels\n        }","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.262692Z","iopub.execute_input":"2023-11-14T12:43:34.263004Z","iopub.status.idle":"2023-11-14T12:43:34.279537Z","shell.execute_reply.started":"2023-11-14T12:43:34.262973Z","shell.execute_reply":"2023-11-14T12:43:34.278645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_transforms(phase: str):\n    #transforms = {\n    #    \"train\": [{\"name\": \"Normalize\"}],\n    #    \"valid\": [{\"name\": \"Normalize\"}]\n    #}\n    transforms = CFG.transforms\n    if transforms is None:\n        return None\n    else:\n        if transforms[phase] is None:\n            return None\n        trns_list = []\n        for trns_conf in transforms[phase]:\n            trns_name = trns_conf[\"name\"]\n            trns_params = {} if trns_conf.get(\"params\") is None else \\\n                trns_conf[\"params\"]\n            if globals().get(trns_name) is not None:\n                trns_cls = globals()[trns_name]\n                trns_list.append(trns_cls(**trns_params))\n\n        if len(trns_list) > 0:\n            return Compose(trns_list)\n        else:\n            return None\n        \n        \nclass Normalize:\n    def __call__(self, y: np.ndarray):\n        max_vol = np.abs(y).max()\n        y_vol = y * 1 / max_vol\n        return np.asfortranarray(y_vol)\n\n\nclass Compose:\n    def __init__(self, transforms: list):\n        self.transforms = transforms\n\n    def __call__(self, y: np.ndarray):\n        for trns in self.transforms:\n            y = trns(y)\n        return y","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.280864Z","iopub.execute_input":"2023-11-14T12:43:34.281178Z","iopub.status.idle":"2023-11-14T12:43:34.295271Z","shell.execute_reply.started":"2023-11-14T12:43:34.281141Z","shell.execute_reply":"2023-11-14T12:43:34.294493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def init_layer(layer):\n    nn.init.xavier_uniform_(layer.weight)\n\n    if hasattr(layer, \"bias\"):\n        if layer.bias is not None:\n            layer.bias.data.fill_(0.)\n\n\ndef init_bn(bn):\n    bn.bias.data.fill_(0.)\n    bn.weight.data.fill_(1.0)\n\n\ndef init_weights(model):\n    classname = model.__class__.__name__\n    if classname.find(\"Conv2d\") != -1:\n        nn.init.xavier_uniform_(model.weight, gain=np.sqrt(2))\n        model.bias.data.fill_(0)\n    elif classname.find(\"BatchNorm\") != -1:\n        model.weight.data.normal_(1.0, 0.02)\n        model.bias.data.fill_(0)\n    elif classname.find(\"GRU\") != -1:\n        for weight in model.parameters():\n            if len(weight.size()) > 1:\n                nn.init.orghogonal_(weight.data)\n    elif classname.find(\"Linear\") != -1:\n        model.weight.data.normal_(0, 0.01)\n        model.bias.data.zero_()\n\n\ndef interpolate(x: torch.Tensor, ratio: int):\n    \"\"\"Interpolate data in time domain. This is used to compensate the\n    resolution reduction in downsampling of a CNN.\n    Args:\n      x: (batch_size, time_steps, classes_num)\n      ratio: int, ratio to interpolate\n    Returns:\n      upsampled: (batch_size, time_steps * ratio, classes_num)\n    \"\"\"\n    (batch_size, time_steps, classes_num) = x.shape\n    upsampled = x[:, :, None, :].repeat(1, 1, ratio, 1)\n    upsampled = upsampled.reshape(batch_size, time_steps * ratio, classes_num)\n    return upsampled\n\n\ndef pad_framewise_output(framewise_output: torch.Tensor, frames_num: int):\n    \"\"\"Pad framewise_output to the same length as input frames. The pad value\n    is the same as the value of the last frame.\n    Args:\n      framewise_output: (batch_size, frames_num, classes_num)\n      frames_num: int, number of frames to pad\n    Outputs:\n      output: (batch_size, frames_num, classes_num)\n    \"\"\"\n    output = F.interpolate(\n        framewise_output.unsqueeze(1),\n        size=(frames_num, framewise_output.size(2)),\n        align_corners=True,\n        mode=\"bilinear\").squeeze(1)\n\n    return output\n\n\ndef gem(x: torch.Tensor, p=3, eps=1e-6):\n    \"\"\"\n    Input Tensor : (batch_size, channels, height, width)\n    Output Tensor : (batch_size, channels, 1, 1)\n    \"\"\"\n    return F.avg_pool2d(x.clamp(min=eps).pow(p), (x.size(-2), x.size(-1))).pow(1. / p)\n\n\nclass GeM(nn.Module):\n    def __init__(self, p=3, eps=1e-6):\n        super().__init__()\n        self.p = nn.Parameter(torch.ones(1) * p)\n        self.eps = eps\n\n    def forward(self, x):\n        return gem(x, p=self.p, eps=self.eps)\n\n    def __repr__(self):\n        return self.__class__.__name__ + f\"(p={self.p.data.tolist()[0]:.4f}, eps={self.eps})\"\n\n\nclass AttBlockV2(nn.Module):\n    def __init__(self,\n                 in_features: int,\n                 out_features: int,\n                 activation=\"linear\"):\n        super().__init__()\n\n        self.activation = activation\n        self.att = nn.Conv1d(\n            in_channels=in_features,\n            out_channels=out_features,\n            kernel_size=1,\n            stride=1,\n            padding=0,\n            bias=True)\n        self.cla = nn.Conv1d(\n            in_channels=in_features,\n            out_channels=out_features,\n            kernel_size=1,\n            stride=1,\n            padding=0,\n            bias=True)\n\n        self.init_weights()\n\n    def init_weights(self):\n        init_layer(self.att)\n        init_layer(self.cla)\n\n    def forward(self, x):\n        # (batch_size, channels*some, frames) (From network output)\n        \n        # x: (n_samples, n_in, n_time)\n        norm_att = torch.softmax(torch.tanh(self.att(x)), dim=-1)\n        cla = self.nonlinear_transform(self.cla(x))\n        x = torch.sum(norm_att * cla, dim=2)\n        \n        \"\"\"\n        # x shape : (batch_size, channels*some)\n        # norm_att shape : (batch_size,channels*some, frames)\n        # cla shape : (batch_size, channels*some, frames)\n        \"\"\"\n        return x, norm_att, cla\n\n    def nonlinear_transform(self, x):\n        if self.activation == 'linear':\n            return x\n        elif self.activation == 'sigmoid':\n            return torch.sigmoid(x)\n\n\nclass TimmSED(nn.Module):\n    def __init__(self, base_model_name: str, pretrained=False, num_classes=24, in_channels=1):\n        super().__init__()\n        # Spectrogram extractor\n        self.spectrogram_extractor = Spectrogram(n_fft=CFG.n_fft, hop_length=CFG.hop_length,\n                                                 win_length=CFG.n_fft, window=\"hann\", center=True, pad_mode=\"reflect\",\n                                                 freeze_parameters=True)\n\n        # Logmel feature extractor\n        self.logmel_extractor = LogmelFilterBank(sr=CFG.sample_rate, n_fft=CFG.n_fft,\n                                                 n_mels=CFG.n_mels, fmin=CFG.fmin, fmax=CFG.fmax, ref=1.0, amin=1e-10, top_db=None,\n                                                 freeze_parameters=True)\n\n        # Spec augmenter\n        self.spec_augmenter = SpecAugmentation(time_drop_width=64, time_stripes_num=2,\n                                               freq_drop_width=8, freq_stripes_num=2)\n\n        self.bn0 = nn.BatchNorm2d(CFG.n_mels)\n\n        base_model = timm.create_model(\n            base_model_name, pretrained=pretrained, in_chans=in_channels)\n        layers = list(base_model.children())[:-2]\n        self.encoder = nn.Sequential(*layers)\n\n        if hasattr(base_model, \"fc\"):\n            in_features = base_model.fc.in_features\n        else:\n            in_features = base_model.classifier.in_features\n        self.fc1 = nn.Linear(in_features, in_features, bias=True)\n        self.att_block = AttBlockV2(\n            in_features, num_classes, activation=\"sigmoid\")\n\n        self.init_weight()\n\n    def init_weight(self):\n        init_layer(self.fc1)\n        init_bn(self.bn0)\n\n    def forward(self, input):\n        # (batch_size, 1, time_steps, freq_bins)\n        x = self.spectrogram_extractor(input)\n        x = self.logmel_extractor(x)    # (batch_size, 1, time_steps, mel_bins)\n\n        frames_num = x.shape[2]\n\n        x = x.transpose(1, 3)\n        x = self.bn0(x)\n        x = x.transpose(1, 3)\n\n        #if self.training:\n        #    x = self.spec_augmenter(x)\n\n        x = x.transpose(2, 3)\n        # (batch_size, channels, freq, frames)\n        x = self.encoder(x)\n\n        # (batch_size, channels, frames)\n        x = torch.mean(x, dim=2)\n\n        # channel smoothing\n        x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1)\n        x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1)\n        # (batch_size, channels, frames)\n        x = x1 + x2\n        \n        x = F.dropout(x, p=0.5, training=self.training)\n        x = x.transpose(1, 2)\n        # (batch_size, channels*some, frames)\n        x = F.relu_(self.fc1(x))\n        x = x.transpose(1, 2)\n        x = F.dropout(x, p=0.5, training=self.training)\n        # x shape : (batch_size, channels*some, frames)\n        # clipwise_output shape : (batch_size, channels*some*some2)\n        # norm_att shape : (batch_size,channels*some*some2, frames)\n        # segmentwise_output shape : (batch_size, channels*some*some2, frames) ->sigmoid(self.cla(x))\n        (clipwise_output, norm_att, segmentwise_output) = self.att_block(x)\n        logit = torch.sum(norm_att * self.att_block.cla(x), dim=2)\n        #print(\"before segmentwise_logit:\",x,x.shape)\n        segmentwise_logit = self.att_block.cla(x).transpose(1, 2)\n        #print(\"after segmentwise_logit:\",segmentwise_logit,segmentwise_logit.shape)\n        segmentwise_output = segmentwise_output.transpose(1, 2)\n        #print(\"frames_num:\",frames_num)\n        interpolate_ratio = frames_num // segmentwise_output.size(1)\n\n        # Get framewise output\n        framewise_output = interpolate(segmentwise_output,\n                                       interpolate_ratio)\n        framewise_output = pad_framewise_output(framewise_output, frames_num)\n\n        framewise_logit = interpolate(segmentwise_logit, interpolate_ratio)\n        framewise_logit = pad_framewise_output(framewise_logit, frames_num)\n        #print(\"after framewise_logit:\",framewise_logit,framewise_logit.shape)\n        output_dict = {\n            \"framewise_output\": framewise_output, # applied interpolation at segmentwise_output\n            \"segmentwise_output\": segmentwise_output,\n            \"logit\": logit,\n            \"framewise_logit\": framewise_logit,\n            \"clipwise_output\": clipwise_output\n        }\n\n        return output_dict","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.296682Z","iopub.execute_input":"2023-11-14T12:43:34.296996Z","iopub.status.idle":"2023-11-14T12:43:34.343492Z","shell.execute_reply.started":"2023-11-14T12:43:34.296957Z","shell.execute_reply":"2023-11-14T12:43:34.342779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class PretrainedTimmSED(TimmSED):\n    def __init__(self, base_model_name: str, pretrained=False, num_classes=21, in_channels=1):\n        super().__init__(base_model_name=base_model_name, pretrained=False, num_classes=131,in_channels=in_channels)\n        # Spectrogram extractor\n        self.spectrogram_extractor = Spectrogram(n_fft=CFG.n_fft, hop_length=CFG.hop_length,\n                                                 win_length=CFG.n_fft, window=\"hann\", center=True, pad_mode=\"reflect\",\n                                                 freeze_parameters=True)\n\n        # Logmel feature extractor\n        self.logmel_extractor = LogmelFilterBank(sr=CFG.sample_rate, n_fft=CFG.n_fft,\n                                                 n_mels=CFG.n_mels, fmin=CFG.fmin, fmax=CFG.fmax, ref=1.0, amin=1e-10, top_db=None,\n                                                 freeze_parameters=True)\n\n        # Spec augmenter\n        self.spec_augmenter = SpecAugmentation(time_drop_width=64, time_stripes_num=2,\n                                               freq_drop_width=8, freq_stripes_num=2)\n        self.bn0 = nn.BatchNorm2d(CFG.n_mels)\n        print(\"timm base_model_name : \", base_model_name)\n        base_model = TimmSED(\n            base_model_name=CFG.base_model_name,\n            pretrained=False,\n            num_classes=131,\n            in_channels=CFG.in_channels)\n        \n        #ckpt = torch.load(CFG.pretrain_checkpoint)\n        #base_model.load_state_dict(ckpt['model_state_dict'])\n        \n\n        \n        layers = list(base_model.children())[4:-2]\n        self.encoder = nn.Sequential(*layers)\n\n        #if hasattr(base_model, \"fc\"):\n        #    in_features = base_model.fc.in_features\n        #else:\n        #    in_features = base_model.classifier.in_features\n        #self.fc1 = nn.Linear(in_features, in_features, bias=True)\n        self.fc1 = nn.Linear(1280, 1280, bias=True)\n        self.att_block = AttBlockV2(\n            1280, num_classes, activation=\"sigmoid\")\n\n        self.init_weight()\n\n    def init_weight(self):\n        init_layer(self.fc1)\n        init_bn(self.bn0)\n        return\n    \n    def forward(self, input):\n        # (batch_size, 1, time_steps, freq_bins)\n        x = self.spectrogram_extractor(input)\n        x = self.logmel_extractor(x)    # (batch_size, 1, time_steps, mel_bins)\n\n        frames_num = x.shape[2]\n\n        x = x.transpose(1, 3)\n        x = self.bn0(x)\n        x = x.transpose(1, 3)\n\n        if self.training:\n            x = self.spec_augmenter(x)\n        \n        x = x.transpose(2, 3)\n        # (batch_size, channels, freq, frames)\n        #print(\"before encoder : \", x, x.shape)\n        #print(self.encoder)\n        x = self.encoder(x)\n        #print(\"after encoder : \", x, x.shape)\n        # (batch_size, channels, frames)\n        x = torch.mean(x, dim=2)\n\n        # channel smoothing\n        x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1)\n        x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1)\n        # (batch_size, channels, frames)\n        x = x1 + x2\n        \n        x = F.dropout(x, p=0.5, training=self.training)\n        x = x.transpose(1, 2)\n        # (batch_size, channels*some, frames)\n        x = F.relu_(self.fc1(x))\n        x = x.transpose(1, 2)\n        x = F.dropout(x, p=0.5, training=self.training)\n        # x shape : (batch_size, channels*some, frames)\n        # clipwise_output shape : (batch_size, channels*some*some2)\n        # norm_att shape : (batch_size,channels*some*some2, frames)\n        # segmentwise_output shape : (batch_size, channels*some*some2, frames) ->sigmoid(self.cla(x))\n        (clipwise_output, norm_att, segmentwise_output) = self.att_block(x)\n        logit = torch.sum(norm_att * self.att_block.cla(x), dim=2)\n        segmentwise_logit = self.att_block.cla(x).transpose(1, 2)\n        segmentwise_output = segmentwise_output.transpose(1, 2)\n        \n        interpolate_ratio = frames_num // segmentwise_output.size(1)\n\n        # Get framewise output\n        framewise_output = interpolate(segmentwise_output,\n                                       interpolate_ratio)\n        framewise_output = pad_framewise_output(framewise_output, frames_num)\n\n        framewise_logit = interpolate(segmentwise_logit, interpolate_ratio)\n        framewise_logit = pad_framewise_output(framewise_logit, frames_num)\n\n        output_dict = {\n            \"framewise_output\": framewise_output, # applied interpolation at segmentwise_output\n            \"segmentwise_output\": segmentwise_output,\n            \"logit\": logit,\n            \"framewise_logit\": framewise_logit,\n            \"clipwise_output\": clipwise_output\n        }\n\n        return output_dict","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.344704Z","iopub.execute_input":"2023-11-14T12:43:34.344968Z","iopub.status.idle":"2023-11-14T12:43:34.368913Z","shell.execute_reply.started":"2023-11-14T12:43:34.344933Z","shell.execute_reply":"2023-11-14T12:43:34.368158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TimmNFNETGRUSED(nn.Module):\n    def __init__(self, base_model_name: str, pretrained=False, num_classes=24, in_channels=1):\n        super().__init__()\n        # Spectrogram extractor\n        self.spectrogram_extractor = Spectrogram(n_fft=CFG.n_fft, hop_length=CFG.hop_length,\n                                                 win_length=CFG.n_fft, window=\"hann\", center=True, pad_mode=\"reflect\",\n                                                 freeze_parameters=True)\n\n        # Logmel feature extractor\n        self.logmel_extractor = LogmelFilterBank(sr=CFG.sample_rate, n_fft=CFG.n_fft,\n                                                 n_mels=CFG.n_mels, fmin=CFG.fmin, fmax=CFG.fmax, ref=1.0, amin=1e-10, top_db=None,\n                                                 freeze_parameters=True)\n\n        # Spec augmenter\n        self.spec_augmenter = SpecAugmentation(time_drop_width=64, time_stripes_num=2,\n                                               freq_drop_width=8, freq_stripes_num=2)\n\n        self.bn0 = nn.BatchNorm2d(CFG.n_mels)\n\n        base_model = timm.create_model(\n            base_model_name, pretrained=pretrained, in_chans=in_channels)\n        layers = list(base_model.children())[:-2]\n        self.encoder = nn.Sequential(*layers)\n        #print(base_model)\n        if hasattr(base_model, \"head\"):\n            in_features = base_model.head.fc.in_features\n        else:\n            in_features = base_model.classifier.in_features\n        self.gru = torch.nn.GRU(input_size=in_features, hidden_size=in_features, \n                        num_layers=2, dropout=0.3, batch_first=True, bidirectional=True)\n        self.fc1 = nn.Linear(in_features*2, in_features, bias=True)\n        self.att_block = AttBlockV2(\n            in_features, num_classes, activation=\"sigmoid\")\n\n        self.init_weight()\n\n    def init_weight(self):\n        init_layer(self.fc1)\n        init_bn(self.bn0)\n\n    def forward(self, input):\n        # (batch_size, 1, time_steps, freq_bins)\n        #print(\"input : \", input, input.shape)\n        x = self.spectrogram_extractor(input)\n        #print(\"after spectrogram : \", x, x.shape)\n        x = self.logmel_extractor(x)    # (batch_size, 1, time_steps, mel_bins)\n\n        frames_num = x.shape[2]\n\n        x = x.transpose(1, 3)\n        x = self.bn0(x)\n        x = x.transpose(1, 3)\n\n        if self.training:\n            x = self.spec_augmenter(x)\n\n        x = x.transpose(2, 3)\n        # (batch_size, channels, freq, frames)\n        #print(\"before encoder : \", x, x.shape)\n        x = self.encoder(x)\n\n        # (batch_size, channels, frames)\n        x = torch.mean(x, dim=2)\n\n        # channel smoothing\n        x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1)\n        x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1)\n        # (batch_size, channels, frames)\n        x = x1 + x2\n        \n        x = F.dropout(x, p=0.5, training=self.training)\n        x = x.transpose(1, 2)\n        (x, _) = self.gru(x)\n        # (batch_size, channels*some, frames)\n        x = F.relu_(self.fc1(x))\n        x = x.transpose(1, 2)\n        x = F.dropout(x, p=0.5, training=self.training)\n        # x shape : (batch_size, channels*some, frames)\n        # clipwise_output shape : (batch_size, channels*some*some2)\n        # norm_att shape : (batch_size,channels*some*some2, frames)\n        # segmentwise_output shape : (batch_size, channels*some*some2, frames) ->sigmoid(self.cla(x))\n        (clipwise_output, norm_att, segmentwise_output) = self.att_block(x)\n        logit = torch.sum(norm_att * self.att_block.cla(x), dim=2)\n        segmentwise_logit = self.att_block.cla(x).transpose(1, 2)\n        segmentwise_output = segmentwise_output.transpose(1, 2)\n        #print(\"frames_num : \", frames_num)\n        interpolate_ratio = frames_num // segmentwise_output.size(1)\n\n        # Get framewise output\n        framewise_output = interpolate(segmentwise_output,\n                                       interpolate_ratio)\n        framewise_output = pad_framewise_output(framewise_output, frames_num)\n\n        framewise_logit = interpolate(segmentwise_logit, interpolate_ratio)\n        framewise_logit = pad_framewise_output(framewise_logit, frames_num)\n\n        output_dict = {\n            \"framewise_output\": framewise_output, # applied interpolation at segmentwise_output\n            \"segmentwise_output\": segmentwise_output,\n            \"logit\": logit,\n            \"framewise_logit\": framewise_logit,\n            \"clipwise_output\": clipwise_output\n        }\n\n        return output_dict\n\n\nclass PretrainedNFNETGRUTimmSED(TimmNFNETGRUSED):\n    def __init__(self, base_model_name: str, pretrained=False, num_classes=21, in_channels=1):\n        super().__init__(base_model_name=base_model_name, pretrained=pretrained, num_classes=CFG.n_pretrain_classes,in_channels=in_channels)\n        # Spectrogram extractor\n        self.spectrogram_extractor = Spectrogram(n_fft=CFG.n_fft, hop_length=CFG.hop_length,\n                                                 win_length=CFG.n_fft, window=\"hann\", center=True, pad_mode=\"reflect\",\n                                                 freeze_parameters=True)\n\n        # Logmel feature extractor\n        self.logmel_extractor = LogmelFilterBank(sr=CFG.sample_rate, n_fft=CFG.n_fft,\n                                                 n_mels=CFG.n_mels, fmin=CFG.fmin, fmax=CFG.fmax, ref=1.0, amin=1e-10, top_db=None,\n                                                 freeze_parameters=True)\n\n        # Spec augmenter\n        self.spec_augmenter = SpecAugmentation(time_drop_width=64, time_stripes_num=2,\n                                               freq_drop_width=8, freq_stripes_num=2)\n        self.bn0 = nn.BatchNorm2d(CFG.n_mels)\n        print(\"timm base_model_name : \", base_model_name)\n        base_model = TimmNFNETGRUSED(\n            base_model_name=\"eca_nfnet_l0\",\n            pretrained=False,\n            num_classes=131,\n            in_channels=CFG.in_channels)\n        \n        #ckpt = torch.load(CFG.pretrain_checkpoint)\n        #base_model.load_state_dict(ckpt['model_state_dict'])\n        \n        #print(base_model)\n        \n        layers = list(base_model.children())[4:-3]\n        self.encoder = nn.Sequential(*layers)\n\n        #if hasattr(base_model, \"head\"):\n        #    in_features = base_model.head.fc.in_features\n        #else:\n        #    in_features = base_model.classifier.in_features\n        in_features=2304\n        self.gru = torch.nn.GRU(input_size=in_features, hidden_size=in_features, \n                        num_layers=2, dropout=0.3, batch_first=True, bidirectional=True)\n        self.fc1 = nn.Linear(in_features*2, in_features, bias=True)\n        self.att_block = AttBlockV2(\n            in_features, num_classes, activation=\"sigmoid\")\n\n        self.init_weight()\n\n    def init_weight(self):\n        init_layer(self.fc1)\n        #init_bn(self.bn0)\n\n    def forward(self, input):\n        # (batch_size, 1, time_steps, freq_bins)\n        #print(\"input : \", input, input.shape)\n        x = self.spectrogram_extractor(input)\n        #print(\"after spectrogram : \", x, x.shape)\n        x = self.logmel_extractor(x)    # (batch_size, 1, time_steps, mel_bins)\n\n        frames_num = x.shape[2]\n\n        x = x.transpose(1, 3)\n        x = self.bn0(x)\n        x = x.transpose(1, 3)\n\n        if self.training:\n            x = self.spec_augmenter(x)\n\n        x = x.transpose(2, 3)\n        # (batch_size, channels, freq, frames)\n        #print(\"before encoder : \", x, x.shape)\n        x = self.encoder(x)\n\n        # (batch_size, channels, frames)\n        x = torch.mean(x, dim=2)\n\n        # channel smoothing\n        x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1)\n        x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1)\n        # (batch_size, channels, frames)\n        x = x1 + x2\n        \n        x = F.dropout(x, p=0.5, training=self.training)\n        x = x.transpose(1, 2)\n        (x, _) = self.gru(x)\n        # (batch_size, channels*some, frames)\n        x = F.relu_(self.fc1(x))\n        x = x.transpose(1, 2)\n        x = F.dropout(x, p=0.5, training=self.training)\n        # x shape : (batch_size, channels*some, frames)\n        # clipwise_output shape : (batch_size, channels*some*some2)\n        # norm_att shape : (batch_size,channels*some*some2, frames)\n        # segmentwise_output shape : (batch_size, channels*some*some2, frames) ->sigmoid(self.cla(x))\n        (clipwise_output, norm_att, segmentwise_output) = self.att_block(x)\n        logit = torch.sum(norm_att * self.att_block.cla(x), dim=2)\n        segmentwise_logit = self.att_block.cla(x).transpose(1, 2)\n        segmentwise_output = segmentwise_output.transpose(1, 2)\n        #print(\"frames_num : \", frames_num)\n        interpolate_ratio = frames_num // segmentwise_output.size(1)\n\n        # Get framewise output\n        framewise_output = interpolate(segmentwise_output,\n                                       interpolate_ratio)\n        framewise_output = pad_framewise_output(framewise_output, frames_num)\n\n        framewise_logit = interpolate(segmentwise_logit, interpolate_ratio)\n        framewise_logit = pad_framewise_output(framewise_logit, frames_num)\n\n        output_dict = {\n            \"framewise_output\": framewise_output, # applied interpolation at segmentwise_output\n            \"segmentwise_output\": segmentwise_output,\n            \"logit\": logit,\n            \"framewise_logit\": framewise_logit,\n            \"clipwise_output\": clipwise_output\n        }\n\n        return output_dict","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.375838Z","iopub.execute_input":"2023-11-14T12:43:34.37606Z","iopub.status.idle":"2023-11-14T12:43:34.419791Z","shell.execute_reply.started":"2023-11-14T12:43:34.376033Z","shell.execute_reply":"2023-11-14T12:43:34.419041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Mixup(nn.Module):\n    def __init__(self, mix_beta=1):\n\n        super(Mixup, self).__init__()\n        self.beta_distribution = torch.distributions.Beta(mix_beta, mix_beta)\n\n    def forward(self, X, Y, weight=None):\n\n        bs = X.shape[0]\n        n_dims = len(X.shape)\n        perm = torch.randperm(bs)\n        coeffs = self.beta_distribution.rsample(torch.Size((bs,))).to(X.device)\n\n        if n_dims == 2:\n            X = coeffs.view(-1, 1) * X + (1 - coeffs.view(-1, 1)) * X[perm]\n        elif n_dims == 3:\n            X = coeffs.view(-1, 1, 1) * X + (1 - coeffs.view(-1, 1, 1)) * X[perm]\n        else:\n            X = coeffs.view(-1, 1, 1, 1) * X + (1 - coeffs.view(-1, 1, 1, 1)) * X[perm]\n\n        Y = coeffs.view(-1, 1) * Y + (1 - coeffs.view(-1, 1)) * Y[perm]\n\n        if weight is None:\n            return X, Y\n        else:\n            weight = coeffs.view(-1) * weight + (1 - coeffs.view(-1)) * weight[perm]\n            return X, Y, weight\n\nclass MixupTimmNFNETSED(nn.Module):\n    def __init__(self, base_model_name: str, pretrained=False, num_classes=24, in_channels=1):\n        super().__init__()\n        # Spectrogram extractor\n        self.spectrogram_extractor = Spectrogram(n_fft=CFG_NFNETMIXUP.n_fft, hop_length=CFG_NFNETMIXUP.hop_length,\n                                                 win_length=CFG_NFNETMIXUP.n_fft, window=\"hann\", center=True, pad_mode=\"reflect\",\n                                                 freeze_parameters=True)\n\n        # Logmel feature extractor\n        self.logmel_extractor = LogmelFilterBank(sr=CFG_NFNETMIXUP.sample_rate, n_fft=CFG_NFNETMIXUP.n_fft,\n                                                 n_mels=CFG_NFNETMIXUP.n_mels, fmin=CFG_NFNETMIXUP.fmin, fmax=CFG_NFNETMIXUP.fmax, ref=1.0, amin=1e-10, top_db=None,\n                                                 freeze_parameters=True)\n\n        # Spec augmenter\n        self.spec_augmenter = SpecAugmentation(time_drop_width=64, time_stripes_num=2,\n                                               freq_drop_width=8, freq_stripes_num=2)\n\n        \n        self.mixup = Mixup()\n        self.bn0 = nn.BatchNorm2d(CFG_NFNETMIXUP.n_mels)\n\n        base_model = timm.create_model(\n            base_model_name, pretrained=pretrained, in_chans=in_channels)\n        layers = list(base_model.children())[:-2]\n        self.encoder = nn.Sequential(*layers)\n        if hasattr(base_model, \"head\"):\n            in_features = base_model.head.fc.in_features\n        else:\n            in_features = base_model.classifier.in_features\n\n        self.fc1 = nn.Linear(in_features, in_features, bias=True)\n        self.att_block = AttBlockV2(\n            in_features, num_classes, activation=\"sigmoid\")\n\n        self.init_weight()\n\n    def init_weight(self):\n        init_layer(self.fc1)\n        init_bn(self.bn0)\n\n    def forward(self, batch):\n        # batch -> image, target for mixup\n        input, target = batch\n\n        # (batch_size, 1, time_steps, freq_bins)\n        x = self.spectrogram_extractor(input)\n        x = self.logmel_extractor(x)    # (batch_size, 1, time_steps, mel_bins)\n        frames_num = x.shape[2]\n\n        if self.training:\n            x, mixup_target = self.mixup(x, target)\n            #if CFG.DEBUG:\n            #    print(mixup_target.shape, weight.shape)\n            #mixup_target = mixup_target * weight.view(-1,1)\n            #print(\"mixuped :\", mixup_target, \"before : \", target)\n        \n\n        x = x.transpose(1, 3)\n        x = self.bn0(x)\n        x = x.transpose(1, 3)\n\n        #if self.training:\n        #    x = self.spec_augmenter(x)\n\n        x = x.transpose(2, 3)\n        x = self.encoder(x)\n\n        # (batch_size, channels, frames)\n        x = torch.mean(x, dim=2)\n\n        # channel smoothing\n        x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1)\n        x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1)\n        # (batch_size, channels, frames)\n        x = x1 + x2\n        \n        x = F.dropout(x, p=0.5, training=self.training)\n        x = x.transpose(1, 2)\n        #(x, _) = self.gru(x)\n        # (batch_size, channels*some, frames)\n        x = F.relu_(self.fc1(x))\n        x = x.transpose(1, 2)\n        x = F.dropout(x, p=0.5, training=self.training)\n        # x shape : (batch_size, channels*some, frames)\n        # clipwise_output shape : (batch_size, channels*some*some2)\n        # norm_att shape : (batch_size,channels*some*some2, frames)\n        # segmentwise_output shape : (batch_size, channels*some*some2, frames) ->sigmoid(self.cla(x))\n        (clipwise_output, norm_att, segmentwise_output) = self.att_block(x)\n        logit = torch.sum(norm_att * self.att_block.cla(x), dim=2)\n        segmentwise_logit = self.att_block.cla(x).transpose(1, 2)\n        segmentwise_output = segmentwise_output.transpose(1, 2)\n        #print(\"frames_num : \", frames_num)\n        interpolate_ratio = frames_num // segmentwise_output.size(1)\n\n        # Get framewise output\n        framewise_output = interpolate(segmentwise_output,\n                                       interpolate_ratio)\n        framewise_output = pad_framewise_output(framewise_output, frames_num)\n\n        framewise_logit = interpolate(segmentwise_logit, interpolate_ratio)\n        framewise_logit = pad_framewise_output(framewise_logit, frames_num)\n        if self.training:\n            output_dict = {\n                \"framewise_output\": framewise_output, # applied interpolation at segmentwise_output\n                \"segmentwise_output\": segmentwise_output,\n                \"logit\": logit,\n                \"framewise_logit\": framewise_logit,\n                \"clipwise_output\": clipwise_output,\n                \"target\":mixup_target\n            }\n        else:\n            output_dict = {\n                \"framewise_output\": framewise_output, # applied interpolation at segmentwise_output\n                \"segmentwise_output\": segmentwise_output,\n                \"logit\": logit,\n                \"framewise_logit\": framewise_logit,\n                \"clipwise_output\": clipwise_output,\n                \"target\":target\n            }\n\n        return output_dict","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.420902Z","iopub.execute_input":"2023-11-14T12:43:34.421125Z","iopub.status.idle":"2023-11-14T12:43:34.453931Z","shell.execute_reply.started":"2023-11-14T12:43:34.421097Z","shell.execute_reply":"2023-11-14T12:43:34.453179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MixupTimmEFFV2SPECSED_LOW(nn.Module):\n    def __init__(self, base_model_name: str, pretrained=False, num_classes=24, in_channels=1):\n        super().__init__()\n        # Spectrogram extractor\n        self.spectrogram_extractor = Spectrogram(n_fft=CFG_EFFV2DOUBLEMIXUP.n_fft, hop_length=CFG_EFFV2DOUBLEMIXUP.hop_length,\n                                                 win_length=CFG_EFFV2DOUBLEMIXUP.n_fft, window=\"hann\", center=True, pad_mode=\"reflect\",\n                                                 freeze_parameters=True)\n\n        # Logmel feature extractor\n        self.logmel_extractor = LogmelFilterBank(sr=CFG_EFFV2DOUBLEMIXUP.sample_rate, n_fft=CFG_EFFV2DOUBLEMIXUP.n_fft,\n                                                 n_mels=CFG_EFFV2DOUBLEMIXUP.n_mels, fmin=CFG_EFFV2DOUBLEMIXUP.fmin, fmax=CFG_EFFV2DOUBLEMIXUP.fmax, ref=1.0, amin=1e-10, top_db=None,\n                                                 freeze_parameters=True)\n\n        \n        self.spec_augmenter = SpecAugmentation(time_drop_width=64, time_stripes_num=2,\n                                               freq_drop_width=8, freq_stripes_num=2)\n\n        self.mixup = Mixup()\n        self.bn0 = nn.BatchNorm2d(CFG_EFFV2DOUBLEMIXUP.n_mels)\n\n        base_model = timm.create_model(\n            base_model_name, pretrained=pretrained, in_chans=in_channels)\n        layers = list(base_model.children())[:-2]\n        self.encoder = nn.Sequential(*layers)\n        if hasattr(base_model, \"head\"):\n            in_features = base_model.head.fc.in_features\n        else:\n            in_features = base_model.classifier.in_features\n\n        self.fc1 = nn.Linear(in_features, in_features, bias=True)\n        self.att_block = AttBlockV2(\n            in_features, num_classes, activation=\"sigmoid\")\n\n        self.init_weight()\n\n    def init_weight(self):\n        init_layer(self.fc1)\n        init_bn(self.bn0)\n\n    def forward(self, batch):\n        # batch -> image, target for mixup\n        input, target = batch\n\n        # (batch_size, 1, time_steps, freq_bins)\n        x = self.spectrogram_extractor(input)\n        x = self.logmel_extractor(x)    # (batch_size, 1, time_steps, mel_bins)\n        frames_num = x.shape[2]\n\n        if self.training:\n            \n            x, mixup_target, weight = self.mixup(x, target)\n            if np.random.uniform() < 0.5:\n                x, mixup_target, weight = self.mixup(x, mixup_target)\n\n            #mixup_target = mixup_target * weight.view(-1,1)\n            #print(\"mixuped :\", mixup_target, \"before : \", target)\n        \n\n        x = x.transpose(1, 3)\n        x = self.bn0(x)\n        x = x.transpose(1, 3)\n        if self.training:\n            x = self.spec_augmenter(x)\n        #if self.training:\n        #    x = self.spec_augmenter(x)\n\n        x = x.transpose(2, 3)\n        x = self.encoder(x)\n\n        # (batch_size, channels, frames)\n        x = torch.mean(x, dim=2)\n\n        # channel smoothing\n        x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1)\n        x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1)\n        # (batch_size, channels, frames)\n        x = x1 + x2\n        \n        x = F.dropout(x, p=0.5, training=self.training)\n        x = x.transpose(1, 2)\n        #(x, _) = self.gru(x)\n        # (batch_size, channels*some, frames)\n        x = F.relu_(self.fc1(x))\n        x = x.transpose(1, 2)\n        x = F.dropout(x, p=0.5, training=self.training)\n        # x shape : (batch_size, channels*some, frames)\n        # clipwise_output shape : (batch_size, channels*some*some2)\n        # norm_att shape : (batch_size,channels*some*some2, frames)\n        # segmentwise_output shape : (batch_size, channels*some*some2, frames) ->sigmoid(self.cla(x))\n        (clipwise_output, norm_att, segmentwise_output) = self.att_block(x)\n        logit = torch.sum(norm_att * self.att_block.cla(x), dim=2)\n        segmentwise_logit = self.att_block.cla(x).transpose(1, 2)\n        segmentwise_output = segmentwise_output.transpose(1, 2)\n        #print(\"frames_num : \", frames_num)\n        interpolate_ratio = frames_num // segmentwise_output.size(1)\n\n        # Get framewise output\n        framewise_output = interpolate(segmentwise_output,\n                                       interpolate_ratio)\n        framewise_output = pad_framewise_output(framewise_output, frames_num)\n\n        framewise_logit = interpolate(segmentwise_logit, interpolate_ratio)\n        framewise_logit = pad_framewise_output(framewise_logit, frames_num)\n        if self.training:\n            output_dict = {\n                \"framewise_output\": framewise_output, # applied interpolation at segmentwise_output\n                \"segmentwise_output\": segmentwise_output,\n                \"logit\": logit,\n                \"framewise_logit\": framewise_logit,\n                \"clipwise_output\": clipwise_output,\n                \"target\":mixup_target\n            }\n        else:\n            output_dict = {\n                \"framewise_output\": framewise_output, # applied interpolation at segmentwise_output\n                \"segmentwise_output\": segmentwise_output,\n                \"logit\": logit,\n                \"framewise_logit\": framewise_logit,\n                \"clipwise_output\": clipwise_output,\n                \"target\":target\n            }\n\n        return output_dict","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.455268Z","iopub.execute_input":"2023-11-14T12:43:34.455634Z","iopub.status.idle":"2023-11-14T12:43:34.482532Z","shell.execute_reply.started":"2023-11-14T12:43:34.455561Z","shell.execute_reply":"2023-11-14T12:43:34.481773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MixupTimmEFFV2SPECSED_64MEL(nn.Module):\n    def __init__(self, base_model_name: str, pretrained=False, num_classes=24, in_channels=1):\n        super().__init__()\n        # Spectrogram extractor\n        self.spectrogram_extractor = Spectrogram(n_fft=CFG_EFFV2DOUBLEMIXUP_64MEL.n_fft, hop_length=CFG_EFFV2DOUBLEMIXUP_64MEL.hop_length,\n                                                 win_length=CFG_EFFV2DOUBLEMIXUP_64MEL.n_fft, window=\"hann\", center=True, pad_mode=\"reflect\",\n                                                 freeze_parameters=True)\n\n        # Logmel feature extractor\n        self.logmel_extractor = LogmelFilterBank(sr=CFG_EFFV2DOUBLEMIXUP_64MEL.sample_rate, n_fft=CFG_EFFV2DOUBLEMIXUP_64MEL.n_fft,\n                                                 n_mels=CFG_EFFV2DOUBLEMIXUP_64MEL.n_mels, fmin=CFG_EFFV2DOUBLEMIXUP_64MEL.fmin, fmax=CFG_EFFV2DOUBLEMIXUP_64MEL.fmax, ref=1.0, amin=1e-10, top_db=None,\n                                                 freeze_parameters=True)\n\n        \n        self.spec_augmenter = SpecAugmentation(time_drop_width=64, time_stripes_num=2,\n                                               freq_drop_width=8, freq_stripes_num=2)\n\n        self.mixup = Mixup()\n        self.bn0 = nn.BatchNorm2d(CFG_EFFV2DOUBLEMIXUP_64MEL.n_mels)\n\n        base_model = timm.create_model(\n            base_model_name, pretrained=pretrained, in_chans=in_channels)\n        layers = list(base_model.children())[:-2]\n        self.encoder = nn.Sequential(*layers)\n        if hasattr(base_model, \"head\"):\n            in_features = base_model.head.fc.in_features\n        else:\n            in_features = base_model.classifier.in_features\n\n        self.fc1 = nn.Linear(in_features, in_features, bias=True)\n        self.att_block = AttBlockV2(\n            in_features, num_classes, activation=\"sigmoid\")\n\n        self.init_weight()\n\n    def init_weight(self):\n        init_layer(self.fc1)\n        init_bn(self.bn0)\n\n    def forward(self, batch):\n        # batch -> image, target for mixup\n        input, target = batch\n\n        # (batch_size, 1, time_steps, freq_bins)\n        x = self.spectrogram_extractor(input)\n        x = self.logmel_extractor(x)    # (batch_size, 1, time_steps, mel_bins)\n        frames_num = x.shape[2]\n\n        if self.training:\n            \n            x, mixup_target = self.mixup(x, target)\n            if np.random.uniform() < 0.5:\n                x, mixup_target = self.mixup(x, mixup_target)\n\n            #mixup_target = mixup_target * weight.view(-1,1)\n            #print(\"mixuped :\", mixup_target, \"before : \", target)\n        \n\n        x = x.transpose(1, 3)\n        x = self.bn0(x)\n        x = x.transpose(1, 3)\n        if self.training:\n            x = self.spec_augmenter(x)\n        #if self.training:\n        #    x = self.spec_augmenter(x)\n\n        x = x.transpose(2, 3)\n        x = self.encoder(x)\n\n        # (batch_size, channels, frames)\n        x = torch.mean(x, dim=2)\n\n        # channel smoothing\n        x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1)\n        x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1)\n        # (batch_size, channels, frames)\n        x = x1 + x2\n        \n        x = F.dropout(x, p=0.5, training=self.training)\n        x = x.transpose(1, 2)\n        #(x, _) = self.gru(x)\n        # (batch_size, channels*some, frames)\n        x = F.relu_(self.fc1(x))\n        x = x.transpose(1, 2)\n        x = F.dropout(x, p=0.5, training=self.training)\n        # x shape : (batch_size, channels*some, frames)\n        # clipwise_output shape : (batch_size, channels*some*some2)\n        # norm_att shape : (batch_size,channels*some*some2, frames)\n        # segmentwise_output shape : (batch_size, channels*some*some2, frames) ->sigmoid(self.cla(x))\n        (clipwise_output, norm_att, segmentwise_output) = self.att_block(x)\n        logit = torch.sum(norm_att * self.att_block.cla(x), dim=2)\n        segmentwise_logit = self.att_block.cla(x).transpose(1, 2)\n        segmentwise_output = segmentwise_output.transpose(1, 2)\n        #print(\"frames_num : \", frames_num)\n        interpolate_ratio = frames_num // segmentwise_output.size(1)\n\n        # Get framewise output\n        framewise_output = interpolate(segmentwise_output,\n                                       interpolate_ratio)\n        framewise_output = pad_framewise_output(framewise_output, frames_num)\n\n        framewise_logit = interpolate(segmentwise_logit, interpolate_ratio)\n        framewise_logit = pad_framewise_output(framewise_logit, frames_num)\n        if self.training:\n            output_dict = {\n                \"framewise_output\": framewise_output, # applied interpolation at segmentwise_output\n                \"segmentwise_output\": segmentwise_output,\n                \"logit\": logit,\n                \"framewise_logit\": framewise_logit,\n                \"clipwise_output\": clipwise_output,\n                \"target\":mixup_target\n            }\n        else:\n            output_dict = {\n                \"framewise_output\": framewise_output, # applied interpolation at segmentwise_output\n                \"segmentwise_output\": segmentwise_output,\n                \"logit\": logit,\n                \"framewise_logit\": framewise_logit,\n                \"clipwise_output\": clipwise_output,\n                \"target\":target\n            }\n\n        return output_dict","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.483721Z","iopub.execute_input":"2023-11-14T12:43:34.484194Z","iopub.status.idle":"2023-11-14T12:43:34.50948Z","shell.execute_reply.started":"2023-11-14T12:43:34.484152Z","shell.execute_reply":"2023-11-14T12:43:34.508791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# https://www.kaggle.com/c/rfcx-species-audio-detection/discussion/213075\nclass BCEFocalLoss(nn.Module):\n    def __init__(self, alpha=0.25, gamma=2.0):\n        super().__init__()\n        self.alpha = alpha\n        self.gamma = gamma\n\n    def forward(self, preds, targets):\n        bce_loss = nn.BCEWithLogitsLoss(reduction='none')(preds, targets)\n        probas = torch.sigmoid(preds)\n        loss = targets * self.alpha * \\\n            (1. - probas)**self.gamma * bce_loss + \\\n            (1. - targets) * probas**self.gamma * bce_loss\n        loss = loss.mean()\n        return loss\n\n\nclass BCEFocal2WayLoss(nn.Module):\n    def __init__(self, weights=[1, 1], class_weights=None):\n        super().__init__()\n\n        self.focal = BCEFocalLoss()\n\n        self.weights = weights\n\n    def forward(self, input, target):\n        input_ = input[\"logit\"]\n        target = target.float()\n\n        framewise_output = input[\"framewise_logit\"]\n        clipwise_output_with_max, _ = framewise_output.max(dim=1)\n\n        loss = self.focal(input_, target)\n        aux_loss = self.focal(clipwise_output_with_max, target)\n\n        return self.weights[0] * loss + self.weights[1] * aux_loss","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.51053Z","iopub.execute_input":"2023-11-14T12:43:34.510798Z","iopub.status.idle":"2023-11-14T12:43:34.52364Z","shell.execute_reply.started":"2023-11-14T12:43:34.510762Z","shell.execute_reply":"2023-11-14T12:43:34.52301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"__CRITERIONS__ = {\n    \"BCEFocalLoss\": BCEFocalLoss,\n    \"BCEFocal2WayLoss\": BCEFocal2WayLoss\n}\n\n\ndef get_criterion():\n    if hasattr(nn, CFG.loss_name):\n        return nn.__getattribute__(CFG.loss_name)(**CFG.loss_params)\n    elif __CRITERIONS__.get(CFG.loss_name) is not None:\n        return __CRITERIONS__[CFG.loss_name](**CFG.loss_params)\n    else:\n        raise NotImplementedError","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.524642Z","iopub.execute_input":"2023-11-14T12:43:34.525381Z","iopub.status.idle":"2023-11-14T12:43:34.536892Z","shell.execute_reply.started":"2023-11-14T12:43:34.525337Z","shell.execute_reply":"2023-11-14T12:43:34.536208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Custom optimizer\n__OPTIMIZERS__ = {}\n\n\ndef get_optimizer(model: nn.Module):\n    optimizer_name = CFG.optimizer_name\n    if optimizer_name == \"SAM\":\n        base_optimizer_name = CFG.base_optimizer\n        if __OPTIMIZERS__.get(base_optimizer_name) is not None:\n            base_optimizer = __OPTIMIZERS__[base_optimizer_name]\n        else:\n            base_optimizer = optim.__getattribute__(base_optimizer_name)\n        return SAM(model.parameters(), base_optimizer, **CFG.optimizer_params)\n\n    if __OPTIMIZERS__.get(optimizer_name) is not None:\n        return __OPTIMIZERS__[optimizer_name](model.parameters(),\n                                              **CFG.optimizer_params)\n    else:\n        return optim.__getattribute__(optimizer_name)(model.parameters(),\n                                                      **CFG.optimizer_params)\n\n\ndef get_scheduler(optimizer):\n    scheduler_name = CFG.scheduler_name\n\n    if scheduler_name is None:\n        return\n    else:\n        return optim.lr_scheduler.__getattribute__(scheduler_name)(\n            optimizer, **CFG.scheduler_params)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.538007Z","iopub.execute_input":"2023-11-14T12:43:34.538289Z","iopub.status.idle":"2023-11-14T12:43:34.548122Z","shell.execute_reply.started":"2023-11-14T12:43:34.538253Z","shell.execute_reply":"2023-11-14T12:43:34.547564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"warnings.filterwarnings(\"ignore\")\n\nlogdir = Path(\"out\")\nlogdir.mkdir(exist_ok=True, parents=True)\nif (logdir / \"train.log\").exists():\n    os.remove(logdir / \"train.log\")\nlogger = init_logger(log_file=logdir / \"train.log\")","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.549018Z","iopub.execute_input":"2023-11-14T12:43:34.549217Z","iopub.status.idle":"2023-11-14T12:43:34.561236Z","shell.execute_reply.started":"2023-11-14T12:43:34.549192Z","shell.execute_reply":"2023-11-14T12:43:34.560594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# environment\nset_seed(CFG.seed)\ndevice = get_device()\n\n# validation\nsplitter = getattr(model_selection, CFG.split)(**CFG.split_params)\n\n# data\ntrain = train_meta","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.562094Z","iopub.execute_input":"2023-11-14T12:43:34.562326Z","iopub.status.idle":"2023-11-14T12:43:34.571332Z","shell.execute_reply.started":"2023-11-14T12:43:34.562288Z","shell.execute_reply":"2023-11-14T12:43:34.570637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.57241Z","iopub.execute_input":"2023-11-14T12:43:34.572943Z","iopub.status.idle":"2023-11-14T12:43:34.581166Z","shell.execute_reply.started":"2023-11-14T12:43:34.572904Z","shell.execute_reply":"2023-11-14T12:43:34.580469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from contextlib import contextmanager\nfrom typing import Optional\nimport time\n@contextmanager\ndef timer(name: str, logger: Optional[logging.Logger] = None):\n    t0 = time.time()\n    msg = f\"[{name}] start\"\n    if logger is None:\n        print(msg)\n    else:\n        logger.info(msg)\n    yield\n\n    msg = f\"[{name}] done in {time.time() - t0:.2f} s\"\n    if logger is None:\n        print(msg)\n    else:\n        logger.info(msg)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.582263Z","iopub.execute_input":"2023-11-14T12:43:34.582521Z","iopub.status.idle":"2023-11-14T12:43:34.59121Z","shell.execute_reply.started":"2023-11-14T12:43:34.582484Z","shell.execute_reply":"2023-11-14T12:43:34.590622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# For Test Prepraration","metadata":{}},{"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":"2023-11-14T12:43:34.592051Z","iopub.execute_input":"2023-11-14T12:43:34.592226Z","iopub.status.idle":"2023-11-14T12:43:34.609699Z","shell.execute_reply.started":"2023-11-14T12:43:34.592204Z","shell.execute_reply":"2023-11-14T12:43:34.609047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TestDataset(Dataset):\n    def __init__(self, clip: np.ndarray,afile='woong',index_list=[],\n                 waveform_transforms=None):\n        self.clip = clip\n        self.waveform_transforms=waveform_transforms\n        self.afile = afile\n        self.index_list = index_list\n        \n    def __len__(self):\n        return len(self.clip)\n    \n    def __getitem__(self, idx: int):\n        afile=self.afile\n        SR = 32000\n        sample = self.clip[idx]\n        index = self.index_list[idx]\n\n        y = sample.astype(np.float32)\n\n        y = np.nan_to_num(y)\n\n        if self.waveform_transforms:\n            y = self.waveform_transforms(y)\n\n        y = np.nan_to_num(y)\n        return y, afile, index","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.610648Z","iopub.execute_input":"2023-11-14T12:43:34.610878Z","iopub.status.idle":"2023-11-14T12:43:34.619036Z","shell.execute_reply.started":"2023-11-14T12:43:34.610852Z","shell.execute_reply":"2023-11-14T12:43:34.618191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepare_model_for_inference(model, path: Path):\n    if not torch.cuda.is_available():\n        ckpt = torch.load(path, map_location=\"cpu\")\n    else:\n        ckpt = torch.load(path)\n    model.load_state_dict(ckpt[\"model_state_dict\"])\n    model.eval()\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.620111Z","iopub.execute_input":"2023-11-14T12:43:34.620588Z","iopub.status.idle":"2023-11-14T12:43:34.629401Z","shell.execute_reply.started":"2023-11-14T12:43:34.620551Z","shell.execute_reply":"2023-11-14T12:43:34.628769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepare_model_for_mixup_inference(model, path: Path):\n    if not torch.cuda.is_available():\n        ckpt = torch.load(path, map_location=\"cpu\")\n    else:\n        ckpt = torch.load(path)\n    model.load_state_dict(ckpt[\"model\"])\n    model.eval()\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.630278Z","iopub.execute_input":"2023-11-14T12:43:34.630484Z","iopub.status.idle":"2023-11-14T12:43:34.640228Z","shell.execute_reply.started":"2023-11-14T12:43:34.630448Z","shell.execute_reply":"2023-11-14T12:43:34.639495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prediction_for_clip(clip: np.ndarray,\n                        afile: str,\n                        model, \n                        threshold=0.5):\n\n    dataset = TestDataset(clip=clip,\n                          afile=afile,\n                          waveform_transforms=get_transforms(phase=\"test\"))\n    loader = DataLoader(dataset, batch_size=1, shuffle=False)\n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    \n    model.eval()\n    prediction_dict = {}\n    pred = {'row_id': [], 'target': []}\n    for i, (image,afile) in enumerate(tqdm(loader)):\n        \n        image = image.to(device)\n        #print(image, image.shape)\n        with torch.no_grad():\n            prediction = model(image)\n            #print(prediction, prediction.shape)\n            proba = prediction[\"clipwise_output\"].detach().cpu().numpy().reshape(-1)\n        \n        chunk_end_time = (i + 1) * 5\n        \n        for bird in scored_birds:\n            try:\n                score = proba[np.where(np.array(scored_birds)==bird)]\n                #print(score)\n                #print(\"npwhere\",np.where(scored_birds==bird))\n                #print(\"score : \", score)\n                \n            except IndexError:\n                score = 0\n            #print(\"afile:\",afile)\n            #print(\"type afile:\",type(afile))\n            #print(\"bird:\",bird)\n            #print(\"chunk_end_time:\",chunk_end_time)\n            \n            row_id = afile[0] + '_' + bird + '_' + str(chunk_end_time)\n            #print(\"row_id:\",row_id)\n            pred['row_id'].append(row_id)\n            pred['target'].append(True if score > threshold else False)\n            #pred['score'].append(score)\n\n    return pred","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.641167Z","iopub.execute_input":"2023-11-14T12:43:34.641397Z","iopub.status.idle":"2023-11-14T12:43:34.652843Z","shell.execute_reply.started":"2023-11-14T12:43:34.64137Z","shell.execute_reply":"2023-11-14T12:43:34.652067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prediction_each_for_framewise(clip: np.ndarray,\n                             afile: str,\n                             index_list: list,\n                             model, \n                             threshold=0.5):\n\n    dataset = TestDataset(clip=clip,\n                          afile=afile,\n                          index_list=index_list,\n                          waveform_transforms=get_transforms(phase=\"test\"))\n    loader = DataLoader(dataset, batch_size=1, shuffle=False)\n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    #print(\"len loader : \", len(loader))\n    model.eval()\n    prediction_dict = {}\n    pred = {'row_id': [], 'target': []}\n    before_proba_split = None\n    for i, (image,afile,idx) in enumerate(tqdm(loader)):\n        #print(\"image shape: \", image.shape)\n        image = image.to(device)\n        with torch.no_grad():\n            prediction = model(image)\n            #print(prediction, prediction.shape)\n            #proba = torch.sigmoid(prediction[\"framewise_logit\"]).detach().cpu().numpy().reshape(-1)\n            proba = torch.sigmoid(prediction[\"framewise_logit\"])\n            proba = proba.detach().cpu().numpy()[0]\n        \n        #chunk_end_time = (i + 1) * 5\n        chunk_start_time = idx.detach().cpu().numpy()[0]\n        chunk_start_time *= 20\n        #print(\"chunk start time : \", chunk_start_time)\n        proba_split = np.array_split(proba, 8, axis=0)\n        cur_proba_split = proba_split[:4]\n        \n        #print(\"proba split shape: \", proba_split, len(proba_split), \"proba last shape:\", proba_split[-1].shape)\n        \n        for time_index, each_proba in enumerate(cur_proba_split):\n            #first_each_proba = each_proba[::2].copy()\n            #second_each_proba = each_proba[1::2].copy()\n            #if len(each_proba)%2==0:\n            #    first_each_proba = (first_each_proba + second_each_proba) / 2\n            #    avg_each_proba = first_each_proba.copy()\n            #else:\n            #    first_each_proba = first_each_proba[:-1]\n            #    avg_each_proba = ((first_each_proba + second_each_proba) / 2).copy()\n            if before_proba_split is not None:\n                avg_each_proba = np.vstack([each_proba,before_proba_split[time_index]])\n            else:\n                avg_each_proba = each_proba\n            for bird in scored_birds:\n                #print(\"bird: \", bird)\n                try:\n                    \n                    \n                        \n                    #print(\"before each_proba: \", each_proba)\n                        result_proba = np.max(avg_each_proba, axis=0)\n\n                        score = result_proba[np.where(np.array(scored_birds)==bird)]\n                    #print(\"each_proba_success\")\n                except IndexError:\n                    score = 0\n                    #print(\"each_proba_failed\")\n\n                #row_id = afile[0] + '_' + bird + '_' + str(chunk_end_time)\n                row_id = afile[0] + '_' + bird + '_' + str(chunk_start_time + (time_index+1)*5)\n\n                pred['row_id'].append(row_id)\n                pred['target'].append(True if score > threshold else False)\n        \n        if i < len(loader):\n            before_proba_split = proba_split[4:]\n            #pred['score'].append(score)\n\n    return pred","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.654225Z","iopub.execute_input":"2023-11-14T12:43:34.654476Z","iopub.status.idle":"2023-11-14T12:43:34.67124Z","shell.execute_reply.started":"2023-11-14T12:43:34.654443Z","shell.execute_reply":"2023-11-14T12:43:34.67057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prediction_each_for_framewise_ensemble(clip: np.ndarray,\n                             afile: str,\n                             index_list: list,\n                             model_list: list, \n                             threshold=0.5):\n\n    dataset = TestDataset(clip=clip,\n                          afile=afile,\n                          index_list=index_list,\n                          waveform_transforms=get_transforms(phase=\"test\"))\n    loader = DataLoader(dataset, batch_size=1, shuffle=False)\n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    #print(\"len loader : \", len(loader))\n    for model in model_list:\n        model.eval()\n    prediction_dict = {}\n    pred = {'row_id': [], 'target': []}\n    before_proba_split = None\n    for i, (image,afile,idx) in enumerate(tqdm(loader)):\n        #print(\"image shape: \", image.shape)\n        image = image.to(device)\n        for model_idx, model in enumerate(model_list):\n            with torch.no_grad():\n                if model_idx == 0:\n                    prediction = model(image)\n                    proba_before = torch.sigmoid(prediction[\"framewise_logit\"]) / len(model_list)\n                    proba_after = proba_before.detach().cpu().numpy()[0]\n                else:\n                    prediction = model(image)\n                    proba_before = torch.sigmoid(prediction[\"framewise_logit\"]) / len(model_list)\n                    proba_after += proba_before.detach().cpu().numpy()[0]\n        proba = proba_after.copy()\n        chunk_start_time = idx.detach().cpu().numpy()[0]\n        chunk_start_time *= 10\n        #print(proba, proba.shape)\n        proba_split = np.array_split(proba, 4, axis=0)\n        cur_proba_split = proba_split[:2]\n        \n        for time_index, each_proba in enumerate(cur_proba_split):\n            if before_proba_split is not None:\n                #print(time_index)\n                avg_each_proba = np.vstack([each_proba,before_proba_split[time_index]])\n            else:\n                avg_each_proba = each_proba\n            for bird in scored_birds:\n                #print(\"bird: \", bird)\n                try:\n                        result_proba = np.max(avg_each_proba, axis=0)\n\n                        score = result_proba[np.where(np.array(scored_birds)==bird)]\n                    #print(\"each_proba_success\")\n                except IndexError:\n                    score = 0\n                    #print(\"each_proba_failed\")\n\n                #row_id = afile[0] + '_' + bird + '_' + str(chunk_end_time)\n                row_id = afile[0] + '_' + bird + '_' + str(chunk_start_time + (time_index+1)*5)\n\n                pred['row_id'].append(row_id)\n                pred['target'].append(True if score > threshold else False)\n        \n        if i < len(loader):\n            before_proba_split = proba_split[1:]\n            #pred['score'].append(score)\n\n    return pred","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.672315Z","iopub.execute_input":"2023-11-14T12:43:34.672785Z","iopub.status.idle":"2023-11-14T12:43:34.688954Z","shell.execute_reply.started":"2023-11-14T12:43:34.672734Z","shell.execute_reply":"2023-11-14T12:43:34.688226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prediction_each_for_framewise_voting_10sec(clip: np.ndarray,\n                             afile: str,\n                             index_list: list,\n                             model_list: list, \n                             threshold=0.5,\n                             model_with_mixup_bool = list()):\n    batch_size=64\n    dataset = TestDataset(clip=clip,\n                          afile=afile,\n                          index_list=index_list,\n                          waveform_transforms=get_transforms(phase=\"test\"))\n    loader = DataLoader(dataset, batch_size=batch_size, shuffle=False)\n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    \n    #for model in model_list:\n    #    model.eval()\n    prediction_dict = {}\n    pred_list = []\n    before_proba_split = None\n    for model_idx, (model, mixup_bool) in enumerate(zip(model_list, model_with_mixup_bool)):\n        pred = {'row_id': [], 'target': [], 'score':[]}\n        with torch.no_grad():\n            for i, (image,afile,idx) in enumerate(tqdm(loader)):\n                #print(image.shape, afile, \"at batch size: \", batch_size)\n\n                image = image.to(device)\n                target = torch.normal(2, 3, size=(image.shape[0], 1)).to(device)\n                with torch.no_grad():\n                    if mixup_bool==False:\n                        prediction = model(image)\n                        proba_before = torch.sigmoid(prediction[\"framewise_logit\"])\n                        proba_after = proba_before.cpu().numpy()\n                    else:\n                        prediction = model((image,target))\n                        proba_before = torch.sigmoid(prediction[\"framewise_logit\"])\n                        proba_after = proba_before.cpu().numpy()\n                #print(proba_after.shape, afile, idx)\n                proba_length = proba_after.shape[0]\n                for interval_idx in range(proba_length):\n                    proba = proba_after[interval_idx]\n                    chunk_start_time = idx.cpu().numpy()[interval_idx]\n                    chunk_start_time *= 10\n            #print(proba, proba.shape)\n                    proba_split = np.array_split(proba, 4, axis=0)\n                    cur_proba_split = proba_split[:2]\n\n                    for time_index, each_proba in enumerate(cur_proba_split):\n                        if before_proba_split is not None:\n                        #print(time_index)\n                            avg_each_proba = np.vstack([each_proba,before_proba_split[time_index]])\n                        else:\n                            avg_each_proba = each_proba\n                        for bird in scored_birds:\n                            #print(\"bird: \", bird)\n                            try:\n                                result_proba = np.max(avg_each_proba, axis=0)\n\n                                score = result_proba[np.where(np.array(bird_label_total)==bird)]\n                            #print(\"each_proba_success\")\n                            except IndexError:\n                                score = 0\n                            #print(\"each_proba_failed\")\n\n                        #row_id = afile[0] + '_' + bird + '_' + str(chunk_end_time)\n                            row_id = afile[0] + '_' + bird + '_' + str(chunk_start_time + (time_index+1)*5)\n                            #print(row_id)\n                            pred['row_id'].append(row_id)\n                            pred['score'].append(score[0])\n                            pred['target'].append(True if score > threshold else False)\n\n                    if i < len(loader):\n                        before_proba_split = proba_split[2:]\n            pred_list.append(pred)\n            #pred['score'].append(score)\n\n    return pred_list","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.69015Z","iopub.execute_input":"2023-11-14T12:43:34.690344Z","iopub.status.idle":"2023-11-14T12:43:34.709468Z","shell.execute_reply.started":"2023-11-14T12:43:34.690319Z","shell.execute_reply":"2023-11-14T12:43:34.708732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prediction_each_for_framewise_voting_15sec(clip: np.ndarray,\n                             afile: str,\n                             index_list: list,\n                             model_list: list, \n                             threshold=0.5,\n                             model_with_mixup_bool = list()):\n    batch_size=64\n    dataset = TestDataset(clip=clip,\n                          afile=afile,\n                          index_list=index_list,\n                          waveform_transforms=get_transforms(phase=\"test\"))\n    loader = DataLoader(dataset, batch_size=batch_size, shuffle=False)\n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    \n    #for model in model_list:\n    #    model.eval()\n    prediction_dict = {}\n    pred_list = []\n    before_proba_split = None\n    for model_idx, (model, mixup_bool) in enumerate(zip(model_list, model_with_mixup_bool)):\n        pred = {'row_id': [], 'target': [], 'score':[]}\n        with torch.no_grad():\n            for i, (image,afile,idx) in enumerate(tqdm(loader)):\n                #print(image.shape, afile, \"at batch size: \", batch_size)\n\n                image = image.to(device)\n                target = torch.normal(2, 3, size=(image.shape[0], 1)).to(device)\n                with torch.no_grad():\n                    if mixup_bool==False:\n                        prediction = model(image)\n                        proba_before = torch.sigmoid(prediction[\"framewise_logit\"])\n                        proba_after = proba_before.cpu().numpy()\n                    else:\n                        prediction = model((image,target))\n                        proba_before = torch.sigmoid(prediction[\"framewise_logit\"])\n                        proba_after = proba_before.cpu().numpy()\n                #print(proba_after.shape, afile, idx)\n                proba_length = proba_after.shape[0]\n                for interval_idx in range(proba_length):\n                    proba = proba_after[interval_idx]\n                    chunk_start_time = idx.cpu().numpy()[interval_idx]\n                    chunk_start_time *= 30\n            #print(proba, proba.shape)\n                    proba_split = np.array_split(proba, 6, axis=0)\n                    cur_proba_split = proba_split[:6]\n\n                    for time_index, each_proba in enumerate(cur_proba_split):\n                        if before_proba_split is not None:\n                        #print(time_index)\n                            avg_each_proba = np.vstack([each_proba,before_proba_split[time_index]])\n                        else:\n                            avg_each_proba = each_proba\n                        for bird in scored_birds:\n                            #print(\"bird: \", bird)\n                            try:\n                                result_proba = np.max(avg_each_proba, axis=0)\n\n                                score = result_proba[np.where(np.array(bird_label_total)==bird)]\n                            #print(\"each_proba_success\")\n                            except IndexError:\n                                score = 0\n                            #print(\"each_proba_failed\")\n\n                        #row_id = afile[0] + '_' + bird + '_' + str(chunk_end_time)\n                            row_id = afile[0] + '_' + bird + '_' + str(chunk_start_time + (time_index+1)*5)\n                            #print(row_id)\n                            pred['row_id'].append(row_id)\n                            pred['score'].append(score[0])\n                            pred['target'].append(True if score > threshold else False)\n\n                    if i < len(loader):\n                        before_proba_split = proba_split[6:]\n            pred_list.append(pred)\n            #pred['score'].append(score)\n\n    return pred_list\n","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.710524Z","iopub.execute_input":"2023-11-14T12:43:34.710719Z","iopub.status.idle":"2023-11-14T12:43:34.729303Z","shell.execute_reply.started":"2023-11-14T12:43:34.710694Z","shell.execute_reply":"2023-11-14T12:43:34.728485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ndef prediction_framewise_ensemble(test_audios,\n               weights_path: list,\n               threshold):\n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    model_list = []\n    for i in range(len(weights_path)):\n        if i <= 14:\n            model = PretrainedTimmNFNETGRUSED(base_model_name=\"nfnet_eca_l0\",\n                            pretrained=False,\n                            num_classes=21,\n                            in_channels=CFG.in_channels)\n            model_list.append(prepare_model_for_mixup_inference(model, weights_path[i]).to(device).eval())\n        else:\n            model = TimmSED(base_model_name=CFG.base_model_name,\n                        pretrained=False,\n                        num_classes=CFG.num_classes,\n                        in_channels=CFG.in_channels)\n            model_list.append(prepare_model_for_inference(model, weights_path[i]).to(device).eval())\n        gc.collect()\n    \n    warnings.filterwarnings(\"ignore\")\n    prediction_dfs = []\n    \n    pred = {'row_id': [], 'target': []}\n    \n    for audio_path in test_audios:\n        #[(audio_id.name.split(\"_\")[:2]) for audio_id in all_audios]\n        afile = audio_path.name.split(\".\")[0]\n        #print(\"first afile\")\n        with timer(f\"Loading {str(audio_path)}\", logger):\n            clip, _ = sf.read(audio_path)\n        len_wav_shape = len(clip.shape)\n        if len_wav_shape == 1:\n            pass\n        else:\n            clip = clip[:,0]\n        length_clip = clip.shape[0]\n        cal_frame_for_interval = 5\n        intervals = round(length_clip/32000/cal_frame_for_interval)\n        \n        #effective_test_length = 32000*cal_frame*intervals\n        #if not length_clip < 32000*5*12:\n        #clip = clip[:effective_test_length]\n        clip_list = []\n        #print(intervals)\n        index_list = []\n        for index in range(intervals):\n            added_clip = clip[index*32000*cal_frame_for_interval:(index+1)*32000*cal_frame_for_interval+32000*cal_frame_for_interval]\n            print(len(added_clip))\n            clip_list.append(added_clip)\n            index_list.append(index)\n        clip_list = np.array(clip_list)\n        with timer(f\"Prediction on {audio_path}\", logger):\n            prediction_dict = prediction_each_for_framewise_ensemble(clip=clip_list,\n                                                       afile=afile,\n                                                       index_list = index_list,\n                                                       model_list=model_list,\n                                                       threshold=threshold)\n        row_id = list(prediction_dict['row_id'])\n        target = list(prediction_dict['target'])\n        prediction_df = pd.DataFrame({\n            \"row_id\": row_id,\n            \"target\": target,\n        })\n        prediction_dfs.append(prediction_df)\n    \n    prediction_df = pd.concat(prediction_dfs, axis=0, sort=False).reset_index(drop=True)\n    return prediction_df","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.730254Z","iopub.execute_input":"2023-11-14T12:43:34.730463Z","iopub.status.idle":"2023-11-14T12:43:34.746937Z","shell.execute_reply.started":"2023-11-14T12:43:34.730437Z","shell.execute_reply":"2023-11-14T12:43:34.746163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import OrderedDict\ndef prediction_each_for_framewise_voting_15sec_speedup(clip: np.ndarray,\n                             afile: str,\n                             index_list: list,\n                             model_list: list, \n                             threshold=0.5,\n                             model_with_mixup_bool = list()):\n    batch_size=64\n    dataset = TestDataset(clip=clip,\n                          afile=afile,\n                          index_list=index_list,\n                          waveform_transforms=get_transforms(phase=\"test\"))\n    loader = DataLoader(dataset, batch_size=batch_size, shuffle=False)\n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    \n    #for model in model_list:\n    #    model.eval()\n    prediction_dict = {}\n    #pred_dict = OrderedDict(k:list() for k in range(len(model_list))}\n    pred_dict = OrderedDict()\n    for idx in range(len(model_list)):\n        pred_dict[idx] = list()\n    #for model_idx, (model, mixup_bool) in enumerate(zip(model_list, model_with_mixup_bool)):\n    if True:\n        #pred = {'row_id': [], 'target': [], 'score':[]}\n        with torch.no_grad():\n            for i, (image,afile,idx) in enumerate(tqdm(loader)):\n                \n                for model_idx, (model, mixup_bool) in enumerate(zip(model_list, model_with_mixup_bool)):\n                #print(image.shape, afile, \"at batch size: \", batch_size)\n                    before_proba_split = None\n                    each_pred = {'row_id': [], 'target': [], 'score':[]}\n                    image = image.to(device)\n                    target = torch.normal(2, 3, size=(image.shape[0], 1)).to(device)\n\n                    if mixup_bool==False:\n                        prediction = model(image)\n                        proba_before = torch.sigmoid(prediction[\"framewise_logit\"])\n                        proba_after = proba_before.cpu().numpy()\n                    else:\n                        prediction = model((image,target))\n                        proba_before = torch.sigmoid(prediction[\"framewise_logit\"])\n                        proba_after = proba_before.cpu().numpy()\n                #print(proba_after.shape, afile, idx)\n                    proba_length = proba_after.shape[0]\n                    for interval_idx in range(proba_length):\n                        proba = proba_after[interval_idx]\n                        chunk_start_time = idx.cpu().numpy()[interval_idx]\n                        chunk_start_time *= 30\n            #print(proba, proba.shape)\n                        proba_split = np.array_split(proba, 12, axis=0)\n                        cur_proba_split = proba_split[:6]\n\n                        for time_index, each_proba in enumerate(cur_proba_split):\n                            if before_proba_split is not None:\n                        #print(time_index)\n                                avg_each_proba = np.vstack([each_proba,before_proba_split[time_index]])\n                            else:\n                                avg_each_proba = each_proba\n                            for bird in scored_birds:\n                            #print(\"bird: \", bird)\n                                try:\n                                    result_proba = np.max(avg_each_proba, axis=0)\n\n                                    score = result_proba[np.where(np.array(bird_label_total)==bird)]\n                            #print(\"each_proba_success\")\n                                except IndexError:\n                                    score = 0\n                            #print(\"each_proba_failed\")\n\n                        #row_id = afile[0] + '_' + bird + '_' + str(chunk_end_time)\n                                row_id = afile[0] + '_' + bird + '_' + str(chunk_start_time + (time_index+1)*5)\n                            #print(row_id)\n                                each_pred['row_id'].append(row_id)\n                                each_pred['score'].append(score[0])\n                                each_pred['target'].append(True if score > threshold else False)\n\n                        if i < len(loader):\n                            before_proba_split = proba_split[6:]\n                    pred_dict[model_idx].append(each_pred)\n            #pred['score'].append(score)\n\n    return pred_dict\n","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.748205Z","iopub.execute_input":"2023-11-14T12:43:34.748486Z","iopub.status.idle":"2023-11-14T12:43:34.769551Z","shell.execute_reply.started":"2023-11-14T12:43:34.74845Z","shell.execute_reply":"2023-11-14T12:43:34.768809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import OrderedDict\ndef prediction_each_for_framewise_voting_10sec_speedup(clip: np.ndarray,\n                             afile: str,\n                             index_list: list,\n                             model_list: list, \n                             threshold=0.5,\n                             model_with_mixup_bool = list()):\n    batch_size=64\n    dataset = TestDataset(clip=clip,\n                          afile=afile,\n                          index_list=index_list,\n                          waveform_transforms=get_transforms(phase=\"test\"))\n    loader = DataLoader(dataset, batch_size=batch_size, shuffle=False)\n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    \n    #for model in model_list:\n    #    model.eval()\n    prediction_dict = {}\n    #pred_dict = OrderedDict(k:list() for k in range(len(model_list))}\n    pred_dict = OrderedDict()\n    for idx in range(len(model_list)):\n        pred_dict[idx] = list()\n    #for model_idx, (model, mixup_bool) in enumerate(zip(model_list, model_with_mixup_bool)):\n    if True:\n        #pred = {'row_id': [], 'target': [], 'score':[]}\n        with torch.no_grad():\n            for i, (image,afile,idx) in enumerate(tqdm(loader)):\n                \n                for model_idx, (model, mixup_bool) in enumerate(zip(model_list, model_with_mixup_bool)):\n                #print(image.shape, afile, \"at batch size: \", batch_size)\n                    before_proba_split = None\n                    each_pred = {'row_id': [], 'target': [], 'score':[]}\n                    image = image.to(device)\n                    target = torch.normal(2, 3, size=(image.shape[0], 1)).to(device)\n\n                    if mixup_bool==False:\n                        prediction = model(image)\n                        proba_before = torch.sigmoid(prediction[\"framewise_logit\"])\n                        proba_after = proba_before.cpu().numpy()\n                    else:\n                        prediction = model((image,target))\n                        proba_before = torch.sigmoid(prediction[\"framewise_logit\"])\n                        proba_after = proba_before.cpu().numpy()\n                #print(proba_after.shape, afile, idx)\n                    proba_length = proba_after.shape[0]\n                    for interval_idx in range(proba_length):\n                        proba = proba_after[interval_idx]\n                        chunk_start_time = idx.cpu().numpy()[interval_idx]\n                        chunk_start_time *= 10\n            #print(proba, proba.shape)\n                        proba_split = np.array_split(proba, 4, axis=0)\n                        cur_proba_split = proba_split[:2]\n\n                        for time_index, each_proba in enumerate(cur_proba_split):\n                            if before_proba_split is not None:\n                        #print(time_index)\n                                avg_each_proba = np.vstack([each_proba,before_proba_split[time_index]])\n                            else:\n                                avg_each_proba = each_proba\n                            for bird in scored_birds:\n                            #print(\"bird: \", bird)\n                                try:\n                                    result_proba = np.max(avg_each_proba, axis=0)\n\n                                    score = result_proba[np.where(np.array(bird_label_total)==bird)]\n                            #print(\"each_proba_success\")\n                                except IndexError:\n                                    score = 0\n                            #print(\"each_proba_failed\")\n\n                        #row_id = afile[0] + '_' + bird + '_' + str(chunk_end_time)\n                                row_id = afile[0] + '_' + bird + '_' + str(chunk_start_time + (time_index+1)*5)\n                            #print(row_id)\n                                each_pred['row_id'].append(row_id)\n                                each_pred['score'].append(score[0])\n                                each_pred['target'].append(True if score > threshold else False)\n\n                        if i < len(loader):\n                            before_proba_split = proba_split[2:]\n                    pred_dict[model_idx].append(each_pred)\n            #pred['score'].append(score)\n\n    return pred_dict\n","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.770571Z","iopub.execute_input":"2023-11-14T12:43:34.770822Z","iopub.status.idle":"2023-11-14T12:43:34.79135Z","shell.execute_reply.started":"2023-11-14T12:43:34.770795Z","shell.execute_reply":"2023-11-14T12:43:34.790662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ndef prediction_framewise_voting_10sec_speedup(test_audios,\n               weights_path: list,\n               threshold):\n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    model_list = []\n    model_with_mixup_bool = [True]*len(weights_path)\n    for i in range(len(weights_path)):\n        if i <=3:\n            model = MixupTimmEFFV2SED(\n                                    base_model_name=CFG_EFFV2DOUBLEMIXUP.base_model_name,\n                                    pretrained=False,\n                                    num_classes=CFG_EFFV2DOUBLEMIXUP.num_classes,\n                                    in_channels=CFG_EFFV2DOUBLEMIXUP.in_channels)\n            model_list.append(prepare_model_for_mixup_inference(model, weights_path[i]).to(device).eval())\n        elif i >= 4:\n            model = MixupTimmEFFV2SED(\n                                    base_model_name=CFG_EFFV2MDOUBLEMIXUP.base_model_name,\n                                    pretrained=False,\n                                    num_classes=CFG_EFFV2MDOUBLEMIXUP.num_classes,\n                                    in_channels=CFG_EFFV2MDOUBLEMIXUP.in_channels)\n            model_list.append(prepare_model_for_mixup_inference(model, weights_path[i]).to(device).eval())\n        elif i >= 8:\n            #CFG_EFFV2MDOUBLEMIXUP\n            model = MixupTimmEFFV2SPECSED_LOW(\n                                    base_model_name=CFG_EFFV2MDOUBLEMIXUP.base_model_name,\n                                    pretrained=False,\n                                    num_classes=CFG_EFFV2MDOUBLEMIXUP.num_classes,\n                                    in_channels=CFG_EFFV2MDOUBLEMIXUP.in_channels)\n            model_list.append(prepare_model_for_mixup_inference(model, weights_path[i]).to(device).eval())\n        else:\n            model = TimmSED(base_model_name=CFG.base_model_name,\n                        pretrained=False,\n                        num_classes=CFG.num_classes,\n                        in_channels=CFG.in_channels)\n            model_list.append(prepare_model_for_inference(model, weights_path[i]).to(device).eval())\n        gc.collect()\n\n    warnings.filterwarnings(\"ignore\")\n    prediction_dfs = []\n    \n    pred = {'row_id': [], 'target': []}\n    \n    for audio_path in test_audios:\n        #[(audio_id.name.split(\"_\")[:2]) for audio_id in all_audios]\n        afile = audio_path.name.split(\".\")[0]\n        #print(\"first afile\")\n        with timer(f\"Loading {str(audio_path)}\", logger):\n            clip, _ = sf.read(audio_path)\n        len_wav_shape = len(clip.shape)\n        if len_wav_shape == 1:\n            pass\n        else:\n            clip = clip[:,0]\n        length_clip = clip.shape[0]\n        cal_frame_for_interval = 10\n        intervals = round(length_clip/32000/cal_frame_for_interval)\n        \n        #effective_test_length = 32000*cal_frame*intervals\n        #if not length_clip < 32000*5*12:\n        #clip = clip[:effective_test_length]\n        clip_list = []\n        #print(intervals)\n        index_list = []\n        for index in range(intervals):\n            added_clip = clip[index*32000*cal_frame_for_interval:(index+1)*32000*cal_frame_for_interval+32000*cal_frame_for_interval]\n            #print(len(added_clip))\n            if len(added_clip)<640000:\n                added_clip = np.pad(added_clip, (0,640000-len(added_clip)))\n            clip_list.append(added_clip)\n            index_list.append(index)\n        clip_list = np.array(clip_list)\n        with timer(f\"Prediction on {audio_path}\", logger):\n            prediction_dict = prediction_each_for_framewise_voting_10sec_speedup(clip=clip_list,\n                                                       afile=afile,\n                                                       index_list = index_list,\n                                                       model_list=model_list,\n                                                       threshold=threshold,\n                                                       model_with_mixup_bool = model_with_mixup_bool)\n        for idx,(model_idx, each_pred_dict) in enumerate(prediction_dict.items()):\n            #print(each_pred_dict)\n            row = []\n            score = []\n            target = []\n            for row_idx, each_row in enumerate(each_pred_dict):\n                #print(each_row)\n                if idx == 0:\n                    row += each_row['row_id']\n                score += each_row['score']\n                target += each_row['target']\n            if idx == 0:\n                prediction_df = pd.DataFrame({\n                    \"row_id\":row,\n                    f\"target_{idx}\":target,\n                    f\"score_{idx}\":score\n                })\n            else:\n                prediction_df[f\"target_{idx}\"]=target\n                prediction_df[f\"score_{idx}\"]=score\n        #if pred_idx==0:\n        #        row_id = list(each_pred_dict['row_id'])\n        #    target = list(each_pred_dict['target'])\n        #    score = list(each_pred_dict['score'])\n        #    if pred_idx==0:\n        #        prediction_df = pd.DataFrame({\n        #            \"row_id\": row_id,\n        #            f\"target_{pred_idx}\": target,\n        #            f\"score_{pred_idx}\" : score\n        #        })\n        #    else:\n        #        \n        #        prediction_df[f\"target_{pred_idx}\"]=target\n        #        prediction_df[f\"score_{pred_idx}\"]=score\n            #print(prediction_df)\n        prediction_dfs.append(prediction_df)\n    \n    prediction_df = pd.concat(prediction_dfs, axis=0, sort=False).reset_index(drop=True)\n    return prediction_df","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.792677Z","iopub.execute_input":"2023-11-14T12:43:34.792927Z","iopub.status.idle":"2023-11-14T12:43:34.816565Z","shell.execute_reply.started":"2023-11-14T12:43:34.7929Z","shell.execute_reply":"2023-11-14T12:43:34.815876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prediction_framewise_voting_15sec_low_speedup(test_audios,\n               weights_path: list,\n               threshold):\n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    model_list = []\n    model_with_mixup_bool = [True]*len(weights_path)\n    for i in range(len(weights_path)):\n        if i <= 4:\n            model = MixupTimmEFFV2SPECSED_LOW(\n                                    base_model_name=CFG_EFFV2DOUBLEMIXUP.base_model_name,\n                                    pretrained=False,\n                                    num_classes=CFG_EFFV2DOUBLEMIXUP.num_classes,\n                                    in_channels=CFG_EFFV2DOUBLEMIXUP.in_channels)\n            model_list.append(prepare_model_for_mixup_inference(model, weights_path[i]).to(device).eval())\n        elif i <=5:\n            model = HighMixupTimmEFFV2SED(\n                                    base_model_name=CFG_HIGH_EFFV2DOUBLEMIXUP.base_model_name,\n                                    pretrained=False,\n                                    num_classes=CFG_HIGH_EFFV2DOUBLEMIXUP.num_classes,\n                                    in_channels=CFG_HIGH_EFFV2DOUBLEMIXUP.in_channels)\n            model_list.append(prepare_model_for_mixup_inference(model, weights_path[i]).to(device).eval())\n        elif i > 7:\n            model = MixupTimmNFNETSED(\n                                    base_model_name=CFG_NFNETMIXUP.base_model_name,\n                                    pretrained=False,\n                                    num_classes=CFG_NFNETMIXUP.num_classes,\n                                    in_channels=CFG_NFNETMIXUP.in_channels)\n            model_list.append(prepare_model_for_mixup_inference(model, weights_path[i]).to(device).eval())\n        elif i <= 12:\n            model = PretrainedNFNETGRUTimmSED(base_model_name=CFG.base_model_name,\n                            pretrained=False,\n                            num_classes=21,\n                            in_channels=CFG.in_channels)\n            model_list.append(prepare_model_for_inference(model, weights_path[i]).to(device).eval())\n        else:\n            model = TimmSED(base_model_name=CFG.base_model_name,\n                        pretrained=False,\n                        num_classes=CFG.num_classes,\n                        in_channels=CFG.in_channels)\n            model_list.append(prepare_model_for_inference(model, weights_path[i]).to(device).eval())\n        gc.collect()\n\n    warnings.filterwarnings(\"ignore\")\n    prediction_dfs = []\n    \n    pred = {'row_id': [], 'target': []}\n    \n    for audio_path in test_audios:\n        #[(audio_id.name.split(\"_\")[:2]) for audio_id in all_audios]\n        afile = audio_path.name.split(\".\")[0]\n        #print(\"first afile\")\n        with timer(f\"Loading {str(audio_path)}\", logger):\n            clip, _ = sf.read(audio_path)\n        len_wav_shape = len(clip.shape)\n        if len_wav_shape == 1:\n            pass\n        else:\n            clip = clip[:,0]\n        length_clip = clip.shape[0]\n        cal_frame_for_interval = 30\n        intervals = round(length_clip/32000/cal_frame_for_interval)\n        \n        #effective_test_length = 32000*cal_frame*intervals\n        #if not length_clip < 32000*5*12:\n        #clip = clip[:effective_test_length]\n        clip_list = []\n        #print(intervals)\n        index_list = []\n        for index in range(intervals):\n            added_clip = clip[index*32000*cal_frame_for_interval:(index+1)*32000*cal_frame_for_interval+32000*cal_frame_for_interval]\n            #print(len(added_clip))\n            if len(added_clip)<320000*6:\n                added_clip = np.pad(added_clip, (0,320000*6-len(added_clip)))\n            clip_list.append(added_clip)\n            index_list.append(index)\n        clip_list = np.array(clip_list)\n        with timer(f\"Prediction on {audio_path}\", logger):\n            prediction_dict = prediction_each_for_framewise_voting_15sec_speedup(clip=clip_list,\n                                                       afile=afile,\n                                                       index_list = index_list,\n                                                       model_list=model_list,\n                                                       threshold=threshold,\n                                                       model_with_mixup_bool = model_with_mixup_bool)\n        #OrderedDict([(0, [{'row_id': ['soundscape_453028782_akiapo_5', \n        #'soundscape_453028782_aniani_5', 'soundscape_453028782_apapan_5', 'soundscape_453028782_barpet_5', \n        #'soundscape_453028782_crehon_5', 'soundscape_453028782_elepai_5', 'soundscape_453028782_ercfra_5', 'soundscape_453028782_hawama_5', 'soundscape_453028782_hawcre_5', 'soundscape_453028782_hawgoo_5', 'soundscape_453028782_hawhaw_5', 'soundscape_453028782_hawpet1_5', 'soundscape_453028782_houfin_5', 'soundscape_453028782_iiwi_5', 'soundscape_453028782_jabwar_5', 'soundscape_453028782_maupar_5', 'soundscape_453028782_omao_5', 'soundscape_453028782_puaioh_5', 'soundscape_453028782_skylar_5', 'soundscape_453028782_warwhe1_5', 'soundscape_453028782_yefcan_5', 'soundscape_453028782_akiapo_10', 'soundscape_453028782_aniani_10', 'soundscape_453028782_apapan_10', 'soundscape_453028782_barpet_10', 'soundscape_453028782_crehon_10', 'soundscape_453028782_elepai_10', 'soundscape_453028782_ercfra_10', 'soundscape_453028782_hawama_10', 'soundscape_453028782_hawcre_10', 'soundscape_453028782_hawgoo_10', 'soundscape_453028782_hawhaw_10', 'soundscape_453028782_hawpet1_10', 'soundscape_453028782_houfin_10', 'soundscape_453028782_iiwi_10', 'soundscape_453028782_jabwar_10', 'soundscape_453028782_maupar_10', 'soundscape_453028782_omao_10', 'soundscape_453028782_puaioh_10', 'soundscape_453028782_skylar_10', 'soundscape_453028782_warwhe1_10', 'soundscape_453028782_yefcan_10', 'soundscape_453028782_akiapo_15', 'so\n        \n        #print(prediction_dict)\n        for idx,(model_idx, each_pred_dict) in enumerate(prediction_dict.items()):\n            #print(each_pred_dict)\n            row = []\n            score = []\n            target = []\n            for row_idx, each_row in enumerate(each_pred_dict):\n                #print(each_row)\n                if idx == 0:\n                    row += each_row['row_id']\n                score += each_row['score']\n                target += each_row['target']\n            if idx == 0:\n                prediction_df = pd.DataFrame({\n                    \"row_id\":row,\n                    f\"target_{idx}\":target,\n                    f\"score_{idx}\":score\n                })\n            else:\n                prediction_df[f\"target_{idx}\"]=target\n                prediction_df[f\"score_{idx}\"]=score\n        #if pred_idx==0:\n        #        row_id = list(each_pred_dict['row_id'])\n        #    target = list(each_pred_dict['target'])\n        #    score = list(each_pred_dict['score'])\n        #    if pred_idx==0:\n        #        prediction_df = pd.DataFrame({\n        #            \"row_id\": row_id,\n        #            f\"target_{pred_idx}\": target,\n        #            f\"score_{pred_idx}\" : score\n        #        })\n        #    else:\n        #        \n        #        prediction_df[f\"target_{pred_idx}\"]=target\n        #        prediction_df[f\"score_{pred_idx}\"]=score\n            #print(prediction_df)\n        prediction_dfs.append(prediction_df)\n    \n    prediction_df = pd.concat(prediction_dfs, axis=0, sort=False).reset_index(drop=True)\n    return prediction_df","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.817657Z","iopub.execute_input":"2023-11-14T12:43:34.817891Z","iopub.status.idle":"2023-11-14T12:43:34.843645Z","shell.execute_reply.started":"2023-11-14T12:43:34.817865Z","shell.execute_reply":"2023-11-14T12:43:34.842945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#MixupTimmNFNETSPECSED\n#CFG_NFNET0_SPEC_64MEL_15SEC","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.84462Z","iopub.execute_input":"2023-11-14T12:43:34.844886Z","iopub.status.idle":"2023-11-14T12:43:34.857163Z","shell.execute_reply.started":"2023-11-14T12:43:34.84486Z","shell.execute_reply":"2023-11-14T12:43:34.856433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prediction_framewise_voting_15sec_64mel_speedup(test_audios,\n               weights_path: list,\n               threshold):\n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    model_list = []\n    model_with_mixup_bool = [True]*len(weights_path)\n    for i in range(len(weights_path)):\n        if i <= 2:\n            model = MixupTimmEFFV2SPECSED_64MEL(\n                                    base_model_name=CFG_EFFV2DOUBLEMIXUP_64MEL.base_model_name,\n                                    pretrained=False,\n                                    num_classes=CFG_EFFV2DOUBLEMIXUP_64MEL.num_classes,\n                                    in_channels=CFG_EFFV2DOUBLEMIXUP_64MEL.in_channels)\n            model_list.append(prepare_model_for_mixup_inference(model, weights_path[i]).to(device).eval())\n        elif i <=6:\n            model = MixupTimmNFNETSPECSED(\n                                    base_model_name=CFG_NFNET0_SPEC_64MEL_15SEC.base_model_name,\n                                    pretrained=False,\n                                    num_classes=CFG_NFNET0_SPEC_64MEL_15SEC.num_classes,\n                                    in_channels=CFG_NFNET0_SPEC_64MEL_15SEC.in_channels)\n            model_list.append(prepare_model_for_mixup_inference(model, weights_path[i]).to(device).eval())\n        elif i <=10:\n            model = MixupTimmNFNETSPECSED(\n                                    base_model_name=\"eca_nfnet_l1\",\n                                    pretrained=False,\n                                    num_classes=CFG_NFNET0_SPEC_64MEL_15SEC.num_classes,\n                                    in_channels=CFG_NFNET0_SPEC_64MEL_15SEC.in_channels)\n            model_list.append(prepare_model_for_mixup_inference(model, weights_path[i]).to(device).eval())\n        elif i <= 12:\n            model = PretrainedNFNETGRUTimmSED(base_model_name=CFG.base_model_name,\n                            pretrained=False,\n                            num_classes=21,\n                            in_channels=CFG.in_channels)\n            model_list.append(prepare_model_for_inference(model, weights_path[i]).to(device).eval())\n        else:\n            model = TimmSED(base_model_name=CFG.base_model_name,\n                        pretrained=False,\n                        num_classes=CFG.num_classes,\n                        in_channels=CFG.in_channels)\n            model_list.append(prepare_model_for_inference(model, weights_path[i]).to(device).eval())\n        gc.collect()\n\n    warnings.filterwarnings(\"ignore\")\n    prediction_dfs = []\n    \n    pred = {'row_id': [], 'target': []}\n    \n    for audio_path in test_audios:\n        #[(audio_id.name.split(\"_\")[:2]) for audio_id in all_audios]\n        afile = audio_path.name.split(\".\")[0]\n        #print(\"first afile\")\n        with timer(f\"Loading {str(audio_path)}\", logger):\n            clip, _ = sf.read(audio_path)\n        len_wav_shape = len(clip.shape)\n        if len_wav_shape == 1:\n            pass\n        else:\n            clip = clip[:,0]\n        length_clip = clip.shape[0]\n        cal_frame_for_interval = 30\n        intervals = round(length_clip/32000/cal_frame_for_interval)\n        \n        #effective_test_length = 32000*cal_frame*intervals\n        #if not length_clip < 32000*5*12:\n        #clip = clip[:effective_test_length]\n        clip_list = []\n        #print(intervals)\n        index_list = []\n        for index in range(intervals):\n            added_clip = clip[index*32000*cal_frame_for_interval:(index+1)*32000*cal_frame_for_interval+32000*cal_frame_for_interval]\n            #print(len(added_clip))\n            if len(added_clip)<320000*6:\n                added_clip = np.pad(added_clip, (0,320000*6-len(added_clip)))\n            clip_list.append(added_clip)\n            index_list.append(index)\n        clip_list = np.array(clip_list)\n        with timer(f\"Prediction on {audio_path}\", logger):\n            prediction_dict = prediction_each_for_framewise_voting_15sec_speedup(clip=clip_list,\n                                                       afile=afile,\n                                                       index_list = index_list,\n                                                       model_list=model_list,\n                                                       threshold=threshold,\n                                                       model_with_mixup_bool = model_with_mixup_bool)\n        #OrderedDict([(0, [{'row_id': ['soundscape_453028782_akiapo_5', \n        #'soundscape_453028782_aniani_5', 'soundscape_453028782_apapan_5', 'soundscape_453028782_barpet_5', \n        #'soundscape_453028782_crehon_5', 'soundscape_453028782_elepai_5', 'soundscape_453028782_ercfra_5', 'soundscape_453028782_hawama_5', 'soundscape_453028782_hawcre_5', 'soundscape_453028782_hawgoo_5', 'soundscape_453028782_hawhaw_5', 'soundscape_453028782_hawpet1_5', 'soundscape_453028782_houfin_5', 'soundscape_453028782_iiwi_5', 'soundscape_453028782_jabwar_5', 'soundscape_453028782_maupar_5', 'soundscape_453028782_omao_5', 'soundscape_453028782_puaioh_5', 'soundscape_453028782_skylar_5', 'soundscape_453028782_warwhe1_5', 'soundscape_453028782_yefcan_5', 'soundscape_453028782_akiapo_10', 'soundscape_453028782_aniani_10', 'soundscape_453028782_apapan_10', 'soundscape_453028782_barpet_10', 'soundscape_453028782_crehon_10', 'soundscape_453028782_elepai_10', 'soundscape_453028782_ercfra_10', 'soundscape_453028782_hawama_10', 'soundscape_453028782_hawcre_10', 'soundscape_453028782_hawgoo_10', 'soundscape_453028782_hawhaw_10', 'soundscape_453028782_hawpet1_10', 'soundscape_453028782_houfin_10', 'soundscape_453028782_iiwi_10', 'soundscape_453028782_jabwar_10', 'soundscape_453028782_maupar_10', 'soundscape_453028782_omao_10', 'soundscape_453028782_puaioh_10', 'soundscape_453028782_skylar_10', 'soundscape_453028782_warwhe1_10', 'soundscape_453028782_yefcan_10', 'soundscape_453028782_akiapo_15', 'so\n        \n        #print(prediction_dict)\n        for idx,(model_idx, each_pred_dict) in enumerate(prediction_dict.items()):\n            #print(each_pred_dict)\n            row = []\n            score = []\n            target = []\n            for row_idx, each_row in enumerate(each_pred_dict):\n                #print(each_row)\n                if idx == 0:\n                    row += each_row['row_id']\n                score += each_row['score']\n                target += each_row['target']\n            if idx == 0:\n                prediction_df = pd.DataFrame({\n                    \"row_id\":row,\n                    f\"target_{idx}\":target,\n                    f\"score_{idx}\":score\n                })\n            else:\n                prediction_df[f\"target_{idx}\"]=target\n                prediction_df[f\"score_{idx}\"]=score\n        #if pred_idx==0:\n        #        row_id = list(each_pred_dict['row_id'])\n        #    target = list(each_pred_dict['target'])\n        #    score = list(each_pred_dict['score'])\n        #    if pred_idx==0:\n        #        prediction_df = pd.DataFrame({\n        #            \"row_id\": row_id,\n        #            f\"target_{pred_idx}\": target,\n        #            f\"score_{pred_idx}\" : score\n        #        })\n        #    else:\n        #        \n        #        prediction_df[f\"target_{pred_idx}\"]=target\n        #        prediction_df[f\"score_{pred_idx}\"]=score\n            #print(prediction_df)\n        prediction_dfs.append(prediction_df)\n    \n    prediction_df = pd.concat(prediction_dfs, axis=0, sort=False).reset_index(drop=True)\n    return prediction_df","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.858228Z","iopub.execute_input":"2023-11-14T12:43:34.858427Z","iopub.status.idle":"2023-11-14T12:43:34.884179Z","shell.execute_reply.started":"2023-11-14T12:43:34.858402Z","shell.execute_reply":"2023-11-14T12:43:34.883494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TARGET_SR = 32000\n\nDATADIR = Path(\"../input/birdclef-2022/test_soundscapes/\")\n\nall_audios = list(DATADIR.glob(\"*.ogg\"))\nall_audio_ids = [\"_\".join(audio_id.name.split(\"_\")[:2]) for audio_id in all_audios]\nsubmission_df = pd.DataFrame({\n    \"row_id\": all_audio_ids\n})\nsubmission_df","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.885074Z","iopub.execute_input":"2023-11-14T12:43:34.885265Z","iopub.status.idle":"2023-11-14T12:43:34.907011Z","shell.execute_reply.started":"2023-11-14T12:43:34.885241Z","shell.execute_reply":"2023-11-14T12:43:34.906362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights_path_10sec = [Path(\"../input/fulleffv2320hop1024nft/totalclass-doublemixup-5sec-length1024-128mel-320hop-weightmixed-tf-efficientnet-v2s-pretrain-fold-0.pth\"),\n                Path(\"../input/fulleffv2320hop1024nft/totalclass-doublemixup-5sec-length1024-128mel-320hop-weightmixed-tf-efficientnet-v2s-pretrain-fold-1.pth\"),\n                Path(\"../input/fulleffv2320hop1024nft/totalclass-doublemixup-5sec-length1024-128mel-320hop-weightmixed-tf-efficientnet-v2s-pretrain-fold-2.pth\"),\n                Path(\"../input/fulleffv2320hop1024nft/totalclass-doublemixup-5sec-length1024-128mel-320hop-weightmixed-tf-efficientnet-v2s-pretrain-fold-3.pth\"),\n                Path(\"../input/effv2m128mel1024nfftdoublemixup/128mel-5sec-effv2m-fold0.pth\"),\n                Path(\"../input/effv2m128mel1024nfftdoublemixup/128mel-5sec-effv2m-fold1.pth\"),\n                Path(\"../input/effv2m128mel1024nfftdoublemixup/128mel-5sec-effv2m-fold2.pth\"),\n                Path(\"../input/effv2m128mel1024nfftdoublemixup/128mel-5sec-effv2m-fold3.pth\"),\n                Path(\"../input/specdiffseed128meleffv2m/fold0-320hop-spec-diffseed-tf-efficientnet-v2m.pth\"),\n                Path(\"../input/specdiffseed128meleffv2m/fold1-320hop-spec-diffseed-tf-efficientnet-v2m.pth\"),\n                Path(\"../input/specdiffseed128meleffv2m/fold3-320hop-spec-diffseed-tf-efficientnet-v2m.pth\"),\n                #Path(\"../input/fullnfnetmixup320hop5sec/totalclass-5sec-320hop-weightmixed-eca-nfnet-l0-pretrain-fold-0.pth\"),\n                #Path(\"../input/nfnetgrufromfinetune/nfnetl0grufromfinetune-10sec-fold0.pth\"),\n                #Path(\"../input/nfnetgrufromfinetune/nfnetl0grufromfinetune-10sec-fold1.pth\"),\n                #Path(\"../input/nfnetgrufromfinetune/nfnetl0grufromfinetune-10sec-fold2.pth\"),\n                #Path(\"../input/nfnetgrufromfinetune/nfnetl0grufromfinetune-10sec-fold3.pth\"),\n                #Path(\"../input/nfnetgrufromfinetune/nfnetl0grufromfinetune-10sec-fold4.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/effb0-frompretrain-5sec-fold2-train.49_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/effb0-frompretrain-5sec-train.35_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/effb0-frompretrain-5sec-train.38_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/effb0-frompretrain-5sec-train.42_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/effb0-frompretrain-5sec-train.49_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/effb0_10sec_fold0_train.31_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/effb0_10sec_fold1_train.47_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/effb0_10sec_fold2_train.50_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/effb0_10sec_fold3_train.48_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/effb0_10sec_fold4_train.49_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/fold0-frompretrain-effb0-15sec-train.50_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/fold1-frompretrain-effb0-15sec-train.50_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/fold2-frompretrain-effb0-15sec-train.28_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/fold3-frompretrain-effb0-15sec-train.34_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/fold4-frompretrain-effb0-15sec-train.46_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/effb0_aug_fold4_train.88_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/effb0_aug_fold0_train.94_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/effb0_aug_fold1_train.91_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/effb0_aug_fold2_train.75_full.pth\"),\n                #Path(\"../input/birdclefclass21effb0aug/effb0_aug_fold3_train.91_full.pth\")\n               ]\nweights_path_15sec = [Path(\"../input/fulleffv2320hop1024nft/fold0-spec-15sec-effv2-320hop.pth\"),\n                      Path(\"../input/speceffv2diffseed1281024/fold0_spec_320hop_diffseed_128mel_effv2s.pth\"),\n                      Path(\"../input/speceffv2diffseed1281024/fold1_spec_320hop_diffseed_128mel_effv2s.pth\"),\n                      Path(\"../input/speceffv2diffseed1281024/fold2_spec_320hop_diffseed_128mel_effv2s.pth\"),\n                      Path(\"../input/speceffv2diffseed1281024/fold3_spec_320hop_diffseed_128mel_effv2s.pth\")\n                     ]\nweights_path_15sec_64mel = [Path(\"../input/15seceffv2sspec64mel2048nfftdoublemixup/spec-64mel-15sec-effv2s-fold1.pth\"),\n                      Path(\"../input/15seceffv2sspec64mel2048nfftdoublemixup/spec-64mel-15sec-effv2s-fold2.pth\"),\n                      Path(\"../input/15seceffv2sspec64mel2048nfftdoublemixup/spec-64mel-15sec-effv2s-fold3.pth\"),\n                      Path(\"../input/nfnet64melspec/fold0-64mel-spec-diffseed-nfnetecal0.pth\"),\n                      Path(\"../input/nfnet64melspec/fold1-64mel-spec-diffseed-nfnetecal0.pth\"),\n                      Path(\"../input/nfnet64melspec/fold2-64mel-spec-diffseed-nfnetecal0.pth\"),\n                      Path(\"../input/nfnet64melspec/fold3-64mel-spec-diffseed-nfnetecal0.pth\"),\n                      Path(\"../input/nfnet64melspec/fold0-64mel-spec-diffseed-nfnetecal1.pth.pth\"),\n                      Path(\"../input/nfnet64melspec/fold1-64mel-spec-diffseed-nfnetecal1.pth.pth\"),\n                      Path(\"../input/nfnet64melspec/fold2-64mel-spec-diffseed-nfnetecal1.pth\"),\n                      Path(\"../input/nfnet64melspec/fold3-64mel-spec-diffseed-nfnetecal1.pth\"),\n                     ]\n#submission = prediction_framewise_ensemble(test_audios=all_audios,\ntemp_submission = prediction_framewise_voting_10sec_speedup(test_audios=all_audios,\n                        weights_path=weights_path_10sec,\n                        threshold=0.065)\ntemp_submission1 = prediction_framewise_voting_15sec_low_speedup(test_audios=all_audios,\n                        weights_path=weights_path_15sec,\n                        threshold=0.065)\ntemp_submission2 = prediction_framewise_voting_15sec_64mel_speedup(test_audios=all_audios,\n                        weights_path=weights_path_15sec_64mel,\n                        threshold=0.065)\n#submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:34.907954Z","iopub.execute_input":"2023-11-14T12:43:34.908167Z","iopub.status.idle":"2023-11-14T12:43:36.466798Z","shell.execute_reply.started":"2023-11-14T12:43:34.908141Z","shell.execute_reply":"2023-11-14T12:43:36.465673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.467595Z","iopub.status.idle":"2023-11-14T12:43:36.46792Z","shell.execute_reply.started":"2023-11-14T12:43:36.467732Z","shell.execute_reply":"2023-11-14T12:43:36.467762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission1","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.469319Z","iopub.status.idle":"2023-11-14T12:43:36.46964Z","shell.execute_reply.started":"2023-11-14T12:43:36.469464Z","shell.execute_reply":"2023-11-14T12:43:36.469487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission2","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.470813Z","iopub.status.idle":"2023-11-14T12:43:36.471097Z","shell.execute_reply.started":"2023-11-14T12:43:36.470946Z","shell.execute_reply":"2023-11-14T12:43:36.470962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission1 = temp_submission1.rename(columns={\"target_0\":\"target_11\", \"score_0\":\"score_11\",'row_id':\"row_id_backup\"})\ntemp_submission1 = temp_submission1.rename(columns={\"target_1\":\"target_12\", \"score_1\":\"score_12\",'row_id':\"row_id_backup\"})\ntemp_submission1 = temp_submission1.rename(columns={\"target_2\":\"target_13\", \"score_2\":\"score_13\",'row_id':\"row_id_backup\"})\ntemp_submission1 = temp_submission1.rename(columns={\"target_3\":\"target_14\", \"score_3\":\"score_14\",'row_id':\"row_id_backup\"})\ntemp_submission1 = temp_submission1.rename(columns={\"target_4\":\"target_15\", \"score_4\":\"score_15\",'row_id':\"row_id_backup\"})","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.47203Z","iopub.status.idle":"2023-11-14T12:43:36.47231Z","shell.execute_reply.started":"2023-11-14T12:43:36.47216Z","shell.execute_reply":"2023-11-14T12:43:36.472175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission2 = temp_submission2.rename(columns={\"target_0\":\"target_16\", \"score_0\":\"score_16\",'row_id':\"row_id_backup\"})\ntemp_submission2 = temp_submission2.rename(columns={\"target_1\":\"target_17\", \"score_1\":\"score_17\",'row_id':\"row_id_backup\"})\ntemp_submission2 = temp_submission2.rename(columns={\"target_2\":\"target_18\", \"score_2\":\"score_18\",'row_id':\"row_id_backup\"})\ntemp_submission2 = temp_submission2.rename(columns={\"target_3\":\"target_19\", \"score_3\":\"score_19\",'row_id':\"row_id_backup\"})\ntemp_submission2 = temp_submission2.rename(columns={\"target_4\":\"target_20\", \"score_4\":\"score_20\",'row_id':\"row_id_backup\"})\ntemp_submission2 = temp_submission2.rename(columns={\"target_5\":\"target_21\", \"score_5\":\"score_21\",'row_id':\"row_id_backup\"})\ntemp_submission2 = temp_submission2.rename(columns={\"target_6\":\"target_22\", \"score_6\":\"score_22\",'row_id':\"row_id_backup\"})","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.473527Z","iopub.status.idle":"2023-11-14T12:43:36.473841Z","shell.execute_reply.started":"2023-11-14T12:43:36.47366Z","shell.execute_reply":"2023-11-14T12:43:36.473675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission2 = temp_submission2.rename(columns={\"target_7\":\"target_23\", \"score_7\":\"score_23\",'row_id':\"row_id_backup\"})\ntemp_submission2 = temp_submission2.rename(columns={\"target_8\":\"target_24\", \"score_8\":\"score_24\",'row_id':\"row_id_backup\"})\ntemp_submission2 = temp_submission2.rename(columns={\"target_9\":\"target_25\", \"score_9\":\"score_25\",'row_id':\"row_id_backup\"})\ntemp_submission2 = temp_submission2.rename(columns={\"target_10\":\"target_26\", \"score_10\":\"score_26\",'row_id':\"row_id_backup\"})","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.474964Z","iopub.status.idle":"2023-11-14T12:43:36.475244Z","shell.execute_reply.started":"2023-11-14T12:43:36.475094Z","shell.execute_reply":"2023-11-14T12:43:36.475109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.47642Z","iopub.status.idle":"2023-11-14T12:43:36.476712Z","shell.execute_reply.started":"2023-11-14T12:43:36.476559Z","shell.execute_reply":"2023-11-14T12:43:36.476575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission = pd.concat([temp_submission,temp_submission1,temp_submission2],axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.478493Z","iopub.status.idle":"2023-11-14T12:43:36.478942Z","shell.execute_reply.started":"2023-11-14T12:43:36.478686Z","shell.execute_reply":"2023-11-14T12:43:36.478709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.480483Z","iopub.status.idle":"2023-11-14T12:43:36.480798Z","shell.execute_reply.started":"2023-11-14T12:43:36.480621Z","shell.execute_reply":"2023-11-14T12:43:36.480637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission1","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.481567Z","iopub.status.idle":"2023-11-14T12:43:36.481888Z","shell.execute_reply.started":"2023-11-14T12:43:36.481696Z","shell.execute_reply":"2023-11-14T12:43:36.481711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission2","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.483334Z","iopub.status.idle":"2023-11-14T12:43:36.483615Z","shell.execute_reply.started":"2023-11-14T12:43:36.483467Z","shell.execute_reply":"2023-11-14T12:43:36.483482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len_target = len([x for x in temp_submission.columns if x.startswith(\"target\")])","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.484514Z","iopub.status.idle":"2023-11-14T12:43:36.484826Z","shell.execute_reply.started":"2023-11-14T12:43:36.484639Z","shell.execute_reply":"2023-11-14T12:43:36.484654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len_target)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.486137Z","iopub.status.idle":"2023-11-14T12:43:36.486416Z","shell.execute_reply.started":"2023-11-14T12:43:36.486269Z","shell.execute_reply":"2023-11-14T12:43:36.486284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_columns = [\"target_\"+str(i) for i in range(len_target)]","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.487577Z","iopub.status.idle":"2023-11-14T12:43:36.48791Z","shell.execute_reply.started":"2023-11-14T12:43:36.48771Z","shell.execute_reply":"2023-11-14T12:43:36.487734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_columns = [\"score_\"+str(i) for i in range(len_target)]","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.490384Z","iopub.status.idle":"2023-11-14T12:43:36.490699Z","shell.execute_reply.started":"2023-11-14T12:43:36.490529Z","shell.execute_reply":"2023-11-14T12:43:36.490554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_colwidth', -1)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.492292Z","iopub.status.idle":"2023-11-14T12:43:36.492719Z","shell.execute_reply.started":"2023-11-14T12:43:36.492487Z","shell.execute_reply":"2023-11-14T12:43:36.492509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission = temp_submission[['row_id']+[\"target_\"+str(i) for i in range(len_target)]+[\"score_\"+str(i) for i in range(len_target)]]","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.494278Z","iopub.status.idle":"2023-11-14T12:43:36.494692Z","shell.execute_reply.started":"2023-11-14T12:43:36.494469Z","shell.execute_reply":"2023-11-14T12:43:36.494491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#temp_submission[[f\"score_{i}\" for i in range(11)]][-20:]","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.496118Z","iopub.status.idle":"2023-11-14T12:43:36.49654Z","shell.execute_reply.started":"2023-11-14T12:43:36.496303Z","shell.execute_reply":"2023-11-14T12:43:36.496325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_max_score(df):\n    score_list = []\n    for col in score_columns:\n        score_list.append(df[col])\n\n    return max(score_list)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.497663Z","iopub.status.idle":"2023-11-14T12:43:36.498088Z","shell.execute_reply.started":"2023-11-14T12:43:36.497865Z","shell.execute_reply":"2023-11-14T12:43:36.497887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_mean_score(df):\n    score_list = []\n    for col in score_columns:\n        score_list.append(df[col])\n\n    return np.mean(score_list)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.499437Z","iopub.status.idle":"2023-11-14T12:43:36.499732Z","shell.execute_reply.started":"2023-11-14T12:43:36.499581Z","shell.execute_reply":"2023-11-14T12:43:36.499596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_second_score(df):\n    score_list = []\n    for col in score_columns:\n        score_list.append(df[col])\n\n    return score_list[np.argsort(-np.array(score_list))[1]]","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.500888Z","iopub.status.idle":"2023-11-14T12:43:36.501197Z","shell.execute_reply.started":"2023-11-14T12:43:36.50103Z","shell.execute_reply":"2023-11-14T12:43:36.501053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.502377Z","iopub.status.idle":"2023-11-14T12:43:36.502696Z","shell.execute_reply.started":"2023-11-14T12:43:36.502525Z","shell.execute_reply":"2023-11-14T12:43:36.502549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission.tail()","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.504114Z","iopub.status.idle":"2023-11-14T12:43:36.504419Z","shell.execute_reply.started":"2023-11-14T12:43:36.504251Z","shell.execute_reply":"2023-11-14T12:43:36.504276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_real_target(df):\n    init = 0\n    for col in target_columns:\n        init += df[col]\n\n    return init >= int((len(target_columns) / 2) + 1)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.505664Z","iopub.status.idle":"2023-11-14T12:43:36.505996Z","shell.execute_reply.started":"2023-11-14T12:43:36.505827Z","shell.execute_reply":"2023-11-14T12:43:36.505851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission['target'] = temp_submission.apply(get_real_target, axis=1)\ntemp_submission['max_score'] = temp_submission.apply(get_max_score, axis=1)\ntemp_submission['mean_score'] = temp_submission.apply(get_mean_score, axis=1)\ntemp_submission['second_score'] = temp_submission.apply(get_second_score, axis=1)\ntemp_submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.507781Z","iopub.status.idle":"2023-11-14T12:43:36.508195Z","shell.execute_reply.started":"2023-11-14T12:43:36.507966Z","shell.execute_reply":"2023-11-14T12:43:36.507989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#temp_submission[[f\"target_{x}\" for x in range(23)]]","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.50997Z","iopub.status.idle":"2023-11-14T12:43:36.510379Z","shell.execute_reply.started":"2023-11-14T12:43:36.510152Z","shell.execute_reply":"2023-11-14T12:43:36.510179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(temp_submission['max_score']>0.200).sum()","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.51189Z","iopub.status.idle":"2023-11-14T12:43:36.512185Z","shell.execute_reply.started":"2023-11-14T12:43:36.512032Z","shell.execute_reply":"2023-11-14T12:43:36.512048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(temp_submission['mean_score']>0.0855).sum()","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.513116Z","iopub.status.idle":"2023-11-14T12:43:36.513396Z","shell.execute_reply.started":"2023-11-14T12:43:36.513245Z","shell.execute_reply":"2023-11-14T12:43:36.51326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(temp_submission['second_score']>0.20).sum()","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.514874Z","iopub.status.idle":"2023-11-14T12:43:36.515285Z","shell.execute_reply.started":"2023-11-14T12:43:36.51506Z","shell.execute_reply":"2023-11-14T12:43:36.515082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#temp_submission['target'] = temp_submission['max_score']>0.2\ntemp_submission['target'] = temp_submission['second_score']>0.20","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.516575Z","iopub.status.idle":"2023-11-14T12:43:36.517024Z","shell.execute_reply.started":"2023-11-14T12:43:36.516773Z","shell.execute_reply":"2023-11-14T12:43:36.516796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = temp_submission[['row_id','target']]","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.518132Z","iopub.status.idle":"2023-11-14T12:43:36.518558Z","shell.execute_reply.started":"2023-11-14T12:43:36.518312Z","shell.execute_reply":"2023-11-14T12:43:36.518334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.519943Z","iopub.status.idle":"2023-11-14T12:43:36.520359Z","shell.execute_reply.started":"2023-11-14T12:43:36.520128Z","shell.execute_reply":"2023-11-14T12:43:36.520152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.522037Z","iopub.status.idle":"2023-11-14T12:43:36.522439Z","shell.execute_reply.started":"2023-11-14T12:43:36.522219Z","shell.execute_reply":"2023-11-14T12:43:36.522242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['target'].sum()","metadata":{"execution":{"iopub.status.busy":"2023-11-14T12:43:36.523524Z","iopub.status.idle":"2023-11-14T12:43:36.523939Z","shell.execute_reply.started":"2023-11-14T12:43:36.523699Z","shell.execute_reply":"2023-11-14T12:43:36.523721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}