Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<choose_model_class>
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.callbacks import LearningRateScheduler, ModelCheckpoint, ReduceLROnPlateau
from sklearn.model_selection import train_test_split
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dropout,Dense,Activation,Conv2D,MaxPooling2D,Flatten, BatchNormalization, MaxPool2D
from tensorflow.keras.metrics import AUC
from kerastuner.tuners import RandomSearch
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.preprocessing.image import ImageDataGenerator
model = models.Sequential()
model.add(layers.Conv2D(filters=32, kernel_size=(4, 4), strides=(1,1), padding = 'same', activation='relu', input_shape = data_shape))
model.add(layers.BatchNormalization())
 model.add(layers.LeakyReLU(0.2))
model.add(layers.Conv2D(filters=32, kernel_size=(4, 4), strides=(1,1), padding = 'same', activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.LeakyReLU(0.2))
model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Dropout(0.35))
model.add(layers.Conv2D(filters=64, kernel_size=(3, 3), strides=(1,1), padding = 'same', activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.LeakyReLU(0.2))
model.add(layers.Conv2D(filters=64, kernel_size=(3, 3), strides=(1,1), padding = 'same', activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.LeakyReLU(0.2))
model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Dropout(0.35))
model.add(layers.Conv2D(filters=64, kernel_size=(2, 2), strides=(1,1), padding = 'same', activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.LeakyReLU(0.2))
model.add(layers.Conv2D(filters=64, kernel_size=(2, 2), strides=(1,1), padding = 'same', activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.LeakyReLU(0.2))
model.add(layers.Dropout(0.35))
model.add(layers.Flatten())
 model.add(layers.Dense(units=256, activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.Dropout(0.35))
model.add(layers.Dense(units=256, activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.Dropout(0.35))
model.add(layers.Dense(units=10, activation='softmax'))
model.summary()<init_hyperparams>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 x_test = pd.read_csv('.. /input/digit-recognizer/test.csv')
 x_train = train.drop(columns = 'label')
 y_train = train['label']
x_train.shape
counter = 0
<init_hyperparams>
x_train = x_train.values.reshape(-1, int(np.sqrt(784)) , int(np.sqrt(784)) , 1)/255.0
x_test = x_test.values.reshape(-1, int(np.sqrt(784)) , int(np.sqrt(784)) , 1)/255.0
<init_hyperparams>
y_train = to_categorical(y_train, num_classes=10 )
<init_hyperparams>
x_train_trim ,x_valid , y_train_trim, y_valid = train_test_split(x_train, y_train, test_size= 0.1 ,random_state = 1455)
 print(f'Training Set size: {x_train_trim.shape[0]}')
 print(f'Validation Set size: {x_valid.shape[0]}' )
<categorify>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same', activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
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(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
<init_hyperparams>
def build_lrfn(lr_start=1e-4, lr_max=1e-3,
lr_min=0, lr_rampup_epochs=16,
lr_sustain_epochs=0, lr_exp_decay=.8):
def lrfn(epoch):
if epoch < lr_rampup_epochs:
lr =(lr_max - lr_start)/ lr_rampup_epochs * epoch + lr_start
elif epoch < lr_rampup_epochs + lr_sustain_epochs:
lr = lr_max
else:
lr =(lr_max - lr_min)*\
lr_exp_decay**(epoch - lr_rampup_epochs\
- lr_sustain_epochs)+ lr_min
return lr
return lrfn
lrfn = build_lrfn()
lr_schedule = LearningRateScheduler(lrfn, verbose=True)
 checkpoint = ModelCheckpoint(
 filepath='best_weights.hdf5',
save_weights_only=True,
monitor='accuracy',
mode='max',
save_best_only=True)
 optimizer_rmsprop = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
<train_model>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(x_train_trim )
epochs_num = 40
batch_size = 128
validation_split_part = 0.2
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
 history = model.fit(train_image, train_label, epochs=epochs_num, batch_size=batch_size, validation_split=validation_split_part, shuffle=True )<save_model>
model.compile(optimizer=optimizer_rmsprop,
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
 batch_size = 512
epochs = 40

 train_history = model.fit_generator(datagen.flow(x_train_trim, y_train_trim, batch_size=batch_size),
epochs = epochs, validation_data =(x_valid,y_valid),
verbose = 2, steps_per_epoch=x_train_trim.shape[0] // batch_size
, callbacks=[lr_schedule, checkpoint] )
model.save('Digit_Recognition_CNN.model' )<predict_on_test>
visualize_training(train_history )
predictions = model.predict(test_image )<prepare_output>
model.load_weights('./best_weights.hdf5')
 predictions_probs = model.predict(x_test)
 predictions = np.argmax(predictions_probs, axis = 1)
 counter = 0
predictions = np.argmax(predictions, axis = 1 )<create_dataframe>
ImageId = np.arange(1,x_test.shape[0] + 1)
 output = pd.DataFrame({'ImageId':ImageId, 'Label':predictions})
 output.to_csv('submission.csv', index=False)
 print(output )