Kaggle competition: Costa Rican Household Poverty Level Prediction
code_map =dict(zip(train_all['primary_label'].tolist() ,train_all['ebird_code'].tolist()))
len(code_map )<feature_engineering>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 print("Shape of training set: {}".format(train.shape))
print("Shape of training set: {}".format(test.shape))
train.head()
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<feature_engineering>
print("Head of house: {}".format(train.shape[0]))
print("Number of unique house IDs: {}".format(len(train['idhogar'].unique())) )
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']] 
<data_type_conversions>
train['child_per_adult'] = train['hogar_nin'] / train['hogar_adul']
test['child_per_adult'] = test['hogar_nin'] / test['hogar_adul']
train['room_per_person'] = train['rooms'] / train['tamviv']
test['room_per_person'] = test['rooms'] / test['tamviv']
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)) )<create_dataframe>
train.loc[(train['epared1'] == 1)&(train['etecho1'] == 1), 'bad_walls_roof'] = 1
test.loc[(test['epared1'] == 1)&(test['etecho1'] == 1), 'bad_walls_roof'] = 1
train.loc[(train['bad_walls_roof'] == 1)&(train['eviv1'] == 1), 'bad_walls_roof_floor'] = 1
test.loc[(test['bad_walls_roof'] == 1)&(test['eviv1'] == 1), 'bad_walls_roof_floor'] = 1
train.loc[(train['abastaguadentro'] != 1)&(train['noelec'] == 1), 'no_elec_or_water'] = 1
test.loc[(test['abastaguadentro'] != 1)&(test['noelec'] == 1), 'no_elec_or_water'] = 1
train.loc[(train['v14a'] == 1)&(train['refrig'] == 1), 'bath_and_fridge'] = 1
test.loc[(test['v14a'] == 1)&(test['refrig'] == 1), 'bath_and_fridge'] = 1
train.loc[(train['computer'] == 1)&(train['television'] == 1), 'pc_and_tv'] = 1
test.loc[(test['computer'] == 1)&(test['television'] == 1), 'pc_and_tv'] = 1
train['bad_walls_roof'] = train['bad_walls_roof'].fillna(0)
 train['bad_walls_roof_floor'] = train['bad_walls_roof_floor'].fillna(0)
 train['no_elec_or_water'] = train['no_elec_or_water'].fillna(0)
 train['bath_and_fridge'] = train['bath_and_fridge'].fillna(0)
 train['pc_and_tv'] = train['pc_and_tv'].fillna(0)
 test['pc_and_tv'] = test['pc_and_tv'].fillna(0)
 test['bath_and_fridge'] = test['bath_and_fridge'].fillna(0)
 test['bad_walls_roof'] = test['bad_walls_roof'].fillna(0)
 test['bad_walls_roof_floor'] = test['bad_walls_roof_floor'].fillna(0)
 test['no_elec_or_water'] = test['no_elec_or_water'].fillna(0)
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)
 ]<define_variables>
train.loc[train['edjefa'] == "no", "edjefa"] = 0
train.loc[train['edjefe'] == "no", "edjefe"] = 0
test.loc[test['edjefa'] == "no", "edjefa"] = 0
test.loc[test['edjefe'] == "no", "edjefe"] = 0
train.loc[(train['edjefa'] == "yes")&(train['parentesco1'] == 1), "edjefa"] = train.loc[(train['edjefa'] == "yes")&(train['parentesco1'] == 1), "escolari"]
train.loc[(train['edjefe'] == "yes")&(train['parentesco1'] == 1), "edjefe"] = train.loc[(train['edjefe'] == "yes")&(train['parentesco1'] == 1), "escolari"]
test.loc[(test['edjefa'] == "yes")&(test['parentesco1'] == 1), "edjefa"] = test.loc[(test['edjefa'] == "yes")&(test['parentesco1'] == 1), "escolari"]
test.loc[(test['edjefe'] == "yes")&(test['parentesco1'] == 1), "edjefe"] = test.loc[(test['edjefe'] == "yes")&(test['parentesco1'] == 1), "escolari"]
train.loc[train['edjefa'] == "yes", "edjefa"] = 4
train.loc[train['edjefe'] == "yes", "edjefe"] = 4
test.loc[test['edjefa'] == "yes", "edjefa"] = 4
test.loc[test['edjefe'] == "yes", "edjefe"] = 4
train['edjefe'] = train['edjefe'].astype("int")
 train['edjefa'] = train['edjefa'].astype("int")
 test['edjefe'] = test['edjefe'].astype("int")
 test['edjefa'] = test['edjefa'].astype("int")
 train['HoH_EduMax'] = np.max(train[['edjefa','edjefe']], axis=1)
 test['HoH_EduMax'] = np.max(test[['edjefa','edjefe']], axis=1)
 train['v2a1']=train['v2a1'].fillna(-1)
 test['v2a1']=test['v2a1'].fillna(-1)
 train['qmobilephone']=train['qmobilephone'].fillna(0)
 test['qmobilephone']=test['qmobilephone'].fillna(0)
 test['v18q1']=test['v18q1'].fillna(0)
 train['v18q1']=train['v18q1'].fillna(0 )
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"<load_pretrained>
train = train[train.meaneduc.isnull() == False]
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<load_pretrained>
NUMERICAL_COLUMNS = ['v2a1','hacdor','hacapo','rooms', 'bedrooms', 'overcrowding', 'dependency', 'meaneduc','v18q1','r4h1','r4h2','r4h3','r4m1','r4m2','r4m3','r4t1','r4t2','r4t3','hogar_adul','hogar_nin','hogar_mayor','hogar_total','room_per_person','HoH_EduMax', 'age', 'child_per_adult', 'qmobilephone']
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")
<categorify>
WALL = ['paredblolad','paredzocalo','paredpreb','pareddes','paredmad','paredzinc','paredfibras','paredother']
FLOOR = ['pisomoscer','pisocemento','pisoother','pisonatur','pisonotiene','pisomadera']
ROOF = ['techozinc','techoentrepiso','techocane','techootro']
WATER =['abastaguadentro','abastaguafuera','abastaguano']
ELEC = ['public','planpri','noelec','coopele']
TOILET = ['sanitario1','sanitario2','sanitario3','sanitario5','sanitario6']
COOK = ['energcocinar1','energcocinar2','energcocinar3','energcocinar4']
RUBBISH = ['elimbasu1','elimbasu2','elimbasu3','elimbasu4','elimbasu5','elimbasu6']
WALL_QLTY = ['epared1', 'epared1','epared3']
ROOF_QLTY = ['etecho1','etecho2','etecho3']
FLOOR_QLTY = ['eviv1','eviv2','eviv3']
SEX = ['male', 'female']
CIVIL = ['estadocivil1','estadocivil2','estadocivil3','estadocivil4','estadocivil5','estadocivil6','estadocivil7']
H_OWNER = ['tipovivi1','tipovivi2','tipovivi3','tipovivi4','tipovivi5']
REGION = ['lugar1','lugar2','lugar3','lugar4','lugar5','lugar6']
AREA = ['area1', 'area2']
ALL_LISTS = [AREA, REGION, H_OWNER, CIVIL, SEX, FLOOR_QLTY, ROOF_QLTY, WALL_QLTY, RUBBISH, COOK, TOILET, ELEC, WATER, ROOF, FLOOR, WALL]
LIST_NAMES = ['AREA', 'REGION', 'H_OWNER', 'CIVIL', 'SEX', 'FLOOR_QLTY', 'ROOF_QLTY', 'WALL_QLTY', 'RUBBISH', 'COOK', 'TOILET', 'ELEC', 'WATER', 'ROOF', 'FLOOR', 'WALL']
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<load_pretrained>
def OHE_to_cat(df, lists_of_cols, names):
for cols, name in zip(lists_of_cols, names):
df[name] =(df[cols] == 1 ).idxmax(1)
 df[name] = df[name].apply(lambda x: cols.index(x))
df = df.drop(columns=cols)
 return df
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<predict_on_test>
train = OHE_to_cat(train, ALL_LISTS, LIST_NAMES)
 test = OHE_to_cat(test, ALL_LISTS, LIST_NAMES )
prediction_df = prediction(test_df=test,
test_audio=TEST_AUDIO_DIR,
model_config=model_config,
weights_path=weights_path,
threshold=0.7 )<groupby>
REPEAT_COLS = ['v18q','tamhog', 'tamviv', 'rez_esc','hhsize','parentesco1',
'parentesco2','parentesco3','parentesco4','parentesco5','parentesco6','parentesco7',
'parentesco8','parentesco9','parentesco10','parentesco11', 'parentesco12',
'idhogar','mobilephone', 'edjefa', 'edjefe', 'escolari', 'instlevel1',
'instlevel2', 'instlevel3', 'instlevel4', 'instlevel5', 'instlevel6', 'instlevel7',
'instlevel8', 'instlevel9']
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 )<prepare_output>
train = train.drop(columns=REPEAT_COLS)
 train = train.drop(columns=['Id'])
 test = test.drop(columns=REPEAT_COLS )
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()<save_to_csv>
y_train = train["Target"]
train.Target.value_counts()
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 )<count_values>

 count_class_4, count_class_2, count_class_3, count_class_1 = train.Target.value_counts()
train_class_1 = train[train['Target'] == 1]
train_class_2 = train[train['Target'] == 2]
train_class_3 = train[train['Target'] == 3]
train_class_4 = train[train['Target'] == 4]
train_class_1_under = train_class_1.sample(count_class_1, random_state=99)
 train_class_2_under = train_class_2.sample(round(count_class_2), random_state=99)
 train_class_3_under = train_class_3.sample(round(count_class_3), random_state=99)
 train_class_4_under = train_class_4.sample(round(count_class_4*.25), random_state=99)
 train = pd.concat([train_class_1_under, train_class_2_under, train_class_3_under, train_class_4_under], axis=0)
 train.Target.value_counts()
submission.birds.value_counts()<set_options>
y_train = train['Target']
%matplotlib inline
sns.set_palette(sns.color_palette('tab20', 20))
InteractiveShell.ast_node_interactivity = "all"
init_notebook_mode(connected=True )<feature_engineering>
train_cols = train.columns
sm = SMOTE(random_state=2)
 train_res, y_train_res = sm.fit_sample(train, y_train.ravel())
 train_res = pd.DataFrame(train_res, columns=train_cols)
 train_res.Target.value_counts()
nb_start = datetime.now()
plt.rcParams['figure.figsize'] = [16, 10]
plt.rcParams['font.size'] = 14
pd.set_option('display.max_columns', 99)
 pd.set_option('display.max_rows', 99 )<define_variables>
X_train = train_res.drop(['Target'], axis = 1)
 y_train = y_train_res
@dataclass
class BirdLists:
all = [
'aldfly', 'ameavo', 'amebit', 'amecro', 'amegfi', 'amekes', 'amepip', 'amered', 'amerob', 'amewig', 'amewoo',
'amtspa', 'annhum', 'astfly', 'baisan', 'baleag', 'balori', 'banswa', 'barswa', 'bawwar', 'belkin1', 'belspa2',
'bewwre', 'bkbcuc', 'bkbmag1', 'bkbwar', 'bkcchi', 'bkchum', 'bkhgro', 'bkpwar', 'bktspa', 'blkpho', 'blugrb1',
'blujay', 'bnhcow', 'boboli', 'bongul', 'brdowl', 'brebla', 'brespa', 'brncre', 'brnthr', 'brthum', 'brwhaw',
'btbwar', 'btnwar', 'btywar', 'buffle', 'buggna', 'buhvir', 'bulori', 'bushti', 'buwtea', 'buwwar', 'cacwre',
'calgul', 'calqua', 'camwar', 'cangoo', 'canwar', 'canwre', 'carwre', 'casfin', 'caster1', 'casvir', 'cedwax',
'chispa', 'chiswi', 'chswar', 'chukar', 'clanut', 'cliswa', 'comgol', 'comgra', 'comloo', 'commer', 'comnig',
'comrav', 'comred', 'comter', 'comyel', 'coohaw', 'coshum', 'cowscj1', 'daejun', 'doccor', 'dowwoo', 'dusfly',
'eargre', 'easblu', 'easkin', 'easmea', 'easpho', 'eastow', 'eawpew', 'eucdov', 'eursta', 'evegro', 'fiespa',
'fiscro', 'foxspa', 'gadwal', 'gcrfin', 'gnttow', 'gockin', 'gocspa', 'goleag', 'grbher3', 'grcfly', 'greegr',
'greroa', 'greyel', 'grhowl', 'grnher', 'grtgra', 'grycat', 'gryfly', 'haiwoo', 'hamfly', 'herthr', 'hoomer',
'hoowar', 'horgre', 'horlar', 'houfin', 'houspa', 'houwre', 'indbun', 'juntit1', 'killde', 'labwoo', 'larspa',
'lazbun', 'leabit', 'leafly', 'leasan', 'lecthr', 'lesgol', 'lesnig', 'lesyel', 'lewwoo', 'linspa', 'lobcur',
'lobdow', 'logshr', 'lotduc', 'louwat', 'macwar', 'magwar', 'mallar3', 'marwre', 'merlin', 'moublu', 'mouchi',
'moudov', 'norcar', 'norfli', 'norhar2', 'normoc', 'norpar', 'norpin', 'norsho', 'norwat', 'nrwswa', 'nutwoo',
'olsfly', 'orcwar', 'osprey', 'ovenbi1', 'palwar', 'pasfly', 'pecsan', 'perfal', 'phaino', 'pibgre', 'pilwoo',
'pingro', 'pinjay', 'pinsis', 'pinwar', 'plsvir', 'prawar', 'purfin', 'pygnut', 'rebmer', 'rebnut', 'rebsap',
'rebwoo', 'redcro', 'redhea', 'reevir1', 'renpha', 'reshaw', 'rethaw', 'rewbla', 'ribgul', 'rinduc', 'robgro',
'rocpig', 'rocwre', 'rthhum', 'ruckin', 'rudduc', 'rufgro', 'rufhum', 'rusbla', 'sagspa1', 'sagthr', 'savspa',
'saypho', 'scatan', 'scoori', 'semplo', 'semsan', 'sheowl', 'shshaw', 'snobun', 'snogoo', 'solsan', 'sonspa',
'sora', 'sposan', 'spotow', 'stejay', 'swahaw', 'swaspa', 'swathr', 'treswa', 'truswa', 'tuftit', 'tunswa',
'veery', 'vesspa', 'vigswa', 'warvir', 'wesblu', 'wesgre', 'weskin', 'wesmea', 'wessan', 'westan', 'wewpew',
'whbnut', 'whcspa', 'whfibi', 'whtspa', 'whtswi', 'wilfly', 'wilsni1', 'wiltur', 'winwre3', 'wlswar', 'wooduc',
'wooscj2', 'woothr', 'y00475', 'yebfly', 'yebsap', 'yehbla', 'yelwar', 'yerwar', 'yetvir'
]
top_165 = [
'amecro', 'amegfi', 'amekes', 'amered', 'amerob', 'amewig', 'amtspa', 'annhum', 'baleag', 'balori', 'barswa',
'bawwar', 'belkin1', 'bewwre', 'bkbmag1', 'bkcchi', 'bkhgro', 'blkpho', 'blujay', 'bnhcow', 'bongul', 'brdowl',
'brebla', 'brncre', 'brnthr', 'btbwar', 'btnwar', 'buffle', 'buggna', 'buhvir', 'bushti', 'buwtea', 'calgul',
'calqua', 'cangoo', 'carwre', 'caster1', 'cedwax', 'chispa', 'chiswi', 'chswar', 'cliswa', 'comgol', 'comgra',
'comloo', 'commer', 'comrav', 'comyel', 'coohaw', 'cowscj1', 'daejun', 'doccor', 'dowwoo', 'easblu', 'easkin',
'easmea', 'easpho', 'eastow', 'eawpew', 'eucdov', 'eursta', 'fiespa', 'fiscro', 'foxspa', 'gadwal', 'gockin',
'gocspa', 'grbher3', 'grcfly', 'greegr', 'greyel', 'grhowl', 'grnher', 'grtgra', 'grycat', 'haiwoo', 'herthr',
'hoomer', 'horgre', 'horlar', 'houfin', 'houspa', 'houwre', 'indbun', 'killde', 'leafly', 'leasan', 'lesgol',
'lesyel', 'linspa', 'logshr', 'magwar', 'mallar3', 'marwre', 'merlin', 'mouchi', 'moudov', 'norcar', 'norfli',
'norhar2', 'normoc', 'norpar', 'norpin', 'norsho', 'nrwswa', 'nutwoo', 'orcwar', 'osprey', 'ovenbi1', 'palwar',
'perfal', 'pibgre', 'pilwoo', 'pinsis', 'pinwar', 'purfin', 'rebmer', 'rebnut', 'rebwoo', 'redhea', 'reevir1',
'reshaw', 'rethaw', 'rewbla', 'ribgul', 'rinduc', 'robgro', 'rocpig', 'rthhum', 'ruckin', 'rudduc', 'savspa',
'saypho', 'scatan', 'semplo', 'shshaw', 'snogoo', 'sonspa', 'sposan', 'spotow', 'stejay', 'swaspa', 'swathr',
'treswa', 'tuftit', 'veery', 'vigswa', 'warvir', 'wesblu', 'weskin', 'wesmea', 'westan', 'whbnut', 'whcspa',
'whtspa', 'wilsni1', 'wiltur', 'wlswar', 'wooduc', 'woothr', 'y00475', 'yebsap', 'yelwar', 'yerwar', 'yetvir'
]
non_zero = [
'ameavo', 'amebit', 'amecro', 'amegfi', 'amekes', 'amepip', 'amerob', 'amewig', 'amewoo', 'amtspa', 'annhum',
'astfly', 'baisan', 'baleag', 'banswa', 'belkin1', 'belspa2', 'bkbcuc', 'bkbmag1', 'bkbwar', 'bkcchi', 'bkchum',
'bkpwar', 'bktspa', 'blkpho', 'blugrb1', 'blujay', 'boboli', 'bongul', 'brdowl', 'brebla', 'brespa', 'brthum',
'brwhaw', 'btbwar', 'btnwar', 'btywar', 'buffle', 'buhvir', 'bushti', 'buwtea', 'buwwar', 'cacwre', 'calgul',
'calqua', 'camwar', 'cangoo', 'canwar', 'canwre', 'casfin', 'caster1', 'casvir', 'cedwax', 'chiswi', 'chswar',
'chukar', 'clanut', 'cliswa', 'comgol', 'commer', 'comnig', 'comred', 'comter', 'comyel', 'coohaw', 'coshum',
'cowscj1', 'daejun', 'doccor', 'dusfly', 'eargre', 'easkin', 'easmea', 'eawpew', 'eucdov', 'evegro', 'fiespa',
'fiscro', 'gadwal', 'gcrfin', 'gnttow', 'gocspa', 'goleag', 'grcfly', 'greroa', 'greyel', 'grnher', 'grtgra',
'grycat', 'gryfly', 'hamfly', 'herthr', 'hoomer', 'hoowar', 'horgre', 'horlar', 'indbun', 'juntit1', 'labwoo',
'larspa', 'lazbun', 'leabit', 'leafly', 'leasan', 'lecthr', 'lesgol', 'lesnig', 'lesyel', 'lewwoo', 'linspa',
'lobcur', 'lobdow', 'logshr', 'lotduc', 'louwat', 'macwar', 'magwar', 'merlin', 'moublu', 'mouchi', 'norfli',
'norhar2', 'norpin', 'norsho', 'norwat', 'nrwswa', 'olsfly', 'ovenbi1', 'palwar', 'pasfly', 'pecsan', 'perfal',
'phaino', 'pibgre', 'pingro', 'pinjay', 'pinsis', 'pinwar', 'plsvir', 'prawar', 'purfin', 'pygnut', 'rebmer',
'rebsap', 'rebwoo', 'redhea', 'reevir1', 'renpha', 'reshaw', 'rewbla', 'rinduc', 'robgro', 'rocwre', 'rthhum',
'rudduc', 'rufgro', 'rufhum', 'rusbla', 'sagspa1', 'sagthr', 'saypho', 'scatan', 'scoori', 'semplo', 'semsan',
'sheowl', 'shshaw', 'snobun', 'snogoo', 'solsan', 'sonspa', 'sora', 'sposan', 'stejay', 'swahaw', 'swaspa',
'truswa', 'tunswa', 'veery', 'vesspa', 'vigswa', 'wesblu', 'wesgre', 'weskin', 'wessan', 'westan', 'wewpew',
'whcspa', 'whfibi', 'whtswi', 'wilfly', 'wilsni1', 'wiltur', 'winwre3', 'wooduc', 'wooscj2', 'woothr', 'y00475',
'yebfly', 'yebsap', 'yehbla', 'yetvir'
]<define_variables>
X_train, X_dev, y_train, y_dev = train_test_split(X_train,y_train,test_size = 0.2,random_state = 0 )
SAMPLE_RATE = 32000
CHUNK_DURATION = 5
BATCH_SIZE = 64
chunk_len = SAMPLE_RATE * CHUNK_DURATION<set_options>
sc_X = StandardScaler()
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 
<categorify>
X_train_bin = X_train.drop(NUMERICAL_COLUMNS, axis = 1)
 X_dev_bin = X_dev.drop(NUMERICAL_COLUMNS, axis = 1)
 X_train = X_train[NUMERICAL_COLUMNS]
X_dev = X_dev[NUMERICAL_COLUMNS]
def do_mixup(x, mixup_lambda):

 out =(x[0:: 2].transpose(0, -1)* mixup_lambda[0:: 2] + \
x[1:: 2].transpose(0, -1)* mixup_lambda[1:: 2] ).transpose(0, -1)
 return out
def interpolate(x, ratio):

(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, frames_num):

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

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

return output
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.)
class ConvBlock(nn.Module):
def __init__(self, in_channels, out_channels):
super(ConvBlock, self ).__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 ConvBlock5x5(nn.Module):
def __init__(self, in_channels, out_channels):
super(ConvBlock5x5, self ).__init__()
self.conv1 = nn.Conv2d(in_channels=in_channels,
out_channels=out_channels,
kernel_size=(5, 5), stride=(1, 1),
padding=(2, 2), bias=False)
 self.bn1 = nn.BatchNorm2d(out_channels)
 self.init_weight()
def init_weight(self):
init_layer(self.conv1)
 init_bn(self.bn1)
 def forward(self, input, pool_size=(2, 2), pool_type='avg'):
x = input
x = F.relu_(self.bn1(self.conv1(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, n_in, n_out, activation='linear', temperature=1.) :
super(AttBlock, self ).__init__()
self.activation = activation
self.temperature = temperature
self.att = nn.Conv1d(in_channels=n_in, out_channels=n_out, kernel_size=1, stride=1, padding=0, bias=True)
 self.cla = nn.Conv1d(in_channels=n_in, out_channels=n_out, kernel_size=1, stride=1, padding=0, bias=True)
 self.bn_att = nn.BatchNorm1d(n_out)
 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)
 class Cnn14(nn.Module):
def __init__(self, sample_rate, window_size, hop_size, mel_bins, fmin,
fmax, classes_num):
super(Cnn14, self ).__init__()
window = 'hann'
center = True
pad_mode = 'reflect'
ref = 1.0
amin = 1e-10
top_db = None
self.dataset_mean = 0.
 self.dataset_std = 1.
 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=32, time_stripes_num=2,
freq_drop_width=8, freq_stripes_num=2)
 self.bn0 = nn.BatchNorm2d(64)
 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.fc_audioset = nn.Linear(2048, classes_num, bias=True)
 self.init_weight()
def init_weight(self):
init_bn(self.bn0)
 init_layer(self.fc1)
 init_layer(self.fc_audioset)
 def forward(self, input, mixup_lambda=None):

 x = self.spectrogram_extractor(input)
 x = self.logmel_extractor(x)
 x =(x - self.dataset_mean)/ self.dataset_std
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)
 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)
 x = torch.mean(x, dim=3)
(x1, _)= torch.max(x, dim=2)
 x2 = torch.mean(x, dim=2)
 x = x1 + x2
x = F.dropout(x, p=0.5, training=self.training)
 x = F.relu_(self.fc1(x))
embedding = F.dropout(x, p=0.5, training=self.training)
 clipwise_output = torch.sigmoid(self.fc_audioset(x))
output_dict = {'clipwise_output': clipwise_output, 'embedding': embedding}
return output_dict<concatenate>
X_train2 = pd.DataFrame(sc_X.fit_transform(X_train))
X_dev2= pd.DataFrame(sc_X.transform(X_dev))
def _resnet_conv3x3(in_planes, out_planes):
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=1,
padding=1, groups=1, bias=False, dilation=1)
 def _resnet_conv1x1(in_planes, out_planes):
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=1, bias=False)
 class _ResnetBasicBlock(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
base_width=64, dilation=1, norm_layer=None):
super(_ResnetBasicBlock, self ).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
if groups != 1 or base_width != 64:
raise ValueError('_ResnetBasicBlock only supports groups=1 and base_width=64')
 if dilation > 1:
raise NotImplementedError("Dilation > 1 not supported in _ResnetBasicBlock")
 self.stride = stride
self.conv1 = _resnet_conv3x3(inplanes, planes)
 self.bn1 = norm_layer(planes)
 self.relu = nn.ReLU(inplace=True)
 self.conv2 = _resnet_conv3x3(planes, planes)
 self.bn2 = norm_layer(planes)
 self.downsample = downsample
self.stride = stride
self.init_weights()
def init_weights(self):
init_layer(self.conv1)
 init_bn(self.bn1)
 init_layer(self.conv2)
 init_bn(self.bn2)
 nn.init.constant_(self.bn2.weight, 0)
 def forward(self, x):
identity = x
if self.stride == 2:
out = F.avg_pool2d(x, kernel_size=(2, 2))
else:
out = x
out = self.conv1(out)
 out = self.bn1(out)
 out = self.relu(out)
 out = F.dropout(out, p=0.1, training=self.training)
 out = self.conv2(out)
 out = self.bn2(out)
 if self.downsample is not None:
identity = self.downsample(identity)
 out += identity
out = self.relu(out)
 return out
class _ResnetBottleneck(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
base_width=64, dilation=1, norm_layer=None):
super(_ResnetBottleneck, self ).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
width = int(planes *(base_width / 64.))* groups
self.stride = stride
self.conv1 = _resnet_conv1x1(inplanes, width)
 self.bn1 = norm_layer(width)
 self.conv2 = _resnet_conv3x3(width, width)
 self.bn2 = norm_layer(width)
 self.conv3 = _resnet_conv1x1(width, planes * self.expansion)
 self.bn3 = norm_layer(planes * self.expansion)
 self.relu = nn.ReLU(inplace=True)
 self.downsample = downsample
self.stride = stride
self.init_weights()
def init_weights(self):
init_layer(self.conv1)
 init_bn(self.bn1)
 init_layer(self.conv2)
 init_bn(self.bn2)
 init_layer(self.conv3)
 init_bn(self.bn3)
 nn.init.constant_(self.bn3.weight, 0)
 def forward(self, x):
identity = x
if self.stride == 2:
x = F.avg_pool2d(x, kernel_size=(2, 2))
out = self.conv1(x)
 out = self.bn1(out)
 out = self.relu(out)
 out = self.conv2(out)
 out = self.bn2(out)
 out = self.relu(out)
 out = F.dropout(out, p=0.1, training=self.training)
 out = self.conv3(out)
 out = self.bn3(out)
 if self.downsample is not None:
identity = self.downsample(identity)
 out += identity
out = self.relu(out)
 return out
class _ResNet(nn.Module):
def __init__(self, block, layers, zero_init_residual=False,
groups=1, width_per_group=64, replace_stride_with_dilation=None,
norm_layer=None):
super(_ResNet, self ).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
self._norm_layer = norm_layer
self.inplanes = 64
self.dilation = 1
if replace_stride_with_dilation is None:
replace_stride_with_dilation = [False, False, False]
if len(replace_stride_with_dilation)!= 3:
raise ValueError("replace_stride_with_dilation should be None "
"or a 3-element tuple, got {}".format(replace_stride_with_dilation))
self.groups = groups
self.base_width = width_per_group
self.layer1 = self._make_layer(block, 64, layers[0], stride=1)
 self.layer2 = self._make_layer(block, 128, layers[1], stride=2,
dilate=replace_stride_with_dilation[0])
 self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
dilate=replace_stride_with_dilation[1])
 self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
dilate=replace_stride_with_dilation[2])
 def _make_layer(self, block, planes, blocks, stride=1, dilate=False):
norm_layer = self._norm_layer
downsample = None
previous_dilation = self.dilation
if dilate:
self.dilation *= stride
stride = 1
if stride != 1 or self.inplanes != planes * block.expansion:
if stride == 1:
downsample = nn.Sequential(
 _resnet_conv1x1(self.inplanes, planes * block.expansion),
norm_layer(planes * block.expansion), 
)
 init_layer(downsample[0])
 init_bn(downsample[1])
 elif stride == 2:
downsample = nn.Sequential(
 nn.AvgPool2d(kernel_size=2),
_resnet_conv1x1(self.inplanes, planes * block.expansion),
norm_layer(planes * block.expansion), 
)
 init_layer(downsample[1])
 init_bn(downsample[2])
 layers = []
layers.append(block(self.inplanes, planes, stride, downsample, self.groups,
self.base_width, previous_dilation, norm_layer))
self.inplanes = planes * block.expansion
for _ in range(1, blocks):
layers.append(block(self.inplanes, planes, groups=self.groups,
base_width=self.base_width, dilation=self.dilation,
norm_layer=norm_layer))
return nn.Sequential(*layers)
 def forward(self, x):
x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)
 return x
class ResNet38(nn.Module):
def __init__(self, sample_rate, window_size, hop_size, mel_bins, fmin,
fmax, classes_num):
super(ResNet38, self ).__init__()
window = 'hann'
center = True
pad_mode = 'reflect'
ref = 1.0
amin = 1e-10
top_db = None
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(64)
 self.conv_block1 = ConvBlock(in_channels=1, out_channels=64)
 self.resnet = _ResNet(block=_ResnetBasicBlock, layers=[3, 4, 6, 3], zero_init_residual=True)
 self.conv_block_after1 = ConvBlock(in_channels=512, out_channels=2048)
 self.fc1 = nn.Linear(2048, 2048)
 self.fc_audioset = nn.Linear(2048, classes_num, bias=True)
 self.init_weights()
def init_weights(self):
init_bn(self.bn0)
 init_layer(self.fc1)
 init_layer(self.fc_audioset)
 def forward(self, input, mixup_lambda=None):

 x = self.spectrogram_extractor(input)
 x = self.logmel_extractor(x)
 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)
 x = self.conv_block1(x, pool_size=(2, 2), pool_type='avg')
 x = F.dropout(x, p=0.2, training=self.training, inplace=True)
 x = self.resnet(x)
 x = F.avg_pool2d(x, kernel_size=(2, 2))
x = F.dropout(x, p=0.2, training=self.training, inplace=True)
 x = self.conv_block_after1(x, pool_size=(1, 1), pool_type='avg')
 x = F.dropout(x, p=0.2, training=self.training, inplace=True)
 x = torch.mean(x, dim=3)
(x1, _)= torch.max(x, dim=2)
 x2 = torch.mean(x, dim=2)
 x = x1 + x2
x = F.dropout(x, p=0.5, training=self.training)
 x = F.relu_(self.fc1(x))
embedding = F.dropout(x, p=0.5, training=self.training)
 clipwise_output = torch.sigmoid(self.fc_audioset(x))
output_dict = {'clipwise_output': clipwise_output, 'embedding': embedding}
return output_dict<choose_model_class>
X_train2.columns = X_train.columns.values
X_dev2.columns = X_dev.columns.values
X_train2.index = X_train.index.values
X_dev2.index = X_dev.index.values
X_train = pd.concat([X_train2, X_train_bin],axis=1, sort=False)
 X_dev = pd.concat([X_dev2,X_dev_bin],axis=1, sort=False )
_window = 'hann'
_center = True
_pad_mode = 'reflect'
_ref = 1.0
_amin = 1e-10
_top_db = None
_model_config = {
"sample_rate": 32000,
"window_size": 1024,
"hop_size": 320,
"mel_bins": 64,
"fmin": 50,
"fmax": 14000,
"classes_num": 527
}
def my_cnn14(n_fft, n_mels, n_classes=100, hop_size=320, fmin=160, fmax=10300):
model = Cnn14(**_model_config)
 model.fc_audioset = nn.Linear(2048, n_classes, bias=True)
 init_layer(model.fc_audioset)
 model.spectrogram_extractor = Spectrogram(
 n_fft=n_fft, hop_length=hop_size, win_length=n_fft, window=_window,
center=_center, pad_mode=_pad_mode, freeze_parameters=True)
 model.logmel_extractor = LogmelFilterBank(
 sr=SAMPLE_RATE, n_fft=n_fft, n_mels=n_mels, fmin=fmin, fmax=fmax,
ref=_ref, amin=_amin, top_db=_top_db, freeze_parameters=True)
 model.bn0 = nn.BatchNorm2d(n_mels)
 init_bn(model.bn0)
 return model 
<normalization>
random_forest = RandomForestClassifier(n_estimators=400)
 y_train = pd.DataFrame(y_train)
 random_forest.fit(X_train, y_train.values.ravel() )
def read_audio_fast(path):
clip, sr_native = librosa.core.audio.__audioread_load(
 path, offset=0.0, duration=None, dtype=np.float32)
 clip = librosa.to_mono(clip)
 if sr_native > 0:
clip = librosa.resample(clip, sr_native, SAMPLE_RATE, res_type='kaiser_fast')
 return clip, SAMPLE_RATE
def random_chunk(y, duration, sr=SAMPLE_RATE):
sample_size = int(duration * sr)
 if y.shape[0] < sample_size:
raise ValueError('Too short clip')
 t = np.random.randint(0, y.shape[0] - sample_size)
 return y[t: t + sample_size]<load_from_csv>
y_pred = random_forest.predict(X_dev)
 print("Accuracy:")
 print(round(random_forest.score(X_dev, y_dev), 3))
print("Precision score:")
 print(round(precision_score(y_dev, y_pred, average='macro'), 3))
print("Recall score:")
 print(round(recall_score(y_dev, y_pred, average='macro'), 3))
print("Global F1 score:")
 print(round(f1_score(y_dev, y_pred, average='macro'), 3))
if os.path.exists('/kaggle/input/birdsong-recognition/test_audio'):
BASE_TEST_DIR = '/kaggle/input/birdsong-recognition'
TEST_AUDIO_DIR = '/kaggle/input/birdsong-recognition/test_audio'
else:
BASE_TEST_DIR = '/kaggle/input/cornellfaketest'
TEST_AUDIO_DIR = '/kaggle/input/cornellfaketest'
print(BASE_TEST_DIR, TEST_AUDIO_DIR)
 df_test = pd.read_csv(f'{BASE_TEST_DIR}/test.csv')
 df_test.shape
df_test.head()
df_test.groupby('site' ).nunique()<remove_duplicates>
test_id = test['Id']
test = test.drop(columns=['Id'] )
audio_site = {a: s for a, s in df_test[['audio_id', 'site']].drop_duplicates().values}<categorify>
test2 = pd.DataFrame(sc_X.transform(test))
test2.columns = test.columns.values
test2.index = test.index.values
test = pd.concat([test2, test_bin],axis=1, sort=False)
 print(test.shape)
 test.head()
def get_model_predictions_for_clip_fast(y, model, device, classes):
duration = y.shape[0] // SAMPLE_RATE
batch = []
start_seconds = []
for start in range(0, duration - CHUNK_DURATION + 1, 5):
end = start + 5
start_seconds.append(start)
 chunk = y[start * SAMPLE_RATE: end * SAMPLE_RATE]
if len(chunk)!= CHUNK_DURATION * SAMPLE_RATE:
print(chunk.shape)
 break
batch.append(chunk)
 batch = np.asarray(batch)
 tensors = torch.from_numpy(batch)
 tensors = tensors.to(device)
 with torch.no_grad() :
preds = model(tensors)['clipwise_output']
test_preds = preds.cpu().numpy()
pred_df = pd.DataFrame(test_preds, columns=classes)
 pred_df['start_second'] = start_seconds
return pred_df<feature_engineering>
test_pred = random_forest.predict(test)
 test_pred = pd.DataFrame(test_pred)
 my_preds = pd.concat([test_id, test_pred],axis=1, sort=False)
 my_preds.columns = ['Id', 'Target']
my_preds.tail()
<init_hyperparams><EOS> 
my_preds.to_csv('190128_g_submission.csv', index=False )