Kaggle competition: YKC-2nd
def sigmoid_mse_loss(input_logits, target_logits):
assert input_logits.size() == target_logits.size()
input_softmax = torch.sigmoid(input_logits)
 target_softmax = torch.sigmoid(target_logits)
 num_classes = input_logits.size() [1]
return F.mse_loss(input_softmax, target_softmax, size_average=False 
)/ num_classes
class MeanTeacherLoss(nn.Module):
def __init__(self):
super().__init__()
self.positive_weight = torch.ones(
 NUM_CLASSES ).to(config.device)* config.positive_weight
self.class_criterion = nn.BCEWithLogitsLoss(
 reduction='none', pos_weight=self.positive_weight)
 self.consistency_criterion = sigmoid_mse_loss
def make_safe(self, pred):
pred = torch.where(torch.isnan(pred), torch.zeros_like(pred), pred)
 return torch.where(torch.isinf(pred), torch.zeros_like(pred), pred)
 def get_consistency_weight(self, epoch):
return config.consistency_weight * sigmoid_rampup(
 epoch, config.consistency_rampup)
 def forward(self, student_pred, teacher_pred, target, classif_weights, epoch):
student_pred = self.make_safe(student_pred)
 teacher_pred = self.make_safe(teacher_pred ).detach().data
batch_size = len(target)
 labeled_batch_size = target.ne(NO_LABEL ).all(axis=1 ).sum().item() + 1e-3
student_classif, student_consistency = student_pred, student_pred
student_class_loss =(self.class_criterion(
 student_classif, target)* classif_weights / labeled_batch_size ).sum()
consistency_weights = self.get_consistency_weight(epoch)
 consistency_loss = consistency_weights * self.consistency_criterion(
 student_consistency, teacher_pred)/ batch_size
loss = student_class_loss + consistency_loss
return loss, student_class_loss, consistency_loss, consistency_weights<create_dataframe>
train = pd.read_csv(".. /input/ykc-cup-2nd/train.csv")
 test = pd.read_csv(".. /input/ykc-cup-2nd/test.csv")
 sub = pd.read_csv(".. /input/ykc-cup-2nd/sample_submission.csv")
 train.shape, test.shape, sub.shape
class MeanTeacherDataset(Dataset):
def __init__(self, df, transforms, period=5,
data_path=".. /input/rfcx-species-audio-detection/train",
val=False, percent_unlabeled=0.0):
self.period = period
self.transforms = transforms
self.data_path = data_path
self.val = val
self.percent_unlabeled = percent_unlabeled
dfgby = df.groupby("recording_id" ).agg(lambda x: list(x)).reset_index()
self.recording_ids = dfgby["recording_id"].values
self.species_ids = dfgby["species_id"].values
self.t_mins = dfgby["t_min"].values
self.t_maxs = dfgby["t_max"].values
def __len__(self):
return int(len(self.recording_ids)*(1 + self.percent_unlabeled))
def __getitem__(self, idx):
if idx >= len(self.recording_ids):
audio, label, rec_id, sr = self.get_unlabeled_item(idx)
 classif_weights = np.zeros(NUM_CLASSES, dtype='f')
 else:
audio, label, rec_id, sr = self.get_labeled_item(idx)
 classif_weights = np.ones(NUM_CLASSES, dtype='f')
 audio_teacher = np.copy(audio)
 audio = self.transforms(samples=audio, sample_rate=sr)
 audio_teacher = self.transforms(samples=audio_teacher, sample_rate=sr)
 return {
"waveform": audio,
"teacher_waveform": audio_teacher,
"target": torch.tensor(label, dtype=torch.float),
"classification_weights": classif_weights,
"id": rec_id
}
def get_labeled_item(self, idx):
recording_id = self.recording_ids[idx]
species_id = self.species_ids[idx]
t_min, t_max = self.t_mins[idx], self.t_maxs[idx]
rec, sr = sf.read(f"{self.data_path}/{recording_id}.flac")
 len_rec = len(rec)
 effective_length = sr * self.period
rint = np.random.randint(len(t_min))
tmin, tmax = round(sr * t_min[rint]), round(sr * t_max[rint])
 dur = tmax - tmin
min_dur = min(dur, round(sr * self.period))
center = round(( tmin + tmax)/ 2)
 rand_start = center - effective_length + max(min_dur - dur//2, 0)
 if rand_start < 0:
rand_start = 0
rand_end = center - max(min_dur - dur//2, 0)
 start = np.random.randint(rand_start, rand_end)
 rec = rec[start:start + effective_length]
if len(rec)< effective_length:
new_rec = np.zeros(effective_length, dtype=rec.dtype)
 start1 = np.random.randint(effective_length - len(rec))
new_rec[start1:start1 + len(rec)] = rec
rec = new_rec.astype(np.float32)
 else:
rec = rec.astype(np.float32)
 start_time = start / sr
end_time =(start + effective_length)/ sr
label = np.zeros(NUM_CLASSES, dtype='f')
 for i in range(len(t_min)) :
if(t_min[i] >= start_time)&(t_max[i] <= end_time):
label[species_id[i]] = 1
elif start_time <=(( t_min[i] + t_max[i])/ 2)<= end_time:
label[species_id[i]] = 1
return rec, label, recording_id, sr
def get_unlabeled_item(self, idx, random_sample=False):
real_idx = idx - len(self.recording_ids)
 if self.val:
rec_id = self.recording_ids[real_idx]
else:
rec_id = random.sample(list(self.recording_ids), 1)[0]
rec, sr = sf.read(f"{self.data_path}/{rec_id}.flac")
 effective_length = int(sr * self.period)
 max_end = len(rec)- effective_length
if self.val:
start = int(idx * 16963 % max_end)
 else:
start = np.random.randint(0, max_end)
 rec = rec[start:(start+effective_length)]
rec = rec.astype(np.float32)
 label = np.ones(NUM_CLASSES, dtype='f')* NO_LABEL
return rec, label, rec_id, sr
def get_data_loader(df, is_val=False):
dataset = MeanTeacherDataset(
 df=df,
transforms=config.augmenter,
period=config.period,
percent_unlabeled=config.percent_unlabeled 
)
 return torch.utils.data.DataLoader(
 dataset,
batch_size=config.batch_size,
shuffle=not is_val,
drop_last=not is_val,
num_workers=config.num_workers
)<train_on_grid>
df = pd.concat([train, test])
 df = df.reset_index(drop=True)
 df.shape
def update_teacher_params(student, teacher, alpha, global_step):
alpha = min(1 - 1 /(global_step + 1), alpha)
 for ema_param, param in zip(teacher.parameters() , student.parameters()):
ema_param.data.mul_(alpha ).add_(1 - alpha, param.data)
 def train_one_epoch(student, mean_teacher, loader,
criterion, optimizer, scheduler, epoch, is_val=False):
global_step = 0
losses = AverageMeter()
consistency_loss_avg = AverageMeter()
class_loss_avg = AverageMeter()
comp_metric = MetricMeter()
if is_val:
student.eval()
mean_teacher.eval()
context = torch.no_grad()
else:
student.train()
mean_teacher.train()
context = nullcontext()
with context:
t = tqdm(loader)
 for i, sample in enumerate(t):
student_input = sample['waveform'].to(config.device)
 teacher_input = sample['teacher_waveform'].to(config.device)
 target = sample['target'].to(config.device)
 classif_weights = sample['classification_weights'].to(config.device)
 batch_size = len(target)
 student_pred, _ = student(student_input)
 teacher_pred, _ = mean_teacher(teacher_input)
 loss, class_loss, consistency_loss, consistency_weight = criterion(
 student_pred, teacher_pred, target, classif_weights, epoch)
 if not is_val:
optimizer.zero_grad()
loss.backward()
optimizer.step()
update_teacher_params(student, mean_teacher,
config.ema_decay, global_step)
 scheduler.step()
comp_metric.update(target, student_pred)
 losses.update(loss.item() , batch_size)
 consistency_loss_avg.update(consistency_loss.item() , batch_size)
 class_loss_avg.update(class_loss.item() , batch_size)
 global_step += 1
t.set_description(f"Epoch:{epoch} - Loss:{losses.avg:0.4f}")
 t.close()
return {'lwlrap':comp_metric.avg,
'loss':losses.avg,
'consistency_loss':consistency_loss_avg.avg,
'class_loss':class_loss_avg.avg,
'consistency_weight':consistency_weight}<choose_model_class>
target = "department_id"
n_split = 5
kfold = StratifiedKFold(n_splits=n_split, shuffle = True, random_state=42 )
def train(df, fold):
train_df = df[df.fold != fold]
val_df = df[df.fold == fold]
train_loader = get_data_loader(train_df)
 val_loader = get_data_loader(val_df)
 student_model = get_model()
teacher_model = get_model(is_mean_teacher=True)
 optimizer = torch.optim.AdamW(student_model.parameters() , lr=config.lr)
 num_train_steps = int(len(train_loader)* config.epochs)
 scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
 optimizer, T_max=num_train_steps)
 criterion = MeanTeacherLoss()
best_val_metric = -np.inf
val_metrics = []
train_metrics = []
for epoch in range(0, config.epochs):
train_loss_metrics = train_one_epoch(
 student_model, teacher_model, train_loader,
criterion, optimizer, scheduler, epoch)
 val_loss_metrics = train_one_epoch(
 student_model, teacher_model, val_loader,
criterion, optimizer, scheduler, epoch, is_val=True)
 train_metrics.append(train_loss_metrics)
 val_metrics.append(val_loss_metrics)
 pretty_print_metrics(fold, epoch, optimizer,
train_loss_metrics, val_loss_metrics)
 if val_loss_metrics['lwlrap'] > best_val_metric:
print(f" LWLRAP Improved from {best_val_metric} --> {val_loss_metrics['lwlrap']}
")
 torch.save(teacher_model.state_dict() ,
os.path.join(config.save_path, f'fold-{fold}.bin'))
best_val_metric = val_loss_metrics['lwlrap']
df = get_n_fold_df(config.train_tp_csv)
 for fold in range(5 if config.train_5_folds else 1):
train(df, fold )<create_dataframe>
df["product_name"] = df["product_name"].apply(lambda words : words.lower().replace(",", "" ).replace("&", "" ).split(" "))
df.head()
def test(test_df, train_fold):
test_dataset = TestDataset(
 df=test_df,
data_path=".. /input/rfcx-species-audio-detection/test",
period=config.period,
step=config.step 
)
 test_loader = torch.utils.data.DataLoader(
 test_dataset,
batch_size=1,
shuffle=False,
drop_last=False,
num_workers=config.num_workers 
)
 weights_path = os.path.join(config.save_path, f'fold-{train_fold}.bin')
 model = get_model()
model.load_state_dict(torch.load(weights_path, map_location=config.device), strict=False)
 test_pred, ids = predict_on_test(model, test_loader)
 test_pred_df = pd.DataFrame({
"recording_id": test_df.recording_id.values
})
 target_cols = test_df.columns[1:].values.tolist()
test_pred_df = test_pred_df.join(pd.DataFrame(np.array(test_pred),
columns=target_cols))
test_pred_df.to_csv(os.path.join(config.save_path,
f"fold-{train_fold}-submission.csv"),
index=False)
 test_df = pd.read_csv(config.test_csv)
 for fold in range(5 if config.train_5_folds else 1):
test(test_df, fold )<save_to_csv>
model_names = ["crawl-300d-2M", "crawl-300d-2M-subword", "wiki-news-300d-1M", "wiki-news-300d-1M-subword"]
def ensemble(submission_path):
dfs = [pd.read_csv(os.path.join(
 config.save_path, f"fold-{i}-submission.csv")) for i in range(5)]
anchor = dfs[0].copy()
cols = anchor.columns[1:]
for c in cols:
total = 0
for df in dfs:
total += df[c]
anchor[c] = total / len(dfs)
 anchor.to_csv(submission_path, index=False)
 submission_path = os.path.join(config.save_path, f"submission.csv")
 if config.train_5_folds:
ensemble(submission_path)
 else:
fold0_submission = os.path.join(config.save_path, f"fold-0-submission.csv")
 os.rename(fold0_submission, submission_path)
<statistical_test>
fasttext_pretrain_cols = []
unused_words = defaultdict(int)
 for model_name in model_names:
model = pd.read_pickle(f".. /input/fasttext/{model_name}.pkl")
 def to_mean_vec(x, model):
v = np.zeros(model.vector_size)
 for w in x:
try:
v += model[w]
except:
unused_words[w] += 1
v = v /(np.sqrt(np.sum(v ** 2)) + 1e-16)
 return v
def to_max_vec(x, model):
v = np.zeros(model.vector_size)
 for w in x:
try:
v = np.maximum(v, model[w])
 except:
pass
return v
mean_vecs = df["product_name"].apply(lambda x : to_mean_vec(x, model))
mean_vecs = np.vstack(mean_vecs)
 cols = [f"fasttext_pretrain_{model_name}_mean_vec{k}" for k in range(mean_vecs.shape[1])]
fasttext_pretrain_cols += cols
mean_vec_df = pd.DataFrame(mean_vecs, columns=cols)
 df = pd.concat([df, mean_vec_df], axis = 1)
 max_vecs = df["product_name"].apply(lambda x : to_max_vec(x, model))
max_vecs = np.vstack(max_vecs)
 cols = [f"fasttext_pretrain_{model_name}_max_vec{k}" for k in range(max_vecs.shape[1])]
fasttext_pretrain_cols += cols
max_vec_df = pd.DataFrame(max_vecs, columns=cols)
 df = pd.concat([df, max_vec_df], axis = 1)
 df.head()
def addNoisy(img):
noise_img = random_noise(img)
 return addChannels(noise_img)
 def contrast_stretching(img):
p2, p98 = np.percentile(img,(2, 98))
contrast_img = exposure.rescale_intensity(img, in_range=(p2, p98))
return addChannels(contrast_img)
 def log_correction(img):
log_img = exposure.adjust_log(img)
 return addChannels(log_img)
 def randomGaussian(img):
gaussian_img = gaussian(img, sigma=random.randint(0, 5))
return addChannels(gaussian_img)
 def addChannels(img):
return np.stack(( img, img, img))
def spec_to_image(spec):
spec = resize(spec,(224, 400))
eps=1e-6
mean = spec.mean()
std = spec.std()
spec_norm =(spec - mean)/(std + eps)
 spec_min, spec_max = spec_norm.min() , spec_norm.max()
spec_scaled = 255 *(spec_norm - spec_min)/(spec_max - spec_min)
 spec_scaled = spec_scaled.astype(np.uint8)
 spec_scaled = np.asarray(spec_scaled)
 return spec_scaled<load_pretrained>
train = df[~df[target].isna() ]
test = df[df[target].isna() ]
class AudioData(Dataset):
def __init__(self, _data, data_type):
self.data = []
self.labels = []
for i in range(0, len(_data)) :
wav, sr = librosa.load('/kaggle/input/rfcx-species-audio-detection/train/' + _data[i][0] + '.flac', sr=None)
 t_min = float(_data[i][3])* sr
t_max = float(_data[i][5])* sr
center = np.round(( t_min + t_max)/ 2)
 beginning = center - length / 2
if beginning < 0:
beginning = 0
ending = beginning + length
if ending > len(wav):
ending = len(wav)
 beginning = ending - length
slice = wav[int(beginning):int(ending)]
spec=librosa.feature.melspectrogram(slice, sr=sr,n_fft=fft,hop_length=hop,fmin=fmin,fmax=fmax)
 spec_db=librosa.power_to_db(spec,top_db=80)
 img = spec_to_image(spec_db)
 mel_spec = np.stack(( img, img, img))
self.data.append(mel_spec)
 label = int(_data[i][1])
 self.labels.append(label)
 if data_type == "train":
augmentation_functions = [
addNoisy, contrast_stretching,
randomGaussian, log_correction
]
for fun in augmentation_functions:
mel_spec = fun(img)
 self.data.append(mel_spec)
 self.labels.append(label)
 def __len__(self):
return len(self.data)
 def __getitem__(self, idx):
return self.data[idx], self.labels[idx]<load_from_csv>
def to_weighted_count_vec(x, word_sets):
v = np.zeros(21)
 for w in x:
hits = []
for i in range(21):
if w in word_sets[i]:
hits.append(i)
 for i in hits:
v[i] += 1.0 / len(hits)
 return v
weighted_count_cols = [f"weighted_count_vec{k}" for k in range(21)]
fft = 2048
hop = 512
sr = 48000
length = 10 * sr
with open('/kaggle/input/rfcx-species-audio-detection/train_tp.csv')as f:
reader = csv.reader(f)
 next(reader, None)
 data = list(reader)
 fmin = 24000
fmax = 0
for i in range(0, len(data)) :
if fmin > float(data[i][4]):
fmin = float(data[i][4])
 if fmax < float(data[i][6]):
fmax = float(data[i][6])
 fmin = int(fmin * 0.9)
 fmax = int(fmax * 1.1)
 print('Minimum frequency: ' + str(fmin)+ ', maximum frequency: ' + str(fmax))
percentage_train = 90
random.shuffle(data)
 total = len(data)
 train_data_amount = round(total / 100 * percentage_train)
 train_audio = data[:train_data_amount]
val_audio = data[train_data_amount:]
train_data = AudioData(train_audio, "train")
 valid_data = AudioData(val_audio, "valid")
 train_loader = DataLoader(train_data, batch_size=16, shuffle=True)
 valid_loader = DataLoader(valid_data, batch_size=16, shuffle=True )<train_model>
train_weighted_count = pd.DataFrame(index=train.index, columns=weighted_count_cols, dtype=np.float32)
 for i_fold,(train_idx, valid_idx)in enumerate(kfold.split(train, train[target])) :
tr = train.loc[train_idx]
va = train.loc[valid_idx]
word_sets = [set(sum(tr[tr["department_id"] == i]["product_name"], [])) for i in range(21)]
vecs = va["product_name"].apply(lambda x : to_weighted_count_vec(x, word_sets))
vecs = np.vstack(vecs)
 vec_df = pd.DataFrame(vecs, index=va.index, columns=weighted_count_cols)
 train_weighted_count.loc[valid_idx, :] = vec_df
train = pd.concat([train, train_weighted_count], axis=1 )
print("train", len(train_data))
print("valid", len(valid_data))<import_modules>
test_weighted_count = pd.DataFrame(index=test.index, columns=weighted_count_cols, dtype=np.float32)
 word_sets = [set(sum(train[train["department_id"] == i]["product_name"], [])) for i in range(21)]
vecs = test["product_name"].apply(lambda x : to_weighted_count_vec(x, word_sets))
vecs = np.vstack(vecs)
 test_weighted_count.loc[:, :] = pd.DataFrame(vecs, index=test.index, columns=weighted_count_cols)
 test = pd.concat([test, test_weighted_count], axis=1 )
from torchvision.models import resnet50
import torch
import torch.nn as nn
from torch.optim import lr_scheduler
import copy<choose_model_class>
features = fasttext_pretrain_cols + weighted_count_cols + ["order_rate", "order_dow_mode", "order_hour_of_day_mode"]
num_birds = 24
if torch.cuda.is_available() :
device=torch.device('cuda:0')
 else:
device=torch.device('cpu')
 resnet_model = resnet50(pretrained=True)
 num_ftrs = resnet_model.fc.in_features
resnet_model.fc = nn.Linear(num_ftrs, num_birds)
 resnet_model = resnet_model.to(device )<choose_model_class>
scaler = preprocessing.StandardScaler()
train[features] = scaler.fit_transform(train[features])
 test[features] = scaler.transform(test[features] )
learning_rate = 2e-4
optimizer = torch.optim.Adam(resnet_model.parameters() , lr=learning_rate)
 epochs = 20
loss_fn = nn.CrossEntropyLoss()
def setlr(optimizer, lr):
for param_group in optimizer.param_groups:
param_group['lr'] = lr
return optimizer
def lr_decay(optimizer, epoch):
if epoch%10==0:
new_lr = learning_rate /(10**(epoch//10))
optimizer = setlr(optimizer, new_lr)
 print(f'Changed learning rate to {new_lr}')
 return optimizer
def train(model, loss_fn, train_loader, valid_loader, epochs, optimizer, change_lr=None):
best_model_wts = copy.deepcopy(model.state_dict())
 best_acc = 0.0
train_losses = []
valid_losses = []
for epoch in tqdm(range(1,epochs+1)) :
model.train()
batch_losses=[]
if change_lr:
optimizer = change_lr(optimizer, epoch)
 for i, data in enumerate(train_loader):
x, y = data
optimizer.zero_grad()
x = x.to(device, dtype=torch.float32)
 y = y.to(device, dtype=torch.long)
 y_hat = model(x)
 loss = loss_fn(y_hat, y)
 loss.backward()
batch_losses.append(loss.item())
 optimizer.step()
train_losses.append(batch_losses)
 print(f'Epoch - {epoch} Train-Loss : {np.mean(train_losses[-1])}')
 model.eval()
batch_losses=[]
trace_y = []
trace_yhat = []
for i, data in enumerate(valid_loader):
x, y = data
x = x.to(device, dtype=torch.float32)
 y = y.to(device, dtype=torch.long)
 y_hat = model(x)
 loss = loss_fn(y_hat, y)
 trace_y.append(y.cpu().detach().numpy())
 trace_yhat.append(y_hat.cpu().detach().numpy())
 batch_losses.append(loss.item())
 valid_losses.append(batch_losses)
 trace_y = np.concatenate(trace_y)
 trace_yhat = np.concatenate(trace_yhat)
 accuracy = np.mean(trace_yhat.argmax(axis=1)==trace_y)
 print(f'Epoch - {epoch} Valid-Loss : {np.mean(valid_losses[-1])} Valid-Accuracy : {accuracy}')
 if accuracy > best_acc:
best_acc = accuracy
best_model_wts = copy.deepcopy(model.state_dict())
 model.load_state_dict(best_model_wts)
 return model<train_model>
def seed_everything(seed : int)-> NoReturn :
random.seed(seed)
 np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 tf.random.set_seed(seed)
 seed_everything(1220)
 params = {
'hidden_layers': 1,
'hidden_units': 128,
'hidden_activation': 'relu',
'lr': 1e-4,
'batch_size': 128,
'epochs': 100
}
def nn_model(L : int):

 n_neuron = params['hidden_units']
inputs = layers.Input(shape=(L,), dtype='float32')
 x = layers.Dense(n_neuron, activation=params['hidden_activation'] )(inputs)
 for i in np.arange(params['hidden_layers'] - 1):
x = layers.Dense(n_neuron //(2 *(i+1)) , activation=params['hidden_activation'] )(x)
 out = layers.Dense(21, activation='softmax', name = 'out' )(x)
 model = models.Model(inputs=inputs, outputs=out)
 model.compile(loss="categorical_crossentropy", optimizer=optimizers.Adam(lr=params['lr']))
return model
history = model.fit(train_set['X'], train_set['y'], callbacks=[er, ReduceLR],
epochs=params['epochs'], batch_size=params['batch_size'],
validation_data=(val_set['X'], val_set['y']))
return model, fi
resnet_model = train(resnet_model, loss_fn, train_loader, valid_loader, epochs, optimizer, lr_decay )<load_pretrained>
preds_test = []
scores = []
oof = np.zeros(( len(train), 21))
for i_fold,(train_idx, valid_idx)in enumerate(kfold.split(train, train[target])) :
print(f"--------fold {i_fold}-------")
 x_tr = train.loc[train_idx, features]
y_tr = train.loc[train_idx, target]
x_va = train.loc[valid_idx, features]
y_va = train.loc[valid_idx, target]
ohe = preprocessing.OneHotEncoder(sparse=False, categories='auto')
 y_tr_ohe = ohe.fit_transform(y_tr.values.reshape(-1, 1))
y_va_ohe = ohe.transform(y_va.values.reshape(-1, 1))
nn = nn_model(len(features))
er = callbacks.EarlyStopping(patience=8, min_delta=params['lr'], restore_best_weights=True, monitor='val_loss')
 ReduceLR = callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1, epsilon=params['lr'], mode='min')
 checkpoint_filepath = ''
model_checkpoint_callback = callbacks.ModelCheckpoint(filepath=f'mybestweight_fold{i_fold}.hdf5', save_weights_only=True, monitor='val_loss', save_best_only=True)
 nn.fit(x_tr.values, y_tr_ohe, callbacks=[er, ReduceLR, model_checkpoint_callback], epochs=params['epochs'], batch_size=params['batch_size'],
validation_data=(x_va.values, y_va_ohe))
def predict_proba(x):
return nn.predict(x)
 pred_val = predict_proba(x_va)
 oof[valid_idx] += pred_val
score = {
"logloss" : log_loss(y_va, pred_val),
"f1_micro" : f1_score(y_va, np.argmax(pred_val, axis = 1), average = "micro")}
print(score)
 scores.append(score)
 pred_test = predict_proba(test[features])
 preds_test.append(pred_test )
def load_test_file(f):
wav, sr = librosa.load('/kaggle/input/rfcx-species-audio-detection/test/' + f, sr=None)
 segments = len(wav)/ length
segments = int(np.ceil(segments))
mel_array = []
for i in range(0, segments):
if(i + 1)* length > len(wav):
slice = wav[len(wav)- length:len(wav)]
else:
slice = wav[i * length:(i + 1)* length]
spec=librosa.feature.melspectrogram(slice, sr=sr,n_fft=fft,hop_length=hop,fmin=fmin,fmax=fmax)
 spec_db=librosa.power_to_db(spec,top_db=80)
 img = spec_to_image(spec_db)
 mel_spec = np.stack(( img, img, img))
mel_array.append(mel_spec)
 return mel_array<save_to_csv>
score_df = pd.DataFrame(scores)
 score_df
print('Starting prediction loop')
 with open('submission.csv', 'w', newline='')as csvfile:
submission_writer = csv.writer(csvfile, delimiter=',')
 submission_writer.writerow(['recording_id','s0','s1','s2','s3','s4','s5','s6','s7','s8','s9','s10','s11',
's12','s13','s14','s15','s16','s17','s18','s19','s20','s21','s22','s23'])
 test_files = os.listdir('/kaggle/input/rfcx-species-audio-detection/test/')
 print(len(test_files))
for i in range(0, len(test_files)) :
data = load_test_file(test_files[i])
 data = torch.tensor(data)
 data = data.float()
if torch.cuda.is_available() :
data = data.cuda()
output = resnet_model(data)
 maxed_output = torch.max(output, dim=0)[0]
maxed_output = maxed_output.cpu().detach()
file_id = str.split(test_files[i], '.')[0]
write_array = [file_id]
for out in maxed_output:
write_array.append(out.item())
 submission_writer.writerow(write_array)
 if i % 100 == 0 and i > 0:
print('Predicted for ' + str(i)+ ' of ' + str(len(test_files)+ 1)+ ' files')
 print('Submission generated' )<set_options>
oof_df = pd.DataFrame(oof)
 oof_df.to_csv("oof_nn.csv", index = False )
!nvidia-smi<install_modules>
for i in range(len(preds_test)) :
pred_df = pd.DataFrame(preds_test[i])
 pred_df.to_csv(f"pred_{i}_nn.csv", index = False )
!pip install resnest > /dev/null<load_from_url>
pred_test_final = np.array(preds_test ).mean(axis = 0)
 pred_test_final = np.argmax(pred_test_final, axis = 1 )
!curl https://raw.githubusercontent.com/pytorch/xla/master/contrib/scripts/env-setup.py -o pytorch-xla-env-setup.py > /dev/null
!python pytorch-xla-env-setup.py --apt-packages libomp5 libopenblas-dev > /dev/null<import_modules>
sub["department_id"] = pred_test_final
sub.to_csv("submission_nn.csv", index = False)
 sub.head()