Kaggle competition: Cornell Birdcall Identification
<SOS> metric: MeanFScoreBeta Kaggle data source: cornell-birdcall-identification<load_from_csv>
from pathlib import Path
import pandas as pd
import torch
from fastprogress import progress_bar
import numpy as np
import warnings
from collections import defaultdict
from collections import Counter
df_mask = pd.DataFrame(res)
 df_mask = df_mask.fillna('')
 df_label_1 = pd.read_csv('./resnet34-cls-tta-0.50_test.csv' ).fillna('' ).sort_values(by=['ImageId_ClassId'])
 fp1 = list(df_label_1['EncodedPixels']=='')
 assert(np.all(df_mask['ImageId_ClassId'].values == df_label_1['ImageId_ClassId'].values))
print("How many predictions in the segementation result will be removed?")
 print(( df_mask.loc[fp1,'EncodedPixels'] != '' ).sum())
 print("How many postive predictions in the segementation?")
 print(( df_mask['EncodedPixels'] != '' ).sum())
 print("How many postive predictions in the classification?")
 print(( df_label_1['EncodedPixels'] != '' ).sum())
 df_mask.loc[fp1,'EncodedPixels']=''
df_mask.to_csv('submission.csv', index=False)
 df = df_mask<feature_engineering>
torch.manual_seed(42)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(42 )
df['Image'] = df['ImageId_ClassId'].map(lambda x: x.split('_')[0])
 df['Class'] = df['ImageId_ClassId'].map(lambda x: x.split('_')[1])
 df['empty'] = df['EncodedPixels'].map(lambda x: not x)
 df[df['empty'] == False]['Class'].value_counts()<feature_engineering>
ROOT = Path.cwd().parent
INPUT_ROOT = ROOT / "input"
RAW_DATA = INPUT_ROOT / "birdsong-recognition"
TRAIN_AUDIO_DIR = RAW_DATA / "train_audio"
TEST_AUDIO_DIR = RAW_DATA / "test_audio"
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")
 device = torch.device("cuda" if torch.cuda.is_available() else "cpu" )
%matplotlib inline
df = pd.read_csv('submission.csv')[:40]
df['Image'] = df['ImageId_ClassId'].map(lambda x: x.split('_')[0])
 df['Class'] = df['ImageId_ClassId'].map(lambda x: x.split('_')[1])
 for row in df.itertuples() :
img_path = os.path.join(img_folder, row.Image)
 img = cv2.imread(img_path)
 mask = rle2mask(row.EncodedPixels,(1600, 256)) \
if isinstance(row.EncodedPixels, str)else np.zeros(( 256, 1600))
if mask.sum() == 0:
continue
fig, axes = plt.subplots(1, 2, figsize=(20, 60))
axes[0].imshow(img/255)
 axes[1].imshow(mask*60)
 axes[0].set_title(row.Image)
 axes[1].set_title(row.Class)
 plt.show()<set_options>
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() }
warnings.filterwarnings('ignore')
<load_pretrained>

 class DFTBase(nn.Module):
def __init__(self):

 super(DFTBase, self ).__init__()
def dft_matrix(self, n): 
(x, y)= np.meshgrid(np.arange(n), np.arange(n))
omega = np.exp(-2 * np.pi * 1j / n)
 W = np.power(omega, x * y)
 return W
def idft_matrix(self, n): 
(x, y)= np.meshgrid(np.arange(n), np.arange(n))
omega = np.exp(2 * np.pi * 1j / n)
 W = np.power(omega, x * y)
 return W
class STFT(DFTBase):
def __init__(self, n_fft=2048, hop_length=None, win_length=None,
window='hann', center=True, pad_mode='reflect', freeze_parameters=True):

 super(STFT, self ).__init__()
assert pad_mode in ['constant', 'reflect']
self.n_fft = n_fft
self.center = center
self.pad_mode = pad_mode
if win_length is None:
win_length = n_fft
if hop_length is None:
hop_length = int(win_length // 4)
 fft_window = librosa.filters.get_window(window, win_length, fftbins=True)
 fft_window = librosa.util.pad_center(fft_window, n_fft)
 self.W = self.dft_matrix(n_fft)
 out_channels = n_fft // 2 + 1
self.conv_real = nn.Conv1d(in_channels=1, out_channels=out_channels,
kernel_size=n_fft, stride=hop_length, padding=0, dilation=1,
groups=1, bias=False)
 self.conv_imag = nn.Conv1d(in_channels=1, out_channels=out_channels,
kernel_size=n_fft, stride=hop_length, padding=0, dilation=1,
groups=1, bias=False)
 self.conv_real.weight.data = torch.Tensor(
 np.real(self.W[:, 0 : out_channels] * fft_window[:, None] ).T)[:, None, :]
self.conv_imag.weight.data = torch.Tensor(
 np.imag(self.W[:, 0 : out_channels] * fft_window[:, None] ).T)[:, None, :]
if freeze_parameters:
for param in self.parameters() :
param.requires_grad = False
def forward(self, input):

 x = input[:, None, :]
if self.center:
x = F.pad(x, pad=(self.n_fft // 2, self.n_fft // 2), mode=self.pad_mode)
 real = self.conv_real(x)
 imag = self.conv_imag(x)
 real = real[:, None, :, :].transpose(2, 3)
 imag = imag[:, None, :, :].transpose(2, 3)
 return real, imag
class Spectrogram(nn.Module):
def __init__(self, n_fft=2048, hop_length=None, win_length=None,
window='hann', center=True, pad_mode='reflect', power=2.0,
freeze_parameters=True):

 super(Spectrogram, self ).__init__()
self.power = power
self.stft = STFT(n_fft=n_fft, hop_length=hop_length,
win_length=win_length, window=window, center=center,
pad_mode=pad_mode, freeze_parameters=True)
 def forward(self, input):

(real, imag)= self.stft.forward(input)
 spectrogram = real ** 2 + imag ** 2
if self.power == 2.0:
pass
else:
spectrogram = spectrogram **(power / 2.0)
 return spectrogram
class LogmelFilterBank(nn.Module):
def __init__(self, sr=32000, n_fft=2048, n_mels=64, fmin=50, fmax=14000, is_log=True,
ref=1.0, amin=1e-10, top_db=80.0, freeze_parameters=True):

 super(LogmelFilterBank, self ).__init__()
self.is_log = is_log
self.ref = ref
self.amin = amin
self.top_db = top_db
self.melW = librosa.filters.mel(sr=sr, n_fft=n_fft, n_mels=n_mels,
fmin=fmin, fmax=fmax ).T
self.melW = nn.Parameter(torch.Tensor(self.melW))
if freeze_parameters:
for param in self.parameters() :
param.requires_grad = False
def forward(self, input):

 mel_spectrogram = torch.matmul(input, self.melW)
 if self.is_log:
output = self.power_to_db(mel_spectrogram)
 else:
output = mel_spectrogram
return output
def power_to_db(self, input):

 ref_value = self.ref
log_spec = 10.0 * torch.log10(torch.clamp(input, min=self.amin, max=np.inf))
log_spec -= 10.0 * np.log10(np.maximum(self.amin, ref_value))
if self.top_db is not None:
if self.top_db < 0:
raise ParameterError('top_db must be non-negative')
 log_spec = torch.clamp(log_spec, min=log_spec.max().item() - self.top_db, max=np.inf)
 return log_spec
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
unet_se_resnext50_32x4d = load('/kaggle/input/severstalmodels/unet_se_resnext50_32x4d.pth' ).cuda()
unet_mobilenet2 = load('/kaggle/input/severstalmodels/unet_mobilenet2.pth' ).cuda()
unet_resnet34 = load('/kaggle/input/severstalmodels/unet_resnet34.pth' ).cuda()<normalization>
def get_model(ModelClass: object, config: dict, weights_path: str):
model = ModelClass(**config)
 checkpoint = torch.load(weights_path, map_location='cpu')
 model.load_state_dict(checkpoint["model"])
 model.to(device)
 model.eval()
return model
class Model:
def __init__(self, models):
self.models = models
def __call__(self, x):
res = []
x = x.cuda()
with torch.no_grad() :
for m in self.models:
res.append(m(x))
res = torch.stack(res)
 return torch.mean(res, dim=0)
 model = Model([unet_se_resnext50_32x4d, unet_mobilenet2, unet_resnet34] )<categorify>
list_of_models = [
{
"model_class": PANNsDense121Att,
"config": {
"sample_rate": 32000,
"window_size": 1024,
"hop_size": 320,
"mel_bins": 64,
"fmin": 50,
"fmax": 14000,
"classes_num": 264,
"apply_aug": True,
"top_db": None
},
"weights_path": ".. /input/birdsongdetectionfinalsubmission1/final_sed_dense121_nomix_fold0_checkpoint_50_score0.7057.pt",
"clip_threshold": 0.3,
"threshold": 0.3
},
{
"model_class": PANNsDense121Att,
"config": {
"sample_rate": 32000,
"window_size": 1024,
"hop_size": 320,
"mel_bins": 64,
"fmin": 50,
"fmax": 14000,
"classes_num": 264,
"apply_aug": True,
"top_db": None
},
"weights_path": ".. /input/birdsongdetectionfinalsubmission1/final_sed_dense121_nomix_fold1_checkpoint_48_score0.6943.pt",
"clip_threshold": 0.3,
"threshold": 0.3
},
{
"model_class": PANNsDense121Att,
"config": {
"sample_rate": 32000,
"window_size": 1024,
"hop_size": 320,
"mel_bins": 64,
"fmin": 50,
"fmax": 14000,
"classes_num": 264,
"apply_aug": True,
"top_db": None
},
"weights_path": ".. /input/birdsongdetectionfinalsubmission1/final_sed_dense121_nomix_fold2_augd_checkpoint_50_score0.6666.pt",
"clip_threshold": 0.3,
"threshold": 0.3
},
{
"model_class": PANNsDense121Att,
"config": {
"sample_rate": 32000,
"window_size": 1024,
"hop_size": 320,
"mel_bins": 64,
"fmin": 50,
"fmax": 14000,
"classes_num": 264,
"apply_aug": True,
"top_db": None
},
"weights_path": ".. /input/birdsongdetectionfinalsubmission1/final_sed_dense121_nomix_fold3_augd_checkpoint_50_score0.6713.pt",
"clip_threshold": 0.3,
"threshold": 0.3
},
{
"model_class": PANNsDense121Att,
"config": {
"sample_rate": 32000,
"window_size": 1024,
"hop_size": 320,
"mel_bins": 64,
"fmin": 50,
"fmax": 14000,
"classes_num": 264,
"apply_aug": True,
"top_db": None
},
"weights_path": ".. /input/birdsongdetectionfinalsubmission1/final_5fold_sed_dense121_nomix_fold0_checkpoint_50_score0.7219.pt",
"clip_threshold": 0.3,
"threshold": 0.3
},
{
"model_class": PANNsDense121Att,
"config": {
"sample_rate": 32000,
"window_size": 1024,
"hop_size": 320,
"mel_bins": 64,
"fmin": 50,
"fmax": 14000,
"classes_num": 264,
"apply_aug": True,
"top_db": None
},
"weights_path": ".. /input/birdsongdetectionfinalsubmission1/final_5fold_sed_dense121_nomix_fold1_checkpoint_44_score0.7645.pt",
"clip_threshold": 0.3,
"threshold": 0.3
},
{
"model_class": PANNsDense121Att,
"config": {
"sample_rate": 32000,
"window_size": 1024,
"hop_size": 320,
"mel_bins": 64,
"fmin": 50,
"fmax": 14000,
"classes_num": 264,
"apply_aug": True,
"top_db": None
},
"weights_path": ".. /input/birdsongdetectionfinalsubmission1/final_5fold_sed_dense121_nomix_fold2_checkpoint_50_score0.7737.pt",
"clip_threshold": 0.3,
"threshold": 0.3
},
{
"model_class": PANNsDense121Att,
"config": {
"sample_rate": 32000,
"window_size": 1024,
"hop_size": 320,
"mel_bins": 64,
"fmin": 50,
"fmax": 14000,
"classes_num": 264,
"apply_aug": True,
"top_db": None
},
"weights_path": ".. /input/birdsongdetectionfinalsubmission1/final_5fold_sed_dense121_nomix_fold3_checkpoint_48_score0.7746.pt",
"clip_threshold": 0.3,
"threshold": 0.3
},
{
"model_class": PANNsDense121Att,
"config": {
"sample_rate": 32000,
"window_size": 1024,
"hop_size": 320,
"mel_bins": 64,
"fmin": 50,
"fmax": 14000,
"classes_num": 264,
"apply_aug": True,
"top_db": None
},
"weights_path": ".. /input/birdsongdetectionfinalsubmission1/final_5fold_sed_dense121_nomix_fold4_checkpoint_50_score0.7728.pt",
"clip_threshold": 0.3,
"threshold": 0.3
},
{
"model_class": PANNsDense121Att,
"config": {
"sample_rate": 32000,
"window_size": 1024,
"hop_size": 320,
"mel_bins": 64,
"fmin": 50,
"fmax": 14000,
"classes_num": 264,
"apply_aug": True,
"top_db": None
},
"weights_path": ".. /input/birdsongdetectionfinalsubmission1/final_sed_dense121_mix_fold0_2_checkpoint_50_score0.6842.pt",
"clip_threshold": 0.3,
"threshold": 0.3
},
{
"model_class": PANNsDense121Att,
"config": {
"sample_rate": 32000,
"window_size": 1024,
"hop_size": 320,
"mel_bins": 64,
"fmin": 50,
"fmax": 14000,
"classes_num": 264,
"apply_aug": True,
"top_db": None
},
"weights_path": ".. /input/birdsongdetectionfinalsubmission1/final_sed_dense121_mix_fold1_2_checkpoint_50_score0.6629.pt",
"clip_threshold": 0.3,
"threshold": 0.3
},
{
"model_class": PANNsDense121Att,
"config": {
"sample_rate": 32000,
"window_size": 1024,
"hop_size": 320,
"mel_bins": 64,
"fmin": 50,
"fmax": 14000,
"classes_num": 264,
"apply_aug": True,
"top_db": None
},
"weights_path": ".. /input/birdsongdetectionfinalsubmission1/final_sed_dense121_mix_fold2_2_checkpoint_50_score0.6884.pt",
"clip_threshold": 0.3,
"threshold": 0.3
},
{
"model_class": PANNsDense121Att,
"config": {
"sample_rate": 32000,
"window_size": 1024,
"hop_size": 320,
"mel_bins": 64,
"fmin": 50,
"fmax": 14000,
"classes_num": 264,
"apply_aug": True,
"top_db": None
},
"weights_path": ".. /input/birdsongdetectionfinalsubmission1/final_sed_dense121_mix_fold3_2_checkpoint_50_score0.6870.pt",
"clip_threshold": 0.3,
"threshold": 0.3
}
]
PERIOD = 30
SR = 32000
vote_lim = 4
TTA = 10
def create_transforms(additional):
res = list(additional)
 res.extend([
A.Normalize(
 mean=(0.485, 0.456, 0.406), std=(0.230, 0.225, 0.223)
),
ChannelTranspose()
])
 res = A.Compose(res)
 return res
img_folder = '/kaggle/input/severstal-steel-defect-detection/test_images'
batch_size = 2
num_workers = 0
transforms = [
[],
[A.HorizontalFlip(p=1)]
]
transforms = [create_transforms(t)for t in transforms]
datasets = [TtaWrap(ImageDataset(img_folder=img_folder, transforms=t), tfms=t)for t in transforms]
loaders = [DataLoader(d, num_workers=num_workers, batch_size=batch_size, shuffle=False)for d in datasets]<define_search_space>
for lm in list_of_models:
lm["model"] = get_model(lm["model_class"], lm["config"], lm["weights_path"] )
thresholds = [0.5, 0.7, 0.5, 0.5]
min_area = [600, 600, 1000, 2000]
res = []
total = len(datasets[0])//batch_size
for loaders_batch in tqdm_notebook(zip(*loaders), total=total):
preds = []
image_file = []
for i, batch in enumerate(loaders_batch):
features = batch['features'].cuda()
p = torch.sigmoid(model(features))
p = datasets[i].inverse(p)
 preds.append(p)
 image_file = batch['image_file']
preds = torch.stack(preds)
 preds = torch.mean(preds, dim=0)
 preds = preds.detach().cpu().numpy()
for p, file in zip(preds, image_file):
file = os.path.basename(file)
 for i in range(4):
p_channel = p[i]
imageid_classid = file+'_'+str(i+1)
 p_channel =(p_channel>thresholds[i] ).astype(np.uint8)
 if p_channel.sum() < min_area[i]:
p_channel = np.zeros(p_channel.shape, dtype=p_channel.dtype)
 res.append({
'ImageId_ClassId': imageid_classid,
'EncodedPixels': mask2rle(p_channel)
 })
 df = pd.DataFrame(res)
 df = df.fillna('' )<import_modules>
def prediction_for_clip(test_df: pd.DataFrame,
clip: np.ndarray,
model,
threshold,
clip_threshold):
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)
 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 tensors:
image = image.unsqueeze(0 ).unsqueeze(0)
 image = image.expand(image.shape[0], TTA, image.shape[2])
 image = image.to(device)
 with torch.no_grad() :
prediction = model(( image, None))
framewise_outputs = prediction["framewise_output"].detach(
 ).cpu().numpy() [0].mean(axis=0)
 clipwise_outputs = prediction["clipwise_output"].detach(
 ).cpu().numpy() [0].mean(axis=0)
 thresholded = framewise_outputs >= threshold
clip_thresholded = clipwise_outputs >= clip_threshold
clip_indices = np.argwhere(clip_thresholded ).reshape(-1)
 clip_codes = []
for ci in clip_indices:
clip_codes.append(INV_BIRD_CODE[ci])
 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()
if INV_BIRD_CODE[target_idx] in clip_codes:
estimated_event = {
"site": site,
"audio_id": audio_id,
"ebird_code": INV_BIRD_CODE[target_idx],
"clip_codes": clip_codes,
"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
def prediction(test_df: pd.DataFrame,
test_audio: Path,
list_of_model_details):
unique_audio_id = test_df.audio_id.unique()
warnings.filterwarnings("ignore")
 prediction_dfs_dict = defaultdict(list)
 for audio_id in progress_bar(unique_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)
 for i, model_details in enumerate(list_of_model_details):
prediction_df = prediction_for_clip(test_df_for_audio_id,
clip=clip,
model=model_details["model"],
threshold=model_details["threshold"],
clip_threshold=model_details["clip_threshold"])
 prediction_dfs_dict[i].append(prediction_df)
 list_of_prediction_df = []
for key, prediction_dfs in prediction_dfs_dict.items() :
prediction_df = pd.concat(prediction_dfs, axis=0, sort=False ).reset_index(drop=True)
 list_of_prediction_df.append(prediction_df)
 return list_of_prediction_df
import matplotlib.pyplot as plt
import random
import math as math
import os
import numpy as np
import pandas as pd
import cv2
from PIL import Image
import os
import tensorflow as tf
import keras.backend as K
from keras.utils import Sequence
from keras.applications import Xception
from keras.layers import UpSampling2D, Conv2D, Activation, LeakyReLU, BatchNormalization
from keras import Model
from keras.losses import binary_crossentropy
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint
from keras.optimizers import Adam
import imgaug as ia
import imgaug.augmenters as iaa
import segmentation_models as sm
import efficientnet.keras as efn
from keras.optimizers import Adam
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten<choose_model_class>
list_of_prediction_df = prediction(test_df=test,
test_audio=TEST_AUDIO_DIR,
list_of_model_details=list_of_models )
base = efn.EfficientNetB3(weights=None, include_top=False, input_shape=(256, 1600, 3), pooling='avg')
 base.trainable=True
dropout_dense_layer = 0.2
classifier_model = Sequential()
classifier_model.add(base)
 classifier_model.add(Dropout(dropout_dense_layer))
classifier_model.add(Dense(4, activation='sigmoid'))
classifier_model.compile(
 loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] 
)
 classifier_model.load_weights('.. /input/single-models/B3_opt_multi_new3.h5')
 IMAGE_RGB_MEAN = [0.485, 0.456, 0.406]
IMAGE_RGB_STD = [0.229, 0.224, 0.225]
def normalize(images):
images = images / 255.0
images[:,:,:,0] =(images[:,:,:,0]-IMAGE_RGB_MEAN[0])/IMAGE_RGB_STD[0]
images[:,:,:,1] =(images[:,:,:,1]-IMAGE_RGB_MEAN[1])/IMAGE_RGB_STD[1]
images[:,:,:,2] =(images[:,:,:,2]-IMAGE_RGB_MEAN[2])/IMAGE_RGB_STD[2]
return images
def denormalize(images):
images[:,:,:,0] = images[:,:,:,0]*IMAGE_RGB_STD[0]+IMAGE_RGB_MEAN[0]
images[:,:,:,1] = images[:,:,:,1]*IMAGE_RGB_STD[1]+IMAGE_RGB_MEAN[1]
images[:,:,:,2] = images[:,:,:,2]*IMAGE_RGB_STD[2]+IMAGE_RGB_MEAN[2]
images = np.array(images * 255 ).astype('uint8')
 TEST_PATH = '.. /input/severstal-steel-defect-detection/test_images/'
def prepareData(source, path, cleanup=True):
source['defect'] = False
if cleanup: source.EncodedPixels = ''
source['ClassId'] = source['ImageId_ClassId'].str[-1:]
source['ImageId'] = source['ImageId_ClassId'].str[:-2]
source = source[['ImageId_ClassId', 'ImageId','ClassId','defect','EncodedPixels']]
source.ClassId = source.ClassId.astype('int')
 source['path'] = path + source['ImageId']
return source<load_from_csv>
all_row_id = test[["row_id"]]
list_of_submissions = []
for prediction_df in list_of_prediction_df:
post_processed = get_post_post_process_predictions(prediction_df)
 submission = post_processed.fillna("nocall")
 submission = submission.set_index('row_id')
 list_of_submissions.append(submission )
test_df = pd.read_csv('.. /input/severstal-steel-defect-detection/sample_submission.csv')
 test_df = prepareData(test_df, TEST_PATH)
 ids = test_df['ImageId'].unique()
test_df.EncodedPixels = ''
class_weights = [0.5, 0.5, 0.5, 0.5]
for picIdx in tqdm_notebook(range(len(ids))):
batch = np.zeros(( 4, 256, 1600, 3))
filename = ids[picIdx]
img = cv2.imread(TEST_PATH+filename)
 batch[0, :, :, :] = img
batch[1, :, :, :] = img[:,::-1,:]
batch[2, :, :, :] = img[::-1,:,:]
batch[3, :, :, :] = img[::-1,::-1,:]
batch = normalize(batch)
 classTTA = classifier_model.predict(batch)
 hasDefect = np.mean(classTTA, axis=0)>class_weights
for i, classId in enumerate(hasDefect):
if classId == False:
name = filename+"_"+str(i+1)
 line = test_df[test_df.ImageId_ClassId == name].index[0]
test_df.loc[line, 'EncodedPixels'] = -1<count_values>
list_all_of_row_ids = []
for sub_x in list_of_submissions:
list_all_of_row_ids+= list(sub_x.index.values)
 list_all_of_row_ids = list(set(list_all_of_row_ids))
mark = test_df[test_df.EncodedPixels == -1]
mark.ClassId.value_counts()
sub = df.copy()<save_to_csv>
final_submission = []
for row_id in list_all_of_row_ids:
birds = []
for sub in list_of_submissions:
if row_id in sub.index:
birds.extend(sub.loc[row_id].birds.split(" "))
birds = [x for x in birds if "nocall" != x and "" != x]
count_birds = Counter(birds)
 final_birds = []
for key, value in count_birds.items() :
if value >= vote_lim:
final_birds.append(key)
 if len(final_birds)>0:
row_data = {
"row_id": row_id,
"birds": " ".join(sorted(final_birds))
}
else:
row_data = {
"row_id": row_id,
"birds": "nocall"
}
final_submission.append(row_data)
 site_3_data = defaultdict(list)
 for row in final_submission:
if "site_3" in row["row_id"]:
final_row_id = "_".join(row["row_id"].split("_")[0:-1])
 birds = row["birds"].split(" ")
 birds = [x for x in birds if "nocall" != x and "" != x]
site_3_data[final_row_id].extend(birds)
 for key, value in site_3_data.items() :
count_birds = Counter(value)
 final_birds = []
for k, v in count_birds.items() :
if v >= vote_lim:
final_birds.append(k)
 if len(final_birds)>0:
row_data = {
"row_id": key,
"birds": " ".join(sorted(final_birds))
}
else:
row_data = {
"row_id": key,
"birds": "nocall"
}
final_submission.append(row_data)
 final_submission = pd.DataFrame(final_submission)
 final_submission = all_row_id.merge(final_submission, on="row_id", how="left")
 final_submission = final_submission.fillna("nocall" )
<count_values><EOS> 
final_submission.to_csv("submission.csv", index=False)
 final_submission.head(50 )