Accuracy 96% with Simple Deep NN using Keras Theano backend for nonMNist alphabet recognition



Here's first try to build a simple simple deep neurons network by using Keras with Theano backend.

By using the previous data pickles sample nonMNist dataset we have saved, here's the completed coding.

The summarized deep filter layers can be described as combination of common dense layer with 512 units, relu activation, dropout ratio of 0.2, softmax activation, and RMSprop optimizer.

from __future__ import print_function

import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
from keras.utils import np_utils
import os
from six.moves import cPickle as pickle

batch_size = 128
num_classes = 10
epochs = 20

def load_data():
    test_filename = "notMNIST.pickle"
    if os.path.exists(test_filename):
        with open(test_filename, 'rb') as f:
            letter_dataset = pickle.load(f)
    return (letter_dataset)
        
lt_dt = load_data()
    
train_dataset = lt_dt['train_dataset']
train_labels = lt_dt['train_labels']
test_dataset = lt_dt['test_dataset']
test_labels = lt_dt['test_labels']

x_train = train_dataset.reshape(train_dataset.shape[0], 28*28)
y_train = np_utils.to_categorical(train_labels, 10)

x_test = test_dataset.reshape(test_dataset.shape[0],28*28)
y_test = np_utils.to_categorical(test_labels, 10)

x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255

print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))

model.summary()

model.compile(loss='categorical_crossentropy',
              optimizer=RMSprop(),
              metrics=['accuracy'])

history = model.fit(x_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test, y_test))

score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

Finally we get the best accuracy result so far, it's 96.04 % !!
Using Theano backend.
200000 train samples
10000 test samples
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 512)               401920    
_________________________________________________________________
dropout_1 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 512)               262656    
_________________________________________________________________
dropout_2 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 10)                5130      
=================================================================
Total params: 669,706
Trainable params: 669,706
Non-trainable params: 0
_________________________________________________________________
Train on 200000 samples, validate on 10000 samples
Epoch 1/20
200000/200000 [==============================] - 26s 131us/step - loss: 0.6428 - acc: 0.8149 - val_loss: 0.2836 - val_acc: 0.9146
Epoch 2/20
200000/200000 [==============================] - 27s 134us/step - loss: 0.4577 - acc: 0.8610 - val_loss: 0.2358 - val_acc: 0.9305
Epoch 3/20
200000/200000 [==============================] - 27s 134us/step - loss: 0.4071 - acc: 0.8766 - val_loss: 0.2102 - val_acc: 0.9391
Epoch 4/20
200000/200000 [==============================] - 27s 134us/step - loss: 0.3765 - acc: 0.8850 - val_loss: 0.1909 - val_acc: 0.9439
Epoch 5/20
200000/200000 [==============================] - 27s 134us/step - loss: 0.3550 - acc: 0.8917 - val_loss: 0.1725 - val_acc: 0.9500
Epoch 6/20
200000/200000 [==============================] - 27s 134us/step - loss: 0.3385 - acc: 0.8970 - val_loss: 0.1601 - val_acc: 0.9524
Epoch 7/20
200000/200000 [==============================] - 27s 135us/step - loss: 0.3250 - acc: 0.9006 - val_loss: 0.1612 - val_acc: 0.9524
Epoch 8/20
200000/200000 [==============================] - 27s 134us/step - loss: 0.3155 - acc: 0.9035 - val_loss: 0.1658 - val_acc: 0.9501
Epoch 9/20
200000/200000 [==============================] - 27s 133us/step - loss: 0.3051 - acc: 0.9066 - val_loss: 0.1526 - val_acc: 0.9543
Epoch 10/20
200000/200000 [==============================] - 26s 129us/step - loss: 0.2960 - acc: 0.9093 - val_loss: 0.1445 - val_acc: 0.9576
Epoch 11/20
200000/200000 [==============================] - 27s 133us/step - loss: 0.2884 - acc: 0.9115 - val_loss: 0.1401 - val_acc: 0.9588
Epoch 12/20
200000/200000 [==============================] - 27s 134us/step - loss: 0.2812 - acc: 0.9138 - val_loss: 0.1434 - val_acc: 0.9557
Epoch 13/20
200000/200000 [==============================] - 26s 132us/step - loss: 0.2757 - acc: 0.9152 - val_loss: 0.1433 - val_acc: 0.9581
Epoch 14/20
200000/200000 [==============================] - 26s 132us/step - loss: 0.2709 - acc: 0.9171 - val_loss: 0.1423 - val_acc: 0.9590
Epoch 15/20
200000/200000 [==============================] - 27s 133us/step - loss: 0.2644 - acc: 0.9182 - val_loss: 0.1472 - val_acc: 0.9578
Epoch 16/20
200000/200000 [==============================] - 26s 132us/step - loss: 0.2589 - acc: 0.9197 - val_loss: 0.1431 - val_acc: 0.9580
Epoch 17/20
200000/200000 [==============================] - 27s 133us/step - loss: 0.2550 - acc: 0.9210 - val_loss: 0.1397 - val_acc: 0.9607
Epoch 18/20
200000/200000 [==============================] - 27s 134us/step - loss: 0.2495 - acc: 0.9227 - val_loss: 0.1438 - val_acc: 0.9607
Epoch 19/20
200000/200000 [==============================] - 26s 131us/step - loss: 0.2448 - acc: 0.9238 - val_loss: 0.1392 - val_acc: 0.9606
Epoch 20/20
200000/200000 [==============================] - 26s 128us/step - loss: 0.2404 - acc: 0.9252 - val_loss: 0.1427 - val_acc: 0.9604
Test loss: 0.142701141765
Test accuracy: 0.9604


Popular posts from this blog

Accuracy for Logistic Regression model is always lower than using Convolutional neural network

Simple data curation practices using Numpy (Udacity Deep Learning Assignment 1)

96.24% accuracy with higher epoch numbers for Convolutional Neural Network

Python to read and record SQM-LE data, search saved data and find IP address..

Using mini batch SGD Neural Network in Alphabet recognition!

Alphabet recognition by using LogisticRegression model from sklearn.linear_model. (Udacity Assignment 1)

Tower Defense (Endless TD Beta)

First try to solve Polynomial equations by Tensorflow

Endless Falling