Kaggle competition: Costa Rican Household Poverty Level Prediction
<SOS> metric: MacroFScore Kaggle data source: costa-rican-household-poverty-level-prediction<load_pretrained>
%matplotlib inline
warnings.filterwarnings("ignore" )
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 def load_model(model_name, model_config):
model = my_cnn14(**model_config)
 model.load_state_dict(torch.load(f'/kaggle/input/cornellmodels/{model_name}', map_location=device))
return model<find_best_params>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
def get_spec_stats(ys, model):
test_means = []
test_stds = []
for y in tqdm(ys):
duration = y.shape[0] // SAMPLE_RATE
batch = []
for start in range(0, duration, 5):
end = start + 5
chunk = y[start * SAMPLE_RATE: end * SAMPLE_RATE]
batch.append(chunk)
 batch = np.array(batch)
 batch = torch.from_numpy(batch)
 tensors = batch.to(device)
 specs = model.spectrogram_extractor(tensors)
 specs = model.logmel_extractor(specs)
 specs = specs.cpu().numpy()
for spec in specs:
test_means.append(spec.mean())
 test_stds.append(spec.std())
 test_mean = np.mean(test_means)
 test_std = np.mean(test_stds)
 print(f'Test stats: {test_mean:.3} +- {test_std:.3}')
 return test_mean, test_std<categorify>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
site12_preds = []
for site in ['site_1', 'site_2']:
test_audios = {}
for audio_id in tqdm(audio_ids_by_site[site]):
path = os.path.join(TEST_AUDIO_DIR, f'{audio_id}.mp3')
 y, _ = read_audio_fast(path)
 test_audios[audio_id] = y
for model_name, model_config in models:
model = load_model(model_name, model_config)
 model = model.to(device)
 _ = model.eval()
if '_C262_' in model_name:
classes = BirdLists.all
elif '_C165_' in model_name:
classes = BirdLists.top_165
elif '_C202_' in model_name:
classes = BirdLists.non_zero
if '_N1_' in model_name:
test_mean, test_std = get_spec_stats(test_audios.values() , model)
 model.dataset_mean = test_mean
model.dataset_std = test_std
for audio_id, y in tqdm(test_audios.items()):
pred_df = get_model_predictions_for_clip_fast(y, model, device, classes)
 pred_df['audio_id'] = audio_id
pred_df['site'] = site
site12_preds.append(pred_df)
 predictions_12 = pd.concat(site12_preds )<define_variables>
train.dependency = train.dependency.replace({"yes": 1, "no": 0} ).astype(np.float64)
 train.edjefa = train.edjefa.replace({"yes": 1, "no": 0} ).astype(np.float64)
 train.edjefe = train.edjefe.replace({"yes": 1, "no": 0} ).astype(np.float64)
 test.dependency = test.dependency.replace({"yes": 1, "no": 0} ).astype(np.float64)
 test.edjefa = test.edjefa.replace({"yes": 1, "no": 0} ).astype(np.float64)
 test.edjefe = test.edjefe.replace({"yes": 1, "no": 0} ).astype(np.float64 )
zero = [
'redcro', 'norcar', 'houwre', 'spotow', 'bewwre', 'carwre', 'swathr', 'comrav', 'warvir', 'whbnut', 'marwre',
'normoc', 'wesmea', 'amered', 'whtspa', 'houfin', 'ruckin', 'eastow', 'tuftit', 'chispa', 'grhowl', 'savspa',
'wlswar', 'buggna', 'brncre', 'aldfly', 'orcwar', 'bkhgro', 'bnhcow', 'houspa', 'yerwar', 'haiwoo', 'dowwoo',
'bawwar', 'foxspa', 'balori', 'rebnut', 'eursta', 'gockin', 'killde', 'comgra', 'barswa', 'osprey', 'mallar3',
'pilwoo', 'moudov', 'easpho', 'comloo', 'treswa', 'easblu', 'rethaw', 'grbher3', 'yelwar', 'greegr', 'ribgul',
'nutwoo', 'rocpig', 'brnthr', 'norpar', 'bulori',
]
common = ['whcspa', 'grycat', 'herthr', 'amerob', 'reevir1', 'comyel', 'norfli', 'amecro']
rare = [
'doccor', 'baleag', 'buffle', 'amekes', 'annhum', 'reshaw', 'coohaw', 'lesgol', 'norsho', 'commer', 'norhar2',
'rinduc', 'amewig', 'blkpho', 'hoomer', 'comgol', 'cowscj1', 'wiltur', 'buwtea', 'rudduc', 'bushti', 'rebmer',
'stejay', 'norpin', 'vigswa', 'gocspa', 'redhea', 'wesblu', 'shshaw', 'horgre', 'calqua', 'perfal', 'labwoo',
'wewpew', 'bkchum', 'truswa', 'lotduc', 'brwhaw', 'pasfly', 'swahaw', 'eargre', 'whfibi', 'tunswa', 'phaino',
'brthum', 'rufgro', 'wooscj2', 'cacwre', 'rebsap', 'whtswi', 'goleag', 'snobun', 'renpha', 'coshum', 'baisan',
'sheowl', 'lewwoo', 'lesnig', 'gcrfin', 'sagspa1', 'chukar', 'lecthr',
]
BIRD_THRESHOLD = 0.33
THRESHOLDS = {c: BIRD_THRESHOLD for c in BirdLists.all}
for c in zero:
THRESHOLDS[c] = 0.6
for c in common:
THRESHOLDS[c] = 0.3
for c in rare:
THRESHOLDS[c] = 0.55
THRESHOLDS<categorify>
train.rez_esc = train.rez_esc.replace({99.0 : 5.0} ).astype(np.float64)
 test.rez_esc = test.rez_esc.replace({99.0 : 5.0} ).astype(np.float64 )
mean_predictions_12 = predictions_12.fillna(0 ).groupby(['site', 'audio_id', 'start_second'] ).mean().reset_index()
mean_predictions_12['birdmax'] = mean_predictions_12[BirdLists.all].max(axis=1)
 mean_predictions_12['seconds'] = mean_predictions_12.start_second + CHUNK_DURATION
mean_predictions_12['row_id'] = mean_predictions_12.site + '_' + mean_predictions_12.audio_id + '_' + mean_predictions_12.seconds.astype(str)
 multi_birds = []
for _, row in mean_predictions_12[BirdLists.all].iterrows() :
guesses = ' '.join([bird for bird, p in row.items() if p >= THRESHOLDS[bird]])
 multi_birds.append(guesses)
 mean_predictions_12['bird_guesses'] = multi_birds
mean_predictions_12.shape
mean_predictions_12.head()<categorify>
ind_bool = ['v18q', 'dis', 'male', 'female', 'estadocivil1', 'estadocivil2', 'estadocivil3',
'estadocivil4', 'estadocivil5', 'estadocivil6', 'estadocivil7',
'parentesco1', 'parentesco2', 'parentesco3', 'parentesco4', 'parentesco5',
'parentesco6', 'parentesco7', 'parentesco8', 'parentesco9', 'parentesco10',
'parentesco11', 'parentesco12', 'instlevel1', 'instlevel2', 'instlevel3',
'instlevel4', 'instlevel5', 'instlevel6', 'instlevel7', 'instlevel8',
'instlevel9', 'mobilephone']
ind_non_bool = ['rez_esc', 'escolari', 'age','SQBescolari','SQBage','agesq']
hh_bool = ['hacdor', 'hacapo', 'v14a', 'refrig', 'paredblolad', 'paredzocalo',
'paredpreb','pisocemento', 'pareddes', 'paredmad',
'paredzinc', 'paredfibras', 'paredother', 'pisomoscer', 'pisoother',
'pisonatur', 'pisonotiene', 'pisomadera',
'techozinc', 'techoentrepiso', 'techocane', 'techootro', 'cielorazo',
'abastaguadentro', 'abastaguafuera', 'abastaguano',
'public', 'planpri', 'noelec', 'coopele', 'sanitario1',
'sanitario2', 'sanitario3', 'sanitario5', 'sanitario6',
'energcocinar1', 'energcocinar2', 'energcocinar3', 'energcocinar4',
'elimbasu1', 'elimbasu2', 'elimbasu3', 'elimbasu4',
'elimbasu5', 'elimbasu6', 'epared1', 'epared2', 'epared3',
'etecho1', 'etecho2', 'etecho3', 'eviv1', 'eviv2', 'eviv3',
'tipovivi1', 'tipovivi2', 'tipovivi3', 'tipovivi4', 'tipovivi5',
'computer', 'television', 'lugar1', 'lugar2', 'lugar3',
'lugar4', 'lugar5', 'lugar6', 'area1', 'area2']
hh_non_bool = ['v2a1', 'v18q1', 'meaneduc', 'SQBovercrowding', 'SQBdependency',
'SQBmeaned', 'overcrowding', 'rooms', 'r4h1', 'r4h2', 'r4h3', 'r4m1',
'r4m2', 'r4m3', 'r4t1', 'r4t2', 'r4t3', 'tamhog', 'tamviv', 'hhsize',
'hogar_nin', 'hogar_adul', 'hogar_mayor', 'hogar_total', 'bedrooms',
'qmobilephone', 'SQBhogar_total', 'SQBedjefe', 'SQBhogar_nin']
hh_cont = [ 'dependency', 'edjefe', 'edjefa']
ids = ['Id', 'idhogar', 'Target']
site3_preds = []
site = 'site_3'
test_audios = {}
for audio_id in tqdm(audio_ids_by_site[site]):
path = os.path.join(TEST_AUDIO_DIR, f'{audio_id}.mp3')
 y, _ = read_audio_fast(path)
 test_audios[audio_id] = y
for model_name, model_config in models[:1]:
model = load_model(model_name, model_config)
 model = model.to(device)
 _ = model.eval()
classes = BirdLists.all
for audio_id, y in tqdm(test_audios.items()):
pred_df = get_model_predictions_for_clip_fast(y, model, device, classes)
 pred_df['audio_id'] = audio_id
pred_df['site'] = site
site3_preds.append(pred_df)
 predictions_3 = pd.concat(site3_preds )<feature_engineering>
test['Target'] = np.nan
data = train.append(test)
 data.info()
predictions_3['birdmax'] = predictions_3[BirdLists.all].max(axis=1)
 predictions_3['bird_guess'] = [BirdLists.all[idx] for idx in predictions_3[BirdLists.all].values.argmax(axis=1)]<groupby>
train.idhogar.nunique() , test.idhogar.nunique() , data.idhogar.nunique()
guesses_3 = predictions_3[predictions_3.birdmax > 0.5].groupby(['audio_id', 'bird_guess'])[['site']].count().reset_index()
guesses_3<merge>
miss_count = data.isnull().sum() > 0
misvalue_counts = data.isnull().sum() [miss_count]
misvalue_percent = misvalue_counts/data.shape[0]*100
misvalue_percent
site_3_birds = []
for audio_id, df in guesses_3.groupby('audio_id'):
site_3_birds.append([audio_id, ' '.join(df.bird_guess.values)])
 site_3_birds = pd.DataFrame(site_3_birds, columns=['audio_id', 'bird_guesses'])
 site_3_birds = site_3_birds.merge(df_test[['audio_id', 'row_id']], how='left', on='audio_id')
 site_3_birds<concatenate>
imputer = SimpleImputer(missing_values=np.nan, strategy='mean' )
guesses = pd.concat([
site12_birds[['row_id', 'bird_guesses']],
site_3_birds[['row_id', 'bird_guesses']]
])
 guesses.shape
guesses.head()
guesses.tail()<load_from_csv>
hh_train = train.loc[train.parentesco1 == 1, ids+hh_bool+hh_non_bool+hh_cont].reset_index()
target = hh_train[['Target']]
hh_train_ids = hh_train[['idhogar']]
hh_train = hh_train.drop(['Id','idhogar','Target','index'], axis=1)
 hh_train_df = pd.DataFrame(imputer.fit_transform(hh_train),columns=list(hh_train.columns))
hh_train['idhogar'] = hh_train_ids
hh_train_df['idhogar'] = hh_train_ids
hh_train['Target'] = target
hh_train_df['Target'] = target
submission = pd.read_csv('/kaggle/input/birdsong-recognition/sample_submission.csv')
 submission.head()
submission.shape
submission = submission.merge(guesses[['row_id', 'bird_guesses']], how='left', on=['row_id'])
 idx =(submission.bird_guesses.str.len() > 1)
 submission.loc[idx, 'birds'] = submission.loc[idx, 'bird_guesses']
submission<save_to_csv>
ind_train = train.loc[ :, ids+ind_bool+ind_non_bool].reset_index()
ind_train_ids = ind_train[['idhogar']]
ind_target = ind_train[['Target']]
ind_train = ind_train.drop(['Id','idhogar','Target','index'], axis=1)
 ind_train_df=pd.DataFrame(imputer.fit_transform(ind_train),columns=list(ind_train.columns))
ind_train['idhogar'] = ind_train_ids
ind_train['Target'] = ind_target
ind_train_df['idhogar'] = ind_train_ids
ind_train_df['Target'] = ind_target
submission[['row_id', 'birds']].to_csv('submission.csv', index=False )<feature_engineering>
mis_hh = test.groupby(by='idhogar' ).parentesco1.agg('sum')==0
mis_idhogar = test.groupby(by='idhogar' ).parentesco1.agg('sum')[mis_hh].index
end = datetime.now()
print(end,(end - nb_start ).seconds )<set_options>
pd.options.display.max_columns = 10
test.loc[test.idhogar.isin(mis_idhogar),:][['Id','idhogar','parentesco1']].sort_values(by='idhogar' )
pd.options.display.max_rows = 500
pd.options.display.max_columns = 500<feature_engineering>
test.loc[test.Id == 'ID_99d27ab2f','parentesco1'] = 1
test.loc[test.Id == 'ID_49d05f9e6','parentesco1'] = 1
test.loc[test.Id == 'ID_b0874f522','parentesco1'] = 1
test.loc[test.Id == 'ID_ceeb5dfe2','parentesco1'] = 1
test.loc[test.Id == 'ID_aa8f26c06','parentesco1'] = 1
test.loc[test.Id == 'ID_e42c1dde2','parentesco1'] = 1
test.loc[test.Id == 'ID_9c12f6ebc','parentesco1'] = 1
test.loc[test.Id == 'ID_26d95edff','parentesco1'] = 1
test.loc[test.Id == 'ID_93fa2f7cc','parentesco1'] = 1
test.loc[test.Id == 'ID_bca8a1dde','parentesco1'] = 1
test.loc[test.Id == 'ID_4036d87e3','parentesco1'] = 1
test.loc[test.Id == 'ID_9f025fde6','parentesco1'] = 1
test.loc[test.Id == 'ID_6094ce990','parentesco1'] = 1
test.loc[test.Id == 'ID_00e8a868f','parentesco1'] = 1
test.loc[test.Id == 'ID_d0beee31f','parentesco1'] = 1
test.loc[test.Id == 'ID_894de66bc','parentesco1'] = 1
test.loc[test.Id == 'ID_aa650fb4a','parentesco1'] = 1
test.loc[test.Id == 'ID_139a474f3','parentesco1'] = 1
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()
yield 
<define_variables>
hh_test = test.loc[test.parentesco1 == 1, ids+hh_bool+hh_non_bool+hh_cont].reset_index()
hh_test_ids = hh_test[['idhogar']]
hh_test = hh_test.drop(['Id','idhogar','Target','index'], axis = 1)
 hh_test_df = pd.DataFrame(imputer.fit_transform(hh_test),columns=list(hh_test.columns))
hh_test_df['idhogar'] = hh_test_ids
hh_test['idhogar'] = hh_test_ids
set_seed(1213 )<define_variables>
ind_test = test.loc[:, ids+ind_bool+ind_non_bool].reset_index()
ind_test_ids = ind_test[['idhogar']]
ind_test = ind_test.drop(['Id','idhogar','Target','index'], axis = 1)
 ind_test_df = pd.DataFrame(imputer.fit_transform(ind_test),columns=list(ind_test.columns))
ind_test['idhogar'] = ind_test_ids
ind_test_df['idhogar'] = ind_test_ids
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>
ind_train_groupobj = ind_train_df.groupby(by='idhogar')
 ind_train_data = pd.DataFrame({'idhogar':ind_train_df.idhogar.unique() } )
train = pd.read_csv(RAW_DATA / "train.csv" )<load_from_csv>
def AddFeatures(feature_df, cols, funcs, groupobj):
for func in funcs:
for col in cols:
group_object = groupobj[col].agg(func ).reset_index()
group_object.rename(index=str, columns={col:col+'_'+func}, inplace=True)
 feature_df = feature_df.merge(group_object, on='idhogar', how='left')
 return feature_df
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>
ind_train_data = AddFeatures(ind_train_data, ind_bool, ['mean','sum'], ind_train_groupobj)
 funcs = ['mean','min','max','median','sum','nunique']
ind_train_data = AddFeatures(ind_train_data, ind_non_bool, funcs, ind_train_groupobj )
sub = pd.read_csv(".. /input/birdsong-recognition/sample_submission.csv")
 sub.to_csv("submission.csv", index=False )<init_hyperparams>
ind_test_groupobj = ind_test_df.groupby(by='idhogar')
 ind_test_data = pd.DataFrame({'idhogar':ind_test_df.idhogar.unique() })
 ind_test_data = AddFeatures(ind_test_data, ind_bool, ['mean','sum'], ind_test_groupobj)
 ind_test_data = AddFeatures(ind_test_data, ind_non_bool, funcs, ind_test_groupobj )
TARGET_SR = 32000
model_config = {
"base_model_name": "resnest50_fast_1s1x64d",
"pretrained": False,
"num_classes": 264,
"trained_weights": ".. /input/bird-seed-v2/bird.pth"
}
melspectrogram_parameters = {
"n_mels": 155,
"fmin": 0,
"fmax": 16000,
"n_fft": 1024,
"hop_length": 256
} 
<define_variables>
train_data = hh_train_df.merge(ind_train_data, on = 'idhogar', how='left')
 test_data = hh_test_df.merge(ind_test_data, on = 'idhogar', how='left' )
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() } 
<normalization>
import gc
import lightgbm as lgb
from sklearn.model_selection import KFold
from sklearn.metrics import f1_score
class Autopool(nn.Module):
def __init__(self, input_size,):
super(Autopool, self ).__init__()
self.alpha = nn.Parameter(requires_grad= True)
 self.alpha.data = torch.ones([input_size], dtype=torch.float32, requires_grad= True, device=device)
 self.sigmoid_layer = nn.Sigmoid()
def forward(self,x):
sigmoid_output = self.sigmoid_layer(x)
 alpa_mult_out = torch.mul(sigmoid_output, self.alpha)
 max_tensor = torch.max(alpa_mult_out,dim = 1)
 max_tensor_unsqueezed = max_tensor.values.unsqueeze(dim=1)
 softmax_numerator = torch.exp(alpa_mult_out.sub(max_tensor_unsqueezed))
softmax_den =torch.sum(softmax_numerator, dim = 1)
 softmax_den = softmax_den.unsqueeze(dim=1)
 weights = softmax_numerator/softmax_den
final_out = torch.sum(torch.mul(sigmoid_output, weights),dim = 1)
 return final_out, sigmoid_output
class Yuvsub(nn.Module):
def __init__(self, args):
super(Yuvsub, self ).__init__()
self.species = nn.Sequential(
 nn.Linear(512, 512), nn.ReLU() , nn.Dropout(p=0.2),
nn.Linear(512, 264))
def forward(self, GAP):
spe = self.species(GAP)
 return spe
class Yuvgru(nn.Module):
def __init__(self, args):
super(Yuvgru, self ).__init__()
self.gru_layer = torch.nn.GRU(input_size = 1024, hidden_size=args["gru_hidden_size"],num_layers = args["gru_layers"],
dropout=0,bidirectional = args["gru_bidirectional"])
 def forward(self,x):
Routput, hn = self.gru_layer(x)
 return Routput, hn
class YuvNet(nn.Module):
def __init__(self, args):
super(YuvNet, self ).__init__()
self.model = models.resnet18(pretrained = False)
 del self.model.fc
self.model.fc = Yuvsub(args)
 self.autopool = Autopool(264)
 def forward(self, x):
batch_size, time_steps, C, H, W = x.size()
c_in = x.view(batch_size * time_steps, C, H, W)
 spe = self.model(c_in)
 spe = spe.view(batch_size, time_steps, -1)
 final_output, sigmoid_output = self.autopool(spe)

return final_output, sigmoid_output 
<choose_model_class>
def model(train_data, test_data, n_folds = 10):
train_ids = train_data[['idhogar']]
test_ids = test_data[['idhogar']]
labels = train_data[['Target']].astype(int)
 train_data = train_data.drop(['idhogar','Target'],axis = 1)
 test_data = test_data.drop(['idhogar'], axis = 1)
 feature_names = list(train_data.columns)
 k_fold = KFold(n_splits = n_folds, shuffle = True, random_state = 2018)
 test_predictions = list()
out_of_fold = np.zeros(train_data.shape[0])
 feature_importance_values = np.zeros(len(feature_names))
Valid_F1 = []
Train_F1 = []
Valid_Score = []
Train_Score = []
for train_indices, valid_indices in k_fold.split(train_data):
train_features = train_data.loc[train_indices, :]
train_labels = labels.loc[train_indices, :]
valid_features = train_data.loc[valid_indices, :]
valid_labels = labels.loc[valid_indices, :]
model = lgb.LGBMClassifier(boosting_type='gbdt',n_estimators=2000,
objective = 'multiclass', class_weight = 'balanced',
learning_rate = 0.03, num_leaves = 31,
reg_alpha = 0.1, reg_lambda = 0.3, num_class = 4,
subsample = 0.8, n_jobs = -1, random_state = 2018)
 model.fit(train_features, train_labels, eval_metric = 'multi_error',
eval_set = [(valid_features, valid_labels),(train_features, train_labels)],
eval_names = ['valid', 'train'], categorical_feature = 'auto',
early_stopping_rounds = 100, verbose = 200)
 best_iteration = model.best_iteration_
test_predictions.append(model.predict(test_data, num_iteration = best_iteration))
feature_importance_values += model.feature_importances_ /n_folds
valid_score = model.best_score_['valid']['multi_error']
train_score = model.best_score_['train']['multi_error']
Valid_Score.append(valid_score)
 Train_Score.append(train_score)
 pred_valid = model.predict(valid_features, num_iteration = best_iteration)
 pred_train = model.predict(train_features, num_iteration = best_iteration)
 valid_f1 = f1_score(valid_labels, pred_valid, average='macro')
 train_f1 = f1_score(train_labels, pred_train, average='macro')
 Valid_F1.append(valid_f1)
 Train_F1.append(train_f1)
 out_of_fold[valid_indices] = pred_valid
gc.enable()
del model, train_features, valid_features
gc.collect()
print('................................................ ')
 feature_importances = pd.DataFrame({'feature': feature_names, 'importance': feature_importance_values})
 Valid_F1.append(f1_score(labels, out_of_fold, average='macro'))
Train_F1.append(np.mean(Train_F1))
Valid_Score.append(np.mean(Valid_Score))
Train_Score.append(np.mean(Train_Score))
fold_names = list(range(n_folds))
fold_names.append('overall')
 metrics = pd.DataFrame({'fold': fold_names,
'train error': Train_Score,
'valid error': Valid_Score,
'train f1' : Train_F1,
'valid f1' : Valid_F1})
 predict_df = pd.DataFrame(np.array(test_predictions ).T)
 voting_result = [predict_df.iloc[x,:].value_counts().argmax() for x in range(predict_df.shape[0])]
submission = test_ids.copy()
submission['Target'] = voting_result
return metrics, feature_importances,submission
device = torch.device("cuda")
 def get_model(args: tp.Dict):
model_1 = YuvNet(args)
 state_dict = torch.load(".. /input/bird-fold1-150-v1/bird.pth")
 model_1.load_state_dict(state_dict)
 model_1.to(device)
 model_1.eval()
model_2 = YuvNet(args)
 state_dict = torch.load(".. /input/bird-label-v1/bird.pth")
 model_2.load_state_dict(state_dict)
 model_2.to(device)
 model_2.eval()
model_3 = YuvNet(args)
 state_dict = torch.load(".. /input/bird-fold-22/bird.pth")
 model_3.load_state_dict(state_dict)
 model_3.to(device)
 model_3.eval()
model_4 = YuvNet(args)
 state_dict = torch.load(".. /input/bird-light/bird.pth")
 model_4.load_state_dict(state_dict)
 model_4.to(device)
 model_4.eval()
return model_1, model_2 , model_3, model_4<split>
metric, feature_importance, submission = model(train_data, test_data, 10 )
model_1, model_2, model_3, model_4 = get_model(model_config )<create_dataframe>
metric
def prediction_for_clip(test_df: pd.DataFrame,
clip: np.ndarray,
model: nn.Module,
mel_params: dict,
threshold=0.5):
dataset = TestDataset(df=test_df,
clip=clip,
img_size=224,
melspectrogram_parameters=mel_params)
 loader = data.DataLoader(dataset, batch_size=1, shuffle=False)
 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 model_1.eval()
model_2.eval()
model_3.eval()
model_4.eval()
prediction_dict = {}
for image, row_id, site in progress_bar(loader):
site = site[0]
row_id = row_id[0]
if site in {"site_1", "site_2"}:
image = image.to(device)
 with torch.no_grad() :
prediction1,_ = model_1(image)
 prediction2,_ = model_2(image)
 prediction3,_ = model_3(image)
 prediction4,_ = model_4(image)
 proba1 = prediction1.detach().cpu().numpy().reshape(-1)
 proba2 = prediction2.detach().cpu().numpy().reshape(-1)
 proba3 = prediction3.detach().cpu().numpy().reshape(-1)
 proba4 = prediction4.detach().cpu().numpy().reshape(-1)
 proba =(proba1 + proba2 + proba3 + proba4)/4.
 events = proba >= threshold
labels = np.argwhere(events ).reshape(-1 ).tolist()
else:
batch_size = 32
image = image.squeeze(0)
 whole_size = image.size(0)
 if whole_size % batch_size == 0:
n_iter = whole_size // batch_size
else:
n_iter = whole_size // batch_size + 1
all_events = set()
for batch_i in range(n_iter):
batch = image[batch_i * batch_size:(batch_i + 1)* batch_size]
batch = batch.unsqueeze(0)
 batch = batch.to(device)

with torch.no_grad() :
prediction1,_ = model_1(batch)
 prediction2,_ = model_2(batch)
 proba1 = prediction1.detach().cpu().numpy().reshape(-1)
 proba2 = prediction2.detach().cpu().numpy().reshape(-1)
 proba =(proba1 + proba2)/2.
 events = proba >= threshold
labels = np.argwhere(events ).reshape(-1 ).tolist()
for label in labels:
all_events.add(label)

labels = list(all_events)
 if len(labels)== 0:
prediction_dict[row_id] = "nocall"
else:
labels_str_list = list(map(lambda x: INV_BIRD_CODE[x], labels))
label_string = " ".join(labels_str_list)
 prediction_dict[row_id] = label_string
return prediction_dict<categorify>
submit = test[['Id','idhogar']]
submit = submit.merge(submission, on = 'idhogar')
 submit = submit.drop(['idhogar'],axis = 1)
 submit.to_csv('submit.csv',index = False )