Kaggle competition: Freesound Audio Tagging 2019
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.impute import SimpleImputer
import lightgbm as lgb
import numpy as np<load_from_csv>
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 calculate_per_class_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 lwlrap(scores, truth, **kwargs):
score, weight = calculate_per_class_lwlrap(to_np(truth), to_np(scores))
return torch.Tensor([(score * weight ).sum() ] )
train = pd.read_csv('.. /input/train.csv', parse_dates=['Dates'])
 test = pd.read_csv('.. /input/test.csv', parse_dates=['Dates'], index_col='Id' )<categorify>
def load_pkl(filename):

 with open(filename, 'rb')as f:
return pickle.load(f )
train.drop_duplicates(inplace=True)
 train.replace({'X': -120.5, 'Y': 90.0}, np.NaN, inplace=True)
 test.replace({'X': -120.5, 'Y': 90.0}, np.NaN, inplace=True)
 imp = SimpleImputer(strategy='mean')
 for district in train['PdDistrict'].unique() :
train.loc[train['PdDistrict'] == district, ['X', 'Y']] = imp.fit_transform(
 train.loc[train['PdDistrict'] == district, ['X', 'Y']])
 test.loc[test['PdDistrict'] == district, ['X', 'Y']] = imp.transform(
 test.loc[test['PdDistrict'] == district, ['X', 'Y']] )<feature_engineering>
testing_df_X = pd.read_csv('.. /input/freesound-audio-tagging-2019/sample_submission.csv')
 testing_df_LH = pd.read_csv('.. /input/freesound-audio-tagging-2019/sample_submission.csv')
 testing_df = pd.read_csv('.. /input/freesound-audio-tagging-2019/sample_submission.csv' )
def feature_engineering(data):
data['Date'] = pd.to_datetime(data['Dates'].dt.date)
 data['n_days'] =(
 data['Date'] - data['Date'].min() ).apply(lambda x: x.days)
 data['Day'] = data['Dates'].dt.day
data['DayOfWeek'] = data['Dates'].dt.weekday
data['Month'] = data['Dates'].dt.month
data['Year'] = data['Dates'].dt.year
data['Hour'] = data['Dates'].dt.hour
data['Minute'] = data['Dates'].dt.minute
data['Block'] = data['Address'].str.contains('block', case=False)
 data.drop(columns=['Dates','Date','Address'], inplace=True)
 return data<drop_column>
class EasyDict(dict):
def __init__(self, d=None, **kwargs):
if d is None:
d = {}
if kwargs:
d.update(**kwargs)
 for k, v in d.items() :
setattr(self, k, v)
 for k in self.__class__.__dict__.keys() :
if not(k.startswith('__')and k.endswith('__')) and not k in('update', 'pop'):
setattr(self, k, getattr(self, k))
def __setattr__(self, name, value):
if isinstance(value,(list, tuple)) :
value = [self.__class__(x)
 if isinstance(x, dict)else x for x in value]
elif isinstance(value, dict)and not isinstance(value, self.__class__):
value = self.__class__(value)
 super(EasyDict, self ).__setattr__(name, value)
 super(EasyDict, self ).__setitem__(name, value)
 __setitem__ = __setattr__
def update(self, e=None, **f):
d = e or dict()
d.update(f)
 for k in d:
setattr(self, k, d[k])
 def pop(self, k, d=None):
delattr(self, k)
 return super(EasyDict, self ).pop(k, d )
train = feature_engineering(train)
 train.drop(columns=['Descript','Resolution'], inplace=True)
 test = feature_engineering(test )<categorify>
def read_audio(conf, pathname, trim_long_data):
y, sr = librosa.load(pathname, sr=conf.sampling_rate)
 if 0 < len(y):
y, _ = librosa.effects.trim(y)
 if len(y)> conf.samples:
if trim_long_data:
y = y[0:0+conf.samples]
else:
padding = conf.samples - len(y)
 offset = padding // 2
y = np.pad(y,(offset, conf.samples - len(y)- offset), 'constant')
 return y
def audio_to_melspectrogram(conf, audio):
spectrogram = librosa.feature.melspectrogram(audio,
sr=conf.sampling_rate,
n_mels=conf.n_mels,
hop_length=conf.hop_length,
n_fft=conf.n_fft,
fmin=conf.fmin,
fmax=conf.fmax)
 spectrogram = librosa.power_to_db(spectrogram)
 spectrogram = spectrogram.astype(np.float32)
 return spectrogram
def melspectrogram_to_delta(mels):
return librosa.feature.delta(mels)
 def show_melspectrogram(conf, mels, title='Log-frequency power spectrogram'):
librosa.display.specshow(mels, x_axis='time', y_axis='mel',
sr=conf.sampling_rate, hop_length=conf.hop_length,
fmin=conf.fmin, fmax=conf.fmax)
 plt.colorbar(format='%+2.0f dB')
 plt.title(title)
 plt.show()
def read_as_melspectrogram(conf, pathname, trim_long_data, debug_display=False):
x = read_audio(conf, pathname, trim_long_data)
 mels = audio_to_melspectrogram(conf, x)
 if debug_display:
delta = melspectrogram_to_delta(mels)
 delta_squared = melspectrogram_to_delta(delta)
 IPython.display.display(IPython.display.Audio(x, rate=conf.sampling_rate))
show_melspectrogram(conf, mels)
 show_melspectrogram(conf, delta)
 show_melspectrogram(conf, delta_squared)
 return mels

 def mels_preprocessing(X, mean=None, std=None, norm_max=None, norm_min=None, eps=1e-6):
X = np.stack([X, X, X], axis=-1)
 mean = mean or X.mean()
std = std or X.std()
Xstd =(X - mean)/(std + eps)
 _min, _max = Xstd.min() , Xstd.max()
norm_max = norm_max or _max
norm_min = norm_min or _min
if(_max - _min)> eps:
V = Xstd
V[V < norm_min] = norm_min
V[V > norm_max] = norm_max
V = 255 *(V - norm_min)/(norm_max - norm_min)
 V = V.astype(np.uint8)
 else:
V = np.zeros_like(Xstd, dtype=np.uint8)
 return V
def convert_wav_to_image(df, source, img_dest, conf):
X = []
for i, row in tqdm_notebook(df.iterrows()):
x1 = read_as_melspectrogram(conf, source/str(row.fname), trim_long_data=False)
 x1 = mels_preprocessing(x1)
 X.append(x1)
 return df, X
def split_long_data(conf, X):
L = X.shape[1]
one_length = conf['dims'][1]
loop_length = int(one_length * 0.9)
 min_length = int(one_length * 0.2)
 for idx in range(L // loop_length):
cur = loop_length * idx
rest = L - cur
if one_length <= rest:
yield X[:, cur:cur+one_length]
elif min_length <= rest:
cur = L - one_length
yield X[:, cur:cur+one_length]
def convert_X(df, conf, datapath):
df_x = pd.DataFrame({'fname': [], 'labels': []})
 X = []
for i, row in tqdm_notebook(df.iterrows()):
j = 1
x1 = read_as_melspectrogram(conf, datapath / row.fname, trim_long_data=False)
 x1 = mels_preprocessing(x1)
 for chunk in split_long_data(conf, x1):
X.append(np.expand_dims(chunk, axis=-1))
df_x = df_x.append({'fname': str(j)+ '-' + df.fname[i], 'labels':df.labels[i]}, ignore_index=True)
 f = open('/kaggle/working/trn_curated_X/' + str(j)+ '-' + df.fname[i],"w+")
 j += 1
return df_x, X
def convert_X_test(df, conf, datapath):
df_x = pd.DataFrame({'fname': []})
 X = []
for i, row in tqdm_notebook(df.iterrows()):
j = 1
x1 = read_as_melspectrogram(conf, datapath / row.fname, trim_long_data=False)
 x1 = mels_preprocessing(x1)
 for chunk in split_long_data(conf, x1):
X.append(np.expand_dims(chunk, axis=-1))
df_x = df_x.append({'fname': str(j)+ '-' + df.fname[i]}, ignore_index=True)
 f = open('/kaggle/working/test_X/' + str(j)+ '-' + df.fname[i],"w+")
 j += 1
return df_x, X
le1 = LabelEncoder()
train['PdDistrict'] = le1.fit_transform(train['PdDistrict'])
 test['PdDistrict'] = le1.transform(test['PdDistrict'])
 le2 = LabelEncoder()
y = le2.fit_transform(train.pop('Category'))<create_dataframe>
conf_LH = EasyDict()
conf_LH.sampling_rate = 44100
conf_LH.duration = 2
conf_LH.hop_length = 347 * conf_LH.duration
conf_LH.fmin = 20
conf_LH.fmax = conf_LH.sampling_rate // 2
conf_LH.n_mels = 128
conf_LH.n_fft = conf_LH.n_mels * 20
conf_LH.samples = conf_LH.sampling_rate * conf_LH.duration
conf_LH.dims =(conf_LH.n_mels, 1 + int(np.floor(conf_LH.samples/conf_LH.hop_length)) , 1)
 conf_X = EasyDict()
conf_X.sampling_rate = 44100
conf_X.duration = 8
conf_X.hop_length = 347 * conf_X.duration
conf_X.fmin = 20
conf_X.fmax = conf_X.sampling_rate // 2
conf_X.n_mels = 128
conf_X.n_fft = conf_X.n_mels * 20
conf_X.samples = conf_X.sampling_rate * conf_X.duration
conf_X.dims =(conf_X.n_mels, 1 + int(np.floor(conf_X.samples/conf_X.hop_length)) , 1 )
train_set = lgb.Dataset(
 train, label=y, categorical_feature=['PdDistrict'], free_raw_data=False )<init_hyperparams>
testing_df_X, X_test_X = convert_wav_to_image(testing_df, source=Path('.. /input/freesound-audio-tagging-2019/test'), img_dest=Path('test'), conf = conf_X)
 testing_df_LH, X_test_LH = convert_wav_to_image(testing_df, source=Path('.. /input/freesound-audio-tagging-2019/test'), img_dest=Path('test'), conf = conf_LH)
 print(f"Finished data conversion at {(time.time() -start_time)/3600} hours")
 print(len(X_test_X))
print(len(testing_df_X))
print(len(X_test_LH))
print(len(testing_df_LH))
params = {
'objective': 'multiclass',
'num_class': 39
}<compute_train_metric>
CUR_X_FILES, CUR_X = list(testing_df_X.fname.values), X_test_X
test_X = ImageList.from_csv(Path('/kaggle/working'), Path('.. /input/freesound-audio-tagging-2019/sample_submission.csv'), folder=Path('.. /input/freesound-audio-tagging-2019/test'))
predictions_X = torch.from_numpy(np.zeros(( 3361,80)) ).float()
print(predictions_X.shape)
 num_folds = 5
for i in range(num_folds):
learn_X = load_learner(path = '.', file=Path('/kaggle/input/fat-curated-ensemble-mixup-tta/FAT2019_X_1D_mixup_TTA_Kfold_'+str(i)+'.pkl'), test=test_X)
 preds_X, _ = learn_X.TTA(ds_type=DatasetType.Test)
 print(preds_X.shape)
 predictions_X = predictions_X + preds_X
predictions_X = predictions_X/num_folds
testing_df_X[learn_X.data.classes] = predictions_X
testing_df_X.head()
cv_results = lgb.cv(params, train_set, metrics='multi_logloss', early_stopping_rounds=10 )<compute_test_metric>
print('Best score: ', min(cv_results['multi_logloss-mean']))<train_model>
CUR_X_FILES, CUR_X = list(testing_df_LH.fname.values), X_test_LH
test_LH = ImageList.from_csv(Path('/kaggle/working'), Path('.. /input/freesound-audio-tagging-2019/sample_submission.csv'), folder=Path('.. /input/freesound-audio-tagging-2019/test'))
predictions_LH = torch.from_numpy(np.zeros(( 3361,80)) ).float()
print(predictions_LH.shape)
 num_folds = 5
for i in range(num_folds):
learn_LH = load_learner(path = '.', file=Path('/kaggle/input/fat-curated-ensemble-mixup-tta/FAT2019_LH_1D_mixup_TTA_Kfold_'+str(i)+'.pkl'), test=test_LH)
 preds_LH, _ = learn_LH.TTA(ds_type=DatasetType.Test)
 print(preds_LH.shape)
 predictions_LH = predictions_LH + preds_LH
predictions_LH = predictions_LH/num_folds
testing_df_LH[learn_LH.data.classes] = predictions_LH
testing_df_LH.head()
num_boost_round = np.argmin(cv_results['multi_logloss-mean'])
 print('Best epoch: ', num_boost_round )<train_model>
<predict_on_test><EOS> 
preds =(predictions_X + predictions_LH)/ 2
testing_df[learn_X.data.classes] = preds
testing_df.to_csv('submission.csv', index=False)
 testing_df.head(20 )