Kaggle competition: Cornell Birdcall Identification
<SOS> metric: MeanFScoreBeta Kaggle data source: cornell-birdcall-identification<train_model>
!pip install onnxruntime --no-index --find-links=file:///kaggle/input/save-out-pip-libraries-without-internet/onnxrunt/
print("Train X: {}
Train y: {}
Test X: {}".format(X_train.shape, y_train.shape, X_test.shape))<categorify>
import os
import gc
import math
import random
from pathlib import Path
import cv2
import librosa
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import Conv2d, Module, Linear, BatchNorm2d, ReLU
from torch.nn.modules.utils import _pair
import torch.utils.data as data
le = LabelEncoder()
y_train['surface'] = le.fit_transform(y_train['surface'] )<normalization>
import onnxruntime
scaler = StandardScaler()
X_train_scaled = pd.DataFrame(scaler.fit_transform(X_train))
X_test_scaled = pd.DataFrame(scaler.transform(X_test))<choose_model_class>
ROOT = Path.cwd().parent
INPUT_ROOT = ROOT / "input"
RAW_DATA = INPUT_ROOT / "birdsong-recognition"
TEST_AUDIO_DIR = RAW_DATA / "test_audio"
folds = StratifiedKFold(n_splits=49, shuffle=True, random_state=41 )<predict_on_test>
if not TEST_AUDIO_DIR.exists() :
TEST_AUDIO_DIR = INPUT_ROOT / "birdcall-check" / "test_audio"
test = pd.read_csv(INPUT_ROOT / "birdcall-check" / "test.csv")
 else:
test = pd.read_csv(RAW_DATA / "test.csv" )
sub_preds_rf = np.zeros(( X_test_scaled.shape[0], 9))
oof_preds_rf = np.zeros(( X_train_scaled.shape[0]))
score = 0
for fold_,(trn_idx, val_idx)in enumerate(folds.split(X_train_scaled, y_train['surface'])) :
clf = RandomForestClassifier(n_estimators = 500, n_jobs = -1)
 clf.fit(X_train_scaled.iloc[trn_idx], y_train['surface'][trn_idx])
 oof_preds_rf[val_idx] = clf.predict(X_train_scaled.iloc[val_idx])
 sub_preds_rf += clf.predict_proba(X_test_scaled)/ folds.n_splits
score += clf.score(X_train_scaled.iloc[val_idx], y_train['surface'][val_idx])
 print('Fold: {} score: {}'.format(fold_,clf.score(X_train_scaled.iloc[val_idx], y_train['surface'][val_idx])))
 print('Avg Accuracy', score / folds.n_splits )<predict_on_test>
sub = pd.read_csv(".. /input/birdsong-recognition/sample_submission.csv")
sub_preds_xgboost = np.zeros(( X_test_scaled.shape[0], 9))
oof_preds_xgboost = np.zeros(( X_train_scaled.shape[0]))
score = 0
for fold_,(trn_idx, val_idx)in enumerate(folds.split(X_train_scaled, y_train['surface'])) :
xgb_clf = XGBClassifier(n_jobs = -1)
 xgb_clf.fit(X_train_scaled.iloc[trn_idx], y_train['surface'][trn_idx])
 oof_preds_xgboost[val_idx] = xgb_clf.predict(X_train_scaled.iloc[val_idx])
 sub_preds_xgboost += xgb_clf.predict_proba(X_test_scaled)/ folds.n_splits
score += xgb_clf.score(X_train_scaled.iloc[val_idx], y_train['surface'][val_idx])
 print('Fold: {} score: {}'.format(fold_,xgb_clf.score(X_train_scaled.iloc[val_idx], y_train['surface'][val_idx])))
 print('Avg Accuracy', score / folds.n_splits )<prepare_x_and_y>
TARGET_SR = 32000
model_config = {
"base_model_name": "resnest50_fast_1s1x64d",
"pretrained": False,
"num_classes": 264,
"trained_weights": ".. /input/training-birdsong-baseline-resnest50-fast/best_model.pth"
}
melspectrogram_parameters = {
"n_mels": 128,
"fmin": 20,
"fmax": 16000
}
y_test_pred_final = np.array(sub_preds_rf.argmax(axis=1)*0.5+sub_preds_xgboost.argmax(axis=1)*0.5 )<define_variables>
BIRD_CODE = {
'aldfly': 0, 'ameavo': 1, 'amebit': 2, 'amecro': 3, 'amegfi': 4,
'amekes': 5, 'amepip': 6, 'amered': 7, 'amerob': 8, 'amewig': 9,
'amewoo': 10, 'amtspa': 11, 'annhum': 12, 'astfly': 13, 'baisan': 14,
'baleag': 15, 'balori': 16, 'banswa': 17, 'barswa': 18, 'bawwar': 19,
'belkin1': 20, 'belspa2': 21, 'bewwre': 22, 'bkbcuc': 23, 'bkbmag1': 24,
'bkbwar': 25, 'bkcchi': 26, 'bkchum': 27, 'bkhgro': 28, 'bkpwar': 29,
'bktspa': 30, 'blkpho': 31, 'blugrb1': 32, 'blujay': 33, 'bnhcow': 34,
'boboli': 35, 'bongul': 36, 'brdowl': 37, 'brebla': 38, 'brespa': 39,
'brncre': 40, 'brnthr': 41, 'brthum': 42, 'brwhaw': 43, 'btbwar': 44,
'btnwar': 45, 'btywar': 46, 'buffle': 47, 'buggna': 48, 'buhvir': 49,
'bulori': 50, 'bushti': 51, 'buwtea': 52, 'buwwar': 53, 'cacwre': 54,
'calgul': 55, 'calqua': 56, 'camwar': 57, 'cangoo': 58, 'canwar': 59,
'canwre': 60, 'carwre': 61, 'casfin': 62, 'caster1': 63, 'casvir': 64,
'cedwax': 65, 'chispa': 66, 'chiswi': 67, 'chswar': 68, 'chukar': 69,
'clanut': 70, 'cliswa': 71, 'comgol': 72, 'comgra': 73, 'comloo': 74,
'commer': 75, 'comnig': 76, 'comrav': 77, 'comred': 78, 'comter': 79,
'comyel': 80, 'coohaw': 81, 'coshum': 82, 'cowscj1': 83, 'daejun': 84,
'doccor': 85, 'dowwoo': 86, 'dusfly': 87, 'eargre': 88, 'easblu': 89,
'easkin': 90, 'easmea': 91, 'easpho': 92, 'eastow': 93, 'eawpew': 94,
'eucdov': 95, 'eursta': 96, 'evegro': 97, 'fiespa': 98, 'fiscro': 99,
'foxspa': 100, 'gadwal': 101, 'gcrfin': 102, 'gnttow': 103, 'gnwtea': 104,
'gockin': 105, 'gocspa': 106, 'goleag': 107, 'grbher3': 108, 'grcfly': 109,
'greegr': 110, 'greroa': 111, 'greyel': 112, 'grhowl': 113, 'grnher': 114,
'grtgra': 115, 'grycat': 116, 'gryfly': 117, 'haiwoo': 118, 'hamfly': 119,
'hergul': 120, 'herthr': 121, 'hoomer': 122, 'hoowar': 123, 'horgre': 124,
'horlar': 125, 'houfin': 126, 'houspa': 127, 'houwre': 128, 'indbun': 129,
'juntit1': 130, 'killde': 131, 'labwoo': 132, 'larspa': 133, 'lazbun': 134,
'leabit': 135, 'leafly': 136, 'leasan': 137, 'lecthr': 138, 'lesgol': 139,
'lesnig': 140, 'lesyel': 141, 'lewwoo': 142, 'linspa': 143, 'lobcur': 144,
'lobdow': 145, 'logshr': 146, 'lotduc': 147, 'louwat': 148, 'macwar': 149,
'magwar': 150, 'mallar3': 151, 'marwre': 152, 'merlin': 153, 'moublu': 154,
'mouchi': 155, 'moudov': 156, 'norcar': 157, 'norfli': 158, 'norhar2': 159,
'normoc': 160, 'norpar': 161, 'norpin': 162, 'norsho': 163, 'norwat': 164,
'nrwswa': 165, 'nutwoo': 166, 'olsfly': 167, 'orcwar': 168, 'osprey': 169,
'ovenbi1': 170, 'palwar': 171, 'pasfly': 172, 'pecsan': 173, 'perfal': 174,
'phaino': 175, 'pibgre': 176, 'pilwoo': 177, 'pingro': 178, 'pinjay': 179,
'pinsis': 180, 'pinwar': 181, 'plsvir': 182, 'prawar': 183, 'purfin': 184,
'pygnut': 185, 'rebmer': 186, 'rebnut': 187, 'rebsap': 188, 'rebwoo': 189,
'redcro': 190, 'redhea': 191, 'reevir1': 192, 'renpha': 193, 'reshaw': 194,
'rethaw': 195, 'rewbla': 196, 'ribgul': 197, 'rinduc': 198, 'robgro': 199,
'rocpig': 200, 'rocwre': 201, 'rthhum': 202, 'ruckin': 203, 'rudduc': 204,
'rufgro': 205, 'rufhum': 206, 'rusbla': 207, 'sagspa1': 208, 'sagthr': 209,
'savspa': 210, 'saypho': 211, 'scatan': 212, 'scoori': 213, 'semplo': 214,
'semsan': 215, 'sheowl': 216, 'shshaw': 217, 'snobun': 218, 'snogoo': 219,
'solsan': 220, 'sonspa': 221, 'sora': 222, 'sposan': 223, 'spotow': 224,
'stejay': 225, 'swahaw': 226, 'swaspa': 227, 'swathr': 228, 'treswa': 229,
'truswa': 230, 'tuftit': 231, 'tunswa': 232, 'veery': 233, 'vesspa': 234,
'vigswa': 235, 'warvir': 236, 'wesblu': 237, 'wesgre': 238, 'weskin': 239,
'wesmea': 240, 'wessan': 241, 'westan': 242, 'wewpew': 243, 'whbnut': 244,
'whcspa': 245, 'whfibi': 246, 'whtspa': 247, 'whtswi': 248, 'wilfly': 249,
'wilsni1': 250, 'wiltur': 251, 'winwre3': 252, 'wlswar': 253, 'wooduc': 254,
'wooscj2': 255, 'woothr': 256, 'y00475': 257, 'yebfly': 258, 'yebsap': 259,
'yehbla': 260, 'yelwar': 261, 'yerwar': 262, 'yetvir': 263
}
INV_BIRD_CODE = {v: k for k, v in BIRD_CODE.items() }
y_test_pred_final = np.array([int(val)for val in y_test_pred_final] )<save_to_csv>
settings = {}
settings['dataset'] = {}
settings['dataset']['params'] = {}
settings['dataset']['params']['img_size'] = 300
settings['dataset']['params']['melspectrogram_parameters'] = {}
settings['dataset']['params']['melspectrogram_parameters']['n_mels'] = 300
settings['dataset']['params']['melspectrogram_parameters']['fmin'] = 650
settings['dataset']['params']['melspectrogram_parameters']['fmax'] = 16000
settings['dataset']['params']['melspectrogram_parameters']['n_fft'] = 2400
settings['dataset']['params']['melspectrogram_parameters']['hop_length'] = 533
y_test['surface'] = le.inverse_transform(y_test_pred_final)
 y_test.to_csv('submission_18.csv', index=False )<import_modules>
settings_effnetb1 = {}
settings_effnetb1['dataset'] = {}
settings_effnetb1['dataset']['params'] = {}
settings_effnetb1['dataset']['params']['img_size'] = 240
settings_effnetb1['dataset']['params']['melspectrogram_parameters'] = {}
settings_effnetb1['dataset']['params']['melspectrogram_parameters']['n_mels'] = 240
settings_effnetb1['dataset']['params']['melspectrogram_parameters']['fmin'] = 1000
settings_effnetb1['dataset']['params']['melspectrogram_parameters']['fmax'] = 12500
settings_effnetb1['dataset']['params']['melspectrogram_parameters']['n_fft'] = 1920
settings_effnetb1['dataset']['params']['melspectrogram_parameters']['hop_length'] = 666
sns.set(style="whitegrid")
 np.random.seed(203)
 print(os.listdir(".. /input")) 
<load_from_csv>
settings_thefirst = {}
settings_thefirst['dataset'] = {}
settings_thefirst['dataset']['params'] = {}
settings_thefirst['dataset']['params']['img_size'] = 260
settings_thefirst['dataset']['params']['melspectrogram_parameters'] = {}
settings_thefirst['dataset']['params']['melspectrogram_parameters']['n_mels'] = 260
settings_thefirst['dataset']['params']['melspectrogram_parameters']['fmin'] = 20
settings_thefirst['dataset']['params']['melspectrogram_parameters']['fmax'] = 15000
settings_thefirst['dataset']['params']['melspectrogram_parameters']['n_fft'] = 1500
settings_thefirst['dataset']['params']['melspectrogram_parameters']['hop_length'] = 800
targets = pd.read_csv(".. /input/y_train.csv")
 df_train = pd.read_csv(".. /input/X_train.csv")
 df_test = pd.read_csv(".. /input/X_test.csv" )<prepare_x_and_y>
settings_thesecond = {}
settings_thesecond['dataset'] = {}
settings_thesecond['dataset']['params'] = {}
settings_thesecond['dataset']['params']['img_size'] = 260
settings_thesecond['dataset']['params']['melspectrogram_parameters'] = {}
settings_thesecond['dataset']['params']['melspectrogram_parameters']['n_mels'] = 260
settings_thesecond['dataset']['params']['melspectrogram_parameters']['fmin'] = 260
settings_thesecond['dataset']['params']['melspectrogram_parameters']['fmax'] = 16000
settings_thesecond['dataset']['params']['melspectrogram_parameters']['n_fft'] = 2080
settings_thesecond['dataset']['params']['melspectrogram_parameters']['hop_length'] = 615
targets = pd.get_dummies(targets, columns=["surface"])
 Y = targets.drop(["series_id", "group_id"], axis=1 ).values
X = df_train.drop(["row_id", "series_id", "measurement_number"], axis=1 ).values
test = df_test.drop(["row_id", "series_id", "measurement_number"], axis=1 ).values
scaler = StandardScaler()
X = scaler.fit_transform(X)
 X = X.reshape(( 3810, 128, 10, 1))
X = X.transpose(0, 2, 1, 3)
 test = scaler.transform(test)
 test = test.reshape(( 3816, 128, 10, 1))
test = test.transpose(0, 2, 1, 3 )<feature_engineering>
settings_inception = {}
settings_inception['dataset'] = {}
settings_inception['dataset']['params'] = {}
settings_inception['dataset']['params']['img_size'] = 299
settings_inception['dataset']['params']['melspectrogram_parameters'] = {}
settings_inception['dataset']['params']['melspectrogram_parameters']['n_mels'] = 299
settings_inception['dataset']['params']['melspectrogram_parameters']['fmin'] = 299
settings_inception['dataset']['params']['melspectrogram_parameters']['fmax'] = 16000
settings_inception['dataset']['params']['melspectrogram_parameters']['n_fft'] = 2392
settings_inception['dataset']['params']['melspectrogram_parameters']['hop_length'] = 535
for col in df_train.drop(["row_id", "series_id", "measurement_number"], axis=1 ).columns:
df_train["diff_"+col] = df_train[col]-df_train[col].shift(1)
 df_test["diff_"+col] = df_test[col]-df_test[col].shift(1)
 df_train["ma_"+col] = np.square(abs(df_train[col]-df_train[col].rolling(8 ).mean()))
df_test["ma_"+col] = np.square(abs(df_test[col]-df_test[col].rolling(8 ).mean()))
df_train = df_train[df_train["measurement_number"]>7]
df_test = df_test[df_test["measurement_number"]>7]
aggs = {
"diff_orientation_X":"std",
"diff_orientation_Y":"std",
"diff_orientation_Z":"std",
"diff_orientation_W":"std",
"diff_angular_velocity_X":"std",
"diff_angular_velocity_Y":"std",
"diff_angular_velocity_Z":"std",
"diff_linear_acceleration_X":"std",
"diff_linear_acceleration_Y":"std",
"diff_linear_acceleration_Z":"std",
"ma_orientation_X":"sum",
"ma_orientation_Y":"sum",
"ma_orientation_Z":"sum",
"ma_orientation_W":"sum",
"ma_angular_velocity_X":"sum",
"ma_angular_velocity_Y":"sum",
"ma_angular_velocity_Z":"sum",
"ma_linear_acceleration_X":"sum",
"ma_linear_acceleration_Y":"sum",
"ma_linear_acceleration_Z":"sum"
}
train_smooth = df_train.groupby("series_id" ).agg(aggs)
 test_smooth = df_test.groupby("series_id" ).agg(aggs)
 train_smooth.reset_index(inplace=True)
 test_smooth.reset_index(inplace=True )<normalization>
settings_se = {}
settings_se['dataset'] = {}
settings_se['dataset']['params'] = {}
settings_se['dataset']['params']['img_size'] = 224
settings_se['dataset']['params']['melspectrogram_parameters'] = {}
settings_se['dataset']['params']['melspectrogram_parameters']['n_mels'] = 224
settings_se['dataset']['params']['melspectrogram_parameters']['fmin'] = 224
settings_se['dataset']['params']['melspectrogram_parameters']['fmax'] = 16000
settings_se['dataset']['params']['melspectrogram_parameters']['n_fft'] = 1792
settings_se['dataset']['params']['melspectrogram_parameters']['hop_length'] = 714
X_smooth = train_smooth.drop(["series_id"], axis=1 ).values
test_smooth = test_smooth.drop(["series_id"], axis=1 ).values
scaler = StandardScaler()
X_smooth = scaler.fit_transform(X_smooth)
 test_smooth = scaler.transform(test_smooth )<choose_model_class>
settings_mixup = {}
settings_mixup['dataset'] = {}
settings_mixup['dataset']['params'] = {}
settings_mixup['dataset']['params']['img_size'] = 260
settings_mixup['dataset']['params']['melspectrogram_parameters'] = {}
settings_mixup['dataset']['params']['melspectrogram_parameters']['n_mels'] = 208
settings_mixup['dataset']['params']['melspectrogram_parameters']['fmin'] = 2000
settings_mixup['dataset']['params']['melspectrogram_parameters']['fmax'] = 15000
settings_mixup['dataset']['params']['melspectrogram_parameters']['n_fft'] = 1664
settings_mixup['dataset']['params']['melspectrogram_parameters']['hop_length'] = 769
def init_model() :
FIRST = 30
SECOND = 20
HEIGHT1 = 4
HEIGHT2 = 3
DROPOUT = 0.5
STRIDES = None
PS = 5
input1 = Input(shape=(128, 1))
a = Conv1D(FIRST, HEIGHT1, activation="relu", kernel_initializer="uniform" )(input1)
 a = BatchNormalization()(a)
 a = MaxPooling1D(strides=STRIDES, pool_size=PS )(a)
 a = Conv1D(SECOND, HEIGHT2, activation="relu", kernel_initializer="uniform" )(a)
 a = BatchNormalization()(a)
 a = MaxPooling1D(strides=STRIDES, pool_size=PS )(a)
 a = Flatten()(a)
 a = Dropout(DROPOUT )(a)
 input2 = Input(shape=(128, 1))
b = Conv1D(FIRST, HEIGHT1, activation="relu", kernel_initializer="uniform" )(input2)
 b = BatchNormalization()(b)
 b = MaxPooling1D(strides=STRIDES, pool_size=PS )(b)
 b = Conv1D(SECOND, HEIGHT2, activation="relu", kernel_initializer="uniform" )(b)
 b = BatchNormalization()(b)
 b = MaxPooling1D(strides=STRIDES, pool_size=PS )(b)
 b = Flatten()(b)
 b = Dropout(DROPOUT )(b)
 input3 = Input(shape=(128, 1))
c = Conv1D(FIRST, HEIGHT1, activation="relu", kernel_initializer="uniform" )(input3)
 c = BatchNormalization()(c)
 c = MaxPooling1D(strides=STRIDES, pool_size=PS )(c)
 c = Conv1D(SECOND, HEIGHT2, activation="relu", kernel_initializer="uniform" )(c)
 c = BatchNormalization()(c)
 c = MaxPooling1D(strides=STRIDES, pool_size=PS )(c)
 c = Flatten()(c)
 c = Dropout(DROPOUT )(c)
 input4 = Input(shape=(128, 1))
d = Conv1D(FIRST, HEIGHT1, activation="relu", kernel_initializer="uniform" )(input4)
 d = BatchNormalization()(d)
 d = MaxPooling1D(strides=STRIDES, pool_size=PS )(d)
 d = Conv1D(SECOND, HEIGHT2, activation="relu", kernel_initializer="uniform" )(d)
 d = BatchNormalization()(d)
 d = MaxPooling1D(strides=STRIDES, pool_size=PS )(d)
 d = Flatten()(d)
 d = Dropout(DROPOUT )(d)
 input5 = Input(shape=(128, 1))
e = Conv1D(FIRST, HEIGHT1, activation="relu", kernel_initializer="uniform" )(input5)
 e = BatchNormalization()(e)
 e = MaxPooling1D(strides=STRIDES, pool_size=PS )(e)
 e = Conv1D(SECOND, HEIGHT2, activation="relu", kernel_initializer="uniform" )(e)
 e = BatchNormalization()(e)
 e = MaxPooling1D(strides=STRIDES, pool_size=PS )(e)
 e = Flatten()(e)
 e = Dropout(DROPOUT )(e)
 input6 = Input(shape=(128, 1))
f = Conv1D(FIRST, HEIGHT1, activation="relu", kernel_initializer="uniform" )(input6)
 f = BatchNormalization()(f)
 f = MaxPooling1D(strides=STRIDES, pool_size=PS )(f)
 f = Conv1D(SECOND, HEIGHT2, activation="relu", kernel_initializer="uniform" )(f)
 f = BatchNormalization()(f)
 f = MaxPooling1D(strides=STRIDES, pool_size=PS )(f)
 f = Flatten()(f)
 f = Dropout(DROPOUT )(f)
 input7 = Input(shape=(128, 1))
g = Conv1D(FIRST, HEIGHT1, activation="relu", kernel_initializer="uniform" )(input7)
 g = BatchNormalization()(g)
 g = MaxPooling1D(strides=STRIDES, pool_size=PS )(g)
 g = Conv1D(SECOND, HEIGHT2, activation="relu", kernel_initializer="uniform" )(g)
 g = BatchNormalization()(g)
 g = MaxPooling1D(strides=STRIDES, pool_size=PS )(g)
 g = Flatten()(g)
 g = Dropout(DROPOUT )(g)
 input8 = Input(shape=(128, 1))
h = Conv1D(FIRST, HEIGHT1, activation="relu", kernel_initializer="uniform" )(input8)
 h = BatchNormalization()(h)
 h = MaxPooling1D(strides=STRIDES, pool_size=PS )(h)
 h = Conv1D(SECOND, HEIGHT2, activation="relu", kernel_initializer="uniform" )(h)
 h = BatchNormalization()(h)
 h = MaxPooling1D(strides=STRIDES, pool_size=PS )(h)
 h = Flatten()(h)
 h = Dropout(DROPOUT )(h)
 input9 = Input(shape=(128, 1))
i = Conv1D(FIRST, HEIGHT1, activation="relu", kernel_initializer="uniform" )(input9)
 i = BatchNormalization()(i)
 i = MaxPooling1D(strides=STRIDES, pool_size=PS )(i)
 i = Conv1D(SECOND, HEIGHT2, activation="relu", kernel_initializer="uniform" )(i)
 i = BatchNormalization()(i)
 i = MaxPooling1D(strides=STRIDES, pool_size=PS )(i)
 i = Flatten()(i)
 i = Dropout(DROPOUT )(i)
 input10 = Input(shape=(128, 1))
j = Conv1D(FIRST, HEIGHT1, activation="relu", kernel_initializer="uniform" )(input10)
 j = BatchNormalization()(j)
 j = MaxPooling1D(strides=STRIDES, pool_size=PS )(j)
 j = Conv1D(SECOND, HEIGHT2, activation="relu", kernel_initializer="uniform" )(j)
 j = BatchNormalization()(j)
 j = MaxPooling1D(strides=STRIDES, pool_size=PS )(j)
 j = Flatten()(j)
 j = Dropout(DROPOUT )(j)
 input11 = Input(shape=(20,))
k = Dense(30, activation="relu", kernel_initializer="uniform" )(input11)
 k = Dropout(0.25 )(k)
 merged = Concatenate()([a, b])
 merged = Concatenate()([merged, c])
 merged = Concatenate()([merged, d])
 merged = Concatenate()([merged, e])
 merged = Concatenate()([merged, f])
 merged = Concatenate()([merged, g])
 merged = Concatenate()([merged, h])
 merged = Concatenate()([merged, i])
 merged = Concatenate()([merged, j])
 merged = Concatenate()([merged, k])
 merged = Dense(30, activation = "relu", kernel_initializer="uniform" )(merged)
 merged = Dropout(0.25 )(merged)
 output = Dense(9, activation="softmax", kernel_initializer="uniform" )(merged)
 model = Model([input1, input2, input3, input4, input5, input6, input7, input8, input9, input10, input11], output)
 return model<split>
settings_ls = {}
settings_ls['dataset'] = {}
settings_ls['dataset']['params'] = {}
settings_ls['dataset']['params']['img_size'] = 260
settings_ls['dataset']['params']['melspectrogram_parameters'] = {}
settings_ls['dataset']['params']['melspectrogram_parameters']['n_mels'] = 168
settings_ls['dataset']['params']['melspectrogram_parameters']['fmin'] = 800
settings_ls['dataset']['params']['melspectrogram_parameters']['fmax'] = 14000
settings_ls['dataset']['params']['melspectrogram_parameters']['n_fft'] = 1344
settings_ls['dataset']['params']['melspectrogram_parameters']['hop_length'] = 1024
te = [test[:,i] for i in range(10)]
te.append(test_smooth)
 folds = StratifiedKFold(n_splits=15, shuffle=True, random_state=0)
 oof = np.zeros(( len(X), 9))
test_predictions = np.zeros(( len(test), 9))
for fold_,(trn_idx, val_idx)in enumerate(folds.split(X,np.argmax(Y, axis=1))):
print(fold_)
 X_train, X_test = X[trn_idx], X[val_idx]
y_train, y_test = Y[trn_idx], Y[val_idx]
X_smooth_train, X_smooth_test = X_smooth[trn_idx], X_smooth[val_idx]
X_tr = [X_train[:,i] for i in range(10)]
X_tr.append(X_smooth_train)
 X_te = [X_test[:,i] for i in range(10)]
X_te.append(X_smooth_test)
 model = init_model()
model.compile(loss = "categorical_crossentropy", optimizer = "adam", metrics = ["accuracy"])
 model.fit(X_tr, y_train, validation_data =(X_te, y_test), epochs=100, shuffle=True, class_weight="balanced")
 pre = model.predict(X_te)
 oof[val_idx] = model.predict(X_te)
 test_predictions+= model.predict(te)/20<prepare_output>
class SplAtConv2d(Module):

 def __init__(self, in_channels, channels, kernel_size, stride=(1, 1), padding=(0, 0),
dilation=(1, 1), groups=1, bias=True,
radix=2, reduction_factor=4,
rectify=False, rectify_avg=False, norm_layer=None,
dropblock_prob=0.0, **kwargs):
super(SplAtConv2d, self ).__init__()
padding = _pair(padding)
 self.rectify = rectify and(padding[0] > 0 or padding[1] > 0)
 self.rectify_avg = rectify_avg
inter_channels = max(in_channels*radix//reduction_factor, 32)
 self.radix = radix
self.cardinality = groups
self.channels = channels
self.dropblock_prob = dropblock_prob
if self.rectify:
self.conv = RFConv2d(in_channels, channels*radix, kernel_size, stride, padding, dilation,
groups=groups*radix, bias=bias, average_mode=rectify_avg, **kwargs)
 else:
self.conv = Conv2d(in_channels, channels*radix, kernel_size, stride, padding, dilation,
groups=groups*radix, bias=bias, **kwargs)
 self.use_bn = norm_layer is not None
if self.use_bn:
self.bn0 = norm_layer(channels*radix)
 self.relu = ReLU(inplace=True)
 self.fc1 = Conv2d(channels, inter_channels, 1, groups=self.cardinality)
 if self.use_bn:
self.bn1 = norm_layer(inter_channels)
 self.fc2 = Conv2d(inter_channels, channels*radix, 1, groups=self.cardinality)
 if dropblock_prob > 0.0:
self.dropblock = DropBlock2D(dropblock_prob, 3)
 self.rsoftmax = rSoftMax(radix, groups)
 def forward(self, x):
x = self.conv(x)
 if self.use_bn:
x = self.bn0(x)
 if self.dropblock_prob > 0.0:
x = self.dropblock(x)
 x = self.relu(x)
 batch, rchannel = x.shape[:2]
if self.radix > 1:
if torch.__version__ < '1.5':
splited = torch.split(x, int(rchannel//self.radix), dim=1)
 else:
splited = torch.split(x, rchannel//self.radix, dim=1)
 gap = sum(splited)
 else:
gap = x
gap = F.adaptive_avg_pool2d(gap, 1)
 gap = self.fc1(gap)
 if self.use_bn:
gap = self.bn1(gap)
 gap = self.relu(gap)
 atten = self.fc2(gap)
 atten = self.rsoftmax(atten ).view(batch, -1, 1, 1)
 if self.radix > 1:
if torch.__version__ < '1.5':
attens = torch.split(atten, int(rchannel//self.radix), dim=1)
 else:
attens = torch.split(atten, rchannel//self.radix, dim=1)
 out = sum([att*split for(att, split)in zip(attens, splited)])
 else:
out = atten * x
return out.contiguous()
class rSoftMax(nn.Module):
def __init__(self, radix, cardinality):
super().__init__()
self.radix = radix
self.cardinality = cardinality
def forward(self, x):
batch = x.size(0)
 if self.radix > 1:
x = x.view(batch, self.cardinality, self.radix, -1 ).transpose(1, 2)
 x = F.softmax(x, dim=1)
 x = x.reshape(batch, -1)
 else:
x = torch.sigmoid(x)
 return x
predictions = np.argmax(test_predictions, axis=1)
 true = np.argmax(y_test, axis=1)
 labels = {
0:"carpet",
1:"concrete",
2:"fine_concrete",
3:"hard_tiles",
4:"hard_tiles_large_space",
5:"soft_pvc",
6:"soft_tiles",
7:"tiled",
8:"wood"
}
pre = []
for p in predictions:
pre.append(labels[p] )<save_to_csv>
class DropBlock2D(object):
def __init__(self, *args, **kwargs):
raise NotImplementedError
class GlobalAvgPool2d(nn.Module):
def __init__(self):

 super(GlobalAvgPool2d, self ).__init__()
def forward(self, inputs):
return nn.functional.adaptive_avg_pool2d(inputs, 1 ).view(inputs.size(0), -1)
 class Bottleneck(nn.Module):

 expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None,
radix=1, cardinality=1, bottleneck_width=64,
avd=False, avd_first=False, dilation=1, is_first=False,
rectified_conv=False, rectify_avg=False,
norm_layer=None, dropblock_prob=0.0, last_gamma=False):
super(Bottleneck, self ).__init__()
group_width = int(planes *(bottleneck_width / 64.))* cardinality
self.conv1 = nn.Conv2d(inplanes, group_width, kernel_size=1, bias=False)
 self.bn1 = norm_layer(group_width)
 self.dropblock_prob = dropblock_prob
self.radix = radix
self.avd = avd and(stride > 1 or is_first)
 self.avd_first = avd_first
if self.avd:
self.avd_layer = nn.AvgPool2d(3, stride, padding=1)
 stride = 1
if dropblock_prob > 0.0:
self.dropblock1 = DropBlock2D(dropblock_prob, 3)
 if radix == 1:
self.dropblock2 = DropBlock2D(dropblock_prob, 3)
 self.dropblock3 = DropBlock2D(dropblock_prob, 3)
 if radix >= 1:
self.conv2 = SplAtConv2d(
 group_width, group_width, kernel_size=3,
stride=stride, padding=dilation,
dilation=dilation, groups=cardinality, bias=False,
radix=radix, rectify=rectified_conv,
rectify_avg=rectify_avg,
norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 elif rectified_conv:
self.conv2 = RFConv2d(
 group_width, group_width, kernel_size=3, stride=stride,
padding=dilation, dilation=dilation,
groups=cardinality, bias=False,
average_mode=rectify_avg)
 self.bn2 = norm_layer(group_width)
 else:
self.conv2 = nn.Conv2d(
 group_width, group_width, kernel_size=3, stride=stride,
padding=dilation, dilation=dilation,
groups=cardinality, bias=False)
 self.bn2 = norm_layer(group_width)
 self.conv3 = nn.Conv2d(
 group_width, planes * 4, kernel_size=1, bias=False)
 self.bn3 = norm_layer(planes*4)
 if last_gamma:
zeros_(self.bn3.weight)
 self.relu = nn.ReLU(inplace=True)
 self.downsample = downsample
self.dilation = dilation
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
 out = self.bn1(out)
 if self.dropblock_prob > 0.0:
out = self.dropblock1(out)
 out = self.relu(out)
 if self.avd and self.avd_first:
out = self.avd_layer(out)
 out = self.conv2(out)
 if self.radix == 0:
out = self.bn2(out)
 if self.dropblock_prob > 0.0:
out = self.dropblock2(out)
 out = self.relu(out)
 if self.avd and not self.avd_first:
out = self.avd_layer(out)
 out = self.conv3(out)
 out = self.bn3(out)
 if self.dropblock_prob > 0.0:
out = self.dropblock3(out)
 if self.downsample is not None:
residual = self.downsample(x)
 out += residual
out = self.relu(out)
 return out
class ResNet(nn.Module):

 def __init__(self, block, layers, radix=1, groups=1, bottleneck_width=64,
num_classes=1000, dilated=False, dilation=1,
deep_stem=False, stem_width=64, avg_down=False,
rectified_conv=False, rectify_avg=False,
avd=False, avd_first=False,
final_drop=0.0, dropblock_prob=0,
last_gamma=False, norm_layer=nn.BatchNorm2d):
self.cardinality = groups
self.bottleneck_width = bottleneck_width
self.inplanes = stem_width*2 if deep_stem else 64
self.avg_down = avg_down
self.last_gamma = last_gamma
self.radix = radix
self.avd = avd
self.avd_first = avd_first
super(ResNet, self ).__init__()
self.rectified_conv = rectified_conv
self.rectify_avg = rectify_avg
if rectified_conv:
conv_layer = RFConv2d
else:
conv_layer = nn.Conv2d
conv_kwargs = {'average_mode': rectify_avg} if rectified_conv else {}
if deep_stem:
self.conv1 = nn.Sequential(
 conv_layer(3, stem_width, kernel_size=3, stride=2, padding=1, bias=False, **conv_kwargs),
norm_layer(stem_width),
nn.ReLU(inplace=True),
conv_layer(stem_width, stem_width, kernel_size=3, stride=1, padding=1, bias=False, **conv_kwargs),
norm_layer(stem_width),
nn.ReLU(inplace=True),
conv_layer(stem_width, stem_width*2, kernel_size=3, stride=1, padding=1, bias=False, **conv_kwargs), 
)
 else:
self.conv1 = conv_layer(3, 64, kernel_size=7, stride=2, padding=3,
bias=False, **conv_kwargs)
 self.bn1 = norm_layer(self.inplanes)
 self.relu = nn.ReLU(inplace=True)
 self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
 self.layer1 = self._make_layer(block, 64, layers[0], norm_layer=norm_layer, is_first=False)
 self.layer2 = self._make_layer(block, 128, layers[1], stride=2, norm_layer=norm_layer)
 if dilated or dilation == 4:
self.layer3 = self._make_layer(block, 256, layers[2], stride=1,
dilation=2, norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 self.layer4 = self._make_layer(block, 512, layers[3], stride=1,
dilation=4, norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 elif dilation==2:
self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
dilation=1, norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 self.layer4 = self._make_layer(block, 512, layers[3], stride=1,
dilation=2, norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 else:
self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 self.avgpool = GlobalAvgPool2d()
self.drop = nn.Dropout(final_drop)if final_drop > 0.0 else None
self.fc = nn.Linear(512 * block.expansion, num_classes)
 for m in self.modules() :
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2./ n))
elif isinstance(m, norm_layer):
m.weight.data.fill_(1)
 m.bias.data.zero_()
def _make_layer(self, block, planes, blocks, stride=1, dilation=1, norm_layer=None,
dropblock_prob=0.0, is_first=True):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
down_layers = []
if self.avg_down:
if dilation == 1:
down_layers.append(nn.AvgPool2d(kernel_size=stride, stride=stride,
ceil_mode=True, count_include_pad=False))
else:
down_layers.append(nn.AvgPool2d(kernel_size=1, stride=1,
ceil_mode=True, count_include_pad=False))
down_layers.append(nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=1, bias=False))
else:
down_layers.append(nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False))
down_layers.append(norm_layer(planes * block.expansion))
downsample = nn.Sequential(*down_layers)
 layers = []
if dilation == 1 or dilation == 2:
layers.append(block(self.inplanes, planes, stride, downsample=downsample,
radix=self.radix, cardinality=self.cardinality,
bottleneck_width=self.bottleneck_width,
avd=self.avd, avd_first=self.avd_first,
dilation=1, is_first=is_first, rectified_conv=self.rectified_conv,
rectify_avg=self.rectify_avg,
norm_layer=norm_layer, dropblock_prob=dropblock_prob,
last_gamma=self.last_gamma))
elif dilation == 4:
layers.append(block(self.inplanes, planes, stride, downsample=downsample,
radix=self.radix, cardinality=self.cardinality,
bottleneck_width=self.bottleneck_width,
avd=self.avd, avd_first=self.avd_first,
dilation=2, is_first=is_first, rectified_conv=self.rectified_conv,
rectify_avg=self.rectify_avg,
norm_layer=norm_layer, dropblock_prob=dropblock_prob,
last_gamma=self.last_gamma))
else:
raise RuntimeError("=> unknown dilation size: {}".format(dilation))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes,
radix=self.radix, cardinality=self.cardinality,
bottleneck_width=self.bottleneck_width,
avd=self.avd, avd_first=self.avd_first,
dilation=dilation, rectified_conv=self.rectified_conv,
rectify_avg=self.rectify_avg,
norm_layer=norm_layer, dropblock_prob=dropblock_prob,
last_gamma=self.last_gamma))
return nn.Sequential(*layers)
 def forward(self, x):
x = self.conv1(x)
 x = self.bn1(x)
 x = self.relu(x)
 x = self.maxpool(x)
 x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)
 x = self.avgpool(x)
 x = torch.flatten(x, 1)
 if self.drop:
x = self.drop(x)
 x = self.fc(x)
 return x
sub = pd.read_csv(".. /input/sample_submission.csv")
 sub["surface"] = np.array(pre)
 sub.to_csv("submission.csv", index=False )<set_options>
device = torch.device("cuda" if torch.cuda.is_available() else "cpu" )
warnings.filterwarnings('ignore')
<load_from_csv>
def to_numpy(tensor):
return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
train_features = pd.read_csv(data_dir + 'train_features.csv')
 train_targets_scored = pd.read_csv(data_dir + 'train_targets_scored.csv')
 train_targets_nonscored = pd.read_csv(data_dir + 'train_targets_nonscored.csv')
 train_drug = pd.read_csv(data_dir + 'train_drug.csv')
 test_features = pd.read_csv(data_dir + 'test_features.csv')
 sample_submission = pd.read_csv(data_dir + 'sample_submission.csv')
 print('train_features: {}'.format(train_features.shape))
print('train_targets_scored: {}'.format(train_targets_scored.shape))
print('train_targets_nonscored: {}'.format(train_targets_nonscored.shape))
print('train_drug: {}'.format(train_drug.shape))
print('test_features: {}'.format(test_features.shape))
print('sample_submission: {}'.format(sample_submission.shape))<install_modules>
def prediction_for_clip(test_df: pd.DataFrame,
clip: np.ndarray,
model,
model4,
model13,
model16,
model19,
model_inception,
model_inception2,
model_se,
model_mixup,
model_ls,
model_resnest,
mel_params: dict,
mel_params_resnest: dict,
mel_params_b1: dict,
mel_params_thefirst: dict,
mel_params_thesecond: dict,
mel_params_inception: dict,
mel_params_se: dict,
mel_params_mixup: dict,
mel_params_ls: dict,
threshold=0.5,
maxpreds=3, 
):

 dataset = TestDataset(df=test_df,
clip=clip,
img_size=300,
melspectrogram_parameters=mel_params,
imgsize_resnest=224,
resnest_melspec_params=mel_params_resnest,
img_size_b1=240, melspec_b1= mel_params_b1,
img_size_thefirst=260,melspec_thefirst=mel_params_thefirst,
img_size_thesecond=260,melspec_thesecond=mel_params_thesecond,
img_size_inception=299, melspec_inception=mel_params_inception,
img_size_se=224, melspec_se=mel_params_se,
img_size_mixup=260, melspec_mixup = mel_params_mixup,
img_size_ls=260, melspec_ls = mel_params_ls 
)
 loader = data.DataLoader(dataset, batch_size=1, shuffle=False)
 prediction_dict = {}
for image, row_id, site, image2, image3, image4, image5, image6, image7, image8, image9 in loader:
site = site[0]
row_id = row_id[0]
if site in {"site_1", "site_2"}:
with torch.no_grad() :
inps1 = {model.get_inputs() [0].name: to_numpy(image)}
inps2 = {model4.get_inputs() [0].name: to_numpy(image)}
inps3 = {model13.get_inputs() [0].name: to_numpy(image3)}
inps4 = {model16.get_inputs() [0].name: to_numpy(image4)}
inps5 = {model19.get_inputs() [0].name: to_numpy(image5)}
inps6 = {model_resnest.get_inputs() [0].name: to_numpy(image2)}
inps7 = {model_inception.get_inputs() [0].name: to_numpy(image6)}
inps8 = {model_inception2.get_inputs() [0].name: to_numpy(image6)}
inps9 = {model_se.get_inputs() [0].name: to_numpy(image7)}
inps10 = {model_mixup.get_inputs() [0].name: to_numpy(image8)}
inps11 = {model_ls.get_inputs() [0].name: to_numpy(image9)}
outs1 = model.run(None, inps1)
 outs2 = model4.run(None, inps2)
 outs3 = model13.run(None, inps3)
 outs4 = model16.run(None, inps4)
 outs5 = model19.run(None, inps5)
 outs6 = model_resnest.run(None, inps6)
 outs7 = model_inception.run(None, inps7)
 outs8 = model_inception2.run(None, inps8)
 outs9 = model_se.run(None, inps9)
 outs10 = model_mixup.run(None, inps10)
 outs11 = model_ls.run(None, inps11)
 outs1 = outs1[0][0]
outs2 = outs2[0][0]
outs3 = outs3[0][0]
outs4 = outs4[0][0]
outs5 = outs5[0][0]
outs6 = outs6[0][0]
outs7 = outs7[0][0]
outs8 = outs8[0][0]
outs9 = outs9[0][0]
outs10 = outs10[0][0]
outs11 = outs11[0][0]
outs1 = 1/(1 + np.exp(-outs1))
outs2 = 1/(1 + np.exp(-outs2))
outs3 = 1/(1 + np.exp(-outs3))
outs4 = 1/(1 + np.exp(-outs4))
outs5 = 1/(1 + np.exp(-outs5))
outs6 = 1/(1 + np.exp(-outs6))
outs7 = 1/(1 + np.exp(-outs7))
outs8 = 1/(1 + np.exp(-outs8))
outs9 = 1/(1 + np.exp(-outs9))
outs10 = 1/(1 + np.exp(-outs10))
outs11 = 1/(1 + np.exp(-outs11))
outs1 = outs1**2
outs2 = outs2**2
outs3 = outs3**2
outs4 = outs4**2
outs5 = outs5**2
outs6 = outs6**2
outs7 = outs7**2
outs8 = outs8**2
outs9 = outs9**2
outs10 = outs10**2
outs11 = outs11**2
proba = np.sqrt(( outs1+outs2+outs3+outs4+outs5+outs6+outs7+outs8+outs9+outs10+outs11)/11)
 events = proba >= threshold
labels = np.argsort(-proba)[:events.sum() ].tolist()
else:
image = image.squeeze(0)
 image2 = image2.squeeze(0)
 image3 = image3.squeeze(0)
 image4 = image4.squeeze(0)
 image5 = image5.squeeze(0)
 image6 = image6.squeeze(0)
 image7 = image7.squeeze(0)
 image8 = image8.squeeze(0)
 image9 = image9.squeeze(0)
 batch_size = 16
whole_size = image.size(0)
 if whole_size % batch_size == 0:
n_iter = whole_size // batch_size
else:
n_iter = whole_size // batch_size + 1
all_events = set()
probas = []
for batch_i in range(n_iter):
batch = image[batch_i * batch_size:(batch_i + 1)* batch_size]
batch2 = image2[batch_i * batch_size:(batch_i + 1)* batch_size]
batch3 = image3[batch_i * batch_size:(batch_i + 1)* batch_size]
batch4 = image4[batch_i * batch_size:(batch_i + 1)* batch_size]
batch5 = image5[batch_i * batch_size:(batch_i + 1)* batch_size]
batch6 = image6[batch_i * batch_size:(batch_i + 1)* batch_size]
batch7 = image7[batch_i * batch_size:(batch_i + 1)* batch_size]
batch8 = image8[batch_i * batch_size:(batch_i + 1)* batch_size]
batch9 = image9[batch_i * batch_size:(batch_i + 1)* batch_size]
if batch.ndim == 3:
batch = batch.unsqueeze(0)
 if batch2.ndim == 3:
batch2 = batch2.unsqueeze(0)
 if batch3.ndim == 3:
batch3 = batch3.unsqueeze(0)
 if batch4.ndim == 3:
batch4 = batch4.unsqueeze(0)
 if batch5.ndim == 3:
batch5 = batch5.unsqueeze(0)
 if batch6.ndim == 3:
batch6 = batch6.unsqueeze(0)
 if batch7.ndim == 3:
batch7 = batch7.unsqueeze(0)
 if batch8.ndim == 3:
batch8 = batch8.unsqueeze(0)
 if batch9.ndim == 3:
batch9 = batch9.unsqueeze(0)
 with torch.no_grad() :
inps1 = {model.get_inputs() [0].name: to_numpy(batch)}
inps2 = {model4.get_inputs() [0].name: to_numpy(batch)}
inps3 = {model13.get_inputs() [0].name: to_numpy(batch3)}
inps4 = {model16.get_inputs() [0].name: to_numpy(batch4)}
inps5 = {model19.get_inputs() [0].name: to_numpy(batch5)}
inps6 = {model_resnest.get_inputs() [0].name: to_numpy(batch2)}
inps7 = {model_inception.get_inputs() [0].name: to_numpy(batch6)}
inps8 = {model_inception2.get_inputs() [0].name: to_numpy(batch6)}
inps9 = {model_se.get_inputs() [0].name: to_numpy(batch7)}
inps10 = {model_mixup.get_inputs() [0].name: to_numpy(batch8)}
inps11 = {model_ls.get_inputs() [0].name: to_numpy(batch9)}
outs1 = model.run(None, inps1)
 outs2 = model4.run(None, inps2)
 outs3 = model13.run(None, inps3)
 outs4 = model16.run(None, inps4)
 outs5 = model19.run(None, inps5)
 outs6 = model_resnest.run(None, inps6)
 outs7 = model_inception.run(None, inps7)
 outs8 = model_inception2.run(None, inps8)
 outs9 = model_se.run(None, inps9)
 outs10 = model_mixup.run(None, inps10)
 outs11 = model_ls.run(None, inps11)
 del inps1,inps2,inps3,inps4,inps5,inps6,inps8,inps9,inps10,inps11
gc.collect()
outs1 = outs1[0]
outs2 = outs2[0]
outs3 = outs3[0]
outs4 = outs4[0]
outs5 = outs5[0]
outs6 = outs6[0]
outs7 = outs7[0]
outs8 = outs8[0]
outs9 = outs9[0]
outs10 = outs10[0]
outs11 = outs11[0]
outs1 = 1/(1 + np.exp(-outs1))
outs2 = 1/(1 + np.exp(-outs2))
outs3 = 1/(1 + np.exp(-outs3))
outs4 = 1/(1 + np.exp(-outs4))
outs5 = 1/(1 + np.exp(-outs5))
outs6 = 1/(1 + np.exp(-outs6))
outs7 = 1/(1 + np.exp(-outs7))
outs8 = 1/(1 + np.exp(-outs8))
outs9 = 1/(1 + np.exp(-outs9))
outs10 = 1/(1 + np.exp(-outs10))
outs11 = 1/(1 + np.exp(-outs11))
outs1 = outs1**2
outs2 = outs2**2
outs3 = outs3**2
outs4 = outs4**2
outs5 = outs5**2
outs6 = outs6**2
outs7 = outs7**2
outs8 = outs8**2
outs9 = outs9**2
outs10 = outs10**2
outs11 = outs11**2
probas.append(np.sqrt(( outs1+outs2+outs3+outs4+outs5+outs6+outs7+outs8+outs9+outs10+outs11)/11))
del outs1,outs2,outs3,outs4,outs5,outs8,outs9,outs10,outs11
gc.collect()
probas = np.vstack(probas)
 probas = probas.max(0)
 events =(probas>=threshold)
 labels = np.argsort(-probas)[:events.sum() ].tolist()
if len(labels)== 0:
prediction_dict[row_id] = "nocall"
else:
labels_str_list = list(map(lambda x: INV_BIRD_CODE[x], labels))
if site in('site_1','site_2'):
label_string = " ".join(labels_str_list[:maxpreds])
 else:
label_string = " ".join(labels_str_list)
 prediction_dict[row_id] = label_string
return prediction_dict
!pip install /kaggle/input/iterative-stratification/iterative-stratification-master/ 
<categorify>
def prediction(test_df: pd.DataFrame,
test_audio: Path,
model_config: dict,
mel_params: dict,
resnest_melparams: dict,
b1_melparams: dict,
thefirst_melparams: dict,
thesecond_melparams: dict,
inception_melparams: dict,
se_melparams: dict,
mixup_melparams: dict,
ls_melparams: dict,
target_sr: int,
threshold=0.5,
maxpreds = 3, 
):
model = onnxruntime.InferenceSession(".. /input/save-out-onnx-models/model.onnx")
 model5 = onnxruntime.InferenceSession(".. /input/save-out-onnx-models/model4.onnx")
 model14 = onnxruntime.InferenceSession(".. /input/save-out-onnx-models/model13.onnx")
 model17 = onnxruntime.InferenceSession(".. /input/save-out-onnx-models/model16.onnx")
 model20 = onnxruntime.InferenceSession(".. /input/save-out-onnx-models/model19.onnx")
 model_inception = onnxruntime.InferenceSession('.. /input/save-out-onnx-models/model_inception.onnx')
 model_inception2 = onnxruntime.InferenceSession('.. /input/save-out-onnx-models/model_inception2.onnx')
 model_se = onnxruntime.InferenceSession('.. /input/save-out-onnx-models/model_se.onnx')
 model_mixup = onnxruntime.InferenceSession('.. /input/save-out-onnx-models/model_mixup.onnx')
 model_ls = onnxruntime.InferenceSession('.. /input/save-out-onnx-models/model_ls.onnx')
 model4 = onnxruntime.InferenceSession(".. /input/save-out-onnx-models/model_resnest.onnx")
 unique_audio_id = test_df.audio_id.unique()
prediction_dfs = []
for audio_id in unique_audio_id:
clip, sr_native = librosa.core.audio.__audioread_load(test_audio /(audio_id + '.mp3'),
offset=0,
duration=None,
dtype=np.float32)
 clip = librosa.to_mono(clip)
 cpmp_sr = 32000
if sr_native > 0:
clip = librosa.resample(clip, sr_native, cpmp_sr, res_type='kaiser_fast')
 test_df_for_audio_id = test_df.query(
 f"audio_id == '{audio_id}'" ).reset_index(drop=True)
 prediction_dict = prediction_for_clip(test_df_for_audio_id,
clip=clip,
model=model,
model4=model5,
model13=model14,
model16=model17,
model19=model20,
model_inception=model_inception,
model_inception2=model_inception2,
model_se=model_se,
model_mixup=model_mixup,
model_ls=model_ls,
model_resnest=model4,
mel_params=mel_params,
mel_params_resnest=resnest_melparams,
mel_params_b1 = b1_melparams,
mel_params_thefirst = thefirst_melparams,
mel_params_thesecond= thesecond_melparams,
mel_params_inception=inception_melparams,
mel_params_se=se_melparams,
mel_params_mixup=mixup_melparams,
mel_params_ls=ls_melparams,
threshold=threshold,
maxpreds = maxpreds, 
)
 row_id = list(prediction_dict.keys())
 birds = list(prediction_dict.values())
 prediction_df = pd.DataFrame({
"row_id": row_id,
"birds": birds
})
 prediction_dfs.append(prediction_df)
 prediction_df = pd.concat(prediction_dfs, axis=0, sort=False ).reset_index(drop=True)
 return prediction_df
<set_options><EOS> 
submission = prediction(test_df=test,
test_audio=TEST_AUDIO_DIR,
model_config = model_config,
mel_params=settings['dataset']['params']['melspectrogram_parameters'],
resnest_melparams = melspectrogram_parameters,
b1_melparams = settings_effnetb1['dataset']['params']['melspectrogram_parameters'],
thefirst_melparams = settings_thefirst['dataset']['params']['melspectrogram_parameters'],
thesecond_melparams = settings_thesecond['dataset']['params']['melspectrogram_parameters'],
inception_melparams = settings_inception['dataset']['params']['melspectrogram_parameters'],
se_melparams = settings_se['dataset']['params']['melspectrogram_parameters'],
mixup_melparams=settings_mixup['dataset']['params']['melspectrogram_parameters'],
ls_melparams=settings_ls['dataset']['params']['melspectrogram_parameters'],
target_sr=TARGET_SR,
threshold=0.33,
maxpreds=3, 
)
 submission.to_csv("submission.csv", index=False )