Kaggle competition: Freesound Audio Tagging 2019
submission.to_csv('result-with-best.csv', index=False )<set_options>
DATA = Path('.. /input')
 CSV_TRN_CURATED = DATA/'train_curated.csv'
CSV_TRN_NOISY = DATA/'train_noisy.csv'
CSV_SUBMISSION = DATA/'sample_submission.csv'
TRN_CURATED = DATA/'train_curated'
TRN_NOISY = DATA/'train_noisy'
TEST = DATA/'test'
WORK = Path('work')
 IMG_TRN_CURATED = WORK/'image/trn_curated'
IMG_TRN_NOISY = WORK/'image/train_noisy'
IMG_TEST = WORK/'image/test'
for folder in [WORK, IMG_TRN_CURATED, IMG_TRN_NOISY, IMG_TEST]:
Path(folder ).mkdir(exist_ok=True, parents=True)
 df = pd.read_csv(CSV_TRN_CURATED)
 test_df = pd.read_csv(CSV_SUBMISSION )
%reload_ext autoreload
%autoreload 2
%matplotlib inline<import_modules>
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 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:
IPython.display.display(IPython.display.Audio(x, rate=conf.sampling_rate))
show_melspectrogram(conf, mels)
 return mels
class conf:
sampling_rate = 44100
duration = 2
hop_length = 347*duration
fmin = 20
fmax = sampling_rate // 2
n_mels = 128
n_fft = n_mels * 20
samples = sampling_rate * duration
x = read_as_melspectrogram(conf, TRN_CURATED/'0006ae4e.wav', trim_long_data=False, debug_display=True )
import numpy as np
import pandas as pd
from fastai.vision import *<load_from_csv>
def mono_to_color(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):
X = []
for i, row in tqdm_notebook(df.iterrows()):
x = read_as_melspectrogram(conf, source/str(row.fname), trim_long_data=False)
 x_color = mono_to_color(x)
 X.append(x_color)
 return X
X_train = convert_wav_to_image(df, source=TRN_CURATED, img_dest=IMG_TRN_CURATED)
 X_test = convert_wav_to_image(test_df, source=TEST, img_dest=IMG_TEST )
df = pd.read_csv(path/"train_v2.csv")
 df.head()<feature_engineering>
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() ])
tfms = get_transforms(flip_vert = True, max_lighting = 0.1, max_zoom = 1.05, max_warp = 0.)<load_from_csv>
data.show_batch(3 )
np.random.seed(7)
 source =(ImageList.from_csv(path, "train_v2.csv", folder = "train-jpg", suffix = ".jpg")
.split_by_rand_pct(0.2)
.label_from_df(cols = "tags", label_delim = " ")) 
<categorify>
learn = cnn_learner(data, models.resnet18, pretrained=False, metrics=[lwlrap])
 learn.unfreeze()
learn.lr_find() ; learn.recorder.plot()
data =(source.transform(tfms, size = 128)
.databunch() 
.normalize(imagenet_stats))<define_variables>
learn.fit_one_cycle(5, 1e-1)
 learn.fit_one_cycle(10, 1e-2 )
data.show_batch(rows = 4, figsize =(15,15))<compute_test_metric>
learn.fit_one_cycle(20, 3e-3 )
acc_thresh = partial(accuracy_thresh, thresh = 0.2)
 f2_score = partial(fbeta, beta = 2, thresh = 0.2 )<choose_model_class>
learn.fit_one_cycle(20, 1e-3 )
learn = cnn_learner(data, models.resnet50, metrics = [acc_thresh, f2_score], model_dir = "/tmp/models" )<find_best_params>
learn.fit_one_cycle(50, slice(1e-3, 3e-3))
learn.lr_find()
learn.recorder.plot()<train_model>
learn.fit_one_cycle(10, slice(1e-4, 1e-3))
learn.fit_one_cycle(cyc_len = 5, max_lr = slice(1e-2))<save_model>
learn.save('fat2019_fastai_cnn2d_stage-2')
 learn.export()
learn.save("baseline-rn50-128" )<normalization>
CUR_X_FILES, CUR_X = list(test_df.fname.values), X_test
test = ImageList.from_csv(WORK/'image', Path('.. /.. ')/CSV_SUBMISSION, folder='test')
 learn = load_learner(WORK/'image', test=test)
 preds, _ = learn.TTA(ds_type=DatasetType.Test )
np.random.seed(7)
 data2 =(source.transform(tfms, size = 256)
.databunch() 
.normalize(imagenet_stats))<choose_model_class>
test_df[learn.data.classes] = preds
test_df.to_csv('submission.csv', index=False)
 test_df.head()
<train_model><EOS> 
CUR_X_FILES, CUR_X = list(df.fname.values), X_train
learn = cnn_learner(data, models.resnet18, pretrained=False, metrics=[lwlrap])
 learn.load('fat2019_fastai_cnn2d_stage-2');