Kaggle competition: Digit Recognizer
<compute_test_metric><EOS> 
decay=1e-4
xtrain = []
ytrain = []
xtest = []
xval = []
yval = []
for dirname, _, filenames in os.walk('/kaggle/input/digit-recognizer/'):
for filename in filenames:
print(os.path.join(dirname, filename))
train =pd.read_csv(os.path.join(dirname,'train.csv'))
test =pd.read_csv(os.path.join(dirname,'test.csv'))
sample_submission =pd.read_csv(os.path.join(dirname,'sample_submission.csv'))
val = train[35000:]
train = train[:35000]
xtrain=train.drop('label',axis=1)
 ytrain=train.label
xtest = test
xval=val.drop('label',axis=1)
 yval=val.label
xtrain=xtrain.values
xtest=xtest.values
xval=xval.values
ytrain=ytrain.values
yval=yval.values
xtrain=xtrain.reshape(-1,28,28,1 ).astype('float32')
 xtest=xtest.reshape(-1,28,28,1 ).astype('float32')
 xval=xval.reshape(-1,28,28,1 ).astype('float32')
 xtrain=(xtrain-np.mean(xtrain)) /255
xtest=(xtest-np.mean(xtest)) /255
xval=(xval-np.mean(xval)) /255
model=Sequential()
model.add(layers.Conv2D(64,(3,3), padding='same', input_shape=(28, 28, 1)))
 model.add(layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(layers.LeakyReLU(alpha=0.1))
model.add(layers.Conv2D(64,(3,3), padding='same'))
model.add(layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(layers.LeakyReLU(alpha=0.1))
model.add(layers.Conv2D(64,(3,3), padding='same'))
model.add(layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(layers.LeakyReLU(alpha=0.1))
model.add(layers.MaxPooling2D(2, 2))
model.add(layers.Dropout(0.2))
model.add(layers.Conv2D(128,(3,3), padding='same'))
model.add(layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(layers.LeakyReLU(alpha=0.1))
model.add(layers.Conv2D(128,(3,3), padding='same'))
model.add(layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(layers.LeakyReLU(alpha=0.1))
model.add(layers.Conv2D(128,(3,3), padding='same'))
model.add(layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(layers.LeakyReLU(alpha=0.1))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Dropout(0.2))
model.add(layers.Conv2D(256,(3,3), padding='same'))
model.add(layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(layers.LeakyReLU(alpha=0.1))
model.add(layers.Conv2D(256,(3,3), padding='same'))
model.add(layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(layers.LeakyReLU(alpha=0.1))
model.add(layers.MaxPooling2D(2,2))
model.add(layers.Dropout(0.2))
model.add(layers.Flatten())
 model.add(layers.Dense(256))
model.add(layers.LeakyReLU(alpha=0.1))
model.add(layers.BatchNormalization())
 model.add(layers.Dense(256))
model.add(layers.LeakyReLU(alpha=0.1))
model.add(layers.BatchNormalization())
 model.add(layers.Dense(10, activation='softmax'))
adam_opt = Adam(lr=0.0002, beta_1=0.9, beta_2=0.999, decay=0.0, amsgrad=False)
 optimizer = RMSprop(learning_rate=0.002,rho=0.9)
 model.compile(optimizer=optimizer,loss='sparse_categorical_crossentropy',metrics=['accuracy'])
 datagen = ImageDataGenerator(
 rotation_range=12,
zoom_range=0.35,
width_shift_range=0.3,
height_shift_range=0.3, 
)
 datagen.fit(xtrain)
 learning_rate_reduction = tf.keras.callbacks.ReduceLROnPlateau(
 monitor='loss',
factor=0.2,
patience=2,
verbose=1,
mode="min",
min_delta=0.0001,
cooldown=0,
min_lr=0.00001 
)
 es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=300, restore_best_weights=True)
 model.fit_generator(datagen.flow(xtrain, ytrain, batch_size=512),
steps_per_epoch=len(xtrain)//512,
epochs=42,
validation_data=(np.array(xval),np.array(yval)) ,
validation_steps=50,
callbacks=[learning_rate_reduction, es])
 model.summary()
score=model.evaluate(np.array(xval),np.array(yval),batch_size=512)
 results = model.predict_classes(xtest)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False)
 print("accuracy",score[1] )