Kaggle competition: YKC-2nd
<SOS> metric: meanfscore Kaggle data source: ykc-2nd<load_from_csv>
nrows = None
%%time
pred = model.predict(X)
 del X
pred = pred.reshape(-1)
 pred =(pred + 1)/ 2
df_test = pd.read_csv('.. /input/test_V2.csv')
 print("Correcting winPlacePerc")
 for i in range(len(df_test)) :
winPlacePerc = pred[i]
maxPlace = int(df_test.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
pred[i] = winPlacePerc<save_to_csv>
%%time
train = pd.read_csv(".. /input/ykc-cup-2nd/train.csv", nrows = nrows)
 test = pd.read_csv(".. /input/ykc-cup-2nd/test.csv", nrows = nrows)
 sub = pd.read_csv(".. /input/ykc-cup-2nd/sample_submission.csv" )
df_test['winPlacePerc'] = pred
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )<set_options>
df = pd.concat([train, test])
 df = df.reset_index(drop = True)
 df.shape
gc.enable()<train_model>
def clear(x, punct, rep = ""):
for p in punct:
x = x.replace(p, rep)
 return x
punct = ["ñ","\",
def state(message,start = True, time = 0):
if(start):
print('Working on {}...'.format(message))
else :
print('Working on {} took({})Sec
'.format(message,round(time , 3)) )<load_from_csv>
unuse_words = defaultdict(int)
 def get_vec(x):
vs = []
for xx in x:
if len(vs)>= n_length:
break
try:
vs.append(model.wv[xx])
 except:
flg = False
for i in range(1, len(xx)) :
try:
v1 = model.wv[xx[:i]]
v2 = model.wv[xx[i:]]
vs.append(v1)
 vs.append(v2)
 flg = True
break
except:
pass
if flg == False:
for i in range(1, len(xx)) :
try:
v1 = model.wv[xx[:i]]
vs.append(v1)
 break
except:
pass
if len(vs)>= n_length:
break
for i in range(1, len(xx)) :
try:
v2 = model.wv[xx[i:]]
vs.append(v2)
 break
except:
pass
unuse_words[xx] += 1
if len(vs)< n_length:
for i in range(n_length - len(vs)) :
vs.append(np.zeros(model.vector_size))
if len(vs)> n_length:
vs = vs[:n_length]
vs = np.array(vs)
 if len(vs)== 0:
vs = np.zeros([1, model.vector_size])
 return vs
def feature_engineering(is_train=True):
if is_train:
print("processing train_V2.csv")
 df = pd.read_csv('.. /input/train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test_V2.csv")
 df = pd.read_csv('.. /input/test_V2.csv')
 state('totalDistance')
 s = timer()
df['totalDistance'] = df['rideDistance'] + df["walkDistance"] + df["swimDistance"]
e = timer()
state('totalDistance', False, e - s)
 state('rankPoints')
 s = timer()
df['rankPoints'] = np.where(df['rankPoints'] <= 0 ,0 , df['rankPoints'])
 e = timer()
state('rankPoints', False, e-s)
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchDuration")
 features.remove("matchType")
 y = None
if is_train:
y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("get group mean feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('mean')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
if is_train: df_out = agg.reset_index() [['matchId','groupId']]
else: df_out = df[['matchId','groupId']]
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_mean", "_mean_rank"], how='left', on=['matchId', 'groupId'])
 print("get group max feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('max')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_max", "_max_rank"], how='left', on=['matchId', 'groupId'])
 print("get group min feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('min')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_min", "_min_rank"], how='left', on=['matchId', 'groupId'])
 print("get group size feature")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("get match mean feature")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("get match size feature")
 agg = df.groupby(['matchId'] ).size().reset_index(name='match_size')
 df_out = df_out.merge(agg, how='left', on=['matchId'])
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = np.array(df_out, dtype=np.float64)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y<load_from_csv>
%%time
model = pd.read_pickle(".. /input/ykc-cup-2nd-save-fasttext/fasttext_gensim_model.pkl" )
df_test = pd.read_csv('.. /input/test_V2.csv' )<normalization>
def to_vec(x):
vs = get_vec(x)
 return vs
n_length = 15
vecs = df["product_name"].apply(lambda x : to_vec(x))
vecs = np.stack(vecs)
 del model
%%time
x_train, y = feature_engineering(True)
 scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1), copy=False ).fit(x_train )<train_model>
sorted(unuse_words.items() , key=lambda x: x[1], reverse = True)[:100]
print("x_train", x_train.shape, x_train.max() , x_train.min())
 scaler.transform(x_train)
 print("x_train", x_train.shape, x_train.max() , x_train.min() )<train_model>
additional_feats = []
additional_words = [k for k, v in unuse_words.items() if v >= 5 and len(k)> 2]
print(additional_words)
 additional_word_cols = []
for w in additional_words:
c = f"is_{w}"
df[c] = df["product_name"].apply(lambda x : w in x)
 additional_word_cols.append(c)
 additional_feats.append(df[additional_word_cols])
 additional_feats.append(pd.get_dummies(df["order_dow_mode"]))
additional_feats.append(pd.get_dummies(df["order_hour_of_day_mode"]))
additional_feats.append(StandardScaler().fit_transform(df[["order_rate"]]))
additional_feats = np.hstack(additional_feats)
 additional_feats.shape
epoch_train = 30
mlp = MLPRegressor(hidden_layer_sizes=(60, 60, 28), alpha=0.001, learning_rate='adaptive', max_iter=epoch_train)
 mlp.fit(x_train,y)
 del x_train, y
gc.collect()<normalization>
target = "department_id"
train_wv = vecs[~df[target].isna() ]
test_wv = vecs[df[target].isna() ]
train_x = additional_feats[~df[target].isna() ]
test_x = additional_feats[df[target].isna() ]
y_train = df[~df[target].isna() ][target].values
y_train_ohe = to_categorical(y_train, num_classes=None)
 del vecs, additional_feats, df
train_wv.shape, test_wv.shape, train_x.shape, test_x.shape, y_train.shape, y_train_ohe.shape
x_test, _ = feature_engineering(False)
 scaler.transform(x_test)
 print("x_test", x_test.shape, x_test.max() , x_test.min())
 np.clip(x_test, out=x_test, a_min=-1, a_max=1)
 print("x_test", x_test.shape, x_test.max() , x_test.min() )<predict_on_test>
from tensorflow import keras
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.layers import *
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from tensorflow.keras.optimizers import Adam, SGD, Nadam
from tensorflow.keras import backend as K
%%time
pred = mlp.predict(x_test)
 del x_test
gc.collect()<prepare_output>
class F1(keras.callbacks.Callback):
def __init__(self, model, inputs, targets, epoch_max):
self.model = model
self.inputs = inputs
self.targets = targets
self.epoch = 0
self.epoch_max = epoch_max
self.eval_freq = 5
def on_epoch_end(self, epoch, logs):
if(self.epoch % self.eval_freq == 0)or(self.epoch_max - self.epoch < 3):
p = self.model.predict(self.inputs)
 score = get_score(self.targets, p)
 print(f"{self.epoch}, : ")
 print(score)
 self.epoch += 1
def get_score(t, p):
score = {
"logloss" : log_loss(t, p),
"f1_micro" : f1_score(t, np.argmax(p, axis = 1), average = "micro")}
return score
pred = pred.reshape(-1)
 pred =(pred + 1)/ 2<feature_engineering>
def get_model(param):
inp_wv = Input(( n_length, 300))
inp = Input(( train_x.shape[1],))
mask = Lambda(lambda x : 1 /(1 + K.exp(-100 * K.std(x, axis = 2, keepdims = True))))(inp_wv)
 def calc_mean(x, mask, axis = 1, keepdims = False):
return K.sum(x * mask, axis = axis, keepdims = keepdims)/ K.sum(mask, axis = axis, keepdims = keepdims)
 aggs = []
mean = Lambda(lambda x, mask : calc_mean(x, mask, axis = 1))(inp_wv, mask)
 maximum = Lambda(lambda x : K.max(x, axis = 1))(inp_wv)
 aggs.append(BatchNormalization()(mean))
aggs.append(BatchNormalization()(maximum))
h = inp_wv
for f in param["n_units"]:
h = keras.layers.Dense(f, "relu" )(h)
 h = Dropout(param["p_drop"] )(h)
 mean = Lambda(lambda x, mask : calc_mean(x, mask, axis = 1))(h, mask)
 maximum = Lambda(lambda x : K.max(x, axis = 1))(h)
 aggs.append(BatchNormalization()(mean))
aggs.append(BatchNormalization()(maximum))
h = Concatenate()(aggs)
 h = Dropout(param["p_drop_fc"] )(h)
 h = Concatenate()([h, inp])
 for f in param["n_units_fc"]:
h = Dense(f, "relu" )(h)
 h = Dropout(param["p_drop_fc"] )(h)
 out = Dense(21, "softmax" )(h)
 model = keras.models.Model(inputs = [inp_wv, inp], outputs = out)
 return model
%%time
print("fix winPlacePerc")
 for i in range(len(df_test)) :
winPlacePerc = pred[i]
maxPlace = int(df_test.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
pred[i] = winPlacePerc<save_to_csv>
def trainNN(param, x_tr, y_tr, x_va, y_va, y_va_label, verbose = 0):
param_base = {
"lr" : 1e-3,
"lr_min" : 1e-5,
"lr_reduce_factor" : 0.5,
"lr_reduce_patience" : 5,
"epochs" : 100,
}
param.update(param_base)
 param["n_units"] = [int(param["n_unit"] *(param["n_unit_scale"] ** k)) for k in range(param["n_layer"])]
param["n_units_fc"] = [int(param["n_unit_fc"] *(param["n_unit_fc_scale"] ** k)) for k in range(param["n_layer_fc"])]
param["n_units_fc"] = [n for n in param["n_units_fc"] if n >=64]
print(param)
 model = get_model(param)
 model.compile(loss=keras.losses.CategoricalCrossentropy() , optimizer=keras.optimizers.Adam(learning_rate=param["lr"], decay = param["decay"]))
cb_schedule = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=param["lr_reduce_factor"], patience=param["lr_reduce_patience"],
verbose=1, mode='auto', min_delta=0.0001, cooldown=0, min_lr=param["lr_min"])
 cb_earlystop = keras.callbacks.EarlyStopping(monitor='val_loss',patience=10, restore_best_weights=True)
 cb_save = keras.callbacks.ModelCheckpoint(f'model.hdf5', save_best_only=True, monitor='val_loss', mode='min')
 callbacks = [cb_schedule, cb_earlystop, cb_save, F1(model, x_va, y_va_label, param["epochs"])]
model.fit(x_tr, y_tr,
batch_size = param["batch_size"],
epochs=param["epochs"],
callbacks = callbacks,
validation_data=(x_va, y_va), verbose = verbose)
 loss_history = model.history.history["val_loss"]
return model
df_test['winPlacePerc'] = pred
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv("pubg-submission.csv", index=False )<install_modules>
def run_cv(param, n_split = 3, verbose = 1):
preds_test = []
scores = []
oof = np.zeros([len(train), 21])
 kfold = StratifiedKFold(n_splits=n_split, shuffle = True, random_state=42)
 for i_fold,(train_idx, valid_idx)in enumerate(kfold.split(train, y_train)) :
print(f"--------fold {i_fold}-------")
 x_tr_wv = train_wv[train_idx]
x_tr = train_x[train_idx]
y_tr = y_train_ohe[train_idx]
x_va_wv = train_wv[valid_idx]
x_va = train_x[valid_idx]
y_va = y_train_ohe[valid_idx]
y_va_label = y_train[valid_idx]
model = trainNN(param, [x_tr_wv, x_tr], y_tr, [x_va_wv, x_va], y_va, y_va_label, verbose = verbose)
 pred_val = model.predict([x_va_wv, x_va])
 oof[valid_idx] = pred_val
score = get_score(y_va_label, pred_val)
 print(score)
 scores.append(score)
 pred_test = model.predict([test_wv, test_x])
 preds_test.append(pred_test)
 del model
score_df = pd.DataFrame(scores)
 return score_df, oof, preds_test
!pip -q install --upgrade pip
!pip -q install timm
!pip -q install torchlibrosa
!pip -q install audiomentations<import_modules>
import audiomentations as A
import os, time, librosa, random
from functools import partial
import numpy as np
import pandas as pd
from sklearn.model_selection import StratifiedKFold
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch.utils.data import Dataset
from timm.models import resnet34d
from torchlibrosa.stft import Spectrogram, LogmelFilterBank
from torchlibrosa.augmentation import SpecAugmentation
from tqdm import tqdm
import soundfile as sf
from contextlib import nullcontext<define_variables>
params = {'decay': 0.0002561250212980425, 'batch_size': 276, 'n_unit': 2048, 'n_layer': 1, 'n_unit_scale': 0.6165090828302822, 'n_unit_fc': 225, 'n_layer_fc': 1, 'n_unit_fc_scale': 0.5835791877627263, 'p_drop': 0.57113050869184264, 'p_drop_fc': 0.3911374259982809}
score_df, oof, preds_test = run_cv(params, n_split = 10, verbose = False )
NO_LABEL = -1
NUM_CLASSES = 24
class config:
seed = 42
device = "cuda:0"
train_tp_csv = '.. /input/rfcx-species-audio-detection/train_tp.csv'
test_csv = '.. /input/rfcx-species-audio-detection/sample_submission.csv'
save_path = './'
encoder = resnet34d
encoder_features = 512
percent_unlabeled = 1.0
consistency_weight = 100.0
consistency_rampup = 1000
ema_decay = 0.995
positive_weight = 2.0
lr = 1e-3
epochs = 25
batch_size = 32
num_workers = 4
train_5_folds = True
period = 6
step = 1
model_params = {
'sample_rate': 48000,
'window_size': 2048,
'hop_size': 512,
'mel_bins': 384,
'fmin': 20,
'fmax': 48000 // 2,
'classes_num': NUM_CLASSES
}
augmenter = A.Compose([
A.AddGaussianNoise(p=0.33, max_amplitude=0.02),
A.AddGaussianSNR(p=0.33),
A.FrequencyMask(min_frequency_band=0.01, max_frequency_band=0.25, p=0.33),
A.TimeMask(min_band_part=0.01, max_band_part=0.25, p=0.33),
A.Gain(p=0.33)
 ])
<load_from_csv>
pred_test_mean = np.array(preds_test ).mean(axis = 0)
 pred_test_final = np.argmax(pred_test_mean, axis = 1 )
def get_n_fold_df(csv_path, folds=5):
df = pd.read_csv(csv_path)
 df_group = df.groupby("recording_id")[["species_id"]].first().reset_index()
df_group = df_group.sample(frac=1, random_state=config.seed ).reset_index(drop=True)
 df_group.loc[:, 'fold'] = -1
X = df_group["recording_id"].values
y = df_group["species_id"].values
kfold = StratifiedKFold(n_splits=folds, random_state=config.seed)
 for fold,(t_idx, v_idx)in enumerate(kfold.split(X, y)) :
df_group.loc[v_idx, "fold"] = fold
return df.merge(df_group[['recording_id', 'fold']], on="recording_id", how="left")
 def init_layer(layer):
nn.init.xavier_uniform_(layer.weight)
 if hasattr(layer, "bias"):
if layer.bias is not None:
layer.bias.data.fill_(0.)
def init_bn(bn):
bn.bias.data.fill_(0.)
bn.weight.data.fill_(1.0)
 def sigmoid_rampup(current, rampup_length):

 if rampup_length == 0:
return 1.0
else:
current = np.clip(current, 0.0, rampup_length)
 phase = 1.0 - current / rampup_length
return float(np.exp(-5.0 * phase * phase))
class AverageMeter(object):

 def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
class MetricMeter(object):
def __init__(self):
self.reset()
def reset(self):
self.y_true = []
self.y_pred = []
def update(self, y_true, y_pred):
try:
self.y_true.extend(y_true.detach().cpu().numpy().tolist())
 self.y_pred.extend(torch.sigmoid(y_pred ).cpu().detach().numpy().tolist())
 except:
print("UPDATE FAILURE")
 def update_list(self, y_true, y_pred):
self.y_true.extend(y_true)
 self.y_pred.extend(y_pred)
 @property
def avg(self):
score_class, weight = lwlrap(np.array(self.y_true), np.array(self.y_pred))
self.score =(score_class * weight ).sum()
return self.score
def interpolate(x: torch.Tensor, ratio: int):

(batch_size, time_steps, classes_num)= x.shape
upsampled = x[:, :, None, :].repeat(1, 1, ratio, 1)
 upsampled = upsampled.reshape(batch_size, time_steps * ratio, classes_num)
 return upsampled
def _one_sample_positive_class_precisions(scores, truth):
num_classes = scores.shape[0]
pos_class_indices = np.flatnonzero(truth > 0)
 if not len(pos_class_indices):
return pos_class_indices, np.zeros(0)
 retrieved_classes = np.argsort(scores)[::-1]
class_rankings = np.zeros(num_classes, dtype=np.int)
 class_rankings[retrieved_classes] = range(num_classes)
 retrieved_class_true = np.zeros(num_classes, dtype=np.bool)
 retrieved_class_true[class_rankings[pos_class_indices]] = True
retrieved_cumulative_hits = np.cumsum(retrieved_class_true)
 precision_at_hits =(
 retrieved_cumulative_hits[class_rankings[pos_class_indices]] / 
(1 + class_rankings[pos_class_indices].astype(np.float)))
 return pos_class_indices, precision_at_hits
def lwlrap(truth, scores):
assert truth.shape == scores.shape
num_samples, num_classes = scores.shape
precisions_for_samples_by_classes = np.zeros(( num_samples, num_classes))
for sample_num in range(num_samples):
pos_class_indices, precision_at_hits = _one_sample_positive_class_precisions(scores[sample_num, :],
truth[sample_num, :])
 precisions_for_samples_by_classes[sample_num, pos_class_indices] = precision_at_hits
labels_per_class = np.sum(truth > 0, axis=0)
 weight_per_class = labels_per_class / float(np.sum(labels_per_class))
per_class_lwlrap =(np.sum(precisions_for_samples_by_classes, axis=0)/
np.maximum(1, labels_per_class))
return per_class_lwlrap, weight_per_class
def pretty_print_metrics(fold, epoch, optimizer, train_loss_metrics, val_loss_metrics):
print(f)
 class TestDataset(Dataset):
def __init__(self, df, data_path, period=10, step=1):
self.data_path = data_path
self.period = period
self.step = step
self.recording_ids = list(df["recording_id"].unique())
 def __len__(self):
return len(self.recording_ids)
 def __getitem__(self, idx):
recording_id = self.recording_ids[idx]
y, sr = sf.read(f"{self.data_path}/{recording_id}.flac")
 len_y = len(y)
 effective_length = sr * self.period
effective_step = sr * self.step
y_ = []
i = 0
while i+effective_length <= len_y:
y__ = y[i:i + effective_length]
y_.append(y__)
 i = i + effective_step
y = np.stack(y_)
 label = np.zeros(NUM_CLASSES, dtype='f')
 return {
"waveform": y,
"target": torch.tensor(label, dtype=torch.float),
"id": recording_id
}
def predict_on_test(model, test_loader):
model.eval()
pred_list = []
id_list = []
with torch.no_grad() :
t = tqdm(test_loader)
 for i, sample in enumerate(t):
input = sample["waveform"].to(config.device)
 bs, seq, w = input.shape
input = input.reshape(bs * seq, w)
 id = sample["id"]
output, _ = model(input)
 output = output.reshape(bs, seq, -1)
 output, _ = torch.max(output, dim=1)
 output = output.cpu().detach().numpy().tolist()
pred_list.extend(output)
 id_list.extend(id)
 return pred_list, id_list<choose_model_class>
sub["department_id"] = pred_test_final
sub.to_csv("submission.csv", index = False)
 sub.head()
class AttentionHead(nn.Module):
def __init__(self, in_features: int, out_features: int):
super().__init__()
self.conv_attention = nn.Conv1d(in_channels=in_features,
out_channels=out_features,
kernel_size=1, stride=1,
padding=0, bias=True)
 self.conv_classes = nn.Conv1d(in_channels=in_features,
out_channels=out_features,
kernel_size=1, stride=1,
padding=0, bias=True)
 self.batch_norm_attention = nn.BatchNorm1d(out_features)
 self.init_weights()
def init_weights(self):
init_layer(self.conv_attention)
 init_layer(self.conv_classes)
 init_bn(self.batch_norm_attention)
 def forward(self, x):
norm_att = torch.softmax(torch.tanh(self.conv_attention(x)) , dim=-1)
 classes = self.conv_classes(x)
 x = torch.sum(norm_att * classes, dim=2)
 return x, norm_att, classes
class SEDAudioClassifier(nn.Module):
def __init__(self, sample_rate, window_size, hop_size,
mel_bins, fmin, fmax, classes_num):
super().__init__()
self.interpolate_ratio = 32
self.spectrogram_extractor = Spectrogram(n_fft=window_size,
hop_length=hop_size,
win_length=window_size,
window='hann', center=True,
pad_mode='reflect',
freeze_parameters=True)
 self.logmel_extractor = LogmelFilterBank(sr=sample_rate, n_fft=window_size,
n_mels=mel_bins, fmin=fmin,
fmax=fmax, ref=1.0,
amin=1e-10, top_db=None,
freeze_parameters=True)
 self.batch_norm = nn.BatchNorm2d(mel_bins)
 self.encoder = partial(config.encoder, pretrained=True, in_chans=1 )()
self.fc = nn.Linear(config.encoder_features,
config.encoder_features, bias=True)
 self.att_head = AttentionHead(config.encoder_features, classes_num)
 self.avg_pool = nn.modules.pooling.AdaptiveAvgPool2d(( 1, 1))
self.init_weight()
def init_weight(self):
init_bn(self.batch_norm)
 init_layer(self.fc)
 self.att_head.init_weights()
def forward(self, input, spec_aug=False,
mixup_lambda=None, return_encoding=False):
x = self.spectrogram_extractor(input.float())
 x = self.logmel_extractor(x)
 x = x.transpose(1, 3)
 x = self.batch_norm(x)
 x = x.transpose(1, 3)
 x = self.encoder.forward_features(x)
 x = torch.mean(x, dim=3)
 x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1)
 x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1)
 x = x1 + x2
x = F.dropout(x, p=0.5, training=self.training)
 x = x.transpose(1, 2)
 x = F.relu_(self.fc(x))
x = x.transpose(1, 2)
 x = F.dropout(x, p=0.5, training=self.training)
(clipwise_output, norm_att, segmentwise_output)= self.att_head(x)
 segmentwise_output = segmentwise_output.transpose(1, 2)
 framewise_output = interpolate(segmentwise_output, self.interpolate_ratio)
 return clipwise_output, framewise_output
def get_model(is_mean_teacher=False):
model = SEDAudioClassifier(**config.model_params)
 model = model.to(config.device)
 if is_mean_teacher:
for param in model.parameters() :
param.detach_()
return model<compute_train_metric>
pd.to_pickle(
 {"train" : train, "test" : test, "oof" : oof, "pred_test" : pred_test_mean, "sub" : sub}, "data.pkl" )