Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<load_from_csv>
tr = pd.read_csv(f"{ROOT}/train.csv")
 tr.drop_duplicates(keep=False, inplace=True, subset=['Patient','Weeks'])
 chunk = pd.read_csv(f"{ROOT}/test.csv")
 print("add infos")
 sub = pd.read_csv(f"{ROOT}/sample_submission.csv")
 sub['Patient'] = sub['Patient_Week'].apply(lambda x:x.split('_')[0])
 sub['Weeks'] = sub['Patient_Week'].apply(lambda x: int(x.split('_')[-1]))
sub = sub[['Patient','Weeks','Confidence','Patient_Week']]
sub = sub.merge(chunk.drop('Weeks', axis=1), on="Patient" )<concatenate>
test5000 = pd.read_csv(".. /input/Kannada-MNIST/test.csv")
 train = pd.read_csv(".. /input/Kannada-MNIST/train.csv")
 print(train.shape)
 print(test5000.shape )
tr['WHERE'] = 'train'
chunk['WHERE'] = 'val'
sub['WHERE'] = 'test'
data = tr.append([chunk, sub] )<count_unique_values>
X_train = train.drop(labels = ["label"],axis = 1)
 X_train = X_train / 255.0
X_train = X_train.values.reshape(-1,28,28,1)
 Y_trainlabel = train["label"]
Y_train = to_categorical(Y_trainlabel, num_classes = 10)
 X_test5000 = test5000.drop(labels = ["id"],axis = 1)
 X_test5000 = X_test5000 / 255.0
X_test5000 = X_test5000.values.reshape(-1,28,28,1)
 x_train = X_train
y_train = Y_train
print(x_train.shape)
 print(y_train.shape)
 print(X_test5000.shape )
print(tr.shape, chunk.shape, sub.shape, data.shape)
 print(tr.Patient.nunique() , chunk.Patient.nunique() , sub.Patient.nunique() ,
data.Patient.nunique())
<feature_engineering>
init = he_normal(seed=82)
 nets = 3
model = [0] *(nets+1)
 for j in range(nets+1):
model[j] = Sequential()
model[j].add(Conv2D(32, kernel_size=3, activation='relu' , kernel_initializer=init, input_shape=(28, 28, 1)))
 model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size=3, activation='relu' , kernel_initializer=init))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size=5, activation='relu', kernel_initializer=init))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(64, kernel_size=5, activation='relu', kernel_initializer=init))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size=7, activation='relu', kernel_initializer=init))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size=7, activation='relu', kernel_initializer=init))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(128, kernel_size=4, activation='relu', kernel_initializer=init))
model[j].add(BatchNormalization())
 model[j].add(Flatten())
 model[j].add(Dropout(0.4))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
 model[0].summary()
data['min_week'] = data['Weeks']
data.loc[data.WHERE=='test','min_week'] = np.nan
data['min_week'] = data.groupby('Patient')['min_week'].transform('min' )<categorify>
for j in range(nets):
loadmodelname = ".. /input/kmnist-trio/weights_N" + str(j)
 model[j].load_weights(loadmodelname)
 savemodelname = "weights_N" + str(j)
 model[j].save_weights(savemodelname)
 print("Saving", loadmodelname, "back to", savemodelname )
base = data.loc[data.Weeks == data.min_week]
base = base[['Patient','FVC']].copy()
base.columns = ['Patient','min_FVC']
base['nb'] = 1
base['nb'] = base.groupby('Patient')['nb'].transform('cumsum')
 base = base[base.nb==1]
base.drop('nb', axis=1, inplace=True )<merge>
datagen = ImageDataGenerator(rotation_range=10, zoom_range = 0.1, width_shift_range=0.1, height_shift_range=0.1)
 annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x, verbose=0)
 nets2train = 0
history = [0] * nets2train
epoks = 35
for j in range(nets2train):
rs = 10 * j + 1
X_train2, X_val2, Y_train2, Y_val2 = train_test_split(x_train, y_train, train_size = 0.95, random_state = rs)
 print(X_train2.shape,Y_train2.shape, X_val2.shape, Y_val2.shape)
 modelfilename = "weights_N" + str(j)
 checkpoint = ModelCheckpoint(modelfilename, monitor='val_acc', verbose=0, save_best_only=False, save_weights_only=False, period=epoks)
 history[j] = model[j].fit_generator(datagen.flow(X_train2,Y_train2, batch_size = 64), epochs = epoks, steps_per_epoch = X_train2.shape[0]//64, validation_data =(X_val2, Y_val2), callbacks=[annealer, checkpoint], verbose=0)
 print("CNN", j,": Training done." )
<data_type_conversions><EOS> 
results5000 = np.zeros(( X_test5000.shape[0], 10))
nets4predict = 3
allthree = False
for j in range(nets4predict):
if allthree or j== 2:
print("CNN",j)
 loadmodelname = "weights_N" + str(j)
 model[j].load_weights(loadmodelname)
 results5000 = results5000 + model[j].predict(X_test5000)
 results5000 = np.argmax(results5000,axis = 1)
 submission = pd.read_csv(".. /input/Kannada-MNIST/sample_submission.csv")
 submission['label'] = results5000
submission.to_csv("submission.csv",index=False)
 print("DONE." )