Kaggle competition: Cornell Birdcall Identification
<SOS> metric: MeanFScoreBeta Kaggle data source: cornell-birdcall-identification<save_to_csv>
!pip install librosa
sample_submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 sample_submission.set_index('sig_id',inplace=True)
 test_features.set_index('sig_id',inplace=True)
 test_features = test_features.loc[sample_submission.index]
sub = sample_submission.drop(columns=target_cols ).merge(test[target_cols], on='sig_id', how='left' ).fillna(0)
 sub.loc[test_features['cp_type']=='ctl_vehicle', target_cols] =0
sub.to_csv('./submission.csv', index=True)
 sub.head()<filter>
import numpy as np
import pandas as pd
import os
s = sub.loc[test_features['cp_type']=='ctl_vehicle', target_cols].sum().sum()
print('Sub control rows =',s )<install_modules>
audio_data = '/kaggle/input/birdsong-recognition/train_audio/nutwoo/XC462016.mp3'
x , sr = librosa.load(audio_data)
 print(type(x), type(sr))
print(x.shape, sr )
!pip install --no-index --find-links.. /input/pytorchtabnet/pytorch_tabnet-2.0.0-py3-none-any.whl pytorch-tabnet<set_options>
librosa.load(audio_data, sr=44100 )
sys.path.append('.. /input/iterativestratification')
 warnings.filterwarnings('ignore')
 pd.set_option('max_columns', 2000)
 def sigmoid(x):
return 1 /(1 + np.exp(-x))
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything(seed=42)
 def prep_data(n_comp_GENES, n_comp_CELLS, VarianceThreshold_for_FS):
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets_scored = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 train_targets_nonscored = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 Xtrain = pd.get_dummies(train_features.drop('sig_id',axis=1), columns = ['cp_dose'],drop_first = True)
 Xtest = pd.get_dummies(test_features.drop('sig_id',axis=1), columns = ['cp_dose'],drop_first = True)
 target= train_targets_scored.drop('sig_id',axis=1)
 GENES = [col for col in train_features.columns if col.startswith('g-')]
CELLS = [col for col in train_features.columns if col.startswith('c-')]
Xtrain = pd.concat([Xtrain[GENES], Xtrain[CELLS], Xtrain.drop(GENES+CELLS,axis=1)],axis=1)
 Xtest = pd.concat([Xtest[GENES], Xtest[CELLS], Xtest.drop(GENES+CELLS,axis=1)],axis=1)
 for col in tqdm(( GENES + CELLS)) :
transformer = QuantileTransformer(n_quantiles=100,random_state=0, output_distribution="normal")
 Xtrain[col] = transformer.fit_transform(Xtrain[[col]])
 Xtest[col] = transformer.transform(Xtest[[col]])
 data = pd.concat([Xtrain[GENES], Xtest[GENES]])
 data2 = PCA(n_components=n_comp_GENES, random_state=42 ).fit_transform(data)
 Xtrain2 = pd.DataFrame(data2[:train_features.shape[0]], columns = [f'pca_G-{i}' for i in range(n_comp_GENES)])
 Xtest2 = pd.DataFrame(data2[train_features.shape[0]:], columns = [f'pca_G-{i}' for i in range(n_comp_GENES)])
 data = pd.concat([Xtrain[CELLS], Xtest[CELLS]])
 data2 = PCA(n_components=n_comp_CELLS, random_state=42 ).fit_transform(data)
 Xtrain3 = pd.DataFrame(data2[:train_features.shape[0]], columns = [f'pca_C-{i}' for i in range(n_comp_CELLS)])
 Xtest3 = pd.DataFrame(data2[train_features.shape[0]:], columns = [f'pca_C-{i}' for i in range(n_comp_CELLS)])
 Xtrain_tot = pd.concat(( Xtrain, Xtrain2, Xtrain3), axis=1)
 Xtest_tot = pd.concat(( Xtest, Xtest2, Xtest3), axis=1)
 var_thresh = VarianceThreshold(VarianceThreshold_for_FS)
 data = Xtrain_tot.append(Xtest_tot)
 data = data.iloc[:, 2:]
data_transformed = var_thresh.fit_transform(data.drop('cp_type',axis=1))
var = var_thresh.variances_
Xtrain_transformed = pd.DataFrame(data_transformed[ : len(Xtrain)], columns = data.drop('cp_type',axis=1 ).columns[var>VarianceThreshold_for_FS])
 Xtest_transformed = pd.DataFrame(data_transformed[len(Xtrain): ], columns = data.drop('cp_type',axis=1 ).columns[var>VarianceThreshold_for_FS])
 Xtrain_transformed['cp_type'] = Xtrain['cp_type']
Xtest_transformed['cp_type'] = Xtest['cp_type']
Xtrain_transformed['cp_time'] = Xtrain['cp_time']
Xtest_transformed['cp_time'] = Xtest['cp_time']
Xtrain_transformed['cp_dose_D2'] = Xtrain['cp_dose_D2']
Xtest_transformed['cp_dose_D2'] = Xtest['cp_dose_D2']
return Xtrain, Xtest, Xtrain_transformed, Xtest_transformed, target
class SmoothBCEwLogits(_WeightedLoss):
def __init__(self, weight=None, reduction='mean', smoothing=0.0):
super().__init__(weight=weight, reduction=reduction)
 self.smoothing = smoothing
self.weight = weight
self.reduction = reduction
@staticmethod
def _smooth(targets:torch.Tensor, n_labels:int, smoothing=0.0):
assert 0 <= smoothing < 1
with torch.no_grad() :
targets = targets *(1.0 - smoothing)+ 0.5 * smoothing
return targets
def forward(self, inputs, targets):
targets = SmoothBCEwLogits._smooth(targets, inputs.size(-1),
self.smoothing)
 loss = F.binary_cross_entropy_with_logits(inputs, targets,self.weight)
 if self.reduction == 'sum':
loss = loss.sum()
elif self.reduction == 'mean':
loss = loss.mean()
return loss
class LabelSmoothingLoss(nn.Module):
def __init__(self, classes, smoothing=0.0, dim=-1):
super(LabelSmoothingLoss, self ).__init__()
self.confidence = 1.0 - smoothing
self.smoothing = smoothing
self.cls = classes
self.dim = dim
def forward(self, pred, target):
pred = pred.log_softmax(dim=self.dim)
 with torch.no_grad() :
true_dist = torch.zeros_like(pred)
 true_dist.fill_(self.smoothing /(self.cls - 1))
true_dist.scatter_(1, target.data.unsqueeze(1), self.confidence)
 return torch.mean(torch.sum(-true_dist * pred, dim=self.dim))
def score(y, yp):
return - np.mean(y*np.log(yp+10**(-8)) +(1-y)*np.log(1-yp+10**(-8)))
 def run_k_fold(folds, seed, Xtrain, Xtest, target, train):
oof = []
predictions = []
models = []
for fold in range(len(np.unique(folds))):
oof_, pred_ , model = run_training(folds, fold, seed, Xtrain, Xtest, target, train)
 predictions.append(pred_)
 oof.append(oof_)
 oof = pd.concat(oof)
 predictions = np.mean(predictions,axis=0)
 return oof, predictions, models
def run_training(fold, seed, Xtrain, Xtest, target, tabnet_params):
seed_everything(seed)
 num_features=Xtrain.shape[1]-1
num_targets=target.shape[1]
x_train = Xtrain[folds != fold].drop('cp_type',axis=1)
 x_valid = Xtrain[(folds == fold)&(Xtrain.cp_type == 'trt_cp')].drop('cp_type',axis=1)
 y_train = target.loc[x_train.index]
y_valid = target.loc[x_valid.index]
iddx = y_valid.index
x_train, x_valid, y_train, y_valid = x_train.values, x_valid.values, y_train.values, y_valid.values
model = TabNetRegressor(**tabnet_params)
 model.fit(X_train=x_train,
y_train=y_train,
eval_set=[(x_valid, y_valid)],
eval_name = ["val"],
eval_metric = ["logits_ll"],
max_epochs=EPOCHS,
patience=20, batch_size=BATCHSIZE, virtual_batch_size=GBATCHSIZE,
num_workers=1, drop_last=False,
loss_fn=SmoothBCEwLogits(smoothing =0.001))
x_test = Xtest.drop('cp_type',axis=1 ).values
oof = model.predict(x_valid)
 oof = pd.DataFrame(oof, index = iddx)
 predictions = model.predict(x_test)
 return oof, predictions
def run_k_fold(NFOLDS, seed, Xtrain, Xtest, target, tabnet_params):
oof = []
predictions = []
for fold in np.unique(NFOLDS):
oof_, pred_ = run_training(fold, seed, Xtrain, Xtest, target, tabnet_params)
 predictions.append(pred_)
 oof.append(oof_)
 oof = pd.concat(oof ).sort_index()
predictions = np.mean(predictions,axis=0)
 return oof, predictions
def make_all_preds(Xtrain_transformed, Xtest_transformed, target, folds, train):
oof, predictions = run_k_fold(folds, seed, Xtrain_transformed, Xtest_transformed, target, train)
 return oof.sort_index() , predictions
class LogitsLogLoss(Metric):

 def __init__(self):
self._name = "logits_ll"
self._maximize = False
def __call__(self, y_true, y_pred):

 targets = y_true *(1.0 - 1e-4)+ 0.5 * 1e-4
logits = 1 /(1 + np.exp(-y_pred))
aux =(1 - y_true)* np.log(1 - logits + 1e-15)+ y_true * np.log(logits + 1e-15)
 return np.mean(-aux )<init_hyperparams>
ipd.Audio(audio_data )
n_comp_GENES = 450
n_comp_CELLS = 65
VarianceThreshold_for_FS = 0.9
n_d = 32
n_a = 256
n_steps = 1
gamma = 1.5
lambda_sparse = 1e-7
LEARNING_RATE = 8e-3
wd = 1e-5
div_factor = 10
EPOCHS = 500
BATCHSIZE = 248
GBATCHSIZE = 64
Xtrain, Xtest, Xtrain_transformed, Xtest_transformed, target = prep_data(n_comp_GENES, n_comp_CELLS, VarianceThreshold_for_FS)
 BATCHES = int(len(Xtrain)/BATCHSIZE)
 SEED = 0
n_independent= 1
n_shared = 0
GROUP = 'tabnet'
tabnet_params = dict(
 n_d=n_d,
n_a=n_a,
n_steps=n_steps,
n_independent = n_independent,
gamma=gamma,
lambda_sparse=lambda_sparse,
n_shared = n_shared,
optimizer_fn=torch.optim.Adam,
optimizer_params=dict(lr=LEARNING_RATE, weight_decay=wd),
mask_type='entmax',
scheduler_params=dict(mode = "min", patience = 7, min_lr = 1e-5, factor = 0.07),
scheduler_fn=optim.lr_scheduler.ReduceLROnPlateau,
verbose=10,
)<find_best_params>
%matplotlib inline
plt.figure(figsize=(14, 5))
librosa.display.waveplot(x, sr=sr )
preds_tabnet = []
GROUP = 'tabnet_4'
seed = 42
hidden_size = 0
NFOLDS = 5
for SEED in [1,2,3,4,5]:
folds = pd.Series(np.zeros(len(Xtrain)) , index = Xtrain.index)
 mskf = MultilabelStratifiedKFold(n_splits=NFOLDS)
 for f,(t_idx, v_idx)in enumerate(mskf.split(X=Xtrain, y=target)) :
folds.loc[v_idx] = int(f)
 preds = np.zeros(( Xtest.shape[0], target.shape[1]))
oof, preds = make_all_preds(Xtrain, Xtest, target, folds, train = tabnet_params)
 preds_tabnet.append(sigmoid(preds))<prepare_output>
sr = 22050
T = 5.0
t = np.linspace(0, T, int(T*sr), endpoint=False)
 x = 0.5*np.sin(2*np.pi*220*t)
 ipd.Audio(x, rate=sr)
 librosa.output.write_wav('tone_220.wav', x, sr )
preds_tabnet = np.mean(preds_tabnet,axis=0 )<save_to_csv>
import cv2
import audioread
import logging
import os
import random
import time
import warnings
import librosa
import numpy as np
import pandas as pd
import soundfile as sf
import torch
import torch.nn as nn
import torch.cuda
import torch.nn.functional as F
import torch.utils.data as data
from contextlib import contextmanager
from pathlib import Path
from typing import Optional
from fastprogress import progress_bar
from sklearn.metrics import f1_score
from torchvision import models
test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 submit = pd.DataFrame(preds_tabnet, columns = target.columns, index = Xtest.index)
 submit.loc[Xtest.cp_type!='trt_cp'] = 0
submit.index = test_features.sig_id
submit = submit.reset_index()
submit.to_csv('submission.csv', index=False )<set_options>
def set_seed(seed: int = 42):
random.seed(seed)
 np.random.seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
def get_logger(out_file=None):
logger = logging.getLogger()
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
 logger.handlers = []
logger.setLevel(logging.INFO)
 handler = logging.StreamHandler()
handler.setFormatter(formatter)
 handler.setLevel(logging.INFO)
 logger.addHandler(handler)
 if out_file is not None:
fh = logging.FileHandler(out_file)
 fh.setFormatter(formatter)
 fh.setLevel(logging.INFO)
 logger.addHandler(fh)
 logger.info("logger set up")
 return logger
@contextmanager
def timer(name: str, logger: Optional[logging.Logger] = None):
t0 = time.time()
msg = f"[{name}] start"
if logger is None:
print(msg)
 else:
logger.info(msg)
 yield
msg = f"[{name}] done in {time.time() - t0:.2f} s"
if logger is None:
print(msg)
 else:
logger.info(msg )
warnings.filterwarnings('ignore' )<load_from_csv>
logger = get_logger("main.log")
 set_seed(1213 )
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))<create_dataframe>
TARGET_SR = 32000
TEST = Path(".. /input/birdsong-recognition/test_audio" ).exists()
train_features2=train_features.copy()
test_features2=test_features.copy()<install_modules>
if TEST:
DATA_DIR = Path(".. /input/birdsong-recognition/")
 else:
DATA_DIR = Path(".. /input/birdsong-recognition")
 test = pd.read_csv(DATA_DIR / "test.csv")
 test_audio = DATA_DIR / "test_audio"
test.head()
!pip install /kaggle/input/iterative-stratification/iterative-stratification-master/ 
<create_dataframe>
sub = pd.read_csv(".. /input/birdsong-recognition/sample_submission.csv")
 sub.to_csv("submission.csv", index=False )
qt = QuantileTransformer(n_quantiles=100,random_state=42,output_distribution='normal')
 data = pd.concat([pd.DataFrame(train_features[GENES+CELLS]), pd.DataFrame(test_features[GENES+CELLS])])
 data2 = qt.fit_transform(data[GENES+CELLS])
 train_features[GENES+CELLS] = pd.DataFrame(data2[:train_features.shape[0]])
 test_features[GENES+CELLS] = pd.DataFrame(data2[-test_features.shape[0]:] )<set_options>
class ResNet(nn.Module):
def __init__(self, base_model_name: str, pretrained=False,
num_classes=264):
super().__init__()
base_model = models.__getattribute__(base_model_name )(
 pretrained=pretrained)
 layers = list(base_model.children())[:-2]
layers.append(nn.AdaptiveMaxPool2d(1))
self.encoder = nn.Sequential(*layers)
 in_features = base_model.fc.in_features
self.classifier = nn.Sequential(
 nn.Linear(in_features, 1024), nn.ReLU() , nn.Dropout(p=0.2),
nn.Linear(1024, 1024), nn.ReLU() , nn.Dropout(p=0.2),
nn.Linear(1024, num_classes))
def forward(self, x):
batch_size = x.size(0)
 x = self.encoder(x ).view(batch_size, -1)
 x = self.classifier(x)
 multiclass_proba = F.softmax(x, dim=1)
 multilabel_proba = F.sigmoid(x)
 return {
"logits": x,
"multiclass_proba": multiclass_proba,
"multilabel_proba": multilabel_proba
}
SEED_VALUE = 42
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything(seed=SEED_VALUE )<create_dataframe>
model_config = {
"base_model_name": "resnet50",
"pretrained": False,
"num_classes": 264
}
melspectrogram_parameters = {
"n_mels": 128,
"fmin": 20,
"fmax": 16000
}
weights_path = ".. /input/birdcall-resnet50-init-weights/best.pth"
n_comp = 600
data = pd.concat([pd.DataFrame(train_features[GENES]), pd.DataFrame(test_features[GENES])])
 data2 =(PCA(n_components=n_comp, random_state=42 ).fit_transform(data[GENES]))
train2 = data2[:train_features.shape[0]];
test2 = data2[-test_features.shape[0]:]
train_gpca = pd.DataFrame(train2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 test_gpca = pd.DataFrame(test2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 train_features = pd.concat(( train_features, train_gpca), axis=1)
 test_features = pd.concat(( test_features, test_gpca), axis=1 )<create_dataframe>
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() }
n_comp = 50
data = pd.concat([pd.DataFrame(train_features[CELLS]), pd.DataFrame(test_features[CELLS])])
 data2 =(PCA(n_components=n_comp, random_state=42 ).fit_transform(data[CELLS]))
train2 = data2[:train_features.shape[0]]; test2 = data2[-test_features.shape[0]:]
train_cpca = pd.DataFrame(train2, columns=[f'pca_C-{i}' for i in range(n_comp)])
 test_cpca = pd.DataFrame(test2, columns=[f'pca_C-{i}' for i in range(n_comp)])
 train_features = pd.concat(( train_features, train_cpca), axis=1)
 test_features = pd.concat(( test_features, test_cpca), axis=1)
<create_dataframe>
def get_model(config: dict, weights_path: str):
model = ResNet(**config)
 checkpoint = torch.load(weights_path)
 model.load_state_dict(checkpoint["model_state_dict"])
 device = torch.device("cuda")
 model.to(device)
 model.eval()
return model
var_thresh = VarianceThreshold(0.85)
 data = train_features.append(test_features)
 data_transformed = var_thresh.fit_transform(data.iloc[:, 4:])
 train_features_transformed = data_transformed[ : train_features.shape[0]]
test_features_transformed = data_transformed[-test_features.shape[0] : ]
train_features = pd.DataFrame(train_features[['sig_id','cp_type','cp_time','cp_dose']].values.reshape(-1, 4),\
columns=['sig_id','cp_type','cp_time','cp_dose'])
 train_features = pd.concat([train_features, pd.DataFrame(train_features_transformed)], axis=1)
 test_features = pd.DataFrame(test_features[['sig_id','cp_type','cp_time','cp_dose']].values.reshape(-1, 4),\
columns=['sig_id','cp_type','cp_time','cp_dose'])
 test_features = pd.concat([test_features, pd.DataFrame(test_features_transformed)], axis=1 )<concatenate>
def prediction_for_clip(test_df: pd.DataFrame,
clip: np.ndarray,
model: ResNet,
mel_params: dict,
threshold=0.55):
dataset = TestDataset(df=test_df,
clip=clip,
img_size=224,
melspectrogram_parameters=mel_params)
 loader = data.DataLoader(dataset, batch_size=1, shuffle=False)
 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 model.eval()
prediction_dict = {}
for image, row_id, site in progress_bar(loader):
site = site[0]
row_id = row_id[0]
if site in {"site_1", "site_2"}:
image = image.to(device)
 with torch.no_grad() :
prediction = model(image)
 proba = prediction["multilabel_proba"].detach().cpu().numpy().reshape(-1)
 events = proba >= threshold
labels = np.argwhere(events ).reshape(-1 ).tolist()
else:
image = image.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()
for batch_i in range(n_iter):
batch = image[batch_i * batch_size:(batch_i + 1)* batch_size]
if batch.ndim == 3:
batch = batch.unsqueeze(0)
 batch = batch.to(device)
 with torch.no_grad() :
prediction = model(batch)
 proba = prediction["multilabel_proba"].detach().cpu().numpy()
events = proba >= threshold
for i in range(len(events)) :
event = events[i, :]
labels = np.argwhere(event ).reshape(-1 ).tolist()
for label in labels:
all_events.add(label)
 labels = list(all_events)
 if len(labels)== 0:
prediction_dict[row_id] = "nocall"
else:
labels_str_list = list(map(lambda x: INV_BIRD_CODE[x], labels))
label_string = " ".join(labels_str_list)
 prediction_dict[row_id] = label_string
return prediction_dict
def fe_cluster(train, test, n_clusters_g = 22, n_clusters_c = 4, SEED = 42):
features_g = GENES
features_c = CELLS
def create_cluster(train, test, features, kind = 'g', n_clusters = n_clusters_g):
train_ = train[features].copy()
test_ = test[features].copy()
data = pd.concat([train_, test_], axis = 0)
 kmeans = KMeans(n_clusters = n_clusters, random_state = SEED ).fit(data)
 train[f'clusters_{kind}'] = kmeans.labels_[:train.shape[0]]
test[f'clusters_{kind}'] = kmeans.labels_[train.shape[0]:]
train = pd.get_dummies(train, columns = [f'clusters_{kind}'])
 test = pd.get_dummies(test, columns = [f'clusters_{kind}'])
 return train, test
train, test = create_cluster(train, test, features_g, kind = 'g', n_clusters = n_clusters_g)
 train, test = create_cluster(train, test, features_c, kind = 'c', n_clusters = n_clusters_c)
 return train, test
train_features2 ,test_features2=fe_cluster(train_features2,test_features2 )<concatenate>
def prediction(test_df: pd.DataFrame,
test_audio: Path,
model_config: dict,
mel_params: dict,
weights_path: str,
threshold=0.5):
model = get_model(model_config, weights_path)
 unique_audio_id = test_df.audio_id.unique()
warnings.filterwarnings("ignore")
 prediction_dfs = []
for audio_id in unique_audio_id:
with timer(f"Loading {audio_id}", logger):
clip, _ = librosa.load(test_audio /(audio_id + ".mp3"),
sr=TARGET_SR,
mono=True,
res_type="kaiser_fast")
 test_df_for_audio_id = test_df.query(
 f"audio_id == '{audio_id}'" ).reset_index(drop=True)
 with timer(f"Prediction on {audio_id}", logger):
prediction_dict = prediction_for_clip(test_df_for_audio_id,
clip=clip,
model=model,
mel_params=mel_params,
threshold=threshold)
 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
<categorify><EOS> 
submission = prediction(test_df=test,
test_audio=test_audio,
model_config=model_config,
mel_params=melspectrogram_parameters,
weights_path=weights_path,
threshold=0.85)
 submission.to_csv("submission.csv", index=False )