Kaggle competition: M5 Forecasting - Accuracy
learn.unfreeze()
learn.fit_one_cycle(3,max_lr=slice(1e-5,1e-4))<predict_on_test>
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
from tqdm import tqdm
preds,y=learn.get_preds(ds_type=DatasetType.Test )<prepare_output>
df = pd.read_csv('.. /input/m5-forecasting-accuracy/sales_train_validation.csv')
 price_df = pd.read_csv(".. /input/m5-forecasting-accuracy/sell_prices.csv")
 cal_df = pd.read_csv(".. /input/m5-forecasting-accuracy/calendar.csv" )
preds = np.argmax(preds, axis = 1)
 preds_classes = [data.classes[i] for i in preds]<save_to_csv>
cal_df["d"]=cal_df["d"].apply(lambda x: int(x.split("_")[1]))
price_df["id"] = price_df["item_id"] + "_" + price_df["store_id"] + "_validation"
submission = pd.DataFrame({ 'file': os.listdir('input/test'), 'species': preds_classes })
 submission.to_csv('test_classification_results.csv', index=False )<concatenate>
for day in tqdm(range(1886, 1914)) :
wk_id = list(cal_df[cal_df["d"]==day]["wm_yr_wk"])[0]
wk_price_df = price_df[price_df["wm_yr_wk"]==wk_id]
df = df.merge(wk_price_df[["sell_price", "id"]], on=["id"], how='inner')
 df["unit_sales_" + str(day)] = df["sell_price"] * df["d_" + str(day)]
df.drop(columns=["sell_price"], inplace=True )
def change_fc_data(learn, train_index, val_index):
tmpl = f'_{learn.models.name}_{learn.data_.sz}.bc'
names = [os.path.join(learn.tmp_path, p+tmpl)for p in('x_act', 'x_act_val', 'x_act_test')]
learn.get_activations()
act, val_act, test_act = [bcolz.open(p)for p in names]
data_x = np.vstack([val_act, act])
 data_y = np.array(list(learn.data.val_y)+ list(learn.data.trn_y))
train_x = data_x[train_index]
valid_x = data_x[val_index]
train_y = data_y[train_index]
valid_y = data_y[val_index]
learn.fc_data = ImageClassifierData.from_arrays(learn.data_.path, 
(train_x, train_y), 
(valid_x, valid_y), learn.data_.bs, classes=learn.data_.classes,
test = test_act if learn.data_.test_dl else None, num_workers=8)
 return learn<import_modules>
df["dollar_sales"] = df[[c for c in df.columns if c.find("unit_sales")==0]].sum(axis=1 )
scale = 70
seed = 7<define_variables>
df.drop(columns=[c for c in df.columns if c.find("unit_sales")==0], inplace=True )
path_to_images = '.. /input/plant-seedlings-classification/train/*/*.png'
images = glob(path_to_images)
 trainingset = []
traininglabels = []
num = len(images)
 count = 1
for i in images:
print(str(count)+'/'+str(num),end='\r')
 trainingset.append(cv2.resize(cv2.imread(i),(scale,scale)))
 traininglabels.append(i.split('/')[-2])
 count=count+1
trainingset = np.asarray(trainingset)
 traininglabels = pd.DataFrame(traininglabels )<categorify>
df["weight"] = df["dollar_sales"] / df["dollar_sales"].sum()
labels = preprocessing.LabelEncoder()
labels.fit(traininglabels[0])
 print('Classes: '+str(labels.classes_))
encodedlabels = labels.transform(traininglabels[0])
 clearalllabels = np_utils.to_categorical(encodedlabels)
 classes = clearalllabels.shape[1]
print("Number of classes: " + str(classes))
traininglabels[0].value_counts().plot(kind='pie' )<split>
df.drop(columns=["dollar_sales"], inplace=True )
new_train = new_train/255
x_train,x_test,y_train,y_test = train_test_split(new_train,clearalllabels,test_size=0.1,random_state=seed,stratify=clearalllabels)
 print('Train Shape: {}'.format(x_train.shape))<train_model>
df["weight"] /= 12
generator = ImageDataGenerator(
 rotation_range = 180,
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1,
horizontal_flip = True,
vertical_flip = True 
)
 generator.fit(x_train )<choose_model_class>
for d in range(1914, 1942):
df["d_" + str(d)] = np.nan
np.random.seed(seed)
 model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(5, 5), input_shape=(scale, scale, 3), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=64, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Conv2D(filters=128, kernel_size=(5, 5), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=128, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Conv2D(filters=256, kernel_size=(5, 5), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=256, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 model.summary() 
<choose_model_class>
temp = df.drop(columns = [c for c in df.columns if c.find('d_')==0 and int(c.split('_')[1])< 1100] )
lrr = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.4,
min_lr=0.00001)
 filepath='weights.best_{epoch:02d}-{val_acc:.2f}.h5'
checkpoints = ModelCheckpoint(filepath, monitor='val_acc',
verbose=1, save_best_only=True, mode='max')
 filepath='weights.last_auto4.h5'
checkpoints_full = ModelCheckpoint(filepath, monitor='val_acc',
verbose=1, save_best_only=False, mode='max')
 callbacks_list = [checkpoints, lrr, checkpoints_full]
hist = model.fit_generator(generator.flow(x_train, y_train, batch_size=75),
epochs=20,
validation_data=(x_test, y_test),
steps_per_epoch=x_train.shape[0],
callbacks=callbacks_list 
)
 print(model.evaluate(x_train, y_train))
print(model.evaluate(x_test, y_test))
print("Generating plots...")
 plt.style.use("ggplot")
 plt.figure()
N = 20
plt.plot(np.arange(0, N), hist.history["loss"], label="train_loss")
 plt.plot(np.arange(0, N), hist.history["val_loss"], label="val_loss")
 plt.plot(np.arange(0, N), hist.history["acc"], label="train_acc")
 plt.plot(np.arange(0, N), hist.history["val_acc"], label="val_acc")
 plt.title("Training Loss and Accuracy")
 plt.xlabel("Epoch
plt.ylabel("Loss/Accuracy")
 plt.legend(loc="lower left")
 plt.savefig("plot.png" )<save_to_csv>
df_melted = temp.melt(id_vars=[n for n in temp.columns if n.find("id")!=-1],
value_vars=[n for n in temp.columns if n.find("d_")==0],
var_name = 'day', value_name = 'sales')
 del temp
newtestimages=newtestimages/255
prediction = model.predict(newtestimages)
 pred = np.argmax(prediction,axis=1)
 predStr = labels.classes_[pred]
result = {'file':tests,'species':predStr}
result = pd.DataFrame(result)
 result.to_csv("Prediction.csv",index=False)
 print('Prediction result saved as Prediction.csv' )<define_variables>
df_melted["day"]=df_melted["day"].apply(lambda x: int(x.split("_")[1]))
ScaleTo = 70
seed = 7
path = '.. /input/plant-seedlings-classification/train/*/*.png'
files = glob(path)
 trainImg = []
trainLabel = []
j = 1
num = len(files)
 for img in files:
print(str(j)+ "/" + str(num), end="\r")
 trainImg.append(cv2.resize(cv2.imread(img),(ScaleTo, ScaleTo)))
 trainLabel.append(img.split('/')[-2])
 j += 1
trainImg = np.asarray(trainImg)
 trainLabel = pd.DataFrame(trainLabel )<feature_engineering>
df_melted=df_melted.merge(cal_df.drop(columns=["date", "wm_yr_wk",
"weekday"]), left_on=["day"], right_on=["d"] ).drop(columns=["d"] )
clearTrainImg = clearTrainImg / 255<categorify>
df_melted['event_name_1'].value_counts(dropna=False ).head()
le = preprocessing.LabelEncoder()
le.fit(trainLabel[0])
 print("Classes: " + str(le.classes_))
encodeTrainLabels = le.transform(trainLabel[0])
 clearTrainLabel = np_utils.to_categorical(encodeTrainLabels)
 num_clases = clearTrainLabel.shape[1]
print("Number of classes: " + str(num_clases))
trainLabel[0].value_counts().plot(kind='bar' )<split>
df_melted['event_name_1'].astype('category' ).cat.codes.astype("int8" ).value_counts().head()
trainX, testX, trainY, testY = train_test_split(clearTrainImg, clearTrainLabel,
test_size=0.1, random_state=seed,
stratify = clearTrainLabel )<train_model>
df_melted["event_name_1"]=df_melted["event_name_1"].astype('category' ).cat.codes.astype("int8")
 df_melted["event_name_2"]=df_melted["event_name_2"].astype('category' ).cat.codes.astype("int8")
 df_melted["event_type_1"]=df_melted["event_type_1"].astype('category' ).cat.codes.astype("int8")
 df_melted["event_type_2"]=df_melted["event_type_2"].astype('category' ).cat.codes.astype("int8" )
datagen = ImageDataGenerator(
 rotation_range=180,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
vertical_flip=True 
)
 datagen.fit(trainX )<choose_model_class>
useful_ids = ['item_id', 'dept_id', 'cat_id', 'store_id', 'state_id']
id_encodings = [id_col + '_encoding' for id_col in useful_ids]
for id_col in useful_ids:
if id_col == 'item_id':
df_melted[id_col + '_encoding'] = df_melted[id_col].astype('category' ).cat.codes.astype("int16")
 else:
df_melted[id_col + '_encoding'] = df_melted[id_col].astype('category' ).cat.codes.astype("int8" )
numpy.random.seed(seed)
 model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(5, 5), input_shape=(ScaleTo, ScaleTo, 3), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=64, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Conv2D(filters=128, kernel_size=(5, 5), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=128, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Conv2D(filters=256, kernel_size=(5, 5), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=256, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(num_clases, activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'] )<choose_model_class>
df_melted.drop(columns=['year'] + useful_ids, inplace=True )
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.4,
min_lr=0.00001)
 filepath="drive/DataScience/PlantReco/weights.best_{epoch:02d}-{val_acc:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc',
verbose=1, save_best_only=True, mode='max')
 filepath="drive/DataScience/PlantReco/weights.last_auto4.hdf5"
checkpoint_all = ModelCheckpoint(filepath, monitor='val_acc',
verbose=1, save_best_only=False, mode='max')
 callbacks_list = [checkpoint, learning_rate_reduction, checkpoint_all] 
<train_model>
df_melted['test'] = df_melted[["id","sales"]].groupby("id")["sales"].shift(1 ).fillna(-1 ).astype(np.int16)
 print(list(df_melted[df_melted['day']==1101]['test'])== list(df_melted[df_melted['day']==1100]['sales']))
model.load_weights(".. /input/plantrecomodels/weights.best_17-0.96.hdf5")
 data = np.load(".. /input/plantrecomodels/Data.npz")
 d = dict(zip(( "trainX","testX","trainY", "testY"),(data[k] for k in data)))
 trainX = d['trainX']
testX = d['testX']
trainY = d['trainY']
testY = d['testY']
print(model.evaluate(trainX, trainY))
print(model.evaluate(testX, testY))<feature_engineering>
df_melted.drop(columns=['test'], inplace=True )
clearTestImg = clearTestImg / 255<predict_on_test>
for lag in tqdm([28, 35, 42, 49, 56, 63, 70, 77]):
df_melted["lag_" + str(lag)] = df_melted[["id","sales"]].groupby("id")["sales"].shift(lag ).fillna(-1 ).astype(np.int16 )
pred = model.predict(clearTestImg )<save_to_csv>
df_melted=df_melted[df_melted['lag_77']!=-1]
predNum = np.argmax(pred, axis=1)
 predStr = le.classes_[predNum]
res = {'file': testId, 'species': predStr}
res = pd.DataFrame(res)
 res.to_csv("res.csv", index=False )<install_modules>
assert list(df_melted[df_melted['day']==1528]['lag_28'])== list(df_melted[df_melted['day']==1500]['sales'] )
!pip uninstall torch --y
!pip uninstall torchaudio --y<install_modules>
df_melted=df_melted.merge(cal_df[['d', 'wm_yr_wk']], left_on=['day'], right_on=['d'] ).drop(columns=['d'] )
!pip install.. /input/pytorch-160-with-torchvision-070/torch-1.6.0cu101-cp37-cp37m-linux_x86_64.whl
!pip install.. /input/torchaudio/torchaudio-0.6.0-cp37-cp37m-manylinux1_x86_64.whl
pd.options.display.max_rows = 500
pd.options.display.max_columns = 500<load_from_csv>
df_melted=df_melted.merge(price_df[['id', 'sell_price', 'wm_yr_wk']], on=['id', 'wm_yr_wk'], how='inner' )
args = {
'n_mels': 224,
'hop_length': 384,
'std_sr': 32000,
'threshold':.5,
'model': '.. /input/birdweights/c3_10_42.pt',
}
model = torch.jit.load(args['model'], map_location='cpu' ).cuda()
model.eval() ;
root = os.path.join('.. /input', "birdsong-recognition")
 test_dir = os.path.join(root, 'test_audio')
 print(test_dir)
 if os.path.isdir(test_dir):
print('Running actual submission!')
 test_df = pd.read_csv(os.path.join(root, 'test.csv'))
sub = pd.read_csv(".. /input/birdsong-recognition/sample_submission.csv")
 else:
print('Running debug submission')
 test_dir = os.path.join('.. /input', 'birdcall-check', 'test_audio')
 test_df = pd.read_csv(os.path.join('.. /input', 'birdcall-check', 'test.csv'))
sub = pd.read_csv('.. /input/inference-pytorch-birdcall-resnet-baseline/submission.csv')
 print('Naturally no test dir found')
 sub.to_csv("submission.csv", index=False )<define_variables>
del price_df
del cal_df
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() }<set_options>
import lightgbm as lgb
fmin, fmax = 25, 11000
f1 = MelSpectrogram(sample_rate=args['std_sr'], n_fft=1024, hop_length=args['hop_length'], f_min=fmin, f_max=fmax, n_mels=args['n_mels'] ).cuda()
f2 = MelSpectrogram(sample_rate=args['std_sr'], n_fft=2048, hop_length=args['hop_length'], f_min=fmin, f_max=fmax, n_mels=args['n_mels'] ).cuda()
f3 = MelSpectrogram(sample_rate=args['std_sr'], n_fft=4096, hop_length=args['hop_length'], f_min=fmin, f_max=fmax, n_mels=args['n_mels'] ).cuda()<categorify>
best_params = {
"objective" : "poisson",
"metric" :"rmse",
"force_row_wise" : True,
"learning_rate" : 0.05,
"sub_row" : 0.75,
"bagging_freq" : 1,
"lambda_l2" : 0.1,
"metric": ["rmse"],
'verbosity': 1,
'num_iterations' : 2048,
'num_leaves': 64,
"min_data_in_leaf": 50,
}
class TestDataset(data.Dataset):
def __init__(self, df, spectrogram):
self.df = df
self.spectrogram = spectrogram
def __len__(self):
return len(self.df)
 def normalize(self, spect):
spect = spect.contiguous()
miu = spect.view(spect.size(0), -1 ).mean(-1 ).unsqueeze(-1 ).unsqueeze(-1)
 std = spect.view(spect.size(0), -1 ).std(-1 ).unsqueeze(-1 ).unsqueeze(-1)+ 1e-8
return(spect - miu)/ std / 2
def __getitem__(self, idx):
sample = self.df.loc[idx, :]
site = sample.site
row_id = sample.row_id
if site == "site_3":
y = self.spectrogram
len_y = y.shape[-1]
start = 0
end = args['std_sr'] * 5 // args['hop_length']
y_all = []
while len_y > start:
y_batch = self.normalize(y[:, :, start:end])
 if end >= len_y:
clip_length = args['std_sr'] * 5 // args['hop_length']
y_batch = self.normalize(y[:, :, -clip_length:])
 y_all.append(y_batch)
 break
start = end
end = end + args['std_sr'] * 5 // args['hop_length']
y_all.append(y_batch)
 y_all = torch.stack(y_all)
 return y_all, row_id, site
else:
end_seconds = int(sample.seconds)
 start_seconds = int(end_seconds - 5)
 start_index = args['std_sr'] * start_seconds // args['hop_length']
end_index = args['std_sr'] * end_seconds // args['hop_length']
y = self.spectrogram[:, :, start_index:end_index]
y = self.normalize(y)
 return y, row_id, site
def prediction_for_clip(dataset, threshold=0.5):
loader = data.DataLoader(dataset, batch_size=1, shuffle=False)
 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 model.eval()
prediction_dict = {}
for image, row_id, site in loader:
site = site[0]
row_id = row_id[0]
image = image.to(device ).float()
if site in {"site_1", "site_2"}:
image = image.to(device ).float()
with torch.no_grad() :
prediction = model(image)
 proba = prediction.view(-1 ).sigmoid().cpu().numpy()
events = proba >= threshold
labels = np.argwhere(events ).reshape(-1 ).tolist()
else:
image = image.squeeze(0)
 batch_size = 16
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)
 batch = batch.to(device)
 with torch.no_grad() :
prediction = model(batch)
 proba = prediction.view(batch.size(0), -1 ).sigmoid().cpu().numpy()
events = proba >= 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:
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
unique_audio_id = test_df.audio_id.unique()
warnings.filterwarnings("ignore")
 prediction_dfs = []
for audio_id in progress_bar(unique_audio_id):
signal, sample_rate = torchaudio.load(os.path.join(test_dir, audio_id+'.mp3'))
signal = signal.mean(0 ).cuda()
signal = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=args['std_sr'] )(signal)
 spectrogram = torch.log(( torch.stack([f1(signal), f2(signal), f3(signal)], axis=0)) + 1e-8 ).half().float().cpu()
test_df_for_audio_id = test_df.query(
 f"audio_id == '{audio_id}'" ).reset_index(drop=True)
 dataset = TestDataset(test_df_for_audio_id, spectrogram)
 prediction_dict = prediction_for_clip(dataset=dataset, threshold=args['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)
 prediction_df['birds'].value_counts()<save_to_csv>
del wk_price_df
prediction_df.to_csv('submission.csv', index=False)
 prediction_df<set_options>
X_train = df_melted[df_melted["day"] < 1886].drop(columns=["sales"])
 X_val = df_melted[df_melted["day"].between(1886, 1913)].drop(columns=["sales"])
 X_test = df_melted[df_melted["day"] > 1913].drop(columns=["sales"])
 y_train = df_melted[df_melted["day"] < 1886]["sales"]
y_val = df_melted[df_melted["day"].between(1886, 1913)]["sales"]
pd.options.display.max_rows = 500
pd.options.display.max_columns = 500<feature_engineering>
%who DataFrame
def set_seed(seed: int = 42):
random.seed(seed)
 np.random.seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 @contextmanager
def timer(name: str)-> None:

 t0 = time.time()
print("[{}] start".format(name))
yield
print("[{}] done in {:.0f} s".format(name, time.time() - t0))<define_variables>
del df_melted
set_seed(1213 )<define_variables>
%%time
np.random.seed(777)
 fake_valid_inds = np.random.choice(X_train.index.values, 2_000_000, replace = False)
 train_inds = np.setdiff1d(X_train.index.values, fake_valid_inds)
 train_data = lgb.Dataset(X_train.drop(columns=['id'] ).loc[train_inds] , label = y_train.loc[train_inds],
categorical_feature=id_encodings, free_raw_data=False)
 fake_valid_data = lgb.Dataset(X_train.drop(columns=['id'] ).loc[fake_valid_inds], label = y_train.loc[fake_valid_inds],
categorical_feature=id_encodings,
free_raw_data=False )
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>
%%time
m_lgb = lgb.train(best_params, train_data, valid_sets = [fake_valid_data], verbose_eval=1 )
train = pd.read_csv(RAW_DATA / "train.csv" )<load_from_csv>
for d in range(1886, 1914):
df['F_' + str(d)] = m_lgb.predict(X_val[X_val['day']==d].drop(columns=['id']))
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>
h = 28
n = 1885
def rmsse(ground_truth, forecast, train_series, axis=1):
assert axis == 0 or axis == 1
assert type(ground_truth)== np.ndarray and type(forecast)== np.ndarray and type(train_series)== np.ndarray
if axis == 1:
assert ground_truth.shape[1] > 1 and forecast.shape[1] > 1 and train_series.shape[1] > 1
numerator =(( ground_truth - forecast)**2 ).sum(axis=axis)
 if axis == 1:
denominator = 1/(n-1)*(( train_series[:, 1:] - train_series[:, :-1])** 2 ).sum(axis=axis)
 else:
denominator = 1/(n-1)*(( train_series[1:] - train_series[:-1])** 2 ).sum(axis=axis)
 if(numerator < 0 ).any() :
print('nu')
 elif(denominator < 0 ).any() :
print(denominator[denominator < 0])
 return(1/h * numerator/denominator)** 0.5
sub = pd.read_csv(".. /input/birdsong-recognition/sample_submission.csv")
 sub.to_csv("submission.csv", index=False )<init_hyperparams>
level_groupings = {2: ["state_id"], 3: ["store_id"], 4: ["cat_id"], 5: ["dept_id"],
6: ["state_id", "cat_id"], 7: ["state_id", "dept_id"], 8: ["store_id", "cat_id"], 9: ["store_id", "dept_id"],
10: ["item_id"], 11: ["item_id", "state_id"]}
TARGET_SR = 32000
model_config = {
"base_model_name": "resnest50_fast_1s1x64d",
"pretrained": False,
"num_classes": 264,
"trained_weights": ".. /input/training-birdsong-baseline-resnest50-fast/best_model.pth"
}
melspectrogram_parameters = {
"n_mels": 128,
"fmin": 20,
"fmax": 16000
}<define_variables>
new_agg_df = pd.DataFrame(df[[c for c in df.columns if c.find("d_")== 0 or c.find("F_")== 0]].sum() ).transpose()
new_agg_df["level"] = 1
new_agg_df["weight"] = 1/12
column_order = new_agg_df.columns
for level in level_groupings:
temp_df = df.groupby(by=level_groupings[level] ).sum().reset_index()
temp_df["level"] = level
new_agg_df = new_agg_df.append(temp_df[column_order])
 del temp_df
agg_df = new_agg_df
train_series_cols = [c for c in df.columns if c.find("d_")== 0 and int(c.split('_')[1])< 1886]
ground_truth_cols = [c for c in df.columns if c.find("d_")== 0 and int(c.split('_')[1])in range(1886, 1914)]
forecast_cols = [c for c in df.columns if c.find("F_")== 0]
df["rmsse"] = rmsse(np.array(df[ground_truth_cols]),
np.array(df[forecast_cols]), np.array(df[train_series_cols]))
agg_df["rmsse"] = rmsse(np.array(agg_df[ground_truth_cols]),
np.array(agg_df[forecast_cols]), np.array(agg_df[train_series_cols]))
df["wrmsse"] = df["weight"] * df["rmsse"]
agg_df["wrmsse"] = agg_df["weight"] * agg_df["rmsse"]
print(df["wrmsse"].sum() + agg_df["wrmsse"].sum() )
BIRD_CODE = {
'aldfly': 0, 'ameavo': 1, 'amebit': 2, 'amecro': 3, 'amegfi': 4,
'amekes': 5, 'amepip': 6, 'amered': 7, 'amerob': 8, 'amewig': 9,
'amewoo': 10, 'amtspa': 11, 'annhum': 12, 'astfly': 13, 'baisan': 14,
'baleag': 15, 'balori': 16, 'banswa': 17, 'barswa': 18, 'bawwar': 19,
'belkin1': 20, 'belspa2': 21, 'bewwre': 22, 'bkbcuc': 23, 'bkbmag1': 24,
'bkbwar': 25, 'bkcchi': 26, 'bkchum': 27, 'bkhgro': 28, 'bkpwar': 29,
'bktspa': 30, 'blkpho': 31, 'blugrb1': 32, 'blujay': 33, 'bnhcow': 34,
'boboli': 35, 'bongul': 36, 'brdowl': 37, 'brebla': 38, 'brespa': 39,
'brncre': 40, 'brnthr': 41, 'brthum': 42, 'brwhaw': 43, 'btbwar': 44,
'btnwar': 45, 'btywar': 46, 'buffle': 47, 'buggna': 48, 'buhvir': 49,
'bulori': 50, 'bushti': 51, 'buwtea': 52, 'buwwar': 53, 'cacwre': 54,
'calgul': 55, 'calqua': 56, 'camwar': 57, 'cangoo': 58, 'canwar': 59,
'canwre': 60, 'carwre': 61, 'casfin': 62, 'caster1': 63, 'casvir': 64,
'cedwax': 65, 'chispa': 66, 'chiswi': 67, 'chswar': 68, 'chukar': 69,
'clanut': 70, 'cliswa': 71, 'comgol': 72, 'comgra': 73, 'comloo': 74,
'commer': 75, 'comnig': 76, 'comrav': 77, 'comred': 78, 'comter': 79,
'comyel': 80, 'coohaw': 81, 'coshum': 82, 'cowscj1': 83, 'daejun': 84,
'doccor': 85, 'dowwoo': 86, 'dusfly': 87, 'eargre': 88, 'easblu': 89,
'easkin': 90, 'easmea': 91, 'easpho': 92, 'eastow': 93, 'eawpew': 94,
'eucdov': 95, 'eursta': 96, 'evegro': 97, 'fiespa': 98, 'fiscro': 99,
'foxspa': 100, 'gadwal': 101, 'gcrfin': 102, 'gnttow': 103, 'gnwtea': 104,
'gockin': 105, 'gocspa': 106, 'goleag': 107, 'grbher3': 108, 'grcfly': 109,
'greegr': 110, 'greroa': 111, 'greyel': 112, 'grhowl': 113, 'grnher': 114,
'grtgra': 115, 'grycat': 116, 'gryfly': 117, 'haiwoo': 118, 'hamfly': 119,
'hergul': 120, 'herthr': 121, 'hoomer': 122, 'hoowar': 123, 'horgre': 124,
'horlar': 125, 'houfin': 126, 'houspa': 127, 'houwre': 128, 'indbun': 129,
'juntit1': 130, 'killde': 131, 'labwoo': 132, 'larspa': 133, 'lazbun': 134,
'leabit': 135, 'leafly': 136, 'leasan': 137, 'lecthr': 138, 'lesgol': 139,
'lesnig': 140, 'lesyel': 141, 'lewwoo': 142, 'linspa': 143, 'lobcur': 144,
'lobdow': 145, 'logshr': 146, 'lotduc': 147, 'louwat': 148, 'macwar': 149,
'magwar': 150, 'mallar3': 151, 'marwre': 152, 'merlin': 153, 'moublu': 154,
'mouchi': 155, 'moudov': 156, 'norcar': 157, 'norfli': 158, 'norhar2': 159,
'normoc': 160, 'norpar': 161, 'norpin': 162, 'norsho': 163, 'norwat': 164,
'nrwswa': 165, 'nutwoo': 166, 'olsfly': 167, 'orcwar': 168, 'osprey': 169,
'ovenbi1': 170, 'palwar': 171, 'pasfly': 172, 'pecsan': 173, 'perfal': 174,
'phaino': 175, 'pibgre': 176, 'pilwoo': 177, 'pingro': 178, 'pinjay': 179,
'pinsis': 180, 'pinwar': 181, 'plsvir': 182, 'prawar': 183, 'purfin': 184,
'pygnut': 185, 'rebmer': 186, 'rebnut': 187, 'rebsap': 188, 'rebwoo': 189,
'redcro': 190, 'redhea': 191, 'reevir1': 192, 'renpha': 193, 'reshaw': 194,
'rethaw': 195, 'rewbla': 196, 'ribgul': 197, 'rinduc': 198, 'robgro': 199,
'rocpig': 200, 'rocwre': 201, 'rthhum': 202, 'ruckin': 203, 'rudduc': 204,
'rufgro': 205, 'rufhum': 206, 'rusbla': 207, 'sagspa1': 208, 'sagthr': 209,
'savspa': 210, 'saypho': 211, 'scatan': 212, 'scoori': 213, 'semplo': 214,
'semsan': 215, 'sheowl': 216, 'shshaw': 217, 'snobun': 218, 'snogoo': 219,
'solsan': 220, 'sonspa': 221, 'sora': 222, 'sposan': 223, 'spotow': 224,
'stejay': 225, 'swahaw': 226, 'swaspa': 227, 'swathr': 228, 'treswa': 229,
'truswa': 230, 'tuftit': 231, 'tunswa': 232, 'veery': 233, 'vesspa': 234,
'vigswa': 235, 'warvir': 236, 'wesblu': 237, 'wesgre': 238, 'weskin': 239,
'wesmea': 240, 'wessan': 241, 'westan': 242, 'wewpew': 243, 'whbnut': 244,
'whcspa': 245, 'whfibi': 246, 'whtspa': 247, 'whtswi': 248, 'wilfly': 249,
'wilsni1': 250, 'wiltur': 251, 'winwre3': 252, 'wlswar': 253, 'wooduc': 254,
'wooscj2': 255, 'woothr': 256, 'y00475': 257, 'yebfly': 258, 'yebsap': 259,
'yehbla': 260, 'yelwar': 261, 'yerwar': 262, 'yetvir': 263
}
INV_BIRD_CODE = {v: k for k, v in BIRD_CODE.items() }<define_search_model>
submit_df = df[["id"]]
for i in range(1, 29):
submit_df["F" + str(i)] = m_lgb.predict(X_test[X_test['day']==i+1913].drop(columns=['id']))
class SplAtConv2d(Module):

 def __init__(self, in_channels, channels, kernel_size, stride=(1, 1), padding=(0, 0),
dilation=(1, 1), groups=1, bias=True,
radix=2, reduction_factor=4,
rectify=False, rectify_avg=False, norm_layer=None,
dropblock_prob=0.0, **kwargs):
super(SplAtConv2d, self ).__init__()
padding = _pair(padding)
 self.rectify = rectify and(padding[0] > 0 or padding[1] > 0)
 self.rectify_avg = rectify_avg
inter_channels = max(in_channels*radix//reduction_factor, 32)
 self.radix = radix
self.cardinality = groups
self.channels = channels
self.dropblock_prob = dropblock_prob
if self.rectify:
self.conv = RFConv2d(in_channels, channels*radix, kernel_size, stride, padding, dilation,
groups=groups*radix, bias=bias, average_mode=rectify_avg, **kwargs)
 else:
self.conv = Conv2d(in_channels, channels*radix, kernel_size, stride, padding, dilation,
groups=groups*radix, bias=bias, **kwargs)
 self.use_bn = norm_layer is not None
if self.use_bn:
self.bn0 = norm_layer(channels*radix)
 self.relu = ReLU(inplace=True)
 self.fc1 = Conv2d(channels, inter_channels, 1, groups=self.cardinality)
 if self.use_bn:
self.bn1 = norm_layer(inter_channels)
 self.fc2 = Conv2d(inter_channels, channels*radix, 1, groups=self.cardinality)
 if dropblock_prob > 0.0:
self.dropblock = DropBlock2D(dropblock_prob, 3)
 self.rsoftmax = rSoftMax(radix, groups)
 def forward(self, x):
x = self.conv(x)
 if self.use_bn:
x = self.bn0(x)
 if self.dropblock_prob > 0.0:
x = self.dropblock(x)
 x = self.relu(x)
 batch, rchannel = x.shape[:2]
if self.radix > 1:
if torch.__version__ < '1.5':
splited = torch.split(x, int(rchannel//self.radix), dim=1)
 else:
splited = torch.split(x, rchannel//self.radix, dim=1)
 gap = sum(splited)
 else:
gap = x
gap = F.adaptive_avg_pool2d(gap, 1)
 gap = self.fc1(gap)
 if self.use_bn:
gap = self.bn1(gap)
 gap = self.relu(gap)
 atten = self.fc2(gap)
 atten = self.rsoftmax(atten ).view(batch, -1, 1, 1)
 if self.radix > 1:
if torch.__version__ < '1.5':
attens = torch.split(atten, int(rchannel//self.radix), dim=1)
 else:
attens = torch.split(atten, rchannel//self.radix, dim=1)
 out = sum([att*split for(att, split)in zip(attens, splited)])
 else:
out = atten * x
return out.contiguous()
class rSoftMax(nn.Module):
def __init__(self, radix, cardinality):
super().__init__()
self.radix = radix
self.cardinality = cardinality
def forward(self, x):
batch = x.size(0)
 if self.radix > 1:
x = x.view(batch, self.cardinality, self.radix, -1 ).transpose(1, 2)
 x = F.softmax(x, dim=1)
 x = x.reshape(batch, -1)
 else:
x = torch.sigmoid(x)
 return x<import_modules>
submit_df2 = submit_df.copy()
submit_df2["id"] = submit_df2["id"].apply(lambda x: x.replace('validation',
'evaluation'))
class DropBlock2D(object):
def __init__(self, *args, **kwargs):
raise NotImplementedError
class GlobalAvgPool2d(nn.Module):
def __init__(self):

 super(GlobalAvgPool2d, self ).__init__()
def forward(self, inputs):
return nn.functional.adaptive_avg_pool2d(inputs, 1 ).view(inputs.size(0), -1)
 class Bottleneck(nn.Module):

 expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None,
radix=1, cardinality=1, bottleneck_width=64,
avd=False, avd_first=False, dilation=1, is_first=False,
rectified_conv=False, rectify_avg=False,
norm_layer=None, dropblock_prob=0.0, last_gamma=False):
super(Bottleneck, self ).__init__()
group_width = int(planes *(bottleneck_width / 64.))* cardinality
self.conv1 = nn.Conv2d(inplanes, group_width, kernel_size=1, bias=False)
 self.bn1 = norm_layer(group_width)
 self.dropblock_prob = dropblock_prob
self.radix = radix
self.avd = avd and(stride > 1 or is_first)
 self.avd_first = avd_first
if self.avd:
self.avd_layer = nn.AvgPool2d(3, stride, padding=1)
 stride = 1
if dropblock_prob > 0.0:
self.dropblock1 = DropBlock2D(dropblock_prob, 3)
 if radix == 1:
self.dropblock2 = DropBlock2D(dropblock_prob, 3)
 self.dropblock3 = DropBlock2D(dropblock_prob, 3)
 if radix >= 1:
self.conv2 = SplAtConv2d(
 group_width, group_width, kernel_size=3,
stride=stride, padding=dilation,
dilation=dilation, groups=cardinality, bias=False,
radix=radix, rectify=rectified_conv,
rectify_avg=rectify_avg,
norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 elif rectified_conv:
self.conv2 = RFConv2d(
 group_width, group_width, kernel_size=3, stride=stride,
padding=dilation, dilation=dilation,
groups=cardinality, bias=False,
average_mode=rectify_avg)
 self.bn2 = norm_layer(group_width)
 else:
self.conv2 = nn.Conv2d(
 group_width, group_width, kernel_size=3, stride=stride,
padding=dilation, dilation=dilation,
groups=cardinality, bias=False)
 self.bn2 = norm_layer(group_width)
 self.conv3 = nn.Conv2d(
 group_width, planes * 4, kernel_size=1, bias=False)
 self.bn3 = norm_layer(planes*4)
 if last_gamma:
zeros_(self.bn3.weight)
 self.relu = nn.ReLU(inplace=True)
 self.downsample = downsample
self.dilation = dilation
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
 out = self.bn1(out)
 if self.dropblock_prob > 0.0:
out = self.dropblock1(out)
 out = self.relu(out)
 if self.avd and self.avd_first:
out = self.avd_layer(out)
 out = self.conv2(out)
 if self.radix == 0:
out = self.bn2(out)
 if self.dropblock_prob > 0.0:
out = self.dropblock2(out)
 out = self.relu(out)
 if self.avd and not self.avd_first:
out = self.avd_layer(out)
 out = self.conv3(out)
 out = self.bn3(out)
 if self.dropblock_prob > 0.0:
out = self.dropblock3(out)
 if self.downsample is not None:
residual = self.downsample(x)
 out += residual
out = self.relu(out)
 return out
class ResNet(nn.Module):

 def __init__(self, block, layers, radix=1, groups=1, bottleneck_width=64,
num_classes=1000, dilated=False, dilation=1,
deep_stem=False, stem_width=64, avg_down=False,
rectified_conv=False, rectify_avg=False,
avd=False, avd_first=False,
final_drop=0.0, dropblock_prob=0,
last_gamma=False, norm_layer=nn.BatchNorm2d):
self.cardinality = groups
self.bottleneck_width = bottleneck_width
self.inplanes = stem_width*2 if deep_stem else 64
self.avg_down = avg_down
self.last_gamma = last_gamma
self.radix = radix
self.avd = avd
self.avd_first = avd_first
super(ResNet, self ).__init__()
self.rectified_conv = rectified_conv
self.rectify_avg = rectify_avg
if rectified_conv:
conv_layer = RFConv2d
else:
conv_layer = nn.Conv2d
conv_kwargs = {'average_mode': rectify_avg} if rectified_conv else {}
if deep_stem:
self.conv1 = nn.Sequential(
 conv_layer(3, stem_width, kernel_size=3, stride=2, padding=1, bias=False, **conv_kwargs),
norm_layer(stem_width),
nn.ReLU(inplace=True),
conv_layer(stem_width, stem_width, kernel_size=3, stride=1, padding=1, bias=False, **conv_kwargs),
norm_layer(stem_width),
nn.ReLU(inplace=True),
conv_layer(stem_width, stem_width*2, kernel_size=3, stride=1, padding=1, bias=False, **conv_kwargs), 
)
 else:
self.conv1 = conv_layer(3, 64, kernel_size=7, stride=2, padding=3,
bias=False, **conv_kwargs)
 self.bn1 = norm_layer(self.inplanes)
 self.relu = nn.ReLU(inplace=True)
 self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
 self.layer1 = self._make_layer(block, 64, layers[0], norm_layer=norm_layer, is_first=False)
 self.layer2 = self._make_layer(block, 128, layers[1], stride=2, norm_layer=norm_layer)
 if dilated or dilation == 4:
self.layer3 = self._make_layer(block, 256, layers[2], stride=1,
dilation=2, norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 self.layer4 = self._make_layer(block, 512, layers[3], stride=1,
dilation=4, norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 elif dilation==2:
self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
dilation=1, norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 self.layer4 = self._make_layer(block, 512, layers[3], stride=1,
dilation=2, norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 else:
self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 self.avgpool = GlobalAvgPool2d()
self.drop = nn.Dropout(final_drop)if final_drop > 0.0 else None
self.fc = nn.Linear(512 * block.expansion, num_classes)
 for m in self.modules() :
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2./ n))
elif isinstance(m, norm_layer):
m.weight.data.fill_(1)
 m.bias.data.zero_()
def _make_layer(self, block, planes, blocks, stride=1, dilation=1, norm_layer=None,
dropblock_prob=0.0, is_first=True):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
down_layers = []
if self.avg_down:
if dilation == 1:
down_layers.append(nn.AvgPool2d(kernel_size=stride, stride=stride,
ceil_mode=True, count_include_pad=False))
else:
down_layers.append(nn.AvgPool2d(kernel_size=1, stride=1,
ceil_mode=True, count_include_pad=False))
down_layers.append(nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=1, bias=False))
else:
down_layers.append(nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False))
down_layers.append(norm_layer(planes * block.expansion))
downsample = nn.Sequential(*down_layers)
 layers = []
if dilation == 1 or dilation == 2:
layers.append(block(self.inplanes, planes, stride, downsample=downsample,
radix=self.radix, cardinality=self.cardinality,
bottleneck_width=self.bottleneck_width,
avd=self.avd, avd_first=self.avd_first,
dilation=1, is_first=is_first, rectified_conv=self.rectified_conv,
rectify_avg=self.rectify_avg,
norm_layer=norm_layer, dropblock_prob=dropblock_prob,
last_gamma=self.last_gamma))
elif dilation == 4:
layers.append(block(self.inplanes, planes, stride, downsample=downsample,
radix=self.radix, cardinality=self.cardinality,
bottleneck_width=self.bottleneck_width,
avd=self.avd, avd_first=self.avd_first,
dilation=2, is_first=is_first, rectified_conv=self.rectified_conv,
rectify_avg=self.rectify_avg,
norm_layer=norm_layer, dropblock_prob=dropblock_prob,
last_gamma=self.last_gamma))
else:
raise RuntimeError("=> unknown dilation size: {}".format(dilation))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes,
radix=self.radix, cardinality=self.cardinality,
bottleneck_width=self.bottleneck_width,
avd=self.avd, avd_first=self.avd_first,
dilation=dilation, rectified_conv=self.rectified_conv,
rectify_avg=self.rectify_avg,
norm_layer=norm_layer, dropblock_prob=dropblock_prob,
last_gamma=self.last_gamma))
return nn.Sequential(*layers)
 def forward(self, x):
x = self.conv1(x)
 x = self.bn1(x)
 x = self.relu(x)
 x = self.maxpool(x)
 x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)
 x = self.avgpool(x)
 x = torch.flatten(x, 1)
 if self.drop:
x = self.drop(x)
 x = self.fc(x)
 return x<choose_model_class>
submit_df = submit_df.append(submit_df2 ).reset_index(drop=True )
<create_dataframe><EOS> 
submit_df.to_csv("submission.csv", index=False )