Kaggle competition: Digit Recognizer
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(128,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(256, kernel_size=(3, 3),
activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(512,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(512))
model.add(BatchNormalization())
 model.add(Dense(256))
model.add(BatchNormalization())
 model.add(Dense(128))
model.add(BatchNormalization())
 model.add(Dense(10, activation='softmax'))<save_to_csv>
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.callbacks import LearningRateScheduler
plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)
 Image('model.png' )<choose_model_class>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv' )
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"] )<choose_model_class>
X_train=X_train/255.0
test=test/255.0
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.2,
min_lr=0.00001)
 es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=15)
 checkpoint = ModelCheckpoint(filepath='model.h5', monitor='val_loss', mode='min', save_best_only=True, save_weights_only=True )<choose_model_class>
X_train = X_train.values.reshape(-1, 28, 28, 1)
 test = test.values.reshape(-1, 28, 28, 1 )
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,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )<define_variables>
x_train,x_test,y_train,y_test = train_test_split(X_train,Y_train,test_size=0.2 )
epochs = 50
batch_size = 128<train_model>
data_gen = ImageDataGenerator(
 rotation_range=12,
width_shift_range=0.12,
height_shift_range=0.12,
shear_range=0.12,
validation_split=0.2, )
history = model.fit_generator(datagen.flow(X_train, y_train, batch_size=batch_size),
epochs=epochs,
validation_data=(X_val, y_val),
verbose=2,
steps_per_epoch=X_train.shape[0]//batch_size,
callbacks=[learning_rate_reduction, es, checkpoint] )<predict_on_test>
data_gen.fit(x_train)
 data_gen.fit(x_test )
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )<save_to_csv>
y_train = to_categorical(y_train, num_classes=10)
 y_test = to_categorical(y_test, num_classes=10 )
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("submission.csv",index=False )<import_modules>
model = Sequential()
model.add(Conv2D(filters=16, kernel_size=(3, 3), activation="relu", input_shape=(28,28,1)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu'))
model.add(Dropout(0.25))
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters=64, kernel_size=(3, 3), activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
from matplotlib import pyplot as plt
import os
import scipy
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow import keras
import seaborn as sns
from sklearn.model_selection import train_test_split
import cv2<load_from_csv>
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] )
main_path = r".. /input/digit-recognizer"
train_df = pd.read_csv(os.path.join(main_path, "train.csv"))
test_df = pd.read_csv(os.path.join(main_path, "test.csv"))<prepare_x_and_y>
batch_size = 64
epochs = 15
history = model.fit_generator(data_gen.flow(x_train, y_train, batch_size = batch_size), epochs = epochs,
validation_data =(x_test, y_test), verbose=1,
steps_per_epoch=x_train.shape[0] // batch_size, 
)
x_train = train_df.drop(labels=["label"], axis=1)
 y_train = train_df["label"]
y_train.head()<categorify>
pred = np.argmax(model.predict(test), axis=1)
 sub_df = {'ImageId':list(range(1, len(test)+ 1)) ,'Label':pred}
submission = pd.DataFrame(sub_df ).astype('int')
 submission.head()
x_train = x_train.to_numpy() / 255.0
x_test = test_df.to_numpy() / 255.0
x_train = x_train.reshape(-1, 28, 28, 1)
 x_test = x_test.reshape(-1, 28, 28, 1)
 y_train = to_categorical(y_train)
 plt.imshow(x_train[125, :, :, :] )<split>
submission.to_csv('submission.csv', index=False )
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.15, random_state=2)
 datagen = ImageDataGenerator(
 rotation_range=27,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.3,
zoom_range=0.2
)<choose_model_class>
submission.to_csv('submission.csv', index=False )