Kaggle competition: Kannada MNIST
train_df, val_df = train_test_split(train_df, test_size=0.1, random_state = 1001 )<define_variables>
IMG_SIZE = 28
N_CHANNELS = 1
max_features = 95000
max_len = 72
embed_size = 300<feature_engineering>
train_df = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test_df = pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv')
 pred_df = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv' )
%%time
embeddings_index = {}
f = open('.. /input/embeddings/glove.840B.300d/glove.840B.300d.txt')
 for line in tqdm(f):
values = line.split(" ")
 word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
 embeddings_index[word] = coefs
f.close()
gc.collect()
print('Found %s word vectors.' % len(embeddings_index))<categorify>
train_df = train_df.append(test_df )
def text_to_array(text, zeros = 300, split_val = max_len):
empyt_emb = np.zeros(zeros)
 text = text[:-1].split() [:split_val]
embeds = [embeddings_index.get(x, empyt_emb)for x in text]
embeds+= [empyt_emb] *(split_val - len(embeds))
return np.array(embeds )<prepare_x_and_y>
MNIST like datatset for Kannada handwritten digits
print(f'Training set: {train_df.shape}')
 print(f'To be Predicted: {pred_df.shape}' )
val_vects = np.array([text_to_array(X_text)for X_text in tqdm(val_df["question_text"][:5000])])
 val_y = np.array(val_df["target"][:5000] )<define_variables>
X_train = train_df.drop(['label'], axis = 1)
 Y_train = train_df['label']
X_pred = pred_df.drop(['id'], axis = 1 )
batch_size = 256
def batch_gen(train_df):
n_batches = math.ceil(len(train_df)/ batch_size)
 while True:
train_df = train_df.sample(frac=1.)
for i in range(n_batches):
texts = train_df.iloc[i*batch_size:(i+1)*batch_size, 1]
text_arr = np.array([text_to_array(text)for text in texts])
 yield text_arr, np.array(train_df["target"][i*batch_size:(i+1)*batch_size])
<import_modules>
X_train, X_test, Y_train, Y_test = train_test_split(X_train, Y_train, test_size=0.05 )
from keras.models import Sequential,Model
from keras.layers import CuDNNLSTM, Dense, Bidirectional, Input,Dropout, CuDNNGRU
from keras.engine.topology import Layer
from keras import initializers, regularizers, constraints
from keras.callbacks import Callback, EarlyStopping, ModelCheckpoint
from sklearn.metrics import f1_score
from keras.layers.normalization import BatchNormalization
from keras import backend as K
from keras.engine.topology import Layer, InputSpec<set_options>
X_train, X_test, X_pred = X_train.apply(lambda x: x/255), X_test.apply(lambda x: x/255), X_pred.apply(lambda x: x/255 )
class Attention(Layer):
def __init__(self, step_dim,
W_regularizer=None, b_regularizer=None,
W_constraint=None, b_constraint=None,
bias=True, **kwargs):
self.supports_masking = True
self.init = initializers.get('normal')
 self.W_regularizer = regularizers.get(W_regularizer)
 self.b_regularizer = regularizers.get(b_regularizer)
 self.W_constraint = constraints.get(W_constraint)
 self.b_constraint = constraints.get(b_constraint)
 self.bias = bias
self.step_dim = step_dim
self.features_dim = 0
super(Attention, self ).__init__(**kwargs)
 def build(self, input_shape):
assert len(input_shape)== 3
self.W = self.add_weight(( input_shape[-1],),
initializer=self.init,
name='{}_W'.format(self.name),
regularizer=self.W_regularizer,
constraint=self.W_constraint)
 self.features_dim = input_shape[-1]
if self.bias:
self.b = self.add_weight(( input_shape[1],),
initializer='zero',
name='{}_b'.format(self.name),
regularizer=self.b_regularizer,
constraint=self.b_constraint)
 else:
self.b = None
self.built = True
def compute_mask(self, input, input_mask=None):
return None
def call(self, x, mask=None):
features_dim = self.features_dim
step_dim = self.step_dim
eij = K.reshape(K.dot(K.reshape(x,(-1, features_dim)) ,
K.reshape(self.W,(features_dim, 1))),(-1, step_dim))
if self.bias:
eij += self.b
eij = K.tanh(eij)
 a = K.exp(eij)
 if mask is not None:
a *= K.cast(mask, K.floatx())
 a /= K.cast(K.sum(a, axis=1, keepdims=True)+ K.epsilon() , K.floatx())
 a = K.expand_dims(a)
 weighted_input = x * a
return K.sum(weighted_input, axis=1)
 def compute_output_shape(self, input_shape):
return input_shape[0], self.features_dim<predict_on_test>
Y_train, Y_test = pd.get_dummies(Y_train), pd.get_dummies(Y_test)
class F1Evaluation(Callback):
def __init__(self, validation_data=() , interval=1):
super(Callback, self ).__init__()
self.interval = interval
self.X_val, self.y_val = validation_data
def on_epoch_end(self, epoch, logs={}):
if epoch % self.interval == 0:
y_pred = self.model.predict(self.X_val, verbose=0)
 y_pred =(y_pred > 0.5 ).astype(int)
 score = f1_score(self.y_val, y_pred)
 print("
F1 - Epoch: %d - Score: %.6f
" %(epoch+1, score))<choose_model_class>
X_train = X_train.values.reshape(-1, IMG_SIZE, IMG_SIZE, N_CHANNELS)
 X_test = X_test.values.reshape(-1, IMG_SIZE, IMG_SIZE, N_CHANNELS )
esr = EarlyStopping(verbose=2, patience=3 )<compute_test_metric>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=8,
zoom_range = 0.15,
width_shift_range=0.15,
height_shift_range=0.15,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )
f1 = F1Evaluation(validation_data=(val_vects, val_y), interval=1 )<choose_model_class>
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3), padding='SAME', activation='relu', input_shape=(28, 28, 1)))
 model.add(Conv2D(filters=32, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(BatchNormalization(momentum=0.15))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Conv2D(filters=32, kernel_size=(5, 5), padding='SAME', activation='relu'))
model.add(Dropout(rate=0.3))
model.add(Conv2D(filters=64, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(Conv2D(filters=64, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(BatchNormalization(momentum=0.15))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Conv2D(filters=64, kernel_size=(5, 5), padding='SAME', activation='relu'))
model.add(Dropout(rate=0.3))
model.add(Flatten())
 model.add(Dense(128, activation = "relu"))
model.add(Dropout(0.40))
model.add(Dense(64, activation = "relu"))
model.add(Dropout(0.40))
model.add(Dense(10, activation = "softmax"))
inp = Input(shape=(max_len,300))
x = Bidirectional(CuDNNGRU(192, return_sequences=True))(inp)
 x = Bidirectional(CuDNNGRU(64,return_sequences=True))(x)
 x = Attention(max_len )(x)
 x = Dense(32, activation="relu" )(x)
 x = Dropout(0.25 )(x)
 x = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )<choose_model_class>
model.compile(optimizer="adam", loss=['categorical_crossentropy'], metrics=['accuracy'] )
model_name = 'gru_model'
print(model_name )<set_options>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
gc.collect()<train_model>
batch_size=32
epochs = 45
np.random.seed(2018)
 mg = batch_gen(train_df)
 hist = model.fit_generator(mg, epochs=30,
steps_per_epoch=512,
validation_data=(val_vects, val_y), callbacks=[f1, esr, model_checkpoint],
verbose=2 )<load_pretrained>
history = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_test,Y_test),
steps_per_epoch=X_train.shape[0] // batch_size,
callbacks=[learning_rate_reduction] )
model.load_weights(bst_model_path )<import_modules>
preds = model.predict(X_pred.values.reshape(-1, IMG_SIZE, IMG_SIZE, N_CHANNELS))
import matplotlib.pyplot as plt<set_options>
pred_df['label'] = np.argmax(preds, axis=1)
 preds = pred_df[['id', 'label']]
preds.to_csv('sub.csv', index=False )