Kaggle competition: Rainforest Connection Species Audio Detection
<SOS> metric: WeightedLabelRankingAveragePrecision Kaggle data source: rainforest-connection-species-audio-detection<train_model>
!nvidia-smi
logreg = LogisticRegression()
logreg.fit(dtrain, train_label)
 pred_log=logreg.predict(dtest)
 print(accuracy_score(test_label, pred_log))<train_model>
!pip install resnest > /dev/null
rfc = RandomForestClassifier(max_depth=20, n_estimators=100)
 rfc.fit(dtrain , train_label)
 pred = rfc.predict(dtest)
 print(accuracy_score(test_label, pred))<predict_on_test>
!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
dtc = DecisionTreeClassifier(max_depth=10)
 dtc.fit(dtrain , train_label)
 pred = dtc.predict(dtest)
 print(accuracy_score(test_label, pred))<train_model>
import torch_xla
import torch_xla.core.xla_model as xm
xgb = XGBClassifier(max_depth=4, learning_rate=0.03, n_estimators=100,
objective='multi:softprob', subsample=0.6, colsample_bytree=0.6, seed=40)
 xgb.fit(dtrain , train_label)
 pred = xgb.predict(dtest)
 print(accuracy_score(test_label, pred))<predict_on_test>
import numpy as np
import librosa as lb
import soundfile as sf
import pandas as pd
from pathlib import Path
import torch
from torch.utils.data import Dataset, DataLoader
from tqdm.notebook import tqdm
from resnest.torch import resnest50
import time
xgb = XGBClassifier(max_depth=4, learning_rate=0.03, n_estimators=100,
objective='multi:softprob', subsample=0.6, colsample_bytree=0.6, seed=40)
 xgb.fit(X_train, y_trans)
 XGBC_pred_test = xgb.predict(X_test)
 XGBC_pred_test_prob=xgb.predict_proba(X_test )<something_strange>
NUM_CLASSES = 24
SR = 32_000
DURATION = 10
STRIDE = 5
TEST_BATCH_SIZE = 30
TEST_NUM_WORKERS = 2
USE_PRE_COMPUTED_MFCC = True
DEVICE = xm.xla_device()
torch.set_default_tensor_type('torch.FloatTensor')
 TEST_AUDIO_ROOT = Path(".. /input/rfcx-species-audio-detection/test")
 TEST_MFCC_ROOT = ".. /input/kkiller-rfcx-test-mfcc-1-0400/test_mfcc_d10_s10_sr32000"
ids_test = test['id']
ids = []
countries = []
for i in range(len(X_test)) :
idx = ids_test[i]
ids += [idx] * 5
countries += le.inverse_transform(np.argsort(XGBC_pred_test_prob[i])[::-1][:5] ).tolist()<save_to_csv>
def mono_to_color(X, eps=1e-6, mean=None, std=None):
X = np.stack([X, X, X], axis=-1)
 mean = mean or X.mean()
std = std or X.std()
X =(X - mean)/(std + eps)
 _min, _max = X.min() , X.max()
if(_max - _min)> eps:
V = np.clip(X, _min, _max)
 V = 255 *(V - _min)/(_max - _min)
 V = V.astype(np.uint8)
 else:
V = np.zeros_like(X, dtype=np.uint8)
 return V
def normalize(image, mean=None, std=None):
image = image / 255.0
if mean is not None and std is not None:
image =(image - mean)/ std
return np.moveaxis(image, 2, 0 ).astype(np.float32)
 def crop_or_pad(y, length, sr, is_train=True):
if len(y)< length:
y = np.concatenate([y, np.zeros(length - len(y)) ])
 elif len(y)> length:
if not is_train:
start = 0
else:
start = np.random.randint(len(y)- length)
 y = y[start:start + length]
y = y.astype(np.float32, copy=False)
 return y
submission = pd.DataFrame({
"id" : ids,
"country" : countries
})
 submission.to_csv('submission_XGBC.csv', index = False )<define_variables>
class RFCXDataset(Dataset):
def __init__(self, data, sr, n_mels=128, fmin=0, fmax=None, num_classes=NUM_CLASSES, duration=DURATION, stride=STRIDE, root=None):
self.data = data
self.sr = sr
self.n_mels = n_mels
self.fmin = fmin
self.fmax = fmax or self.sr//2
self.num_classes = num_classes
self.duration = duration
self.stride = stride
self.audio_length = self.duration*self.sr
self.root = root or TEST_AUDIO_ROOT
self.mel_spec_computer = MelSpecComputer(sr=self.sr, n_mels=self.n_mels, fmin=self.fmin, fmax=self.fmax)
 self.res_type = "kaiser_best"
def __len__(self):
return len(self.data)
 def load(self, record):
y, _ = lb.load(self.root.joinpath(record ).with_suffix(".flac" ).as_posix() , sr=self.sr, res_type=self.res_type)
 return y
def load2(self, record):
y, orig_sr = sf.read(self.root.joinpath(record ).with_suffix(".flac" ).as_posix())
 y = lb.resample(y, orig_sr=orig_sr, target_sr=self.sr, res_type=self.res_type)
 return y
def read_index(self, idx):
d = self.data.iloc[idx]
record = d["recording_id"]
y = self.load2(record)
 window = self.duration*self.sr
stride = self.stride*self.sr
y = np.stack([y[i:i+window] for i in range(0, 60*self.sr+stride-window, stride)])
 return y
def process(self, y):
melspec = self.mel_spec_computer(y)
 image = mono_to_color(melspec)
 image = normalize(image, mean=None, std=None)
 return image
def __getitem__(self, idx):
y = self.read_index(idx)
 image = np.stack([self.process(_y)for _y in y])
 return image
n_labels=len(set(y_des))
n_labels<init_hyperparams>
class SimpleRFCXDataset(Dataset):
def __init__(self, data):
self.data = data
def __getitem__(self, idx):
row = self.data.iloc[idx]
record_id_path = Path(row.mfcc_root ).joinpath(row.recording_id ).with_suffix(".npy")
 image = np.load(record_id_path)
 return image
def __len__(self):
return len(self.data )
params = {
'objective': 'multi:softprob',
'eval_metric': 'merror',
'num_class': n_labels,
'eta': 0.5,
'max_depth': 6,
'subsample': 0.5,
'colsample_bytree': 0.3,
'silent': 1,
'seed': 123
}<compute_train_metric>
%%time
data = pd.DataFrame({
"recording_id": [path.stem for path in Path(TEST_AUDIO_ROOT ).glob("*.flac")],
})
 data["mfcc_root"] = TEST_MFCC_ROOT
print(data.shape)
 data.head()
num_boost_round = 50
Dtrain = xgb.DMatrix(X_train, y_trans)
 res = xgb.cv(params, Dtrain, num_boost_round=num_boost_round, nfold=5,
callbacks=[xgb.callback.print_evaluation(show_stdv=True),
xgb.callback.early_stop(50)] )<train_model>
TEST_MFCC_ROOTs = [
".. /input/kkiller-rfcx-test-mfcc-0000-0400/test_mfcc_d10_s2_sr32000_0000_0400",
".. /input/kkiller-rfcx-test-mfcc-0400-0800/test_mfcc_d10_s2_sr32000_0400_0800",
".. /input/kkiller-rfcx-test-mfcc-0800-1200/test_mfcc_d10_s2_sr32000_0800_1200",
".. /input/kkiller-rfcx-test-mfcc-1200-1600/test_mfcc_d10_s2_sr32000_1200_1600",
".. /input/kkiller-rfcx-test-mfcc-1600-2000/test_mfcc_d10_s2_sr32000_1600_2000",
]
num_boost_round = res['test-merror-mean'].idxmin()
print(format(num_boost_round))
clf = xgb.train(params, Dtrain, num_boost_round=num_boost_round)
 clf<create_dataframe>
mfccs = []
for mfcc_root in TEST_MFCC_ROOTs:
mfccs += [(mfcc.stem, mfcc.parent.as_posix())for mfcc in Path(mfcc_root ).glob("*.npy")]
mfccs = pd.DataFrame(mfccs, columns = ["recording_id", 'mfcc_root'])
 data = data[["recording_id"]].merge(mfccs, on="recording_id")
 print(data.shape)
 data.head()
importance = clf.get_fscore()
importance_df = pd.DataFrame(
 sorted(importance.items() , key=operator.itemgetter(1)) ,
columns=['feature', 'fscore']
)<split>
ds = RFCXDataset(data=data, sr=SR )
importance_df = importance_df.iloc[-20:, :]<import_modules>
test_data = SimpleRFCXDataset(data)if(USE_PRE_COMPUTED_MFCC and TEST_MFCC_ROOT)else RFCXDataset(data=data, sr=SR)
 test_loader = DataLoader(test_data, batch_size=TEST_BATCH_SIZE, num_workers=TEST_NUM_WORKERS )
!pip install keras==2.2.4 
<compute_test_metric>
def load_net(checkpoint_path):
net = resnest50(pretrained=True)
 n_features = net.fc.in_features
net.fc = torch.nn.Linear(n_features, NUM_CLASSES)
 dummy_device = torch.device("cpu")
 net.load_state_dict(torch.load(checkpoint_path, map_location=dummy_device))
net = net.to(DEVICE)
 net = net.eval()
return net
class Length(layers.Layer):
def call(self, inputs, **kwargs):
return K.sqrt(K.sum(K.square(inputs), -1))
def compute_output_shape(self, input_shape):
return input_shape[:-1]<categorify>
checkpoint_paths = [
".. /input/kkiller-rfcx-species-detection-public-checkpoints/rfcx_resnest50/rfcx_resnest_50_fold0.pth",
".. /input/kkiller-rfcx-species-detection-public-checkpoints/rfcx_resnest50/rfcx_resnest_50_fold1.pth",
".. /input/kkiller-rfcx-species-detection-public-checkpoints/rfcx_resnest50/rfcx_resnest_50_fold2.pth",
".. /input/kkiller-rfcx-species-detection-public-checkpoints/rfcx_resnest50/rfcx_resnest_50_fold3.pth",
".. /input/kkiller-rfcx-species-detection-public-checkpoints/rfcx_resnest50/rfcx_resnest_50_fold4.pth",
]
nets = [
load_net(checkpoint_path)for checkpoint_path in checkpoint_paths
]
class Mask(layers.Layer):
def call(self, inputs, **kwargs):
if type(inputs)is list:
assert len(inputs)== 2
inputs, mask = inputs
else:
x = inputs
x =(x - K.max(x, 1, True)) / K.epsilon() + 1
mask = K.clip(x, 0, 1)
 inputs_masked = K.batch_dot(inputs, mask, [1, 1])
 return inputs_masked
def compute_output_shape(self, input_shape):
if type(input_shape[0])is tuple:
return tuple([None, input_shape[0][-1]])
 else:
return tuple([None, input_shape[-1]] )<statistical_test>
preds = []
with torch.no_grad() :
for xb in tqdm(test_loader):
bsize, nframes = xb.shape[:2]
xb = xb.to(DEVICE ).view(bsize*nframes, *xb.shape[2:])
 pred = 0.
 for net in nets:
o = net(xb)
 o = torch.sigmoid(o)
 o = o.view(bsize, nframes, *o.shape[1:] ).max(1 ).values
o = o.detach().cpu().numpy()
pred += o
pred /= len(nets)
 preds.append(pred)
 preds = np.vstack(preds)
 preds.shape
def squash(vectors, axis=-1):
s_squared_norm = K.sum(K.square(vectors), axis, keepdims=True)
 scale = s_squared_norm /(1 + s_squared_norm)/ K.sqrt(s_squared_norm)
 return scale * vectors<choose_model_class>
sub = pd.DataFrame(preds, columns=[f"s{i}" for i in range(24)])
 sub["recording_id"] = data["recording_id"].values[:len(sub)]
sub = sub[["recording_id"] + [f"s{i}" for i in range(24)]]
print(sub.shape)
 sub.head()
<concatenate><EOS> 
sub.to_csv("submission.csv", index=False )