keras classification

#Keras Classifier Example

import pandas as pd
import numpy as np
from keras.utils import np_utils # For y values

# For Keras
from keras.models import Sequential
from keras.layers.core import Dense, Dropout

#For Tensorboard
from keras.callbacks import TensorBoard
tbCallBack = TensorBoard(log_dir='Graph',
    histogram_freq=0,embeddings_freq=0,write_graph=True, write_images=False)

# To View tensorboard  
#  tensorboard --logdir Graph/\n",


# Set data
data = np.array([
    [0, 0, 0],
    [1, 1, 0],
    [2, 2, 0],
    [3, 3, 0],
    [4, 4, 0],
    [5, 5, 1],
    [6, 6, 1],
    [7, 7, 1],
    [8, 8, 1],
    [9, 9, 1],
])

data = pd.DataFrame(data, columns=['x', 'y', 'class'])

# Split X and y
X = data.iloc[:, :-1].values
y = data.iloc[:, -1:].values

# Get dimensions of input and output
dimof_input = X.shape[1]
dimof_output = np.max(y) + 1
print('dimof_input: ', dimof_input)
print('dimof_output: ', dimof_output)

# Set y categorical
y = np_utils.to_categorical(y, dimof_output)

# Set constants
batch_size = len(data)
dimof_middle = 100
dropout = 0.2
countof_epoch = 300
verbose = 0
print('batch_size: ', batch_size)
print('dimof_middle: ', dimof_middle)
print('dropout: ', dropout)
print('countof_epoch: ', countof_epoch)
print('verbose: ', verbose)
print()

# Set model
model = Sequential()
model.add(Dense(dimof_middle, input_dim=dimof_input, kernel_initializer='uniform', activation='tanh'))
model.add(Dropout(dropout))
model.add(Dense(dimof_middle, kernel_initializer='uniform', activation='tanh'))
model.add(Dropout(dropout))
model.add(Dense(dimof_output, kernel_initializer='uniform', activation='softmax'))
model.compile(loss='mse', optimizer='sgd', metrics=['accuracy'])

# Train
model.fit(
    X, y,
    validation_split=0.2,
    batch_size=batch_size, 
    epochs=countof_epoch, 
    verbose=verbose,
    callbacks=[tbCallBack])

# Evaluate
loss, accuracy = model.evaluate(X, y, verbose=verbose)
print('loss: ', loss)
print('accuracy: ', accuracy*100)
print()

#SHow the Model
model.summary()


# Predict
# model.predict_classes(X, verbose=verbose)
for n in range(0,10):
    print('prediction of [{}, {}]   ->   {}: '.format(n,n,
                                                      model.predict_classes(np.array([[n, n]]), 
                                                                            verbose=verbose)))
dimof_input:  2
dimof_output:  2
batch_size:  10
dimof_middle:  100
dropout:  0.2
countof_epoch:  300
verbose:  0

loss:  0.21046808362
accuracy:  100.0

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_76 (Dense)             (None, 100)               300       
_________________________________________________________________
dropout_51 (Dropout)         (None, 100)               0         
_________________________________________________________________
dense_77 (Dense)             (None, 100)               10100     
_________________________________________________________________
dropout_52 (Dropout)         (None, 100)               0         
_________________________________________________________________
dense_78 (Dense)             (None, 2)                 202       
=================================================================
Total params: 10,602
Trainable params: 10,602
Non-trainable params: 0
_________________________________________________________________
prediction of [0, 0]   ->   [0]: 
prediction of [1, 1]   ->   [0]: 
prediction of [2, 2]   ->   [0]: 
prediction of [3, 3]   ->   [0]: 
prediction of [4, 4]   ->   [0]: 
prediction of [5, 5]   ->   [1]: 
prediction of [6, 6]   ->   [1]: 
prediction of [7, 7]   ->   [1]: 
prediction of [8, 8]   ->   [1]: 
prediction of [9, 9]   ->   [1]:

Output from Tensorboard

Tensorboard - Accuracy

This is the output from tensorboard

Keras CNN Model

This model was used for character classifications.

from keras.models import Sequential
from keras.layers.convolutional import Convolution2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras.layers.core import Dense

class CNN:
    def build(Width, Height, Depth, total_classes, Saved_Weights_Path=None):
        # Initialize the Model
        model = Sequential()

        # First CONV => RELU => POOL Layer
        model.add(Convolution2D(20, 5, 5, border_mode="same", input_shape=(Depth, Height, Width)))
        model.add(Activation("relu"))
        model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

        # Second CONV => RELU => POOL Layer
        model.add(Convolution2D(50, 5, 5, border_mode="same"))
        model.add(Activation("relu"))
        model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

        # Third CONV => RELU => POOL Layer 
        # Convolution -> ReLU Activation Function -> Pooling Layer
        model.add(Convolution2D(100, 5, 5, border_mode="same"))
        model.add(Activation("relu"))
        model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

        # FC => RELU layers
        # Fully Connected Layer -> ReLU Activation Function
        model.add(Flatten())
        model.add(Dense(500))
        model.add(Activation("relu"))

        # Using Softmax Classifier for Linear Classification
        model.add(Dense(total_classes))
        model.add(Activation("softmax"))

        # If the saved_weights file is already present i.e model is pre-trained, load that weights
        if weightsPath is not None:
            model.load_weights(Saved_Weights_Path)
        return model