image_output_1.png
from keras.layers import Conv2D, MaxPooling2D

from keras.layers import Dense, Dropout, Activation, Flatten

from keras.models import Sequential, load_model

from keras.preprocessing.image import ImageDataGenerator

from sklearn import preprocessing

from sklearn.model_selection import StratifiedShuffleSplit

import numpy as np

import os

import pandas as pd

train_df = pd.read_csv('../input/train.csv')
test_df = pd.read_csv('../input/test.csv')
X_train = []
Y_train = []
X_test = []
for index, row in train_df.iterrows():
    X_train.append(row.values[1:].reshape((28, 28, 1)))
    Y_train.append(row['label'])
for index, row in test_df.iterrows():
    X_test.append(row.values.reshape((28, 28, 1)))
X_train = np.array(X_train) / 255.0
Y_train = np.array(Y_train)
X_test = np.array(X_test) / 255.0
lb = preprocessing.LabelBinarizer()
lb.fit(Y_train)
Y_train = lb.transform(Y_train)
sss = StratifiedShuffleSplit(10, 0.2, random_state=15)
for train_idx, val_idx in sss.split(X_train, Y_train):
    X_train_tmp, X_val = (X_train[train_idx], X_train[val_idx])
    Y_train_tmp, Y_val = (Y_train[train_idx], Y_train[val_idx])
X_train = X_train_tmp
Y_train = Y_train_tmp

img_size = (28, 28, 1)
n_classes = 10
if os.path.exists('keras_model.h5'):
    model = load_model('keras_model.h5')
else:
    model = Sequential()
    model.add(Conv2D(32, (5, 5), input_shape=img_size, kernel_initializer='normal'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Conv2D(64, (5, 5), kernel_initializer='normal'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(128))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(n_classes))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

datagen = ImageDataGenerator(featurewise_center=False, samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, zca_whitening=False, rotation_range=0, zoom_range=0.1, width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=False, vertical_flip=False)
datagen.fit(X_train)

model.fit_generator(datagen.flow(X_train, Y_train, batch_size=1000), epochs=20, validation_data=(X_val, Y_val), steps_per_epoch=X_train.shape[0] / 1000, verbose=1)
score, acc = model.evaluate(X_val, Y_val, verbose=1)
print('\nLoss:', score, '\nAcc:', acc)
model.save('keras_model.h5')