In [1]:
%matplotlib inline
import seaborn as sns
import numpy as np
import pandas as pd
from keras.models import Sequential # 1 neural network pls = 1 model
from keras.layers.core import Dense, Activation, Flatten, Dropout
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras.preprocessing import text
from sklearn.cross_validation import train_test_split
img_rows, img_cols: Input image dimensions in pixels
in_shape: For keras (tensorflow), the dimensions of our input for a 2D convolutional layer
batch_size: Number of images used in each minibatch
nb_classes: One class per digit
nb_epoch: Number of times the whole data is used to learn
In [2]:
img_rows, img_cols = 28, 28
in_shape = (img_rows, img_cols, 1)
batch_size = 256
nb_classes = 10
nb_epoch = 3
In [3]:
train = pd.read_csv('train.csv').values
test = pd.read_csv('test.csv').values
In [4]:
X_train = train[:, 1:].reshape(train.shape[0], img_rows, img_cols, 1)
In [5]:
X_train.shape
Out[5]:
Next we need to make sure everything is a float. Then divide it by 255 to normalize the values between [0;1] rather than [0;255]
In [6]:
X_train = X_train.astype('float32')
X_train /= 255
[image: 28,28 ] = [0,0,0,1,0,0,0,0,0,0]
In [7]:
Y_train = y_train = train[:, 0]
In [8]:
Y_train[10]
Out[8]:
In [9]:
Y_train = np_utils.to_categorical(y_train, nb_classes)
In [10]:
Y_train[10]
Out[10]:
In [11]:
X_test = test.reshape(test.shape[0], img_rows, img_cols, 1)
X_test = X_test.astype('float32')
X_test /= 255
In [12]:
print("X_train shape: {}".format(X_train.shape))
print("Y_train shape: {}".format(Y_train.shape))
print("X_test shape : {}".format(X_test.shape))
In [13]:
sns.heatmap(X_train[3].reshape(28,28), cmap='gray')
Out[13]:
In [54]:
model = Sequential()
In [56]:
model.add(Convolution2D(16, 3, 3, border_mode='same', input_shape=in_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
In [57]:
model.summary()
In [58]:
model.add(Convolution2D(32, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
In [59]:
model.summary()
In [60]:
model.add(Convolution2D(32, 6, 4, border_mode='same', activation='relu'))
In [61]:
model.summary()
In [62]:
model.add(Flatten()) # this converts our 2D feature maps to 1D feature vectors
model.summary()
In [63]:
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dropout(0.5)) # helps prevent overfitting
model.summary()
In [64]:
model.add(Dense(nb_classes)) # nb_classes should be 10 for mnist
model.add(Activation('softmax'))
The .summary() allows us to peak inside our model. This allows you to see the overall structure of your model
In [65]:
model.summary()
In [66]:
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=["accuracy"])
loss: the loss of the training data
acc: the accuracy of the training data
val_loss: the loss of the validation set
val_acc: the accuracy of the validation data
In [43]:
model.fit(X_train, Y_train, # specify training data
batch_size=batch_size, # use this many images per mini-batch - memory dependent - 256
nb_epoch=nb_epoch, # go through my training data this number of times - 3
validation_split=.2, # use 20% of the training data as validation data
verbose=True # please print things
)
Out[43]:
In [70]:
Y_pred = model.predict(X_test)
In [71]:
Y_pred[0]
Out[71]:
In [53]:
model.save_weights('mnist_weights.h5')
In [69]:
model.load_weights('mnist_weights.h5')
Save predictions to test file for Kaggle submission
np.savetxt('mnist-pred4.csv', np.c_[range(1,len(Y_pred)+1),Y_pred], delimiter=',', header = 'ImageId,Label', comments = '', fmt='%d')
In [ ]: