Kaggle competition: Digit Recognizer
df.to_csv('submission.csv', index=False )<import_modules>
import tensorflow as tf
from tensorflow import keras
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow.keras import layers, Sequential, optimizers
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.utils import to_categorical
import matplotlib.pyplot as plt
import pandas as pd<load_from_csv>
mnist_test = pd.read_csv("/kaggle/input/mnist-fashion-data-classification/mnist_test.csv")
 mnist_train = pd.read_csv("/kaggle/input/mnist-fashion-data-classification/mnist_train.csv")
<load_from_csv>
raw_csv = "/kaggle/input/digit-recognizer/train.csv"
test_csv = "/kaggle/input/digit-recognizer/test.csv"
raw_df = pd.read_csv(raw_csv)
 test_df = pd.read_csv(test_csv )
sample_submission = pd.read_csv("/kaggle/input/digit-recognizer/sample_submission.csv")
 train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )<feature_engineering>
def get_image_and_label(data_frame):
IMGs = data_frame.drop(["label"], axis=1 ).values if 'label' in data_frame.columns else data_frame.values
IMGs = np.array([image.reshape(( 28, 28)) for image in IMGs])
 IMGs = np.expand_dims(IMGs, axis=3)
 labels = data_frame['label'].values if 'label' in data_frame.columns else None
return IMGs, labels
test['dataset'] = 'test'<feature_engineering>
raw_IMGs, raw_labels = get_image_and_label(raw_df)
 test_IMGs, _ = get_image_and_label(test_df )
train['dataset'] = 'train'<concatenate>
classes = len(set(raw_labels))
classes
dataset = pd.concat([train.drop('label', axis=1), test] ).reset_index()<concatenate>
raw_labels = to_categorical(raw_labels, num_classes=classes)
mnist = pd.concat([mnist_train, mnist_test] ).reset_index(drop=True)
 labels = mnist['label'].values
mnist.drop('label', axis=1, inplace=True)
 mnist.columns = cols<sort_values>
train_IMGs, validation_IMGs, trian_labels, validation_labels = \
train_test_split(raw_IMGs, raw_labels, test_size=0.1, random_state=42)
idx_mnist = mnist.sort_values(by=list(mnist.columns)).index
dataset_from = dataset.sort_values(by=list(mnist.columns)) ['dataset'].values
original_idx = dataset.sort_values(by=list(mnist.columns)) ['index'].values<feature_engineering>
model = Sequential([
layers.Conv2D(64,(3,3), activation="relu", input_shape=(28,28,1)) ,
layers.BatchNormalization() ,
layers.MaxPooling2D(( 2,2)) ,
layers.Conv2D(128,(3, 3), activation="relu"),
layers.BatchNormalization() ,
layers.MaxPooling2D(( 2,2)) ,
layers.Conv2D(256,(3,3), activation="relu"),
layers.BatchNormalization() ,
layers.Flatten() ,
layers.Dense(1024, activation="relu"),
layers.Dropout(0.2),
layers.Dense(256, activation="relu"),
layers.Dropout(0.2),
layers.Dense(64, activation="relu"),
layers.Dropout(0.2),
layers.Dense(32, activation="relu"),
layers.Dropout(0.2),
layers.Dense(16, activation="relu"),
layers.Dropout(0.2),
layers.Dense(int(classes), activation="softmax")
 ])
for i in range(len(idx_mnist)) :
if dataset_from[i] == 'test':
sample_submission.loc[original_idx[i], 'Label'] = labels[idx_mnist[i]]<save_to_csv>
model.compile(loss="categorical_crossentropy",
optimizer=optimizers.Adam(learning_rate=1e-4),
metrics=['accuracy'] )
sample_submission.to_csv('submission.csv', index=False )<load_from_csv>
train_datagen = ImageDataGenerator(
 rescale=1/255,
rotation_range=20,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1,
shear_range=0.1 
)
 validation_datagen = ImageDataGenerator(rescale=1/255)
 test_datagen = ImageDataGenerator(rescale=1/255 )
tf.random.set_seed(42)
 %matplotlib inline
pd.set_option('display.max_rows', 10)
 pd.set_option('display.max_columns', None)
 pd.set_option('float_format', '{:f}'.format)
 mpl.rcParams['figure.dpi'] = 600
warnings.filterwarnings('ignore')
 tf.get_logger().setLevel('INFO')
 train_df = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test_df = pd.read_csv('.. /input/digit-recognizer/test.csv')
 submission = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv', index_col = 'ImageId')
 x = train_df.drop(columns = 'label' ).values.reshape(-1, 28, 28)
 y = train_df['label'].values
x_test = test_df.values.reshape(-1, 28, 28)
 n_labels = len(np.unique(y))
del train_df, test_df
gc.collect()<choose_model_class>
train_generator = train_datagen.flow(train_IMGs, trian_labels, batch_size=32)
 validation_generator = train_datagen.flow(validation_IMGs, validation_labels, batch_size=32)
 test_generator = test_datagen.flow(test_IMGs, batch_size=32, shuffle=False)
 history = model.fit_generator(train_generator, epochs=150, validation_data=validation_generator, verbose=1 )
def define_model(input_shape, n_classes, n_conv_branches, dropout):
inputs = layers.Input(shape = input_shape)
 b_in = layers.experimental.preprocessing.Rescaling(1./ 255 )(inputs)
 branches = [b_in] * n_conv_branches
for i in range(n_conv_branches):
for filter_size in [32, 64, 128, 128]:
branches[i] = layers.Conv2D(
 filters = filter_size,
kernel_size = 3,
padding = 'same',
)(branches[i])
 branches[i] = layers.MaxPool2D(pool_size =(2, 2))(branches[i])
 branches[i] = layers.ReLU()(branches[i])
 branches[i] = layers.Dropout(dropout )(branches[i])
 if n_conv_branches > 1:
b_out = layers.concatenate(branches)
 b_out = layers.Flatten()(b_out)
 else:
b_out = layers.Flatten()(branches[0])
 b_out = layers.Dense(units = 128 )(b_out)
 b_out = layers.BatchNormalization()(b_out)
 b_out = layers.ReLU()(b_out)
 b_out = layers.Dropout(dropout )(b_out)
 outputs = layers.Dense(units = n_classes )(b_out)
 return Model(inputs, outputs )<split>
accuracy = history.history["accuracy"]
val_accuracy = history.history["val_accuracy"]
loss = history.history["loss"]
val_loss = history.history["val_loss"]
epochs = range(len(accuracy))
N_SPLITS = 10
CHECKPOINT_DIR = './checkpoint'
cv = StratifiedKFold(n_splits = N_SPLITS, random_state = 42, shuffle = True)
 oof_pred = np.zeros(( x_test.shape[0], n_labels))
cv_val_scores = np.zeros(N_SPLITS)
 histories = []
k = 0
for train_i, val_i in cv.split(x, y):
x_train = x[train_i, :]
x_valid = x[val_i, :]
y_train = y[train_i]
y_valid = y[val_i]
model = define_model(( x.shape[1], x.shape[2], 1), n_labels, 2, 0.2)
 gc.collect()
optimizer = Adam(
 learning_rate = 5e-4, 
)
 model.compile(
 optimizer = optimizer,
loss = SparseCategoricalCrossentropy(from_logits = True),
metrics = ['accuracy'] 
)
 checkpoint_call = ModelCheckpoint(
 filepath = CHECKPOINT_DIR,
save_weights_only = True,
monitor = 'val_accuracy',
mode = 'max',
save_best_only = True 
)
 stopping_call = EarlyStopping(
 monitor = 'val_accuracy',
patience = 50,
mode = 'max' 
)
 history = model.fit(
 x_train, y_train,
validation_data =(x_valid, y_valid),
epochs = 200,
callbacks = [checkpoint_call, stopping_call],
batch_size = 1024, 
)
 histories += [history]
model.load_weights(CHECKPOINT_DIR)
 predictor_model = tf.keras.Sequential([model, layers.Softmax() ])
 cv_val_scores[k] = model.evaluate(x_valid, y_valid)[1]
oof_pred += predictor_model.predict(x_test)/ N_SPLITS
k += 1<compute_test_metric>
pred_labels = model.predict_generator(test_generator )
print('Validation AUC: {:.6} ± {:.4}'.format(cv_val_scores.mean() , cv_val_scores.std()))<save_to_csv>
pred_labels = np.argmax(pred_labels, axis=-1)
 pred_labels
submission.loc[:, 'Label'] = np.argmax(oof_pred, axis = 1)
 submission.to_csv('submission.csv' )<load_from_csv>
my_submission = pd.DataFrame({'ImageId': test_df.index + 1, 'Label': pred_labels})
 my_submission.head()
train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 train_image = np.array(train.drop(['label'], axis=1), dtype="float32")/ 255
train_image = train_image.reshape(-1, 28, 28, 1)
 train_label = tf.keras.utils.to_categorical(train['label'])
 test = np.array(test, dtype="float32")/ 255
test = test.reshape(-1, 28, 28, 1)
 show_images(train_image[:25], train_label[:25], shape=(5,5))<train_model>
my_submission.to_csv('submission.csv', index=False )