Kaggle competition: Rainforest Connection Species Audio Detection
<SOS> metric: WeightedLabelRankingAveragePrecision Kaggle data source: rainforest-connection-species-audio-detection<define_search_space>
import pandas as pd, numpy as np
import os
lung_window =(1500, -600)
 pe_window =(700, 100)
 mediastinal_window =(400, 40)
 windows =(lung_window, pe_window, mediastinal_window)
 def read_dcm_img(dcm, windows=windows):
"Read single slice in RGB"
return torch.stack([dcm.windowed(*w)for w in windows] )<define_search_space>
paths = [
".. /input/best-submissions/submission_866.csv",
".. /input/best-submissions/submission_869.csv",
".. /input/bestsubmission/submission_879.csv",
".. /input/best-submission/submission_876.csv",
]
weights = np.array([0.038, 0.070, 0.745, 0.147])
 sum(weights )
def get_dls(tensors, size=256, bs=128):
"Get study dataloader"
tfms = [[RandomResizedCropGPU(size, min_scale=0.9)], []]
dsets = Datasets(tensors, tfms=tfms, splits=([0,1], [2,3]))
batch_tfms = []
dls = dsets.dataloaders(bs=bs, after_batch=batch_tfms, num_workers=2)
 return dls
dls = get_dls(torch.zeros(4, 3, 512, 512), size=480, bs=32)
 dls.c = 2<load_pretrained>
cols = [f"s{i}" for i in range(24)]
learn2d_fold0 = cnn_learner(dls, xresnet34, pretrained=False, loss_func=nn.CrossEntropyLoss() , model_dir='.')
 learn2d_fold0.path = cnn2dmodelpath
learn2d_fold0.load('xresnet34-512-PR-fold0');<load_pretrained>
scores = []
for path in paths:
df = pd.read_csv(path ).sort_values("recording_id" ).reset_index(drop=True)
 score = np.empty(( len(df), 24))
o = df[cols].values.argsort(1)
 score[np.arange(len(df)) [:, None], o] = np.arange(24)[None]
scores.append(score)
 scores = np.stack(scores)
 scores.shape
learn2d_fold1 = cnn_learner(dls, xresnet34, pretrained=False, loss_func=nn.CrossEntropyLoss() , model_dir='.')
 learn2d_fold1.path = cnn2dmodelpath
learn2d_fold1.load('xresnet34-512-PR-fold1');<load_pretrained>
sub_score = np.sum(scores*weights[:, None, None], 0)
 print(sub_score.shape)
 sub_score
learn2d_fold2 = cnn_learner(dls, xresnet34, pretrained=False, loss_func=nn.CrossEntropyLoss() , model_dir='.')
 learn2d_fold2.path = cnn2dmodelpath
learn2d_fold2.load('xresnet34-512-PR-fold2');<load_pretrained>
sub = pd.DataFrame(sub_score, columns=cols)
 sub["recording_id"] = df["recording_id"]
sub = sub[["recording_id"] + cols]
print(sub.shape)
 sub.head()
<load_pretrained><EOS> 
sub.to_csv("submission.csv", index=False )