Kaggle competition: Rainforest Connection Species Audio Detection
<SOS> metric: WeightedLabelRankingAveragePrecision Kaggle data source: rainforest-connection-species-audio-detection<predict_on_test>
%matplotlib inline
ked = pd.DataFrame({
'Kernel ID': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
'Symbol': ['SoliSet', '[Inference] ResNest RFCX Audio Detection', 'notebookba481ef16a', 'All-in-one RFCX baseline for beginners', 'RFCX: train resnet50 with TPU', 'RFCX Resnet50 TPU', 'ResNet34 More Augmentations+Mixup+TTA(Inference)', '[Inference][TPU] RFCX Audio Detection Fast++', 'RFCX Bagging'],
'Score': [ 0.589 , 0.594 , 0.613 , 0.748 , 0.793 , 0.824 , 0.845 , 0.861 , 0.871 ],
'File Path': ['.. /input/audio-detection-soliset-201/submission.csv', '.. /input/inference-resnest-rfcx-audio-detection/submission.csv', '.. /input/minimal-fastai-solution-score-0-61/submission.csv', '.. /input/all-in-one-rfcx-baseline-for-beginners/submission.csv', '.. /input/rfcx-train-resnet50-with-tpu/submission.csv', '.. /input/rfcx-resnet50-tpu/submission.csv', '.. /input/resnet34-more-augmentations-mixup-tta-inference/submission.csv', '.. /input/inference-tpu-rfcx-audio-detection-fast/submission.csv', '.. /input/rfcx-bagging-with-different-weights-0871-score/submission.csv'],
'Note' : ['xgboost & cuml(https://rapids.ai)', 'torch & resnest50', 'fastai.vision & torchaudio', 'torch & resnest50', 'tensorflow & tf.keras.Sequential', 'tensorflow & tf.keras.Sequential', 'tensorflow & classification_models.keras', 'torch & resnest50', 'To sort the scores and use their ranks.']
})
 ked
model0 = learn2d_fold0.model.eval().to(device)
 model1 = learn2d_fold1.model.eval().to(device)
 model2 = learn2d_fold2.model.eval().to(device)
 model3 = learn2d_fold3.model.eval().to(device)
 model4 = learn2d_fold4.model.eval().to(device )<import_modules>
class Kernel() :

 def __init__(self, symbol, score, file_path):
self.symbol = symbol
self.score = score
self.file_path = file_path
self.sub = pd.read_csv(self.file_path)
 def __str__(self):
return f'Kernel: {self.symbol}\t| Score: {self.score}'
def __repr__(self):
return f'Class: {self.__class__.__name__}
Name: {repr(self.symbol)}\t| Score: {self.score}'
def print_head(self):
print(self)
 print(f'
Head:
')
 print(self.sub.head())
 def print_description(self):
print(self)
 print(f'
Description:
')
 print(self.sub.describe())
 def generation(self, other, coeff):
g1 = self.sub.copy()
g2 = self.sub.copy()
g3 = self.sub.copy()
g4 = self.sub.copy()
if isinstance(other, Kernel):
for i in self.sub.columns[1:]:
lm, Is = [], []
lm = self.sub[i].tolist()
ls = other.sub[i].tolist()
res1, res2, res3, res4 = [], [], [], []
for j in range(len(self.sub)) :
res1.append(max(lm[j] , ls[j]))
res2.append(min(lm[j] , ls[j]))
res3.append(( lm[j] + ls[j])/ 2)
 res4.append(( lm[j] * coeff)+(ls[j] *(1.- coeff)))
 g1[i] = res1
g2[i] = res2
g3[i] = res3
g4[i] = res4
return g1,g2,g3,g4
for i in range(9):
ked.iloc[i, 0] = Kernel(ked.iloc[i, 1], ked.iloc[i, 2], ked.iloc[i, 3])
from fastai.text.all import *<define_variables>
def generate(main, support, coeff):
g1 = main.copy()
g2 = main.copy()
g3 = main.copy()
g4 = main.copy()
for i in main.columns[1:]:
lm, Is = [], []
lm = main[i].tolist()
ls = support[i].tolist()
res1, res2, res3, res4 = [], [], [], []
for j in range(len(main)) :
res1.append(max(lm[j] , ls[j]))
res2.append(min(lm[j] , ls[j]))
res3.append(( lm[j] + ls[j])/ 2)
 res4.append(( lm[j] * coeff)+(ls[j] *(1.- coeff)))
 g1[i] = res1
g2[i] = res2
g3[i] = res3
g4[i] = res4
return g1,g2,g3,g4
input_pad_idx = None<choose_model_class>
g1,g2,g3,g4 = generate(ked.iloc[6, 0].sub, ked.iloc[5, 0].sub, 0.8)
class AWD_LSTM(Module):
"AWD-LSTM inspired by https://arxiv.org/abs/1708.02182"
initrange=0.1
def __init__(self, emb_sz,n_hid, n_layers, hidden_p=0.2, input_p=0.6, weight_p=0.5, bidir=False):
store_attr('emb_sz,n_hid,n_layers')
 self.bs = 1
self.n_dir = 2 if bidir else 1
self.rnns = nn.ModuleList([self._one_rnn(emb_sz if l == 0 else n_hid,(n_hid)//self.n_dir, bidir, weight_p, l)for l in range(n_layers)])
 self.input_dp = RNNDropout(input_p)
 self.hidden_dps = nn.ModuleList([RNNDropout(hidden_p)for l in range(n_layers)])
 self.reset()
def forward(self, x, from_embeds=False):
if from_embeds: inp = x
else: inp = combined_embeddings[x].to(device)
 bs,sl = inp.shape[:2]
if bs!=self.bs: self._change_hidden(bs)
 output = self.input_dp(inp)
 new_hidden = []
for l,(rnn,hid_dp)in enumerate(zip(self.rnns, self.hidden_dps)) :
output, new_h = rnn(output, self.hidden[l])
 new_hidden.append(new_h)
 if l != self.n_layers - 1: output = hid_dp(output)
 self.hidden = to_detach(new_hidden, cpu=False, gather=False)
 return output
def _change_hidden(self, bs):
self.hidden = [self._change_one_hidden(l, bs)for l in range(self.n_layers)]
self.bs = bs
def _one_rnn(self, n_in, n_out, bidir, weight_p, l):
"Return one of the inner rnn"
rnn = nn.LSTM(n_in, n_out, 1, batch_first=True, bidirectional=bidir, bias=False)
 return WeightDropout(rnn, weight_p)
 def _one_hidden(self, l):
"Return one hidden state"
nh =(self.n_hid)// self.n_dir
return(one_param(self ).new_zeros(self.n_dir, self.bs, nh ).to(device), one_param(self ).new_zeros(self.n_dir, self.bs, nh ).to(device))
def _change_one_hidden(self, l, bs):
if self.bs < bs:
nh =(self.n_hid)// self.n_dir
return tuple(torch.cat([h, h.new_zeros(self.n_dir, bs-self.bs, nh)], dim=1)for h in self.hidden[l])
 if self.bs > bs: return(self.hidden[l][0][:,:bs].contiguous() , self.hidden[l][1][:,:bs].contiguous())
 return self.hidden[l]
def reset(self):
"Reset the hidden states"
[r.reset() for r in self.rnns if hasattr(r, 'reset')]
self.hidden = [self._one_hidden(l)for l in range(self.n_layers)]<categorify>
f1,f2,f3,f4 = generate(ked.iloc[7, 0].sub, g2, 0.8)
lstm_width = 512
layers = [lstm_width * 3] + [lstm_width] + [9]
class MultiHeadedSequenceClassifier(Module):
"dim: input sequence feature dim"
def __init__(self, bptt=72, input_pad_idx=input_pad_idx, n_meta=1, dim=1024):
store_attr('input_pad_idx')
 self.awd_lstm = AWD_LSTM(dim+n_meta, lstm_width, 2, bidir=True)
 self.encoder = SentenceEncoder(bptt=bptt, module=self.awd_lstm, pad_idx=input_pad_idx)
 self.seq_head = LinearDecoder(1, lstm_width, bias=True)
 self.exam_head = PoolingLinearClassifier(layers, ps=[0.4, 0.1], bptt=bptt)
 def forward(self, x):
out, mask = self.encoder(x)
 seq_cls_out,_,_ = self.seq_head(out)
 seq_cls_out = seq_cls_out.squeeze(-1)
 exam_out,_,_ = self.exam_head(( out,mask))
return(seq_cls_out, exam_out)
 def predict(self, x):
out = self.awd_lstm(x, from_embeds=True)
 seq_cls_out,_,_ = self.seq_head(out)
 seq_cls_out = seq_cls_out.squeeze(-1)
 mask = torch.zeros(x.shape[:-1] ).bool().to(device)
 exam_out,_,_ = self.exam_head(( out, mask))
return(seq_cls_out, exam_out)
<categorify>
e1,e2,e3,e4 = generate(ked.iloc[8, 0].sub, f2, 0.7)
layers1 = [512 * 3] + [512] + [12]
class MultiHeadedSoftmaxSequenceClassifier(Module):
"dim: input sequence feature dim"
def __init__(self, bptt=72, input_pad_idx=input_pad_idx, n_meta=2, dim=1024):
store_attr('input_pad_idx')
 self.awd_lstm = AWD_LSTM(dim+n_meta, 512, 2, bidir=True)
 self.encoder = SentenceEncoder(bptt=bptt, module=self.awd_lstm, pad_idx=input_pad_idx)
 self.seq_head = LinearDecoder(1, 512, bias=True)
 self.exam_head = PoolingLinearClassifier(layers1, ps=[0.4, 0.1], bptt=bptt)
 def forward(self, x):
out, mask = self.encoder(x)
 seq_cls_out,_,_ = self.seq_head(out)
 seq_cls_out = seq_cls_out.squeeze(-1)
 exam_out,_,_ = self.exam_head(( out,mask))
posneg_out, rvlv_out, lrc_out, chroacute_out =(exam_out[:,:3],
exam_out[:,3:6],
exam_out[:,6:9],
exam_out[:,9:])
 return(seq_cls_out, posneg_out, rvlv_out, lrc_out, chroacute_out)
 def predict(self, x):
out = self.awd_lstm(x, from_embeds=True)
 seq_cls_out,_,_ = self.seq_head(out)
 seq_cls_out = seq_cls_out.squeeze(-1)
 mask = torch.zeros(x.shape[:-1] ).bool().to(device)
 exam_out,_,_ = self.exam_head(( out, mask))
posneg_out, rvlv_out, lrc_out, chroacute_out =(exam_out[:,:3],
exam_out[:,3:6],
exam_out[:,6:9],
exam_out[:,9:])
 return(seq_cls_out, posneg_out, rvlv_out, lrc_out, chroacute_out )<load_pretrained>
d1,d2,d3,d4 = generate(ked.iloc[8, 0].sub, f2, 0.45)
seq_model0 = SequentialRNN(MultiHeadedSoftmaxSequenceClassifier(bptt=256, dim=1024, n_meta=2))
seq_model0.load_state_dict(torch.load(seqmodelpath/"nometa_sequence_softmax_with_preds_fulldata_fold0.pth")) ;
seq_model1 = SequentialRNN(MultiHeadedSoftmaxSequenceClassifier(bptt=256, dim=1024, n_meta=2))
seq_model1.load_state_dict(torch.load(seqmodelpath/"nometa_sequence_softmax_with_preds_fulldata_fold1.pth")) ;
seq_model2 = SequentialRNN(MultiHeadedSoftmaxSequenceClassifier(bptt=256, dim=1024, n_meta=2))
seq_model2.load_state_dict(torch.load(seqmodelpath/"nometa_sequence_softmax_with_preds_fulldata_fold2.pth")) ;
seq_model3 = SequentialRNN(MultiHeadedSoftmaxSequenceClassifier(bptt=256, dim=1024, n_meta=2))
seq_model3.load_state_dict(torch.load(seqmodelpath/"nometa_sequence_softmax_with_preds_fulldata_fold3.pth")) ;
seq_model4 = SequentialRNN(MultiHeadedSoftmaxSequenceClassifier(bptt=256, dim=1024, n_meta=2))
seq_model4.load_state_dict(torch.load(seqmodelpath/"nometa_sequence_softmax_with_preds_fulldata_fold4.pth")) ;<predict_on_test>
c1,c2,c3,c4 = generate(ked.iloc[8, 0].sub, f2, 0.475 )
seq_model0 = seq_model0[0].to(device ).eval()
seq_model1 = seq_model1[0].to(device ).eval()
seq_model2 = seq_model2[0].to(device ).eval()
seq_model3 = seq_model3[0].to(device ).eval()
seq_model4 = seq_model4[0].to(device ).eval()<define_variables>
sub = c4
<import_modules><EOS> 
sub.to_csv("submission.csv", index=False)
 c1.to_csv("submission1.csv", index=False)
 c2.to_csv("submission2.csv", index=False)
 c3.to_csv("submission3.csv", index=False)
 c4.to_csv("submission4.csv", index=False)
 !ls