<jupyter_start><jupyter_text>Digit Recognition Using Keras<jupyter_code>import warnings

warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>Dependencies For faster training, if your system consists of an NVIDIA GPU, you can install **CUDA** and **CuDNN**.<jupyter_code>HAS_CUDA_CUDNN  = input('CUDA and CuDNN installed? (y/n): ') == 'y'<jupyter_output><empty_output><jupyter_text>Keras Backend<jupyter_code>KERAS_BACKEND   = input('Keras Backend?: (theano / tensorflow): ')
if KERAS_BACKEND == 'theano':
    KERAS_BACKEND = 'Theano'
else:
    if HAS_CUDA_CUDNN:
        KERAS_BACKEND = 'tensorflow-gpu'
    else:
        KERAS_BACKEND = 'tensorflow'
DEPENDENCIES    = [
    'numpy',
    'scipy',
    'scikit-learn',
    'matplotlib',
    'keras',
    KERAS_BACKEND,
    'h5py',
    'cython'
]

import pip

for dependency in DEPENDENCIES:
    pip.main(['install', dependency])<jupyter_output><empty_output><jupyter_text>Visualization<jupyter_code>import numpy as np

import matplotlib.pyplot as pplt
import matplotlib.cm     as cmap

pplt.style.use('seaborn')

% matplotlib inline
def plot(x):
    nsamples = len(x)
    nplots   = 4
    
    for i in range(nplots):
        pplt.subplot(220 + (i + 1))
        
        index  = np.random.randint(0, nsamples)
        sample = Xtrain[index]
        
        pplt.imshow(sample, cmap = cmap.gray)<jupyter_output><empty_output><jupyter_text>Preprocessing  Flattening<jupyter_code>def flatten(x, dtype = np.uint8):
    nsamples, width, height = x.shape
    ncols = width * height
    
    x     = x.reshape(nsamples, ncols).astype(dtype)
    
    return x<jupyter_output><empty_output><jupyter_text>Normalization<jupyter_code>from sklearn.preprocessing import MinMaxScaler
def normalize(x, *args, **kwargs):
    scaler = MinMaxScaler(*args, **kwargs)
    
    scaled = scaler.fit_transform(x)
    
    return scaled<jupyter_output><empty_output><jupyter_text>Encoding<jupyter_code>from keras.utils import np_utils
def one_hot(x):
    encoded = np_utils.to_categorical(x)
    
    return encoded<jupyter_output><empty_output><jupyter_text>Modelling<jupyter_code>from keras.models import Sequential
from keras.layers import Dense
def get_layers(X, y, hidden):
    ninp   = X.shape[1]
    nout   = y.shape[1]
    
    layers = [ninp] + hidden + [nout]
    
    return layers
def get_model(layers, config):
    model = Sequential()
    
    model.add(Dense(layers[ 1], input_shape = (layers[0],), kernel_initializer = 'normal', activation = 'relu'))
    
    for i in range(2, len(layers) - 1):
        model.add(Dense(layers[i], activation = 'relu'))
        
    model.add(Dense(layers[-1], activation = 'softmax'))
    
    model.compile(**config)
    
    return model<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>def fit(model, Xtrain, ytrain, Xtest, ytest, *args, **kwargs):
    history = model.fit(Xtrain, ytrain, validation_data = (Xtest, ytest), *args, **kwargs)
    
    fig, axes = pplt.subplots(2)
    fig.set_size_inches(20, 15)
    
    axes[0].plot(history.history['acc'])
    axes[0].plot(history.history['val_acc'])
    axes[0].set_xlabel('$epoch$')
    axes[0].set_ylabel('$accuracy$')
    axes[0].legend(['$train$', '$test$'])
    
    axes[1].plot(history.history['acc'])
    axes[1].plot(history.history['val_loss'])
    axes[1].set_xlabel('$epoch$')
    axes[1].set_ylabel('$loss$')
    axes[1].legend(['$train$', '$test$'])
    
    return axes, model<jupyter_output><empty_output><jupyter_text>Deployment<jupyter_code>import os, json, errno

RELPATH_DATA   = 'data'
RELPATH_MODELS = os.path.join(RELPATH_DATA, 'models')

def makedirs(path, ignore_exists = False):
    try:
        os.makedirs(path)
    except OSError as e:
        if e.errno == errno.EEXIST:
            if not ignore_exists:
                raise e
        else:
            raise e
            
makedirs(RELPATH_MODELS, ignore_exists = True)
def save(dirname, model, config):
    RELPATH_DIR = os.path.join(RELPATH_MODELS, dirname)
    
    makedirs(RELPATH_DIR, ignore_exists = True)
    
    json_       = model.to_json()

    with open(os.path.join(RELPATH_DIR, 'model.json'), 'w') as f:
        f.write(json_)
        
    model.save_weights(os.path.join(RELPATH_DIR, 'model.h5'))
    
    with open(os.path.join(RELPATH_DIR, 'config.json'), 'w') as f:
        json.dump(config, f)<jupyter_output><empty_output><jupyter_text>MNIST> English  Loading<jupyter_code>from keras.datasets import mnist
(Xtrain, ytrain), (Xtest, ytest) = mnist.load_data()<jupyter_output><empty_output><jupyter_text>Visualization<jupyter_code>plot(Xtrain)<jupyter_output><empty_output><jupyter_text>Preprocessing  Flattening<jupyter_code>Xtrain, Xtest = flatten(Xtrain), flatten(Xtest)<jupyter_output><empty_output><jupyter_text>Normalization<jupyter_code>Xtrain, Xtest = normalize(Xtrain), normalize(Xtest)<jupyter_output><empty_output><jupyter_text>Encoding<jupyter_code>ytrain, ytest = one_hot(ytrain), one_hot(ytest)<jupyter_output><empty_output><jupyter_text>Modelling<jupyter_code>layers = get_layers(Xtrain, ytrain, [512, 512])
config = { "loss": "categorical_crossentropy", "optimizer": "adam", "metrics": ["accuracy"] }
model  = get_model(layers, config)
model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 512)               401920    
_________________________________________________________________
dense_2 (Dense)              (None, 512)               262656    
_________________________________________________________________
dense_3 (Dense)              (None, 10)                5130      
=================================================================
Total params: 669,706
Trainable params: 669,706
Non-trainable params: 0
_________________________________________________________________<jupyter_text>Training<jupyter_code>batch_size  = 128
nepochs     = 20
axes, model = fit(model, Xtrain, ytrain, Xtest, ytest, batch_size = batch_size, epochs = nepochs)<jupyter_output>Train on 60000 samples, validate on 10000 samples
Epoch 1/20
60000/60000 [==============================] - 3s - loss: 0.2160 - acc: 0.9362 - val_loss: 0.1225 - val_acc: 0.9603
Epoch 2/20
60000/60000 [==============================] - 3s - loss: 0.0787 - acc: 0.9756 - val_loss: 0.0816 - val_acc: 0.9744
Epoch 3/20
60000/60000 [==============================] - 3s - loss: 0.0490 - acc: 0.9847 - val_loss: 0.0662 - val_acc: 0.9795
Epoch 4/20
60000/60000 [==============================] - 3s - loss: 0.0349 - acc: 0.9883 - val_loss: 0.0699 - val_acc: 0.9789
Epoch 5/20
60000/60000 [==============================] - 3s - loss: 0.0262 - acc: 0.9914 - val_loss: 0.1033 - val_acc: 0.9727
Epoch 6/20
60000/60000 [==============================] - 3s - loss: 0.0206 - acc: 0.9935 - val_loss: 0.0828 - val_acc: 0.9774
Epoch 7/20
60000/60000 [==============================] - 3s - loss: 0.0156 - acc: 0.9949 - val_loss: 0.0828 - val_acc: 0.9792
Epoch 8/20
60000/60000 [==============================] - 3s [...]<jupyter_text>Testing<jupyter_code>score = model.evaluate(Xtest, ytest)
print('Accuracy: ', score[1] * 100)<jupyter_output>Accuracy:  98.13<jupyter_text>Deploy<jupyter_code>save('mnist', model, config)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.174588171752633, 'fraction_numerical': 0.047529030515798, 'mean_word_length': 4.201544943820225, 'pattern_counts': {'":': 3, '<': 74, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '590923', 'license': 'MIT', 'n_tokens_mistral': 2904, 'n_tokens_neox': 2619, 'n_words': 466, 'path': 'notebooks/Digit Recognition Using Keras.ipynb', 'repo_name': 'kalindkaria/digdet', 'size': '7406'}