Kaggle competition: Kannada MNIST
infold_pred,outfold_pred = np.zeros(train.shape[0]),np.zeros(train.shape[0])
 test_pred = np.zeros(test.shape[0])
 for dev_index, val_index in skf.split(train.text.values,train_labels):
print("TRAIN:", dev_index, "TEST:", val_index)
 dev = train.text.values[dev_index]
dev_dv = train_labels[dev_index]
val = train.text.values[val_index]
val_dv = train_labels[val_index]
dev_input = bert_encode(dev, tokenizer, max_len=max_len)
 val_input = bert_encode(val, tokenizer, max_len=max_len)
 test_input = bert_encode(test.text.values, tokenizer, max_len=max_len)
 model = build_model(bert_layer, max_len=max_len)
 model.summary()
checkpoint = ModelCheckpoint('{}.h5'.format(model_name), monitor='val_loss', save_best_only=True)
 dev_history = model.fit(
 dev_input, dev_dv,
validation_split=0.2,
epochs=3,
callbacks=[checkpoint],
batch_size=16 
)
 model.load_weights('{}.h5'.format(model_name))
infold_pred[dev_index] += model.predict(dev_input)[:,0]
outfold_pred[val_index] += model.predict(val_input)[:,0]
test_pred += model.predict(test_input)[:,0]
os.remove('{}.h5'.format(model_name))
gc.collect()<prepare_output>
train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')
 sub = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')
 print('Our train set have {} rows and {} columns'.format(train.shape[0], train.shape[1]))
print('Our test set have {} rows and {} columns'.format(test.shape[0], test.shape[1]))
infold_pred = infold_pred/4
test_pred = test_pred/5<import_modules>
train.isnull().sum().sum()
import pickle<save_to_csv>
def preprocessing(train, test):
IMG_SIZE = 28
img_train = train.drop(['label'], axis = 1 ).values.reshape(-1, IMG_SIZE, IMG_SIZE, 1 ).astype('float32')
 img_test = test.drop(['id'], axis = 1 ).values.reshape(-1, IMG_SIZE, IMG_SIZE, 1 ).astype('float32')
 img_y = train['label'].values
img_train /= 255
img_test /= 255
x_train, x_val, y_train, y_val = train_test_split(img_train, img_y, test_size = 0.20)
 print('Our transformed train set have the following dimension: ', x_train.shape)
 print('Our transformed valid set have the following dimension: ', x_val.shape)
 return img_test, x_train, x_val, y_train, y_val
with open('predictions.pkl','wb')as f:
pickle.dump({'infold_pred':infold_pred,'outfold_pred':outfold_pred,'test_pred':test_pred},f )<compute_test_metric>
n_classes = train['label'].value_counts().count()
def build_model(input_shape=(28, 28, 1), classes = n_classes):
input_layer = Input(shape=input_shape)
 x = Conv2D(16,(3,3), strides=1, padding="same", name="conv1" )(input_layer)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform", name="batch1" )(x)
 x = Activation('relu',name='relu1' )(x)
 x = Dropout(0.1 )(x)
 x = Conv2D(32,(3,3), strides=1, padding="same", name="conv2" )(x)
 x = BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch2" )(x)
 x = Activation('relu',name='relu2' )(x)
 x = Dropout(0.15 )(x)
 x = MaxPooling2D(pool_size=2, strides=2, padding="same", name="max2" )(x)
 x = Conv2D(64,(5,5), strides=1, padding ="same", name="conv3" )(x)
 x = BatchNormalization(momentum=0.17, epsilon=1e-5, gamma_initializer="uniform", name="batch3" )(x)
 x = Activation('relu', name="relu3" )(x)
 x = MaxPooling2D(pool_size=2, strides=2, padding="same", name="max3" )(x)
 x = Conv2D(128,(5,5), strides=1, padding="same", name="conv4" )(x)
 x = BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch4" )(x)
 x = Activation('relu', name="relu4" )(x)
 x = Dropout(0.17 )(x)
 x = Conv2D(64,(3,3), strides=1, padding="same", name="conv5" )(x)
 x = BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch5" )(x)
 x = Activation('relu', name='relu5' )(x)
 x = Dropout(0.2 )(x)
 x = Conv2D(32,(3,3), strides=1, padding="same", name="conv6" )(x)
 x = BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch6" )(x)
 x = Activation('relu', name="relu6" )(x)
 x = Dropout(0.05 )(x)
 x = Flatten()(x)
 x = Dense(50, name="Dense1" )(x)
 x = Activation('relu', name='relu7' )(x)
 x = Dropout(0.05 )(x)
 x = Dense(25, name="Dense2" )(x)
 x = Activation('relu', name='relu8' )(x)
 x = Dropout(0.03 )(x)
 x = Dense(classes, name="Dense3" )(x)
 x = Activation('softmax' )(x)
 model = Model(inputs=input_layer, outputs=x)
 return model
print('Train score:{}, test score:{}'.format(f1_score(train_labels,infold_pred.round().astype(int)) ,
f1_score(train_labels,outfold_pred.round().astype(int))))<save_to_csv>
def trng_lr_ck_opt(modelname):
train_generator = ImageDataGenerator(rotation_range = 8,
zoom_range = 0.28,
width_shift_range = 0.25,
height_shift_range = 0.25)
 learning_rate_reduction = ReduceLROnPlateau(monitor = 'val_accuracy', patience = 5, verbose = 1, factor = 0.5, min_le = 0.000001)
 checkpoint = ModelCheckpoint(modelname+'.hdf5', monitor = 'val_accuracy', verbose = 1, save_best_only = True)
 return train_generator, learning_rate_reduction, checkpoint
def compile_model() :
optimizer = Adam(lr = 0.001)
 model = build_model(input_shape =(28, 28, 1), classes = n_classes)
 model.compile(loss = 'sparse_categorical_crossentropy', optimizer = optimizer, metrics = ['accuracy'])
 return model
def train_and_evaluate(train, test, batch_size, epochs):
img_test, x_train, x_val, y_train, y_val = preprocessing(train, test)
 model = compile_model()
train_generator, learning_rate_reduction, checkpoint = trng_lr_ck_opt('bestmodel')
 history = model.fit_generator(train_generator.flow(x_train, y_train, batch_size = batch_size),
steps_per_epoch = x_train.shape[0] // batch_size,
epochs = epochs,
validation_data =(x_val, y_val),
callbacks = [checkpoint, learning_rate_reduction])
 return img_test, history
BATCH_SIZE = 64
EPOCHS = 40
img_test, history = train_and_evaluate(train, test, BATCH_SIZE, EPOCHS )
submission['target'] = test_pred.round().astype(int)
 submission.to_csv('{}.csv'.format(model_name), index=False )<define_variables>
def train_and_evaluate_kfold(train, test, batch_size, epochs):
NFOLDS = 5
folds = KFold(n_splits=NFOLDS)
 IMG_SIZE = 28
img_test = test.drop(['id'], axis = 1 ).values.reshape(-1, IMG_SIZE, IMG_SIZE, 1 ).astype('float32')/ 255
img_y = train['label'].values
train = train.drop(['label'], axis = 1)
 oof = np.zeros([train.shape[0], n_classes])
 preds = np.zeros([test.shape[0], n_classes])
 for fold_n,(train_index, val_index)in enumerate(folds.split(train)) :
print('Training fold {}'.format(fold_n + 1))
x_train, x_val = train.iloc[train_index], train.iloc[val_index]
y_train, y_val = img_y[train_index], img_y[val_index]
x_train = x_train.values.reshape(-1, IMG_SIZE, IMG_SIZE, 1 ).astype('float32')/ 255
x_val = x_val.values.reshape(-1, IMG_SIZE, IMG_SIZE, 1 ).astype('float32')/ 255
model = compile_model()
train_generator, learning_rate_reduction, checkpoint = trng_lr_ck_opt('model_fold_{}'.format(fold_n + 1))
history = model.fit_generator(train_generator.flow(x_train, y_train, batch_size = batch_size),
steps_per_epoch = x_train.shape[0] // batch_size,
epochs = epochs,
validation_data =(x_val, y_val),
callbacks = [checkpoint, learning_rate_reduction])
 model.load_weights('model_fold_{}'.format(fold_n + 1)+ '.hdf5')
 preds += model.predict(img_test)/ NFOLDS
oof[val_index] = model.predict(x_val)
 oof = np.argmax(oof, axis = 1)
 preds = np.argmax(preds, axis = 1)
 print('-'*20)
 print('Out of fold cv score: ', metrics.accuracy_score(img_y, oof))
return preds, oof
preds, oof = train_and_evaluate_kfold(train, test, BATCH_SIZE, EPOCHS )
<define_variables><EOS> 
sub['label'] = preds
sub.to_csv('cnn_5kfold_baseline.csv', index = False )