Kaggle competition: TGS Salt Identification Challenge
<SOS> metric: IntersectionOverUnionObjectSegmentation Kaggle data source: tgs-salt-identification-challenge<define_variables>
plt.style.use('seaborn-white')
 sns.set_style("white")
 %matplotlib inline
resnest_model_config = {
"num_classes": 264
}
effnet_model_config = {
"num_classes": 264,
"base_model_name": "efficientnet-b0",
"pretrained": False
}
weights_path = {
"resnest": {
"base": ".. /input/birdcall-resnest-ema-all/ema.pth",
"th04": ".. /input/birdcall-resnest-ema-all-th04/ema.pth",
"th06": ".. /input/birdcall-resnest-ema-all-th06/ema.pth",
"ref2": ".. /input/birdcall-resnest-ema-all-refine2/ema.pth",
"ref2_th03": ".. /input/birdcall-resnest-ema-all-ref2-th03/ema.pth",
"ref2_th04": ".. /input/birdcall-resnest-ema-all-ref2-th04/ema.pth",
"ref2_th06": ".. /input/birdcall-resnest-ema-all-ref2-th06/ema.pth",
"ref2_th07": ".. /input/birdcall-resnest-ema-all-ref2-th07/ema.pth",
"ext": ".. /input/birdcall-resnest-emta-all-ext-ref2-th04/ema.pth"
},
"effnet": {
"eff_th04": ".. /input/birdcall-effnet-ema-all-focal-ref2-th04/ema.pth",
"eff_th05": ".. /input/birdcall-effnet-b0-ema-all-ref2/ema.pth"
}
}
ratio = {
"base": 0.05,
"th06": 0.05,
"th04": 0.01,
"ref2": 0.05,
"ref2_th03": 0.25,
"ref2_th04": 0.14,
"ref2_th06": 0.05,
"ref2_th07": 0.01,
"eff_th04": 0.13,
"eff_th05": 0.01,
"ext": 0.25
}
melspectrogram_parameters = {
"n_mels": 128,
"fmin": 20,
"fmax": 16000
}
pcen_parameters = {
"gain": 0.98,
"bias": 2,
"power": 0.5,
"time_constant": 0.4,
"eps": 0.000001
}<define_variables>
img_size_ori = 101
img_size_target = 101
im_width = 101
im_height = 101
im_chan = 1
basicpath = '.. /input/'
path_train = basicpath + 'train/'
path_test = basicpath + 'test/'
path_train_images = path_train + 'images/'
path_train_masks = path_train + 'masks/'
path_test_images = path_test + 'images/'
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() }<define_variables>
train_df = pd.read_csv(".. /input/train.csv", index_col="id", usecols=[0])
 depths_df = pd.read_csv(".. /input/depths.csv", index_col="id")
 train_df = train_df.join(depths_df)
 test_df = depths_df[~depths_df.index.isin(train_df.index)]
len(train_df )
thresholds = {
'aldfly': 0.8300000000000001, 'ameavo': 0.35000000000000003, 'amebit': 0.73, 'amecro': 0.35000000000000003, 'amegfi': 0.53,
'amekes': 0.47000000000000003, 'amepip': 0.78, 'amered': 0.87, 'amerob': 0.26, 'amewig': 0.98,
'amewoo': 0.81, 'amtspa': 0.74, 'annhum': 0.78, 'astfly': 0.9500000000000001, 'baisan': 0.9400000000000001,
'baleag': 0.61, 'balori': 0.74, 'banswa': 0.85, 'barswa': 0.6, 'bawwar': 0.4,
'belkin1': 0.41000000000000003, 'belspa2': 0.79, 'bewwre': 0.9, 'bkbcuc': 0.76, 'bkbmag1': 0.67,
'bkbwar': 0.54, 'bkcchi': 0.58, 'bkchum': 0.68, 'bkhgro': 0.52, 'bkpwar': 0.5700000000000001,
'bktspa': 0.87, 'blkpho': 0.7000000000000001, 'blugrb1': 0.4, 'blujay': 0.66, 'bnhcow': 0.56,
'boboli': 0.6, 'bongul': 0.7000000000000001, 'brdowl': 0.98, 'brebla': 0.77, 'brespa': 0.9,
'brncre': 0.6900000000000001, 'brnthr': 0.97, 'brthum': 0.86, 'brwhaw': 0.9500000000000001, 'btbwar': 0.89,
'btnwar': 0.9, 'btywar': 0.98, 'buffle': 0.9400000000000001, 'buggna': 0.67, 'buhvir': 0.34,
'bulori': 0.68, 'bushti': 0.67, 'buwtea': 0.3, 'buwwar': 0.61, 'cacwre': 0.59,
'calgul': 0.89, 'calqua': 0.86, 'camwar': 0.67, 'cangoo': 0.46, 'canwar': 0.86,
'canwre': 0.76, 'carwre': 0.53, 'casfin': 0.6900000000000001, 'caster1': 0.48, 'casvir': 0.79,
'cedwax': 0.5, 'chispa': 0.43, 'chiswi': 0.6, 'chswar': 0.9500000000000001, 'chukar': 0.47000000000000003,
'clanut': 0.99, 'cliswa': 0.51, 'comgol': 0.5, 'comgra': 0.63, 'comloo': 0.29,
'commer': 0.79, 'comnig': 0.81, 'comrav': 0.42, 'comred': 0.76, 'comter': 0.67,
'comyel': 0.73, 'coohaw': 0.46, 'coshum': 0.64, 'cowscj1': 0.56, 'daejun': 0.65,
'doccor': 0.72, 'dowwoo': 0.68, 'dusfly': 0.8300000000000001, 'eargre': 0.9, 'easblu': 0.71,
'easkin': 0.97, 'easmea': 0.81, 'easpho': 0.96, 'eastow': 0.5, 'eawpew': 0.6900000000000001,
'eucdov': 0.65, 'eursta': 0.47000000000000003, 'evegro': 0.81, 'fiespa': 0.63, 'fiscro': 0.91,
'foxspa': 0.55, 'gadwal': 0.77, 'gcrfin': 0.6900000000000001, 'gnttow': 0.6900000000000001, 'gnwtea': 0.67,
'gockin': 0.66, 'gocspa': 0.86, 'goleag': 0.48, 'grbher3': 0.59, 'grcfly': 0.91,
'greegr': 0.6900000000000001, 'greroa': 0.81, 'greyel': 0.91, 'grhowl': 0.8200000000000001, 'grnher': 0.59,
'grtgra': 0.52, 'grycat': 0.65, 'gryfly': 0.5, 'haiwoo': 0.76, 'hamfly': 0.51,
'hergul': 0.92, 'herthr': 0.88, 'hoomer': 0.64, 'hoowar': 0.85, 'horgre': 0.47000000000000003,
'horlar': 0.78, 'houfin': 0.36, 'houspa': 0.44, 'houwre': 0.38, 'indbun': 0.71,
'juntit1': 0.96, 'killde': 0.6900000000000001, 'labwoo': 0.64, 'larspa': 0.67, 'lazbun': 0.6900000000000001,
'leabit': 0.88, 'leafly': 0.35000000000000003, 'leasan': 0.62, 'lecthr': 0.9400000000000001, 'lesgol': 0.72,
'lesnig': 0.6, 'lesyel': 0.37, 'lewwoo': 0.62, 'linspa': 0.88, 'lobcur': 0.73,
'lobdow': 0.49, 'logshr': 0.93, 'lotduc': 0.43, 'louwat': 0.6900000000000001, 'macwar': 0.76,
'magwar': 0.99, 'mallar3': 0.68, 'marwre': 0.81, 'merlin': 0.77, 'moublu': 0.79,
'mouchi': 0.85, 'moudov': 0.59, 'norcar': 0.38, 'norfli': 0.5, 'norhar2': 0.48,
'normoc': 0.42, 'norpar': 0.51, 'norpin': 0.84, 'norsho': 0.48, 'norwat': 0.56,
'nrwswa': 0.36, 'nutwoo': 0.38, 'olsfly': 0.96, 'orcwar': 0.78, 'osprey': 0.97,
'ovenbi1': 0.56, 'palwar': 0.48, 'pasfly': 0.9, 'pecsan': 0.41000000000000003, 'perfal': 0.6900000000000001,
'phaino': 0.91, 'pibgre': 0.48, 'pilwoo': 0.64, 'pingro': 0.84, 'pinjay': 0.92,
'pinsis': 0.39, 'pinwar': 0.88, 'plsvir': 0.6, 'prawar': 0.9, 'purfin': 0.55,
'pygnut': 0.9400000000000001, 'rebmer': 0.76, 'rebnut': 0.33, 'rebsap': 0.42, 'rebwoo': 0.38,
'redcro': 0.77, 'redhea': 0.86, 'reevir1': 0.72, 'renpha': 0.48, 'reshaw': 0.5700000000000001,
'rethaw': 0.79, 'rewbla': 0.43, 'ribgul': 0.3, 'rinduc': 0.47000000000000003, 'robgro': 0.92,
'rocpig': 0.71, 'rocwre': 0.58, 'rthhum': 0.44, 'ruckin': 0.85, 'rudduc': 0.67,
'rufgro': 0.27, 'rufhum': 0.86, 'rusbla': 0.8200000000000001, 'sagspa1': 0.75, 'sagthr': 0.86,
'savspa': 0.81, 'saypho': 0.85, 'scatan': 0.96, 'scoori': 0.93, 'semplo': 0.46,
'semsan': 0.96, 'sheowl': 0.43, 'shshaw': 0.44, 'snobun': 0.6, 'snogoo': 0.97,
'solsan': 0.73, 'sonspa': 0.59, 'sora': 0.72, 'sposan': 0.5700000000000001, 'spotow': 0.55,
'stejay': 0.6900000000000001, 'swahaw': 0.58, 'swaspa': 0.6, 'swathr': 0.67, 'treswa': 0.98,
'truswa': 0.93, 'tuftit': 0.71, 'tunswa': 0.55, 'veery': 0.91, 'vesspa': 0.96,
'vigswa': 0.45, 'warvir': 0.86, 'wesblu': 0.65, 'wesgre': 0.6, 'weskin': 0.8200000000000001,
'wesmea': 0.58, 'wessan': 0.63, 'westan': 0.47000000000000003, 'wewpew': 0.78, 'whbnut': 0.48,
'whcspa': 0.72, 'whfibi': 0.66, 'whtspa': 0.76, 'whtswi': 0.96, 'wilfly': 0.68,
'wilsni1': 0.36, 'wiltur': 0.41000000000000003, 'winwre3': 0.68, 'wlswar': 0.77, 'wooduc': 0.61,
'wooscj2': 0.93, 'woothr': 0.8200000000000001, 'y00475': 0.45, 'yebfly': 0.52, 'yebsap': 0.39,
'yehbla': 0.6900000000000001, 'yelwar': 0.66, 'yerwar': 0.64, 'yetvir': 0.9
}
for key in thresholds:
thresholds[key] = 0.4<choose_model_class>
train_df["images"] = [np.array(load_img(".. /input/train/images/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(train_df.index)]
def get_model(resnest_config: dict, effnet_config: dict, weights_path: dict):
models = {}
device = torch.device("cuda")
 for model_key in weights_path:
path_dict = weights_path[model_key]
for path_key in path_dict:
if model_key == "resnest":
model = ResNestSED(**resnest_config)
 else:
model = EfficientNetSED(**effnet_config)
 checkpoint = torch.load(path_dict[path_key])
 model_state_dict = {}
for key in checkpoint["model_state_dict"]:
if key == "n_averaged":
continue
new_key = key.replace("module.", "")
 model_state_dict[new_key] = checkpoint["model_state_dict"][key]
model.load_state_dict(model_state_dict)
 model.to(device)
 model.eval()
models[path_key] = model
return models<categorify>
train_df["masks"] = [np.array(load_img(".. /input/train/masks/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(train_df.index)]
def normalize_melspec(X: np.ndarray):
eps = 1e-6
mean = X.mean()
X = X - mean
std = X.std()
Xstd = X /(std + eps)
 norm_min, norm_max = Xstd.min() , Xstd.max()
if(norm_max - norm_min)> eps:
V = Xstd
V[V < norm_min] = norm_min
V[V > norm_max] = norm_max
V = 255 *(V - norm_min)/(norm_max - norm_min)
 V = V.astype(np.uint8)
 else:
V = np.zeros_like(Xstd, dtype=np.uint8)
 return V<prepare_output>
train_df["coverage"] = train_df.masks.map(np.sum)/ pow(img_size_ori, 2)
 def cov_to_class(val):
for i in range(0, 11):
if val * 10 <= i :
return i
train_df["coverage_class"] = train_df.coverage.map(cov_to_class)
def prediction_for_clip(test_df: pd.DataFrame,
clip: np.ndarray,
models,
melspectrogram_parameters={},
pcen_parameters={}):
PERIOD = 30
images = []
y = clip.astype(np.float32)
 len_y = len(y)
 start = 0
end = PERIOD * TARGET_SR
while True:
y_batch = y[start:end].astype(np.float32)
 if len(y_batch)> 0:
max_vol = np.abs(y_batch ).max()
if max_vol > 0:
y_batch = np.asfortranarray(y_batch * 1 / max_vol)
 if len(y_batch)!= PERIOD * TARGET_SR:
y_pad = np.zeros(PERIOD * TARGET_SR, dtype=np.float32)
 y_pad[:len(y_batch)] = y_batch
melspec = librosa.feature.melspectrogram(y_pad,
sr=TARGET_SR,
**melspectrogram_parameters)
 pcen = librosa.pcen(melspec, sr=TARGET_SR, **pcen_parameters)
 clean_mel = librosa.power_to_db(melspec ** 1.5)
 melspec = librosa.power_to_db(melspec ).astype(np.float32)
 norm_melspec = normalize_melspec(melspec)
 norm_pcen = normalize_melspec(pcen)
 norm_clean_mel = normalize_melspec(clean_mel)
 image = np.stack([norm_melspec, norm_pcen, norm_clean_mel], axis=-1)
 height, width, _ = image.shape
image = cv2.resize(image,(int(width * 224 / height), 224))
image = np.moveaxis(image, 2, 0)
 image =(image / 255.0 ).astype(np.float32)
 images.append(image)
 break
start = end
end += PERIOD * TARGET_SR
melspec = librosa.feature.melspectrogram(y_batch,
sr=TARGET_SR,
**melspectrogram_parameters)
 pcen = librosa.pcen(melspec, sr=TARGET_SR, **pcen_parameters)
 clean_mel = librosa.power_to_db(melspec ** 1.5)
 melspec = librosa.power_to_db(melspec ).astype(np.float32)
 norm_melspec = normalize_melspec(melspec)
 norm_pcen = normalize_melspec(pcen)
 norm_clean_mel = normalize_melspec(clean_mel)
 image = np.stack([norm_melspec, norm_pcen, norm_clean_mel], axis=-1)
 height, width, _ = image.shape
image = cv2.resize(image,(int(width * 224 / height), 224))
image = np.moveaxis(image, 2, 0)
 image =(image / 255.0 ).astype(np.float32)
 images.append(image)
 array = np.asarray(images)
 tensors = torch.from_numpy(array)
 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 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.size(1), image.size(2))
image = image.to(device)
 outputs = {}
with torch.no_grad() :
for key in models:
prediction = models[key](image)
 framewise_outputs = prediction["framewise_output"].detach(
 ).cpu().numpy() [0]
outputs[key] = framewise_outputs
key = list(outputs.keys())[0]
framewise_outputs = np.zeros_like(outputs[key], dtype=np.float32)
 for key in outputs:
framewise_outputs += ratio[key] * outputs[key]
thresholded = np.zeros_like(framewise_outputs)
 for i in range(len(thresholds)) :
thresholded[:, i] = framewise_outputs[:, i] >= thresholds[INV_BIRD_CODE[i]]
sec_per_frame = PERIOD / thresholded.shape[0]
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 = sec_per_frame * detected[
head_idx] + global_time
offset = sec_per_frame * 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<predict_on_test>
ids_train, ids_valid, x_train, x_valid, y_train, y_valid, cov_train, cov_test, depth_train, depth_test = train_test_split(
 train_df.index.values,
np.array(train_df.images.tolist() ).reshape(-1, img_size_target, img_size_target, 1),
np.array(train_df.masks.tolist() ).reshape(-1, img_size_target, img_size_target, 1),
train_df.coverage.values,
train_df.z.values,
test_size=0.2, stratify=train_df.coverage_class, random_state= 1234 )
def prediction(test_df: pd.DataFrame,
test_audio: Path,
resnest_config: dict,
effnet_config: dict,
weights_path: dict,
melspectrogram_parameters={},
pcen_parameters={}):
models = get_model(resnest_config, effnet_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=TARGET_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,
models=models,
melspectrogram_parameters=melspectrogram_parameters,
pcen_parameters=pcen_parameters)
 prediction_dfs.append(prediction_df)
 prediction_df = pd.concat(prediction_dfs, axis=0, sort=False ).reset_index(drop=True)
 return prediction_df<predict_on_test>
ACTIVATION = "prelu"
def convolution_block(x, filters, size, strides=(1,1), padding='same', activation=True, bn=True):
x = Conv2D(filters, size, strides=strides, padding=padding )(x)
 if bn == True:
x = BatchNormalization(momentum=0.95 )(x)
 if activation == True:
x = Activation(ACTIVATION )(x)
 return x
def residual_block(blockInput, num_filters=16):
x = BatchNormalization(momentum=0.95 )(blockInput)
 x = Activation(ACTIVATION )(x)
 x = convolution_block(x, num_filters,(3,3))
x = convolution_block(x, num_filters,(3,3), activation=False, bn=False)
 x = Add()([x, blockInput])
 return x
prediction_df = prediction(test_df=test,
test_audio=test_audio,
resnest_config=resnest_model_config,
effnet_config=effnet_model_config,
weights_path=weights_path,
melspectrogram_parameters=melspectrogram_parameters,
pcen_parameters=pcen_parameters )<groupby>
def build_model(input_layer, start_neurons, DropoutRatio = 0.5):
conv1 = Conv2D(start_neurons * 1,(3, 3), activation=None, padding="same" )(input_layer)
 conv1 = residual_block(conv1,start_neurons * 1)
 conv1 = residual_block(conv1,start_neurons * 1)
 conv1 = Activation(ACTIVATION )(conv1)
 pool1 = MaxPooling2D(( 2, 2))(conv1)
 pool1 = Dropout(DropoutRatio/2 )(pool1)
 conv2 = Conv2D(start_neurons * 2,(3, 3), activation=None, padding="same" )(pool1)
 conv2 = residual_block(conv2,start_neurons * 2)
 conv2 = residual_block(conv2,start_neurons * 2)
 conv2 = Activation(ACTIVATION )(conv2)
 pool2 = MaxPooling2D(( 2, 2))(conv2)
 pool2 = Dropout(DropoutRatio )(pool2)
 conv3 = Conv2D(start_neurons * 4,(3, 3), activation=None, padding="same" )(pool2)
 conv3 = residual_block(conv3,start_neurons * 4)
 conv3 = residual_block(conv3,start_neurons * 4)
 conv3 = Activation(ACTIVATION )(conv3)
 pool3 = MaxPooling2D(( 2, 2))(conv3)
 pool3 = Dropout(DropoutRatio )(pool3)
 conv4 = Conv2D(start_neurons * 8,(3, 3), activation=None, padding="same" )(pool3)
 conv4 = residual_block(conv4,start_neurons * 8)
 conv4 = residual_block(conv4,start_neurons * 8)
 conv4 = Activation(ACTIVATION )(conv4)
 pool4 = MaxPooling2D(( 2, 2))(conv4)
 pool4 = Dropout(DropoutRatio )(pool4)
 convm = Conv2D(start_neurons * 16,(3, 3), activation=None, padding="same" )(pool4)
 convm = residual_block(convm,start_neurons * 16)
 convm = residual_block(convm,start_neurons * 16)
 convm = Activation(ACTIVATION )(convm)
 deconv4 = Conv2DTranspose(start_neurons * 8,(3, 3), strides=(2, 2), padding="same" )(convm)
 uconv4 = concatenate([deconv4, conv4])
 uconv4 = Dropout(DropoutRatio )(uconv4)
 uconv4 = Conv2D(start_neurons * 8,(3, 3), activation=None, padding="same" )(uconv4)
 uconv4 = residual_block(uconv4,start_neurons * 8)
 uconv4 = residual_block(uconv4,start_neurons * 8)
 uconv4 = Activation(ACTIVATION )(uconv4)
 deconv3 = Conv2DTranspose(start_neurons * 4,(3, 3), strides=(2, 2), padding="valid" )(uconv4)
 uconv3 = concatenate([deconv3, conv3])
 uconv3 = Dropout(DropoutRatio )(uconv3)
 uconv3 = Conv2D(start_neurons * 4,(3, 3), activation=None, padding="same" )(uconv3)
 uconv3 = residual_block(uconv3,start_neurons * 4)
 uconv3 = residual_block(uconv3,start_neurons * 4)
 uconv3 = Activation(ACTIVATION )(uconv3)
 deconv2 = Conv2DTranspose(start_neurons * 2,(3, 3), strides=(2, 2), padding="same" )(uconv3)
 uconv2 = concatenate([deconv2, conv2])
 uconv2 = Dropout(DropoutRatio )(uconv2)
 uconv2 = Conv2D(start_neurons * 2,(3, 3), activation=None, padding="same" )(uconv2)
 uconv2 = residual_block(uconv2,start_neurons * 2)
 uconv2 = residual_block(uconv2,start_neurons * 2)
 uconv2 = Activation(ACTIVATION )(uconv2)
 deconv1 = Conv2DTranspose(start_neurons * 1,(3, 3), strides=(2, 2), padding="valid" )(uconv2)
 uconv1 = concatenate([deconv1, conv1])
 uconv1 = Dropout(DropoutRatio )(uconv1)
 uconv1 = Conv2D(start_neurons * 1,(3, 3), activation=None, padding="same" )(uconv1)
 uconv1 = residual_block(uconv1,start_neurons * 1)
 uconv1 = residual_block(uconv1,start_neurons * 1)
 uconv1 = Activation(ACTIVATION )(uconv1)
 uconv1 = Dropout(DropoutRatio/2 )(uconv1)
 output_layer = Conv2D(1,(1,1), padding="same", activation="sigmoid" )(uconv1)
 return output_layer
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)
 site = events[0][4]
for i in range(n_events):
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)
<string_transform>
iou_thresholds = np.array([0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95])
 def iou(img_true, img_pred):
i = np.sum(( img_true*img_pred)>0)
 u = np.sum(( img_true + img_pred)>0)
 if u == 0:
return u
return i/u
def iou_metric(imgs_true, imgs_pred):
num_images = len(imgs_true)
 scores = np.zeros(num_images)
 for i in range(num_images):
if imgs_true[i].sum() == imgs_pred[i].sum() == 0:
scores[i] = 1
else:
scores[i] =(iou_thresholds <= iou(imgs_true[i], imgs_pred[i])).mean()
return scores.mean()
for key in labels:
labels[key] = " ".join(sorted(list(labels[key])))
<prepare_output>
x_train = np.append(x_train, [np.fliplr(x)for x in x_train], axis=0)
 y_train = np.append(y_train, [np.fliplr(x)for x in y_train], axis=0)
 print(x_train.shape)
 print(y_valid.shape )
row_ids = list(labels.keys())
 birds = list(labels.values())
 post_processed = pd.DataFrame({
"row_id": row_ids,
"birds": birds
})
 post_processed.head()<save_to_csv>
input_layer = Input(( img_size_target, img_size_target, 1))
output_layer = build_model(input_layer, 16,0.5)
 model = Model(input_layer, output_layer)
 model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["acc"] )
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 )<set_options>
early_stopping = EarlyStopping(monitor='val_acc', mode = 'max',patience=20, verbose=1)
 model_checkpoint = ModelCheckpoint("./unet_best1.model",monitor='val_acc',
mode = 'max', save_best_only=True, verbose=1)
 reduce_lr = ReduceLROnPlateau(monitor='val_acc', mode = 'max',factor=0.5, patience=6, min_lr=0.001, verbose=1)
 epochs = 200
batch_size = 32
history = model.fit(x_train, y_train,
validation_data=[x_valid, y_valid],
epochs=epochs,
batch_size=batch_size,
callbacks=[early_stopping, model_checkpoint, reduce_lr],
verbose=1 )
pd.options.display.max_rows = 500
pd.options.display.max_columns = 500<feature_engineering>
model = load_model("./unet_best1.model" )
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)
 @contextmanager
def timer(name: str)-> None:

 t0 = time.time()
print("[{}] start".format(name))
yield
print("[{}] done in {:.0f} s".format(name, time.time() - t0))<define_variables>
def predict_result(model,x_test,img_size_target):
preds_test = model.predict(x_test ).reshape(-1, img_size_target, img_size_target)
 preds_test += np.array([ np.fliplr(a)for a in model.predict(np.array([np.fliplr(x)for x in x_test])).reshape(-1, img_size_target, img_size_target)])
 return preds_test/2.0
set_seed(1213 )<define_variables>
preds_valid = predict_result(model,x_valid,img_size_target )
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"<load_from_csv>
def filter_image(img):
if img.sum() < 100:
return np.zeros(img.shape)
 else:
return img
thresholds = np.linspace(0.3, 0.7, 31)
 ious = np.array([iou_metric(y_valid.reshape(( -1, img_size_target, img_size_target)) , [filter_image(img)for img in preds_valid > threshold])for threshold in tqdm_notebook(thresholds)])
train = pd.read_csv(RAW_DATA / "train.csv" )<load_from_csv>
def rle_encode(im):
pixels = im.flatten(order = 'F')
 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 )
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" )<save_to_csv>
del x_train, x_valid, y_train, y_valid, preds_valid
gc.collect()
sub = pd.read_csv(".. /input/birdsong-recognition/sample_submission.csv")
 sub.to_csv("submission.csv", index=False )<init_hyperparams>
x_test = np.array([(np.array(load_img(".. /input/test/images/{}.png".format(idx), grayscale = True)))/ 255 for idx in tqdm_notebook(test_df.index)] ).reshape(-1, img_size_target, img_size_target, 1)
 preds_test = predict_result(model,x_test,img_size_target )
TARGET_SR = 32000
model_config = {
"base_model_name": "resnest50_fast_1s1x64d",
"pretrained": False,
"num_classes": 264,
"trained_weights": ".. /input/training-birdsong-baseline-resnest50-fast/best_model.pth"
}
melspectrogram_parameters = {
"n_mels": 128,
"fmin": 20,
"fmax": 16000
}<define_variables>
t1 = time.time()
pred_dict = {idx: rle_encode(filter_image(preds_test[i] > threshold_best)) for i, idx in enumerate(tqdm_notebook(test_df.index.values)) }
t2 = time.time()
print(f"Usedtime = {t2-t1} s" )
<define_search_model><EOS> 
sub = pd.DataFrame.from_dict(pred_dict,orient='index')
 sub.index.names = ['id']
sub.columns = ['rle_mask']
sub.to_csv('submission.csv' )