Kaggle competition: Digit Recognizer
model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(5,5), padding='same', activation='relu', input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters=64, kernel_size=(5,5), padding='same', activation='relu'))
model.add(BatchNormalization())
 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(BatchNormalization())
 model.add(Conv2D(filters=64, kernel_size=(3,3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters=64, kernel_size=(3,3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(10, activation='softmax'))
model.summary()<choose_model_class>
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 train.head()
class myCallback(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('val_accuracy')> 0.9955):
print("Stop training!")
 self.model.stop_training = True
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
 reduce_lr = ReduceLROnPlateau(
 monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 
)
 epoch_end = myCallback() 
<train_model>
from keras.preprocessing.image import ImageDataGenerator
from keras.utils.np_utils import to_categorical
history = model.fit(datagen.flow(x_train, y_train, batch_size=256),
epochs=200, validation_data=(x_val, y_val),
verbose=1, steps_per_epoch=x_train.shape[0]/256,
callbacks=[reduce_lr, epoch_end] )<save_to_csv>
Y_train = to_categorical(train['label'].values, 10)
 X_train =(train.loc[:, 'pixel0':] / 255 ).values
X_train.shape, Y_train.shape
results = model.predict(x_test)
 results = np.argmax(results, axis=1)
 submission = pd.read_csv(os.path.join(main_path, "sample_submission.csv"))
image_id = range(1, x_test.shape[0]+1)
 submission = pd.DataFrame({'Imageid':image_id, 'Label':results})
 submission.to_csv('cnn2_submission.csv', index=False )<set_options>
X_test =(test / 255 ).values
%matplotlib inline 
<load_from_csv>
datagener = ImageDataGenerator(
 rotation_range=15,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1,
)
train = import_data('.. /input/digit-recognizer/train.csv')
 test = import_data('.. /input/digit-recognizer/test.csv')
 y_lab = train['label']
y = tf.keras.utils.to_categorical(y_lab)
 train.drop('label', axis=1, inplace=True )<prepare_x_and_y>
example = X_train[6].reshape(( 1, 28, 28, 1))
label = Y_train[6]
train_df = np.array(train ).reshape(-1, 28, 28, 1)
 test_df = np.array(test ).reshape(-1, 28, 28, 1)
 del train
del test
del y_lab<data_type_conversions>
def lr_scheduler(epoch, lr):
return lr * 0.99
def change_size(image):
img = array_to_img(image, scale=False)
 img = img.resize(( 75, 75))
img = img.convert(mode='RGB')
 arr = img_to_array(img)
 return arr.astype(np.float32 )<drop_column>
X_train, X_valid, Y_train, Y_valid = train_test_split(X_train, Y_train, test_size=0.2 )
train_array = [change_size(img)for img in train_df]
train = np.array(train_array)
 del train_array
test_array = [change_size(img)for img in test_df]
test = np.array(test_array)
 del test_array<randomize_order>
np.random.seed(42)
 tf.random.set_seed(42)
 model = models.Sequential()
model.add(Conv2D(96, 3, activation='relu', padding='same', input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(SpatialDropout2D(0.4))
model.add(MaxPooling2D(( 2, 2)))
 model.add(Conv2D(160, 3, activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(SpatialDropout2D(0.4))
model.add(MaxPooling2D(( 2, 2)))
 model.add(Conv2D(256, 3, activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(SpatialDropout2D(0.4))
model.add(MaxPooling2D(( 2, 2)))
 model.add(Conv2D(64, 3, activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(SpatialDropout2D(0.4))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(96, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer=optimizers.Adam(lr=1e-2),
loss='categorical_crossentropy', metrics=['categorical_accuracy'])
 checkpoint_path = 'bestmodel.hdf5'
checkpoint = ModelCheckpoint(checkpoint_path, monitor='val_categorical_accuracy',
verbose=0, save_best_only=True, mode='max')
 scheduler = LearningRateScheduler(lr_scheduler, verbose=0)
 early_stop = EarlyStopping(monitor='val_loss', min_delta=0, patience=15, mode='min', verbose=0)
 tqdm_callback = tfa.callbacks.TQDMProgressBar(leave_epoch_progress=False,
leave_overall_progress=True,
show_epoch_progress=False,
show_overall_progress=True)
 callbacks_list = [
checkpoint,
scheduler,
tqdm_callback,
]
history = model.fit_generator(datagener.flow(X_train, Y_train, batch_size=150), epochs=225, steps_per_epoch=X_train.shape[0] // 150,
callbacks=callbacks_list,
verbose=1, validation_data=(X_valid, Y_valid))
def get_random_eraser(p=0.5, s_l=0.02, s_h=0.4, r_1=0.3, r_2=1/0.3, v_l=0, v_h=255, pixel_level=False):
def eraser(input_img):
if input_img.ndim == 3:
img_h, img_w, img_c = input_img.shape
elif input_img.ndim == 2:
img_h, img_w = input_img.shape
p_1 = np.random.rand()
if p_1 > p:
return input_img
while True:
s = np.random.uniform(s_l, s_h)* img_h * img_w
r = np.random.uniform(r_1, r_2)
 w = int(np.sqrt(s / r))
h = int(np.sqrt(s * r))
left = np.random.randint(0, img_w)
 top = np.random.randint(0, img_h)
 if left + w <= img_w and top + h <= img_h:
break
if pixel_level:
if input_img.ndim == 3:
c = np.random.uniform(v_l, v_h,(h, w, img_c))
if input_img.ndim == 2:
c = np.random.uniform(v_l, v_h,(h, w))
else:
c = np.random.uniform(v_l, v_h)
 input_img[top:top + h, left:left + w] = c
return input_img
return eraser<choose_model_class>
model.load_weights(checkpoint_path)
 print(model.evaluate(X_valid, Y_valid))
image_gen = ImageDataGenerator(rescale=1./255,
featurewise_center=False,
preprocessing_function=get_random_eraser(v_l=0, v_h=1),
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
zoom_range=0.1,
rotation_range=10,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.3,
validation_split=0.2)
 train_generator = image_gen.flow(train,
y,
batch_size=32,
shuffle=True,
subset='training',
seed=42)
 valid_generator = image_gen.flow(train,
y,
batch_size=16,
shuffle=True,
subset='validation')
 del train_df
del test_df
del train<choose_model_class>
submit = pd.DataFrame(np.argmax(model.predict(X_test), axis=1), columns=['Label'],
index=pd.read_csv('.. /input/digit-recognizer/sample_submission.csv')['ImageId'])
 submit.index.name = 'ImageId'
submit.to_csv('submittion.csv' )