Kaggle competition: Digit Recognizer
X_train, X_val, y_train, y_val = train_test_split(X_train,
y_train,
test_size=0.25,
random_state=1 )<define_search_space>
def rotate_image(image, angle = 90, scale = 1.0):
h, w = image.shape
M = cv2.getRotationMatrix2D(( w/2, h/2), angle, scale)
 return cv2.warpAffine(image, M,(w, h))
kernel_ =(5,5 )<choose_model_class>
data = np.loadtxt('/kaggle/input/digit-recognizer/train.csv', delimiter = ',', skiprows = 1 )
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(MaxPooling2D(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(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dense(256, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
model.summary()<define_variables>
y = data[:, 0]
x = data[:, 1:].reshape(-1, 28, 28)
 x_train, x_cv, y_train, y_cv = train_test_split(x, y, test_size = 0.1)
 x_train_temp = x_train.copy()
for angle in np.arange(-10, 15, 5):
x_train = np.concatenate(( x_train, np.array([rotate_image(image, angle, scale = 1)for image in x_train_temp])))
 x_train = x_train.reshape(-1, 28, 28, 1)
 x_cv = x_cv.reshape(-1, 28, 28, 1)
 y_train_temp = y_train.copy()
for i in range(x_train.shape[0]//y_train_temp.shape[0] - 1):
y_train = np.concatenate(( y_train, y_train_temp))
aug = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1)
 gen_train = aug.flow(X_train, y_train, batch_size=64)
 gen_val = aug.flow(X_val, y_val, batch_size=64 )<choose_model_class>
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')) )
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'] )<choose_model_class>
YaNet = Sequential(name = 'YaNet')
 YaNet.add(Conv2D(filters = 32,
kernel_size =(5, 5),
kernel_initializer = 'he_uniform',
padding = 'same',
activation = 'relu',
input_shape =(28, 28, 1)))
 YaNet.add(BatchNormalization())
 YaNet.add(Conv2D(filters = 32,
kernel_size =(5, 5),
kernel_initializer = 'he_uniform',
padding = 'same',
activation = 'relu'))
YaNet.add(MaxPooling2D())
 YaNet.add(BatchNormalization())
 YaNet.add(Conv2D(filters = 64,
kernel_size =(3, 3),
kernel_initializer = 'he_uniform',
padding = 'same',
activation = 'relu'))
YaNet.add(MaxPooling2D())
 YaNet.add(BatchNormalization())
 YaNet.add(Conv2D(filters = 128,
kernel_size =(3, 3),
kernel_initializer = 'he_uniform',
padding = 'same',
activation = 'relu'))
YaNet.add(BatchNormalization())
 YaNet.add(Conv2D(filters = 128,
kernel_size =(3, 3),
kernel_initializer = 'he_uniform',
padding = 'same',
activation = 'relu'))
YaNet.add(MaxPooling2D())
 YaNet.add(BatchNormalization())
 YaNet.add(Flatten())
 YaNet.add(Dense(units = 512,
activation = 'relu'))
YaNet.add(Dropout(0.5))
YaNet.add(BatchNormalization())
 YaNet.add(Dense(units = 256,
activation = 'relu'))
YaNet.add(Dropout(0.5))
YaNet.add(BatchNormalization())
 YaNet.add(Dense(units = 128,
activation = 'relu'))
YaNet.add(Dropout(0.5))
YaNet.add(BatchNormalization())
 YaNet.add(Dense(units = 64,
activation = 'relu'))
YaNet.add(Dropout(0.5))
YaNet.add(BatchNormalization())
 YaNet.add(Dense(units = 10,
activation = 'softmax'))
YaNet.compile(optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'])
 YaNet.fit(x_train, y_train,
epochs = 100,
validation_data =(x_cv, y_cv),
use_multiprocessing = True )
checkpoint = tf.keras.callbacks.ModelCheckpoint("weights.hdf5",
monitor='val_accuracy',
verbose=1,
save_best_only=True)
 reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss',
factor=0.5,
patience=4,
min_lr=0.00005,
verbose=1)
 early_stop = tf.keras.callbacks.EarlyStopping(patience=5, restore_best_weights=True )<train_model>
x_test = np.loadtxt('/kaggle/input/digit-recognizer/test.csv', skiprows = 1, delimiter = ',')
 x_test = x_test.reshape(-1, 28, 28, 1 )
<load_pretrained><EOS> 
final_prediction = np.argmax(YaNet.predict(x_test), axis = 1)
 output = pd.DataFrame({'ImageId': np.arange(1, x_test.shape[0]+1), 'Label': final_prediction})
 output.to_csv('my_submission.csv', index = False )