Kaggle competition: COVID19 Global Forecasting (Week 2)
MAX_EPOCH = 200
tabnet_params = dict(
 n_d = 32,
n_a = 32,
n_steps = 1,
gamma = 1.3,
lambda_sparse = 0,
optimizer_fn = optim.Adam,
optimizer_params = dict(lr = 2e-2, weight_decay = 1e-5),
mask_type = "entmax",
scheduler_params = dict(
 mode = "min", patience = 5, min_lr = 1e-5, factor = 0.9),
scheduler_fn = ReduceLROnPlateau,
seed = seed,
verbose = 10
)<import_modules>
import matplotlib.pyplot as plt
import os
class LogitsLogLoss(Metric):

 def __init__(self):
self._name = "logits_ll"
self._maximize = False
def __call__(self, y_true, y_pred):

 logits = 1 /(1 + np.exp(-y_pred))
aux =(1 - y_true)* np.log(1 - logits + 1e-15)+ y_true * np.log(logits + 1e-15)
 return np.mean(-aux )<prepare_x_and_y>
data = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/train.csv',index_col='Date',parse_dates=True )
scores_auc_all = []
test_cv_preds = []
NB_SPLITS = 10
mskf = MultilabelStratifiedKFold(n_splits = NB_SPLITS, random_state = 0, shuffle = True)
 oof_preds = []
oof_targets = []
scores = []
scores_auc = []
for fold_nb,(train_idx, val_idx)in enumerate(mskf.split(train_df, targets)) :
print(b_,"FOLDS: ", r_, fold_nb + 1)
 print(g_, '*' * 60, c_)
 X_train, y_train = train_df.values[train_idx, :], targets.values[train_idx, :]
X_val, y_val = train_df.values[val_idx, :], targets.values[val_idx, :]
model = TabNetRegressor(**tabnet_params)
 model.fit(
 X_train = X_train,
y_train = y_train,
eval_set = [(X_val, y_val)],
eval_name = ["val"],
eval_metric = ["logits_ll"],
max_epochs = MAX_EPOCH,
patience = 20,
batch_size = 1024,
virtual_batch_size = 32,
num_workers = 1,
drop_last = False,
loss_fn = F.binary_cross_entropy_with_logits 
)
 print(y_, '-' * 60)
 preds_val = model.predict(X_val)
 preds = 1 /(1 + np.exp(-preds_val))
score = np.min(model.history["val_logits_ll"])
 oof_preds.append(preds_val)
 oof_targets.append(y_val)
 scores.append(score)
 preds_test = model.predict(X_test)
 test_cv_preds.append(1 /(1 + np.exp(-preds_test)))
 oof_preds_all = np.concatenate(oof_preds)
 oof_targets_all = np.concatenate(oof_targets)
 test_preds_all = np.stack(test_cv_preds )<compute_test_metric>
data = data.fillna(value='empty' )
aucs = []
for task_id in range(oof_preds_all.shape[1]):
aucs.append(roc_auc_score(y_true = oof_targets_all[:, task_id],
y_score = oof_preds_all[:, task_id] 
))
print(f"{b_}Overall AUC: {r_}{np.mean(aucs)}")
 print(f"{b_}Average CV: {r_}{np.mean(scores)}" )<save_to_csv>
data['state_with_country'] = data['Province_State'] +'_'+ data['Country_Region']
all_feat = [col for col in submission.columns if col not in ["sig_id"]]
test = pd.read_csv(data_path + "test_features.csv")
 sig_id = test[test["cp_type"] != "ctl_vehicle"].sig_id.reset_index(drop = True)
 tmp = pd.DataFrame(test_preds_all.mean(axis = 0), columns = all_feat)
 tmp["sig_id"] = sig_id
submission = pd.merge(test[["sig_id"]], tmp, on = "sig_id", how = "left")
 submission.fillna(0, inplace = True)
 submission.to_csv("submission.csv", index = None)
 submission.head()<set_options>
data= data.drop(labels=['Province_State','Country_Region','Id'],axis=1 )
sys.path.insert(0, ".. /input/resnest/")
 pd.options.display.max_rows = 500
pd.options.display.max_columns = 500
gc.enable()
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
device = torch.device("cuda:0")
 rand_seed = 1120
debug_mode = False 
<define_variables>
state_i_data =[]
for i in state_with_country_name:
state_i_data.append(i )
image_size = 224
n_classes = 264
use_fold = 0
model_files = [ 
("resnest50_fast_2s2x40d", ".. /input/bird-custom-ext-v1-fold0/F1_snapshot_epoch_26.pth"), 
("resnest50_fast_2s2x40d", ".. /input/bird-custom-ext-v1-fold1/F1_snapshot_epoch_30.pth"), 
("resnest50_fast_2s2x40d", ".. /input/bird-custom-ext-v1-fold2/F1_snapshot_epoch_29.pth"), 
("resnest50_fast_2s2x40d", ".. /input/bird-custom-ext-v1-fold3/F1_snapshot_epoch_30.pth"), 
("resnest50_fast_2s2x40d", ".. /input/bird-custom-ext-v1-fold4/snapshot_epoch_25.pth"),
]
public_model_file =("resnest50_fast_1s1x64d", ".. /input/training-birdsong-baseline-resnest50-fast/best_model.pth")
 resnest_200_model_files = [ 
("resnest200", ".. /input/resnest200-v2-fold0/resnest200_v2_epoch_44.pth"), 
("resnest200", ".. /input/resnest200-fold0/resnest200_epoch_50.pth")
 ]
nocall_model_files = [ 
("resnest50_fast_2s2x40d", ".. /input/bird-custom-nocall-v2-fold1/F1_snapshot_epoch_4.pth"), 
("resnest50_fast_2s2x40d", ".. /input/bird-custom-nocall-v2-fold1/F1_snapshot_epoch_10.pth")
 ]
infer_batch_size = 1024
prob_threshold = 0.3
num_workers = 4
TARGET_SR = 32000
melspectrogram_parameters = {"n_mels": 128, "fmin": 20, "fmax": 16000}<define_variables>
for i,j in enumerate(state_with_country_name):
state_i_data[i] = data[data['state_with_country'] == j ]
state_i_data[i] = state_i_data[i].drop(['state_with_country'],axis=1)
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>
prev_sub = pd.read_csv('/kaggle/input/rnn-forecasting-data/rnn/test.csv')
 test_whole = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/test.csv',index_col='Date',parse_dates=True)
 test_whole = test_whole.fillna(value='empty')
train = pd.read_csv(RAW_DATA / "train.csv")
 if not TEST_AUDIO_DIR.exists() :
TEST_AUDIO_DIR = INPUT_ROOT / "birdcall-check" / "test_audio"
test = pd.read_csv(INPUT_ROOT / "birdcall-check" / "test.csv")
 else:
test = pd.read_csv(RAW_DATA / "test.csv")
<load_from_csv>
test_whole['state_with_country'] = test_whole['Province_State'] +'_'+ test_whole['Country_Region']
test_whole.head()
sub = pd.read_csv(".. /input/birdsong-recognition/sample_submission.csv")
 print(sub.shape)
 print(sub.head())
<define_variables>
prev_sub.index = test_whole.index
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, 'nocall': 264
}
INV_BIRD_CODE = {v: k for k, v in BIRD_CODE.items() }<load_pretrained>
prev_sub['state_with_country'] = test_whole['state_with_country']
def init_layer(layer):

 if hasattr(layer, 'weight'):
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 CustomResNeSt(nn.Module):
def __init__(self, pretrained_model, n_classes):
super(CustomResNeSt, self ).__init__()
self.backbone = getattr(resnest_torch, pretrained_model )(pretrained=False)
 self.backbone.fc = nn.Sequential(
 nn.Linear(2048, 1024), nn.ReLU() , nn.Dropout(p=0.2),
nn.Linear(1024, 1024), nn.ReLU() , nn.Dropout(p=0.2),
nn.Linear(1024, n_classes))
self.init_weight()
def init_weight(self):
for layer in self.backbone.fc:
init_layer(layer)
 def forward(self, input):

 if self.training:
out = self.backbone(x)
 else:
out = self.backbone(input)
 return out<load_pretrained>
forecast = []
for i in state_with_country_name:
forecast.append(i )
def get_model(pretrained_model, model_file_path):
model = CustomResNeSt(pretrained_model, n_classes)
 print(f"Loading model states from {model_file_path}...... ")
 model.load_state_dict(torch.load(model_file_path))
device = torch.device("cuda")
 model.to(device)
 model.eval()
print_model_info(model)
 gc.collect()
torch.cuda.empty_cache()
return model<choose_model_class>
for i,j in enumerate(state_with_country_name):
forecast[i] = prev_sub[prev_sub['state_with_country'] == j ]
forecast[i] = forecast[i].iloc[13:21]
forecast[i] = forecast[i].drop(['ForecastId','state_with_country'],axis=1)
 state_i_data[i] = state_i_data[i].append(forecast[i] )
def get_resnest_model(pretrained_model, model_file_path):
model = getattr(resnest_torch, pretrained_model )(pretrained=False)
 del model.fc
model.fc = nn.Sequential(nn.Linear(2048,
1024), nn.ReLU() , nn.Dropout(p=0.2),
nn.Linear(1024, 1024), nn.ReLU() ,
nn.Dropout(p=0.2), nn.Linear(1024, n_classes))
print(f"Loading model states from {model_file_path}...... ")
 model.load_state_dict(torch.load(model_file_path))
device = torch.device("cuda")
 model.to(device)
 model.eval()
print_model_info(model)
 gc.collect()
torch.cuda.empty_cache()
return model<load_pretrained>
test_whole = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/test.csv',index_col='Date',parse_dates=True)
 test_whole = test_whole.fillna(value='empty' )
def get_nocall_resnest_model(pretrained_model, model_file_path):
model = CustomResNeSt(pretrained_model, n_classes+1)
 print(f"Loading model states from {model_file_path}...... ")
 model.load_state_dict(torch.load(model_file_path))
device = torch.device("cuda")
 model.to(device)
 model.eval()
print_model_info(model)
 gc.collect()
torch.cuda.empty_cache()
return model<predict_on_test>
test_whole['state_with_country'] = test_whole['Province_State'] +'_'+ test_whole['Country_Region']
test_whole.head()
def prediction_for_clip(test_df: pd.DataFrame,
clip,
models,
public_model,
resnest_models,
nocall_models,
mel_params: dict,
threshold=0.5):
for m in models:
m.eval()
public_model.eval()
for m in resnest_models:
m.eval()
for m in nocall_models:
m.eval()
prediction_dict = {}
dataset = TestDataset(df=test_df,
clip=clip,
img_size=image_size,
melspectrogram_parameters=mel_params)
 if test_df.site.values[0] in ["site_1", "site_2"]:
loader = data.DataLoader(dataset,
batch_size=infer_batch_size,
shuffle=False,
num_workers=num_workers,
drop_last=False,
pin_memory=True)
 for image, row_id, site in progress_bar(loader):
with torch.no_grad() :
image = image.to(device)
 preds = np.zeros(( image.size(0), n_classes+1))
for m in models:
prediction = F.sigmoid(m(image))
proba = prediction.detach().cpu().numpy()
preds[:, :-1] += proba
preds /= len(models)
 public_preds = np.zeros(( image.size(0), n_classes+1))
prediction = F.sigmoid(public_model(image))
public_proba = prediction.detach().cpu().numpy()
public_preds[:, :-1] = public_proba
resnest_preds = np.zeros(( image.size(0), n_classes+1))
for m in resnest_models:
prediction = F.sigmoid(m(image))
proba = prediction.detach().cpu().numpy()
resnest_preds[:, :-1] += proba
resnest_preds /= len(resnest_models)
 nocall_preds = np.zeros(( image.size(0), n_classes+1))
for m in nocall_models:
prediction = F.sigmoid(m(image))
proba = prediction.detach().cpu().numpy()
nocall_preds += proba
nocall_preds /= len(nocall_models)
 preds =(preds + public_preds + resnest_preds + nocall_preds)/4
events = preds >= threshold
for i in range(len(events)) :
event = events[i, :]
labels = np.argwhere(event ).reshape(-1 ).tolist()
frame_id = row_id[i]
if len(labels)== 0:
prediction_dict[frame_id] = "nocall"
else:
if len(labels)> 1 and 264 in labels:
labels.remove(264)
 labels_str_list = list(map(lambda x: INV_BIRD_CODE[x], labels))
label_string = " ".join(labels_str_list)
 prediction_dict[frame_id] = label_string
else:
loader = data.DataLoader(dataset,
batch_size=1,
shuffle=False,
num_workers=num_workers,
drop_last=False,
pin_memory=True)
 for image, row_id, site in progress_bar(loader):
site = site[0]
row_id = row_id[0]
image = image.squeeze(0)
 batch_size = infer_batch_size
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]
if batch.ndim == 3:
batch = batch.unsqueeze(0)
 with torch.no_grad() :
batch = batch.to(device)
 preds = np.zeros(( batch.size(0), n_classes+1))
for m in models:
prediction = F.sigmoid(m(batch))
proba = prediction.detach().cpu().numpy()
preds[:, :-1] += proba
preds /= len(models)
 public_preds = np.zeros(( batch.size(0), n_classes+1))
prediction = F.sigmoid(public_model(batch))
public_proba = prediction.detach().cpu().numpy()
public_preds[:, :-1] = public_proba
resnest_preds = np.zeros(( batch.size(0), n_classes+1))
for m in resnest_models:
prediction = F.sigmoid(m(batch))
proba = prediction.detach().cpu().numpy()
resnest_preds[:, :-1] += proba
resnest_preds /= len(resnest_models)
 nocall_preds = np.zeros(( batch.size(0), n_classes+1))
for m in nocall_models:
prediction = F.sigmoid(m(batch))
proba = prediction.detach().cpu().numpy()
nocall_preds += proba
nocall_preds /= len(nocall_models)
 preds =(preds + public_preds + resnest_preds + nocall_preds)/4
events = preds >= threshold
for i in range(len(events)) :
event = events[i, :]
labels = np.argwhere(event ).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:
if len(labels)> 1 and 264 in labels:
labels.remove(264)
 labels_str_list = list(map(lambda x: INV_BIRD_CODE[x], labels))
label_string = " ".join(labels_str_list)
 prediction_dict[row_id] = label_string
del loader, dataset
gc.collect()
torch.cuda.empty_cache()
return prediction_dict<choose_model_class>
state_i_data_for_test=[]
for i in state_with_country_name_for_test:
state_i_data_for_test.append(i)
def prediction(test_df: pd.DataFrame,
test_audio: Path,
mel_params: dict,
target_sr: int,
threshold=0.5):
models = []
for m, p in model_files:
models.append(get_model(m, p))
public_model = get_resnest_model(public_model_file[0], public_model_file[1])
 resnest_models = []
for m, p in resnest_200_model_files:
resnest_models.append(get_resnest_model(m, p))
nocall_models = []
for m, p in nocall_model_files:
nocall_models.append(get_nocall_resnest_model(m, p))
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_dict = prediction_for_clip(test_df_for_audio_id,
clip=clip,
models=models,
public_model=public_model,
resnest_models=resnest_models,
nocall_models=nocall_models,
mel_params=mel_params,
threshold=threshold)
 row_id = list(prediction_dict.keys())
 birds = list(prediction_dict.values())
 prediction_df = pd.DataFrame({"row_id": row_id, "birds": birds})
 prediction_dfs.append(prediction_df)
 prediction_df = pd.concat(prediction_dfs, axis=0,
sort=False ).reset_index(drop=True)
 return prediction_df<save_to_csv>
for i,j in enumerate(state_with_country_name_for_test):
state_i_data_for_test[i] = test_whole[test_whole['state_with_country'] == j ]
state_i_data_for_test[i] = state_i_data_for_test[i].iloc[21:]
state_i_data_for_test[i] = state_i_data_for_test[i].drop(['ForecastId','state_with_country','Province_State','Country_Region'],axis=1)
 state_i_data_for_test.append(state_i_data_for_test[i] )
submission = prediction(test_df=test,
test_audio=TEST_AUDIO_DIR,
mel_params=melspectrogram_parameters,
target_sr=TARGET_SR,
threshold=prob_threshold)
 submission.to_csv("submission.csv", index=False )<import_modules>
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,LSTM
from tensorflow.keras.preprocessing.sequence import TimeseriesGenerator
import cv2
import audioread
import logging
import os
import random
import time
import warnings
import librosa
import numpy as np
import pandas as pd
import soundfile as sf
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data as data
from contextlib import contextmanager
from pathlib import Path
from typing import Optional
from fastprogress import progress_bar
from sklearn.metrics import f1_score
from torchvision import models<set_options>
result = []
full_scaler = MinMaxScaler()
def set_seed(seed: int = 42):
random.seed(seed)
 np.random.seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
def get_logger(out_file=None):
logger = logging.getLogger()
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
 logger.handlers = []
logger.setLevel(logging.INFO)
 handler = logging.StreamHandler()
handler.setFormatter(formatter)
 handler.setLevel(logging.INFO)
 logger.addHandler(handler)
 if out_file is not None:
fh = logging.FileHandler(out_file)
 fh.setFormatter(formatter)
 fh.setLevel(logging.INFO)
 logger.addHandler(fh)
 logger.info("logger set up")
 return logger
@contextmanager
def timer(name: str, logger: Optional[logging.Logger] = None):
t0 = time.time()
msg = f"[{name}] start"
if logger is None:
print(msg)
 else:
logger.info(msg)
 yield
msg = f"[{name}] done in {time.time() - t0:.2f} s"
if logger is None:
print(msg)
 else:
logger.info(msg )<set_options>
for i in range(len(state_with_country_name_for_test)) :
scaled_full_data = full_scaler.fit_transform(state_i_data[i])
 length = 1
batch_size = 1
generator = TimeseriesGenerator(scaled_full_data, scaled_full_data, length=length, batch_size=1)
 model = Sequential()
model.add(LSTM(96,input_shape=(length,scaled_full_data.shape[1])))
 model.add(Dense(scaled_full_data.shape[1]))
model.compile(optimizer='adam', loss='mse')
 model.fit_generator(generator,epochs=6)
 n_features = scaled_full_data.shape[1]
test_predictions = []
first_eval_batch = scaled_full_data[-length:]
current_batch = first_eval_batch.reshape(( 1, length, n_features))
for j in range(len(state_i_data_for_test[i])) :
current_pred = model.predict(current_batch)[0]
test_predictions.append(current_pred)
 current_batch = np.append(current_batch[:,1:,:],[[current_pred]],axis=1)
 true_predictions = full_scaler.inverse_transform(test_predictions)
 true_predictions = true_predictions.round()
true_predictions = pd.DataFrame(data=true_predictions,columns=state_i_data[1].columns)
 result.append(true_predictions)
 print('count:-',i)
logger = get_logger("main.log")
 set_seed(1213 )<define_variables>
prediction = pd.DataFrame(data= state_i_data[0].iloc[-21:] ,columns=['ConfirmedCases','Fatalities'])
 prediction = prediction.append(result[0] )
TARGET_SR = 32000
TEST = Path(".. /input/birdsong-recognition/test_audio" ).exists()<load_from_csv>
for i in range(1,len(result)) :
prediction = prediction.append(state_i_data[i].iloc[-21:])
 prediction = prediction.append(result[i] )
if TEST:
DATA_DIR = Path(".. /input/birdsong-recognition/")
 else:
DATA_DIR = Path(".. /input/birdcall-check/")
 test = pd.read_csv(DATA_DIR / "test.csv")
 test_audio = DATA_DIR / "test_audio"
test.head()<save_to_csv>
prediction.index = range(0,len(prediction))
sub = pd.read_csv(".. /input/birdsong-recognition/sample_submission.csv")
 sub.to_csv("submission.csv", index=False )<choose_model_class>
sub_format = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/submission.csv' )
class ResNet(nn.Module):
def __init__(self, base_model_name: str, pretrained=False,
num_classes=264):
super().__init__()
base_model = models.__getattribute__(base_model_name )(
 pretrained=pretrained)
 layers = list(base_model.children())[:-2]
layers.append(nn.AdaptiveMaxPool2d(1))
self.encoder = nn.Sequential(*layers)
 in_features = base_model.fc.in_features
self.classifier = nn.Sequential(
 nn.Linear(in_features, 1024), nn.ReLU() , nn.Dropout(p=0.2),
nn.Linear(1024, 1024), nn.ReLU() , nn.Dropout(p=0.2),
nn.Linear(1024, num_classes))
def forward(self, x):
batch_size = x.size(0)
 x = self.encoder(x ).view(batch_size, -1)
 x = self.classifier(x)
 multiclass_proba = F.softmax(x, dim=1)
 multilabel_proba = F.sigmoid(x)
 return {
"logits": x,
"multiclass_proba": multiclass_proba,
"multilabel_proba": multilabel_proba
}<define_variables>
sub_format = sub_format['ForecastId']
model_config = {
"base_model_name": "resnet50",
"pretrained": False,
"num_classes": 264
}
melspectrogram_parameters = {
"n_mels": 128,
"fmin": 20,
"fmax": 16000
}
weights_path = ".. /input/birdcall-resnet50-init-weights/best.pth"<define_variables>
final = pd.concat([sub_format,prediction],axis=1 )
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() }<choose_model_class>
final.to_csv('submission.csv',index=False )