Kaggle competition: Cornell Birdcall Identification
<SOS> metric: MeanFScoreBeta Kaggle data source: cornell-birdcall-identification<load_from_csv>
import cv2
import audioread
import logging
import os
import random
import time
import warnings
import librosa
import librosa.display as display
import numpy as np
import pandas as pd
import soundfile as sf
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.utils.data as data
from contextlib import contextmanager
from IPython.display import Audio
from pathlib import Path
from typing import Optional, List
from catalyst.dl import SupervisedRunner, State, CallbackOrder, Callback, CheckpointCallback
from fastprogress import progress_bar
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import f1_score, average_precision_score
train_df = pd.read_csv('.. /input/severstal-steel-defect-detection/train.csv')
 train_df['ImageId'] = train_df['ImageId_ClassId'].apply(lambda x: x.split('_')[0])
 train_df['ClassId'] = train_df['ImageId_ClassId'].apply(lambda x: x.split('_')[1] ).astype('int8')
 train_df['hasMask'] = ~ train_df['EncodedPixels'].isna()
train_df.drop(['ImageId_ClassId'], axis=1, inplace=True)
 train_df.fillna(0, inplace=True)
 train_df.head()<categorify>
def set_seed(seed: int = 42):
random.seed(seed)
 np.random.seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
def get_logger(out_file=None):
logger = logging.getLogger()
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
 logger.handlers = []
logger.setLevel(logging.INFO)
 handler = logging.StreamHandler()
handler.setFormatter(formatter)
 handler.setLevel(logging.INFO)
 logger.addHandler(handler)
 if out_file is not None:
fh = logging.FileHandler(out_file)
 fh.setFormatter(formatter)
 fh.setLevel(logging.INFO)
 logger.addHandler(fh)
 logger.info("logger set up")
 return logger
@contextmanager
def timer(name: str, logger: Optional[logging.Logger] = None):
t0 = time.time()
msg = f"[{name}] start"
if logger is None:
print(msg)
 else:
logger.info(msg)
 yield
msg = f"[{name}] done in {time.time() - t0:.2f} s"
if logger is None:
print(msg)
 else:
logger.info(msg)
 set_seed(1213 )
DEFECT_TYPES = [1, 2, 3, 4]
for x in DEFECT_TYPES:
train_df.loc[train_df.ClassId==x, 'pixel_count'] = train_df.loc[train_df.ClassId==x, 'EncodedPixels'].apply(pixel_count )<define_variables>
ROOT = Path.cwd().parent
INPUT_ROOT = ROOT / "input"
RAW_DATA = INPUT_ROOT / "birdsong-recognition"
TRAIN_AUDIO_DIR = RAW_DATA / "train_audio"
TRAIN_RESAMPLED_AUDIO_DIRS = [
INPUT_ROOT / "birdsong-resampled-train-audio-{:0>2}".format(i)for i in range(5)
 ]
TEST_AUDIO_DIR = RAW_DATA / "test_audio"
THRESHOLDS = []
for x in DEFECT_TYPES:
pixels = train_df.loc[train_df['ClassId']==x, 'pixel_count']
threshold = np.nanquantile(pixels, 0.1)
 THRESHOLDS.append(int(threshold))
print(f'Quintile threshold for defect type {x} = {int(threshold)}' )<feature_engineering>
train = pd.read_csv(TRAIN_RESAMPLED_AUDIO_DIRS[0] / "train_mod.csv")
 if not TEST_AUDIO_DIR.exists() :
TEST_AUDIO_DIR = INPUT_ROOT / "birdcall-check" / "test_audio"
test = pd.read_csv(INPUT_ROOT / "birdcall-check" / "test.csv")
 else:
test = pd.read_csv(RAW_DATA / "test.csv" )
THRESHOLDS[3] = 3500<install_modules>
class DropStripes(nn.Module):
def __init__(self, dim, drop_width, stripes_num):

 super(DropStripes, self ).__init__()
assert dim in [2, 3]
self.dim = dim
self.drop_width = drop_width
self.stripes_num = stripes_num
def forward(self, input):

 assert input.ndimension() == 4
if self.training is False:
return input
else:
batch_size = input.shape[0]
total_width = input.shape[self.dim]
for n in range(batch_size):
self.transform_slice(input[n], total_width)
 return input
def transform_slice(self, e, total_width):

 for _ in range(self.stripes_num):
distance = torch.randint(low=0, high=self.drop_width, size=(1,)) [0]
bgn = torch.randint(low=0, high=total_width - distance, size=(1,)) [0]
if self.dim == 2:
e[:, bgn : bgn + distance, :] = 0
elif self.dim == 3:
e[:, :, bgn : bgn + distance] = 0
class SpecAugmentation(nn.Module):
def __init__(self, time_drop_width, time_stripes_num, freq_drop_width,
freq_stripes_num):

 super(SpecAugmentation, self ).__init__()
self.time_dropper = DropStripes(dim=2, drop_width=time_drop_width,
stripes_num=time_stripes_num)
 self.freq_dropper = DropStripes(dim=3, drop_width=freq_drop_width,
stripes_num=freq_stripes_num)
 def forward(self, input):
x = self.time_dropper(input)
 x = self.freq_dropper(x)
 return x
!pip install.. /input/pretrainedmodels/pretrainedmodels-0.7.4/pretrainedmodels-0.7.4/ > /dev/null
package_path = '.. /input/unetmodelscript'
sys.path.append(package_path )<import_modules>
def init_layer(layer):
nn.init.xavier_uniform_(layer.weight)
 if hasattr(layer, "bias"):
if layer.bias is not None:
layer.bias.data.fill_(0.)
def init_bn(bn):
bn.bias.data.fill_(0.)
bn.weight.data.fill_(1.0)
 def interpolate(x: torch.Tensor, ratio: int):

(batch_size, time_steps, classes_num)= x.shape
upsampled = x[:, :, None, :].repeat(1, 1, ratio, 1)
 upsampled = upsampled.reshape(batch_size, time_steps * ratio, classes_num)
 return upsampled
def pad_framewise_output(framewise_output: torch.Tensor, frames_num: int):

 pad = framewise_output[:, -1:, :].repeat(
 1, frames_num - framewise_output.shape[1], 1)

output = torch.cat(( framewise_output, pad), dim=1)

return output
class ConvBlock(nn.Module):
def __init__(self, in_channels: int, out_channels: int):
super().__init__()
self.conv1 = nn.Conv2d(
 in_channels=in_channels,
out_channels=out_channels,
kernel_size=(3, 3),
stride=(1, 1),
padding=(1, 1),
bias=False)
 self.conv2 = nn.Conv2d(
 in_channels=out_channels,
out_channels=out_channels,
kernel_size=(3, 3),
stride=(1, 1),
padding=(1, 1),
bias=False)
 self.bn1 = nn.BatchNorm2d(out_channels)
 self.bn2 = nn.BatchNorm2d(out_channels)
 self.init_weight()
def init_weight(self):
init_layer(self.conv1)
 init_layer(self.conv2)
 init_bn(self.bn1)
 init_bn(self.bn2)
 def forward(self, input, pool_size=(2, 2), pool_type='avg'):
x = input
x = F.relu_(self.bn1(self.conv1(x)))
 x = F.relu_(self.bn2(self.conv2(x)))
 if pool_type == 'max':
x = F.max_pool2d(x, kernel_size=pool_size)
 elif pool_type == 'avg':
x = F.avg_pool2d(x, kernel_size=pool_size)
 elif pool_type == 'avg+max':
x1 = F.avg_pool2d(x, kernel_size=pool_size)
 x2 = F.max_pool2d(x, kernel_size=pool_size)
 x = x1 + x2
else:
raise Exception('Incorrect argument!')
 return x
class AttBlock(nn.Module):
def __init__(self,
in_features: int,
out_features: int,
activation="linear",
temperature=1.0):
super().__init__()
self.activation = activation
self.temperature = temperature
self.att = nn.Conv1d(
 in_channels=in_features,
out_channels=out_features,
kernel_size=1,
stride=1,
padding=0,
bias=True)
 self.cla = nn.Conv1d(
 in_channels=in_features,
out_channels=out_features,
kernel_size=1,
stride=1,
padding=0,
bias=True)
 self.bn_att = nn.BatchNorm1d(out_features)
 self.init_weights()
def init_weights(self):
init_layer(self.att)
 init_layer(self.cla)
 init_bn(self.bn_att)
 def forward(self, x):
norm_att = torch.softmax(torch.clamp(self.att(x), -10, 10), dim=-1)
 cla = self.nonlinear_transform(self.cla(x))
x = torch.sum(norm_att * cla, dim=2)
 return x, norm_att, cla
def nonlinear_transform(self, x):
if self.activation == 'linear':
return x
elif self.activation == 'sigmoid':
return torch.sigmoid(x )
import pdb
import cv2
import torch
from tqdm import tqdm
import torch.backends.cudnn as cudnn
from torch.utils.data import DataLoader, Dataset
from albumentations import(Normalize, Compose)
 from albumentations.pytorch import ToTensor
import torch.utils.data as data
from model import Unet<categorify>
class PANNsCNN14Att(nn.Module):
def __init__(self, sample_rate: int, window_size: int, hop_size: int,
mel_bins: int, fmin: int, fmax: int, classes_num: int):
super().__init__()
window = 'hann'
center = True
pad_mode = 'reflect'
ref = 1.0
amin = 1e-10
top_db = None
self.interpolate_ratio = 32
self.spectrogram_extractor = Spectrogram(
 n_fft=window_size,
hop_length=hop_size,
win_length=window_size,
window=window,
center=center,
pad_mode=pad_mode,
freeze_parameters=True)
 self.logmel_extractor = LogmelFilterBank(
 sr=sample_rate,
n_fft=window_size,
n_mels=mel_bins,
fmin=fmin,
fmax=fmax,
ref=ref,
amin=amin,
top_db=top_db,
freeze_parameters=True)
 self.spec_augmenter = SpecAugmentation(
 time_drop_width=64,
time_stripes_num=2,
freq_drop_width=8,
freq_stripes_num=2)
 self.bn0 = nn.BatchNorm2d(mel_bins)
 self.conv_block1 = ConvBlock(in_channels=1, out_channels=64)
 self.conv_block2 = ConvBlock(in_channels=64, out_channels=128)
 self.conv_block3 = ConvBlock(in_channels=128, out_channels=256)
 self.conv_block4 = ConvBlock(in_channels=256, out_channels=512)
 self.conv_block5 = ConvBlock(in_channels=512, out_channels=1024)
 self.conv_block6 = ConvBlock(in_channels=1024, out_channels=2048)
 self.fc1 = nn.Linear(2048, 2048, bias=True)
 self.att_block = AttBlock(2048, classes_num, activation='sigmoid')
 self.init_weight()
def init_weight(self):
init_bn(self.bn0)
 init_layer(self.fc1)
 def cnn_feature_extractor(self, x):
x = self.conv_block1(x, pool_size=(2, 2), pool_type='avg')
 x = F.dropout(x, p=0.2, training=self.training)
 x = self.conv_block2(x, pool_size=(2, 2), pool_type='avg')
 x = F.dropout(x, p=0.2, training=self.training)
 x = self.conv_block3(x, pool_size=(2, 2), pool_type='avg')
 x = F.dropout(x, p=0.2, training=self.training)
 x = self.conv_block4(x, pool_size=(2, 2), pool_type='avg')
 x = F.dropout(x, p=0.2, training=self.training)
 x = self.conv_block5(x, pool_size=(2, 2), pool_type='avg')
 x = F.dropout(x, p=0.2, training=self.training)
 x = self.conv_block6(x, pool_size=(1, 1), pool_type='avg')
 x = F.dropout(x, p=0.2, training=self.training)
 return x
def preprocess(self, input, mixup_lambda=None):
x = self.spectrogram_extractor(input)
 x = self.logmel_extractor(x)
 frames_num = x.shape[2]
x = x.transpose(1, 3)
 x = self.bn0(x)
 x = x.transpose(1, 3)
 if self.training:
x = self.spec_augmenter(x)
 if self.training and mixup_lambda is not None:
x = do_mixup(x, mixup_lambda)
 return x, frames_num
def forward(self, input, mixup_lambda=None):

 x, frames_num = self.preprocess(input, mixup_lambda=mixup_lambda)
 x = self.cnn_feature_extractor(x)
 x = torch.mean(x, dim=3)
 x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1)
 x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1)
 x = x1 + x2
x = F.dropout(x, p=0.5, training=self.training)
 x = x.transpose(1, 2)
 x = F.relu_(self.fc1(x))
x = x.transpose(1, 2)
 x = F.dropout(x, p=0.5, training=self.training)
(clipwise_output, norm_att, segmentwise_output)= self.att_block(x)
 segmentwise_output = segmentwise_output.transpose(1, 2)
 framewise_output = interpolate(segmentwise_output,
self.interpolate_ratio)
 framewise_output = pad_framewise_output(framewise_output, frames_num)
 output_dict = {
'framewise_output': framewise_output,
'clipwise_output': clipwise_output
}
return output_dict
def mask2rle(img):

 pixels= img.T.flatten()
pixels = np.concatenate([[0], pixels, [0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs)
 class TestDataset(Dataset):

 def __init__(self, root, df, mean, std):
self.root = root
df['ImageId'] = df['ImageId_ClassId'].apply(lambda x: x.split('_')[0])
 self.fnames = df['ImageId'].unique().tolist()
self.num_samples = len(self.fnames)
 self.transform = Compose(
 [
Normalize(mean=mean, std=std, p=1),
ToTensor() ,
] 
)
 def __getitem__(self, idx):
fname = self.fnames[idx]
path = os.path.join(self.root, fname)
 image = cv2.imread(path)
 images = self.transform(image=image)["image"]
return fname, images
def __len__(self):
return self.num_samples<categorify>
SR = 32000
y, _ = librosa.load(TRAIN_RESAMPLED_AUDIO_DIRS[0] / "aldfly" / "XC134874.wav",
sr=SR,
res_type="kaiser_fast",
mono=True)
def post_process(probability, threshold, min_size):

 mask = cv2.threshold(probability, threshold, 1, cv2.THRESH_BINARY)[1]
num_component, component = cv2.connectedComponents(mask.astype(np.uint8))
predictions = np.zeros(( 256, 1600), np.float32)
 num = 0
for c in range(1, num_component):
p =(component == c)
 if p.sum() > min_size:
predictions[p] = 1
num += 1
return predictions, num
sample_submission_path = '.. /input/severstal-steel-defect-detection/sample_submission.csv'
test_data_folder = ".. /input/severstal-steel-defect-detection/test_images"
best_threshold = 0.5
num_workers = 2
batch_size = 4
min_size = 0
mean =(0.485, 0.456, 0.406)
 std =(0.229, 0.224, 0.225)
 df = pd.read_csv(sample_submission_path)
 testset = DataLoader(
 TestDataset(test_data_folder, df, mean, std),
batch_size=batch_size,
shuffle=False,
num_workers=num_workers,
pin_memory=True 
)
 ckpt_path = ".. /input/unetstartermodelfile/model.pth"
device = torch.device("cuda")
 model = Unet("resnet18", encoder_weights=None, classes=4, activation=None)
 model.to(device)
 model.eval()
state = torch.load(ckpt_path, map_location=lambda storage, loc: storage)
 model.load_state_dict(state["state_dict"])
 predictions = []
for i, batch in enumerate(testset):
fnames, images = batch
batch_preds = torch.sigmoid(model(images.to(device)))
 batch_preds = batch_preds.detach().cpu().numpy()
for fname, preds in zip(fnames, batch_preds):
for cls, pred in enumerate(preds):
pred, num = post_process(pred, best_threshold, min_size)
 rle = mask2rle(pred)
 name = fname + f"_{cls+1}"
predictions.append([name, rle])
 sub_init = pd.DataFrame(predictions, columns=['ImageId_ClassId', 'EncodedPixels'] )<define_variables>
model_config = {
"sample_rate": 32000,
"window_size": 1024,
"hop_size": 320,
"mel_bins": 64,
"fmin": 20,
"fmax": 16000,
"classes_num": 264
}
model = PANNsCNN14Att(**model_config )
INIT_THRESHOLD = np.full(4, 3500 )<drop_column>
melspec_numpy = melspec.detach().numpy() [0, 0].transpose(1, 0)
 display.specshow(melspec_numpy, sr=SR, y_axis="mel");
sub_1 = sub_init.copy()
sub_2 = sub_init.copy()
for i in range(len(THRESHOLDS)) :
threshold_init = INIT_THRESHOLD[i]
threshold_new = THRESHOLDS[i]
defect = DEFECT_TYPES[i]
sub_1.loc[(sub_1['ClassId']==defect)&(sub_1.pixel_count < threshold_init), 'EncodedPixels'] = ''
sub_2.loc[(sub_2['ClassId']==defect)&(sub_2.pixel_count < threshold_new), 'EncodedPixels'] = ''
sub_1.drop(['ClassId', 'pixel_count'], axis=1, inplace=True)
 sub_2.drop(['ClassId', 'pixel_count'], axis=1, inplace=True )<define_variables>
BIRD_CODE = {
'aldfly': 0, 'ameavo': 1, 'amebit': 2, 'amecro': 3, 'amegfi': 4,
'amekes': 5, 'amepip': 6, 'amered': 7, 'amerob': 8, 'amewig': 9,
'amewoo': 10, 'amtspa': 11, 'annhum': 12, 'astfly': 13, 'baisan': 14,
'baleag': 15, 'balori': 16, 'banswa': 17, 'barswa': 18, 'bawwar': 19,
'belkin1': 20, 'belspa2': 21, 'bewwre': 22, 'bkbcuc': 23, 'bkbmag1': 24,
'bkbwar': 25, 'bkcchi': 26, 'bkchum': 27, 'bkhgro': 28, 'bkpwar': 29,
'bktspa': 30, 'blkpho': 31, 'blugrb1': 32, 'blujay': 33, 'bnhcow': 34,
'boboli': 35, 'bongul': 36, 'brdowl': 37, 'brebla': 38, 'brespa': 39,
'brncre': 40, 'brnthr': 41, 'brthum': 42, 'brwhaw': 43, 'btbwar': 44,
'btnwar': 45, 'btywar': 46, 'buffle': 47, 'buggna': 48, 'buhvir': 49,
'bulori': 50, 'bushti': 51, 'buwtea': 52, 'buwwar': 53, 'cacwre': 54,
'calgul': 55, 'calqua': 56, 'camwar': 57, 'cangoo': 58, 'canwar': 59,
'canwre': 60, 'carwre': 61, 'casfin': 62, 'caster1': 63, 'casvir': 64,
'cedwax': 65, 'chispa': 66, 'chiswi': 67, 'chswar': 68, 'chukar': 69,
'clanut': 70, 'cliswa': 71, 'comgol': 72, 'comgra': 73, 'comloo': 74,
'commer': 75, 'comnig': 76, 'comrav': 77, 'comred': 78, 'comter': 79,
'comyel': 80, 'coohaw': 81, 'coshum': 82, 'cowscj1': 83, 'daejun': 84,
'doccor': 85, 'dowwoo': 86, 'dusfly': 87, 'eargre': 88, 'easblu': 89,
'easkin': 90, 'easmea': 91, 'easpho': 92, 'eastow': 93, 'eawpew': 94,
'eucdov': 95, 'eursta': 96, 'evegro': 97, 'fiespa': 98, 'fiscro': 99,
'foxspa': 100, 'gadwal': 101, 'gcrfin': 102, 'gnttow': 103, 'gnwtea': 104,
'gockin': 105, 'gocspa': 106, 'goleag': 107, 'grbher3': 108, 'grcfly': 109,
'greegr': 110, 'greroa': 111, 'greyel': 112, 'grhowl': 113, 'grnher': 114,
'grtgra': 115, 'grycat': 116, 'gryfly': 117, 'haiwoo': 118, 'hamfly': 119,
'hergul': 120, 'herthr': 121, 'hoomer': 122, 'hoowar': 123, 'horgre': 124,
'horlar': 125, 'houfin': 126, 'houspa': 127, 'houwre': 128, 'indbun': 129,
'juntit1': 130, 'killde': 131, 'labwoo': 132, 'larspa': 133, 'lazbun': 134,
'leabit': 135, 'leafly': 136, 'leasan': 137, 'lecthr': 138, 'lesgol': 139,
'lesnig': 140, 'lesyel': 141, 'lewwoo': 142, 'linspa': 143, 'lobcur': 144,
'lobdow': 145, 'logshr': 146, 'lotduc': 147, 'louwat': 148, 'macwar': 149,
'magwar': 150, 'mallar3': 151, 'marwre': 152, 'merlin': 153, 'moublu': 154,
'mouchi': 155, 'moudov': 156, 'norcar': 157, 'norfli': 158, 'norhar2': 159,
'normoc': 160, 'norpar': 161, 'norpin': 162, 'norsho': 163, 'norwat': 164,
'nrwswa': 165, 'nutwoo': 166, 'olsfly': 167, 'orcwar': 168, 'osprey': 169,
'ovenbi1': 170, 'palwar': 171, 'pasfly': 172, 'pecsan': 173, 'perfal': 174,
'phaino': 175, 'pibgre': 176, 'pilwoo': 177, 'pingro': 178, 'pinjay': 179,
'pinsis': 180, 'pinwar': 181, 'plsvir': 182, 'prawar': 183, 'purfin': 184,
'pygnut': 185, 'rebmer': 186, 'rebnut': 187, 'rebsap': 188, 'rebwoo': 189,
'redcro': 190, 'redhea': 191, 'reevir1': 192, 'renpha': 193, 'reshaw': 194,
'rethaw': 195, 'rewbla': 196, 'ribgul': 197, 'rinduc': 198, 'robgro': 199,
'rocpig': 200, 'rocwre': 201, 'rthhum': 202, 'ruckin': 203, 'rudduc': 204,
'rufgro': 205, 'rufhum': 206, 'rusbla': 207, 'sagspa1': 208, 'sagthr': 209,
'savspa': 210, 'saypho': 211, 'scatan': 212, 'scoori': 213, 'semplo': 214,
'semsan': 215, 'sheowl': 216, 'shshaw': 217, 'snobun': 218, 'snogoo': 219,
'solsan': 220, 'sonspa': 221, 'sora': 222, 'sposan': 223, 'spotow': 224,
'stejay': 225, 'swahaw': 226, 'swaspa': 227, 'swathr': 228, 'treswa': 229,
'truswa': 230, 'tuftit': 231, 'tunswa': 232, 'veery': 233, 'vesspa': 234,
'vigswa': 235, 'warvir': 236, 'wesblu': 237, 'wesgre': 238, 'weskin': 239,
'wesmea': 240, 'wessan': 241, 'westan': 242, 'wewpew': 243, 'whbnut': 244,
'whcspa': 245, 'whfibi': 246, 'whtspa': 247, 'whtswi': 248, 'wilfly': 249,
'wilsni1': 250, 'wiltur': 251, 'winwre3': 252, 'wlswar': 253, 'wooduc': 254,
'wooscj2': 255, 'woothr': 256, 'y00475': 257, 'yebfly': 258, 'yebsap': 259,
'yehbla': 260, 'yelwar': 261, 'yerwar': 262, 'yetvir': 263
}
INV_BIRD_CODE = {v: k for k, v in BIRD_CODE.items() }
initial_sub_len = len(sub_1.loc[sub_1['EncodedPixels'] != '', :])
 modified_sub_len = len(sub_2.loc[sub_2['EncodedPixels'] != '', :])
 print(f'Positive instance case count with 3500 pixel threshold: {initial_sub_len}')
 print(f'Positive instance case count with new pixel thresholds: {modified_sub_len}' )<save_to_csv>
PERIOD = 10
class PANNsDataset(data.Dataset):
def __init__(
 self,
file_list: List[List[str]],
waveform_transforms=None):
self.file_list = file_list
self.waveform_transforms = waveform_transforms
def __len__(self):
return len(self.file_list)
 def __getitem__(self, idx: int):
wav_path, ebird_code = self.file_list[idx]
y, sr = sf.read(wav_path)
 if self.waveform_transforms:
y = self.waveform_transforms(y)
 else:
len_y = len(y)
 effective_length = sr * PERIOD
if len_y < effective_length:
new_y = np.zeros(effective_length, dtype=y.dtype)
 start = np.random.randint(effective_length - len_y)
 new_y[start:start + len_y] = y
y = new_y.astype(np.float32)
 elif len_y > effective_length:
start = np.random.randint(len_y - effective_length)
 y = y[start:start + effective_length].astype(np.float32)
 else:
y = y.astype(np.float32)
 labels = np.zeros(len(BIRD_CODE), dtype="f")
 for b_code in ebird_code:
labels[BIRD_CODE[b_code]] = 1
return {"waveform": y, "targets": labels}
sub_2.to_csv('submission.csv', index=False )<categorify>
class PANNsLoss(nn.Module):
def __init__(self):
super().__init__()
self.bce = nn.BCELoss()
def forward(self, input, target):
input_ = input["clipwise_output"]
input_ = torch.where(torch.isnan(input_),
torch.zeros_like(input_),
input_)
 input_ = torch.where(torch.isinf(input_),
torch.zeros_like(input_),
input_)
 target = target.float()
input_ = torch.clamp(input_, 0.0, 1.0)
 return self.bce(input_, target )
def pixel_postprocess(sub, thresholds):

 def pixel_count(rle):
if np.logical_or(rle==0, rle==''):
return(np.nan)
 array = np.asarray([int(x)for x in rle.split() ])
 lengths = array[1::2]
return sum(lengths)
 DEFECT_TYPES = [1, 2, 3, 4]
sub['ClassId'] = sub['ImageId_ClassId'].apply(lambda x: x.split('_')[1] ).astype('int8')
 for x in DEFECT_TYPES:
sub.loc[sub.ClassId==x, 'pixel_count'] = sub.loc[sub.ClassId==x, 'EncodedPixels'].apply(pixel_count)
 for i in range(len(thresholds)) :
threshold = thresholds[i]
defect = DEFECT_TYPES[i]
sub.loc[(sub['ClassId']==defect)&(sub.pixel_count < threshold), 'EncodedPixels'] = ''
sub.drop(['ClassId', 'pixel_count'], axis=1, inplace=True)
 return(sub )<set_options>
class F1Callback(Callback):
def __init__(self,
input_key: str = "targets",
output_key: str = "logits",
model_output_key: str = "clipwise_output",
prefix: str = "f1"):
super().__init__(CallbackOrder.Metric)
 self.input_key = input_key
self.output_key = output_key
self.model_output_key = model_output_key
self.prefix = prefix
def on_loader_start(self, state: State):
self.prediction: List[np.ndarray] = []
self.target: List[np.ndarray] = []
def on_batch_end(self, state: State):
targ = state.input[self.input_key].detach().cpu().numpy()
out = state.output[self.output_key]
clipwise_output = out[self.model_output_key].detach().cpu().numpy()
self.prediction.append(clipwise_output)
 self.target.append(targ)
 y_pred = clipwise_output.argmax(axis=1)
 y_true = targ.argmax(axis=1)
 score = f1_score(y_true, y_pred, average="macro")
 state.batch_metrics[self.prefix] = score
def on_loader_end(self, state: State):
y_pred = np.concatenate(self.prediction, axis=0 ).argmax(axis=1)
 y_true = np.concatenate(self.target, axis=0 ).argmax(axis=1)
 score = f1_score(y_true, y_pred, average="macro")
 state.loader_metrics[self.prefix] = score
if state.is_valid_loader:
state.epoch_metrics[state.valid_loader + "_epoch_" +
self.prefix] = score
else:
state.epoch_metrics["train_epoch_" + self.prefix] = score
class mAPCallback(Callback):
def __init__(self,
input_key: str = "targets",
output_key: str = "logits",
model_output_key: str = "clipwise_output",
prefix: str = "mAP"):
super().__init__(CallbackOrder.Metric)
 self.input_key = input_key
self.output_key = output_key
self.model_output_key = model_output_key
self.prefix = prefix
def on_loader_start(self, state: State):
self.prediction: List[np.ndarray] = []
self.target: List[np.ndarray] = []
def on_batch_end(self, state: State):
targ = state.input[self.input_key].detach().cpu().numpy()
out = state.output[self.output_key]
clipwise_output = out[self.model_output_key].detach().cpu().numpy()
self.prediction.append(clipwise_output)
 self.target.append(targ)
 score = average_precision_score(targ, clipwise_output, average=None)
 score = np.nan_to_num(score ).mean()
state.batch_metrics[self.prefix] = score
def on_loader_end(self, state: State):
y_pred = np.concatenate(self.prediction, axis=0)
 y_true = np.concatenate(self.target, axis=0)
 score = average_precision_score(y_true, y_pred, average=None)
 score = np.nan_to_num(score ).mean()
state.loader_metrics[self.prefix] = score
if state.is_valid_loader:
state.epoch_metrics[state.valid_loader + "_epoch_" +
self.prefix] = score
else:
state.epoch_metrics["train_epoch_" + self.prefix] = score
%matplotlib inline
warnings.filterwarnings('ignore' )<load_from_csv>
tmp_list = []
for audio_d in TRAIN_RESAMPLED_AUDIO_DIRS:
if not audio_d.exists() :
continue
for ebird_d in audio_d.iterdir() :
if ebird_d.is_file() :
continue
for wav_f in ebird_d.iterdir() :
tmp_list.append([ebird_d.name, wav_f.name, wav_f.as_posix() ])
 train_wav_path_exist = pd.DataFrame(
 tmp_list, columns=["ebird_code", "resampled_filename", "file_path"])
 del tmp_list
train_all = pd.merge(
 train, train_wav_path_exist, on=["ebird_code", "resampled_filename"], how="inner")
 print(train.shape)
 print(train_wav_path_exist.shape)
 print(train_all.shape )
df_train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 df_train.head()<drop_column>
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
 train_all["fold"] = -1
for fold_id,(train_index, val_index)in enumerate(skf.split(train_all, train_all["ebird_code"])) :
train_all.iloc[val_index, -1] = fold_id
fold_proportion = pd.pivot_table(train_all, index="ebird_code", columns="fold", values="xc_id", aggfunc=len)
 print(fold_proportion.shape )
df_train.drop('Id',axis=1,inplace=True )<sort_values>
code_map =dict(zip(train_all['primary_label'].tolist() ,train_all['ebird_code'].tolist()))
len(code_map )
def visualNA(df,perc=0):
NAN = [(c, df[c].isna().mean() *100)for c in df]
NAN = pd.DataFrame(NAN, columns=["column_name", "percentage"])
 NAN = NAN[NAN.percentage > perc]
print(NAN.sort_values("percentage", ascending=False)) 
<categorify>
train_all['sec_code'] = train_all.secondary_labels.apply(lambda x : [code_map[i] for i in eval(x)if i in code_map.keys() ])
 train_all[train_all.sec_code.apply(len)>0].shape
def handleNA(df):
df['Alley'].fillna(value='No alley access',inplace=True)
 df['BsmtQual'].fillna(value='No Basement',inplace=True)
 df['BsmtCond'].fillna(value='No Basement',inplace=True)
 df['BsmtExposure'].fillna(value='No Basement',inplace=True)
 df['BsmtFinType1'].fillna(value='No Basement',inplace=True)
 df['BsmtFinType2'].fillna(value='No Basement',inplace=True)
 df['FireplaceQu'].fillna(value='No Fireplace',inplace=True)
 df['GarageType'].fillna(value='No Garage',inplace=True)
 df['GarageYrBlt'].fillna(value=0,inplace=True)
 df['GarageFinish'].fillna(value='No Garage',inplace=True)
 df['GarageQual'].fillna(value='No Garage',inplace=True)
 df['GarageCond'].fillna(value='No Garage',inplace=True)
 df['MasVnrType'].fillna(value='None',inplace=True)
 df['MasVnrArea'].fillna(value=0.0,inplace=True)
 df['PoolQC'].fillna(value='No Pool',inplace=True)
 df['Fence'].fillna(value='No Fence',inplace=True)
 df['MiscFeature'].fillna(value='None',inplace=True)
<filter>
train_all["final_bird"] = train_all.apply(lambda x : [x['ebird_code']] + x['sec_code'] ,axis=1)
 train_all[['ebird_code','final_bird','sec_code']]
df_train[df_train['Electrical'].isnull() ]<filter>
use_fold = 3
train_file_list = train_all.query("fold != @use_fold")[["file_path", "final_bird"]].values.tolist()
val_file_list = train_all.query("fold == @use_fold")[["file_path", "final_bird"]].values.tolist()
print("[fold {}] train: {}, val: {}".format(use_fold, len(train_file_list), len(val_file_list)) )
df_train[(df_train['Electrical'].notnull())&(df_train['MSSubClass']==80)
 &(df_train['LotFrontage']==73)]['Electrical']<feature_engineering>
device = torch.device("cuda:0")
 loaders = {
"train": data.DataLoader(PANNsDataset(train_file_list, None),
batch_size=40,
shuffle=True,
num_workers=2,
pin_memory=True,
drop_last=True),
"valid": data.DataLoader(PANNsDataset(val_file_list, None),
batch_size=64,
shuffle=False,
num_workers=2,
pin_memory=True,
drop_last=False)
 }
model_config["classes_num"] = 527
model = PANNsCNN14Att(**model_config)
 weights = torch.load(".. /input/pannscnn14-decisionlevelatt-weight/Cnn14_DecisionLevelAtt_mAP0.425.pth")
 model.load_state_dict(weights["model"])
 model.att_block = AttBlock(2048, 264, activation='sigmoid')
 model.att_block.init_weights()
model.to(device)
 optimizer = optim.Adam(model.parameters() , lr=0.001)
 scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=10)
 criterion = PANNsLoss().to(device)
 callbacks = [
F1Callback(input_key="targets", output_key="logits", prefix="f1"),
mAPCallback(input_key="targets", output_key="logits", prefix="mAP"),
CheckpointCallback(save_n_best=0)
 ]
df_train.loc[df_train['Electrical'].isnull() ,'Electrical'] = 'SBrkr'<define_variables>
model_config = {
"sample_rate": 32000,
"window_size": 1024,
"hop_size": 320,
"mel_bins": 64,
"fmin": 20,
"fmax": 16000,
"classes_num": 264
}
weights_path = ".. /input/pan-model-new/fold1/checkpoints/best.pth"
dict_neighbor = {
'NAmes' :{'lat': 42.045830,'lon': -93.620767},
'CollgCr':{'lat': 42.018773,'lon': -93.685543},
'OldTown':{'lat': 42.030152,'lon': -93.614628},
'Edwards':{'lat': 42.021756,'lon': -93.670324},
'Somerst':{'lat': 42.050913,'lon': -93.644629},
'Gilbert':{'lat': 42.060214,'lon': -93.643179},
'NridgHt':{'lat': 42.060357,'lon': -93.655263},
'Sawyer' :{'lat': 42.034446,'lon': -93.666330},
'NWAmes' :{'lat': 42.049381,'lon': -93.634993},
'SawyerW':{'lat': 42.033494,'lon': -93.684085},
'BrkSide':{'lat': 42.032422,'lon': -93.626037},
'Crawfor':{'lat': 42.015189,'lon': -93.644250},
'Mitchel':{'lat': 41.990123,'lon': -93.600964},
'NoRidge':{'lat': 42.051748,'lon': -93.653524},
'Timber' :{'lat': 41.998656,'lon': -93.652534},
'IDOTRR' :{'lat': 42.022012,'lon': -93.622183},
'ClearCr':{'lat': 42.060021,'lon': -93.629193},
'StoneBr':{'lat': 42.060227,'lon': -93.633546},
'SWISU' :{'lat': 42.022646,'lon': -93.644853},
'MeadowV':{'lat': 41.991846,'lon': -93.603460},
'Blmngtn':{'lat': 42.059811,'lon': -93.638990},
'BrDale' :{'lat': 42.052792,'lon': -93.628820},
'Veenker':{'lat': 42.040898,'lon': -93.651502},
'NPkVill':{'lat': 42.049912,'lon': -93.626546},
'Blueste':{'lat': 42.010098,'lon': -93.647269}
}<feature_engineering>
def get_model(config: dict, weights_path: str):
model = PANNsCNN14Att(**config)
 checkpoint = torch.load(weights_path)
 model.load_state_dict(checkpoint["model_state_dict"])
 device = torch.device("cuda")
 model.to(device)
 model.eval()
return model
df_train['Lat'] = df_train['Neighborhood'].map(lambda neighbor: dict_neighbor[neighbor]['lat'])
 df_train['Lon'] = df_train['Neighborhood'].map(lambda neighbor: dict_neighbor[neighbor]['lon'] )<define_variables>
best_model = get_model(model_config, ".. /input/pan-model/pan_model/best.pth")
 model_2 = get_model(model_config, ".. /input/pan-model/pan_model/fold2.pth")
 model_4 = get_model(model_config, ".. /input/pan-model-new/fold4.pth")
 model_5 = get_model(model_config, ".. /input/pan-model/pan_model/fold0.pth")
Categorical_features = df_train.select_dtypes(include=['object'])
 Numerical_features = df_train.select_dtypes(exclude=['object'] )<prepare_x_and_y>
def prediction_for_clip(test_df: pd.DataFrame,
clip: np.ndarray,
model: PANNsCNN14Att,
threshold=0.5):
PERIOD = 10
audios = []
y = clip.astype(np.float32)
 len_y = len(y)
 start = 0
end = PERIOD * SR
while True:
y_batch = y[start:end].astype(np.float32)
 if len(y_batch)!= PERIOD * SR:
y_pad = np.zeros(PERIOD * SR, dtype=np.float32)
 y_pad[:len(y_batch)] = y_batch
audios.append(y_pad)
 break
start = end
end += PERIOD * SR
audios.append(y_batch)
 array = np.asarray(audios)
 tensors = torch.from_numpy(array)
 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 model.eval()
estimated_event_list = []
global_time = 0.0
site = test_df["site"].values[0]
audio_id = test_df["audio_id"].values[0]
for image in progress_bar(tensors):
image = image.view(1, image.size(0))
image = image.to(device)
 with torch.no_grad() :
prediction = model(image)["framewise_output"].detach().cpu().numpy() [0]
prediction_2 = best_model(image)["framewise_output"].detach().cpu().numpy() [0]
prediction_3 = model_2(image)["framewise_output"].detach().cpu().numpy() [0]
prediction_4 = model_4(image)["framewise_output"].detach().cpu().numpy() [0]
prediction_5 = model_5(image)["framewise_output"].detach().cpu().numpy() [0]
framewise_outputs =(prediction + prediction_2 + prediction_3 +prediction_4 +prediction_5)/5
thresholded = framewise_outputs >= threshold
for target_idx in range(thresholded.shape[1]):
if thresholded[:, target_idx].mean() == 0:
pass
else:
detected = np.argwhere(thresholded[:, target_idx] ).reshape(-1)
 head_idx = 0
tail_idx = 0
while True:
if(tail_idx + 1 == len(detected)) or(
 detected[tail_idx + 1] -
detected[tail_idx] != 1):
onset = 0.01 * detected[
head_idx] + global_time
offset = 0.01 * detected[
tail_idx] + global_time
onset_idx = detected[head_idx]
offset_idx = detected[tail_idx]
max_confidence = framewise_outputs[
onset_idx:offset_idx, target_idx].max()
mean_confidence = framewise_outputs[
onset_idx:offset_idx, target_idx].mean()
estimated_event = {
"site": site,
"audio_id": audio_id,
"ebird_code": INV_BIRD_CODE[target_idx],
"onset": onset,
"offset": offset,
"max_confidence": max_confidence,
"mean_confidence": mean_confidence
}
estimated_event_list.append(estimated_event)
 head_idx = tail_idx + 1
tail_idx = tail_idx + 1
if head_idx >= len(detected):
break
else:
tail_idx += 1
global_time += PERIOD
prediction_df = pd.DataFrame(estimated_event_list)
 return prediction_df
X = df_train.drop('SalePrice',axis=1)
 y = df_train['SalePrice']<drop_column>
def prediction(test_df: pd.DataFrame,
test_audio: Path,
model_config: dict,
weights_path: str,
threshold=0.5):
model = get_model(model_config, weights_path)
 unique_audio_id = test_df.audio_id.unique()
warnings.filterwarnings("ignore")
 prediction_dfs = []
for audio_id in unique_audio_id:
with timer(f"Loading {audio_id}"):
clip, _ = librosa.load(test_audio /(audio_id + ".mp3"),
sr=SR,
mono=True,
res_type="kaiser_fast")
 test_df_for_audio_id = test_df.query(
 f"audio_id == '{audio_id}'" ).reset_index(drop=True)
 with timer(f"Prediction on {audio_id}"):
prediction_df = prediction_for_clip(test_df_for_audio_id,
clip=clip,
model=model,
threshold=threshold)
 prediction_dfs.append(prediction_df)
 prediction_df = pd.concat(prediction_dfs, axis=0, sort=False ).reset_index(drop=True)
 return prediction_df
LotFrontageX = X['LotFrontage']
GarageYrBltX = X['GarageYrBlt']<categorify>
prediction_df = prediction(test_df=test,
test_audio=TEST_AUDIO_DIR,
model_config=model_config,
weights_path=weights_path,
threshold=0.7 )
ohe = OneHotEncoder(sparse=False,drop="if_binary")
 Categorical_Encoded = ohe.fit_transform(Categorical_features.astype(str))<prepare_output>
labels = {}
for audio_id, sub_df in prediction_df.groupby("audio_id"):
events = sub_df[["ebird_code", "onset", "offset", "max_confidence", "site"]].values
n_events = len(events)
 removed_event = []
for i in range(n_events):
for j in range(n_events):
if i == j:
continue
if i in removed_event:
continue
if j in removed_event:
continue
event_i = events[i]
event_j = events[j]
if(event_i[1] - event_j[2] >= 0)or(event_j[1] - event_i[2] >= 0):
pass
else:
later_onset = max(event_i[1], event_j[1])
 sooner_onset = min(event_i[1], event_j[1])
 sooner_offset = min(event_i[2], event_j[2])
 later_offset = max(event_i[2], event_j[2])
 intersection = sooner_offset - later_onset
union = later_offset - sooner_onset
iou = intersection / union
if iou > 0.4:
if event_i[3] > event_j[3]:
removed_event.append(j)
 else:
removed_event.append(i)
 site = events[0][4]
for i in range(n_events):
if i in removed_event:
continue
event = events[i][0]
onset = events[i][1]
offset = events[i][2]
if site in {"site_1", "site_2"}:
start_section = int(( onset // 5)* 5)+ 5
end_section = int(( offset // 5)* 5)+ 5
cur_section = start_section
row_id = f"{site}_{audio_id}_{start_section}"
if labels.get(row_id)is not None:
labels[row_id].add(event)
 else:
labels[row_id] = set()
labels[row_id].add(event)
 while cur_section != end_section:
cur_section += 5
row_id = f"{site}_{audio_id}_{cur_section}"
if labels.get(row_id)is not None:
labels[row_id].add(event)
 else:
labels[row_id] = set()
labels[row_id].add(event)
 else:
row_id = f"{site}_{audio_id}"
if labels.get(row_id)is not None:
labels[row_id].add(event)
 else:
labels[row_id] = set()
labels[row_id].add(event )
Categorical_Encoded_Frame = pd.DataFrame(Categorical_Encoded, columns= ohe.get_feature_names(Categorical_features.columns))
Categorical_Encoded_Frame.head()<drop_column>
for key in labels:
labels[key] = " ".join(sorted(list(labels[key])))
 row_ids = list(labels.keys())
 birds = list(labels.values())
 post_processed = pd.DataFrame({
"row_id": row_ids,
"birds": birds
})
 post_processed.head()
Numerical_features_X = Numerical_features.drop(['SalePrice','LotFrontage','GarageYrBlt'],axis=1 )<merge>
all_row_id = test[["row_id"]]
submission = all_row_id.merge(post_processed, on="row_id", how="left")
 submission = submission.fillna("nocall")
 submission.to_csv("submission.csv", index=False)
 submission.head(20 )
<feature_engineering><EOS> 
submission.birds.value_counts()