# -*- coding: utf-8 -*-
"""
Created on Sat Mar 31 19:21:11 2018

@author: sohai
"""

# Mute tensorflow debugging information console
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

from keras.layers import MaxPooling2D, Convolution2D, Dropout, Dense, Flatten
from keras.models import Sequential, save_model
from keras.utils import np_utils
from scipy.io import loadmat
import keras
import numpy as np

width=28
height=28
verbose=True

def load_data(mat_file_path):

    # Local functions
    def rotate(img):
        # Used to rotate images (for some reason they are transposed on read-in)
        flipped = np.fliplr(img)
        return np.rot90(flipped)

    def display(img, threshold=0.5):
        # Debugging only
        render = ''
        for row in img:
            for col in row:
                if col > threshold:
                    render += '@'
                else:
                    render += '.'
            render += '\n'
        return render

    # Load convoluted list structure form loadmat
    mat = loadmat(mat_file_path)

    max_ = len(mat['dataset'][0][0][0][0][0][0])
    training_images = mat['dataset'][0][0][0][0][0][0][:max_].reshape(max_, height, width, 1)
    training_labels = mat['dataset'][0][0][0][0][0][1][:max_]

    # Load testing data
    if max_ == None:
        max_ = len(mat['dataset'][0][0][1][0][0][0])
    else:
        max_ = int(max_ / 6)
    testing_images = mat['dataset'][0][0][1][0][0][0][:max_].reshape(max_, height, width, 1)
    testing_labels = mat['dataset'][0][0][1][0][0][1][:max_]

    # Reshape training data to be valid
    _len = len(training_images)
    for i in range(len(training_images)):
        print('%d/%d (%.2lf%%)' % (i + 1, _len, ((i + 1)/_len) * 100), end='\r')
        training_images[i] = rotate(training_images[i])
    print('')

    # Reshape testing data to be valid
    _len = len(testing_images)
    for i in range(len(testing_images)):
        print('%d/%d (%.2lf%%)' % (i + 1, _len, ((i + 1)/_len) * 100), end='\r')
        testing_images[i] = rotate(testing_images[i])
    print('')

    # Convert type to float32
    training_images = training_images.astype('float32')
    testing_images = testing_images.astype('float32')

    # Normalize to prevent issues with model
    training_images /= 255
    testing_images /= 255

    nb_classes = 62 

    return ((training_images, training_labels), (testing_images, testing_labels), nb_classes)

def build_net(training_data):
    
    # Initialize data
    (x_train, y_train), (x_test, y_test), nb_classes = training_data
    input_shape = (height, width, 1)

    # Hyperparameters
    nb_filters = 256 # number of convolutional filters to use
    pool_size = (2, 2) # size of pooling area for max pooling
    kernel_size = (3, 3) # convolution kernel size

    model = Sequential()
    model.add(Convolution2D(nb_filters,
                            kernel_size,
                            padding='valid',
                            input_shape=input_shape,
                            activation='relu'))
    model.add(Convolution2D(nb_filters,
                            (4,4),
                            activation='relu'))

    model.add(MaxPooling2D(pool_size=pool_size))
    model.add(Dropout(0.25))
    model.add(Flatten())

    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.4))
    model.add(Dense(nb_classes, activation='softmax'))

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

    print(model.summary())
    return model

def train(model, training_data):
    (x_train, y_train), (x_test, y_test), nb_classes = training_data

    # convert class vectors to binary class matrices
    y_train = np_utils.to_categorical(y_train, nb_classes)
    y_test = np_utils.to_categorical(y_test, nb_classes)

   
    tbCallBack = keras.callbacks.TensorBoard(log_dir='./Graph', histogram_freq=0, write_graph=True, write_images=True)

    model.fit(x_train, y_train,
              batch_size=256,
              epochs=20,
              verbose=1,
              validation_data=(x_test, y_test),
              callbacks=[tbCallBack])

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

    # Offload model to file
    model_yaml = model.to_yaml()
    with open("model.yaml", "w") as yaml_file:
        yaml_file.write(model_yaml)
    save_model(model, 'model.h5')

training_data = load_data('matlab/emnist-byclass.mat')
model = build_net(training_data)
train(model, training_data)

3cfb47ce4ac494e7319470a89e6a6ca1a87d9bfa
<mask token>


def load_data(mat_file_path):

    def rotate(img):
        flipped = np.fliplr(img)
        return np.rot90(flipped)

    def display(img, threshold=0.5):
        render = ''
        for row in img:
            for col in row:
                if col > threshold:
                    render += '@'
                else:
                    render += '.'
            render += '\n'
        return render
    mat = loadmat(mat_file_path)
    max_ = len(mat['dataset'][0][0][0][0][0][0])
    training_images = mat['dataset'][0][0][0][0][0][0][:max_].reshape(max_,
        height, width, 1)
    training_labels = mat['dataset'][0][0][0][0][0][1][:max_]
    if max_ == None:
        max_ = len(mat['dataset'][0][0][1][0][0][0])
    else:
        max_ = int(max_ / 6)
    testing_images = mat['dataset'][0][0][1][0][0][0][:max_].reshape(max_,
        height, width, 1)
    testing_labels = mat['dataset'][0][0][1][0][0][1][:max_]
    _len = len(training_images)
    for i in range(len(training_images)):
        print('%d/%d (%.2lf%%)' % (i + 1, _len, (i + 1) / _len * 100), end='\r'
            )
        training_images[i] = rotate(training_images[i])
    print('')
    _len = len(testing_images)
    for i in range(len(testing_images)):
        print('%d/%d (%.2lf%%)' % (i + 1, _len, (i + 1) / _len * 100), end='\r'
            )
        testing_images[i] = rotate(testing_images[i])
    print('')
    training_images = training_images.astype('float32')
    testing_images = testing_images.astype('float32')
    training_images /= 255
    testing_images /= 255
    nb_classes = 62
    return (training_images, training_labels), (testing_images, testing_labels
        ), nb_classes


def build_net(training_data):
    (x_train, y_train), (x_test, y_test), nb_classes = training_data
    input_shape = height, width, 1
    nb_filters = 256
    pool_size = 2, 2
    kernel_size = 3, 3
    model = Sequential()
    model.add(Convolution2D(nb_filters, kernel_size, padding='valid',
        input_shape=input_shape, activation='relu'))
    model.add(Convolution2D(nb_filters, (4, 4), activation='relu'))
    model.add(MaxPooling2D(pool_size=pool_size))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.4))
    model.add(Dense(nb_classes, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adadelta',
        metrics=['accuracy'])
    print(model.summary())
    return model


def train(model, training_data):
    (x_train, y_train), (x_test, y_test), nb_classes = training_data
    y_train = np_utils.to_categorical(y_train, nb_classes)
    y_test = np_utils.to_categorical(y_test, nb_classes)
    tbCallBack = keras.callbacks.TensorBoard(log_dir='./Graph',
        histogram_freq=0, write_graph=True, write_images=True)
    model.fit(x_train, y_train, batch_size=256, epochs=20, verbose=1,
        validation_data=(x_test, y_test), callbacks=[tbCallBack])
    score = model.evaluate(x_test, y_test, verbose=0)
    print('Test score:', score[0])
    print('Test accuracy:', score[1])
    model_yaml = model.to_yaml()
    with open('model.yaml', 'w') as yaml_file:
        yaml_file.write(model_yaml)
    save_model(model, 'model.h5')


<mask token>

<mask token>


def load_data(mat_file_path):

    def rotate(img):
        flipped = np.fliplr(img)
        return np.rot90(flipped)

    def display(img, threshold=0.5):
        render = ''
        for row in img:
            for col in row:
                if col > threshold:
                    render += '@'
                else:
                    render += '.'
            render += '\n'
        return render
    mat = loadmat(mat_file_path)
    max_ = len(mat['dataset'][0][0][0][0][0][0])
    training_images = mat['dataset'][0][0][0][0][0][0][:max_].reshape(max_,
        height, width, 1)
    training_labels = mat['dataset'][0][0][0][0][0][1][:max_]
    if max_ == None:
        max_ = len(mat['dataset'][0][0][1][0][0][0])
    else:
        max_ = int(max_ / 6)
    testing_images = mat['dataset'][0][0][1][0][0][0][:max_].reshape(max_,
        height, width, 1)
    testing_labels = mat['dataset'][0][0][1][0][0][1][:max_]
    _len = len(training_images)
    for i in range(len(training_images)):
        print('%d/%d (%.2lf%%)' % (i + 1, _len, (i + 1) / _len * 100), end='\r'
            )
        training_images[i] = rotate(training_images[i])
    print('')
    _len = len(testing_images)
    for i in range(len(testing_images)):
        print('%d/%d (%.2lf%%)' % (i + 1, _len, (i + 1) / _len * 100), end='\r'
            )
        testing_images[i] = rotate(testing_images[i])
    print('')
    training_images = training_images.astype('float32')
    testing_images = testing_images.astype('float32')
    training_images /= 255
    testing_images /= 255
    nb_classes = 62
    return (training_images, training_labels), (testing_images, testing_labels
        ), nb_classes


def build_net(training_data):
    (x_train, y_train), (x_test, y_test), nb_classes = training_data
    input_shape = height, width, 1
    nb_filters = 256
    pool_size = 2, 2
    kernel_size = 3, 3
    model = Sequential()
    model.add(Convolution2D(nb_filters, kernel_size, padding='valid',
        input_shape=input_shape, activation='relu'))
    model.add(Convolution2D(nb_filters, (4, 4), activation='relu'))
    model.add(MaxPooling2D(pool_size=pool_size))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.4))
    model.add(Dense(nb_classes, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adadelta',
        metrics=['accuracy'])
    print(model.summary())
    return model


def train(model, training_data):
    (x_train, y_train), (x_test, y_test), nb_classes = training_data
    y_train = np_utils.to_categorical(y_train, nb_classes)
    y_test = np_utils.to_categorical(y_test, nb_classes)
    tbCallBack = keras.callbacks.TensorBoard(log_dir='./Graph',
        histogram_freq=0, write_graph=True, write_images=True)
    model.fit(x_train, y_train, batch_size=256, epochs=20, verbose=1,
        validation_data=(x_test, y_test), callbacks=[tbCallBack])
    score = model.evaluate(x_test, y_test, verbose=0)
    print('Test score:', score[0])
    print('Test accuracy:', score[1])
    model_yaml = model.to_yaml()
    with open('model.yaml', 'w') as yaml_file:
        yaml_file.write(model_yaml)
    save_model(model, 'model.h5')


<mask token>
train(model, training_data)

<mask token>
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
<mask token>
width = 28
height = 28
verbose = True


def load_data(mat_file_path):

    def rotate(img):
        flipped = np.fliplr(img)
        return np.rot90(flipped)

    def display(img, threshold=0.5):
        render = ''
        for row in img:
            for col in row:
                if col > threshold:
                    render += '@'
                else:
                    render += '.'
            render += '\n'
        return render
    mat = loadmat(mat_file_path)
    max_ = len(mat['dataset'][0][0][0][0][0][0])
    training_images = mat['dataset'][0][0][0][0][0][0][:max_].reshape(max_,
        height, width, 1)
    training_labels = mat['dataset'][0][0][0][0][0][1][:max_]
    if max_ == None:
        max_ = len(mat['dataset'][0][0][1][0][0][0])
    else:
        max_ = int(max_ / 6)
    testing_images = mat['dataset'][0][0][1][0][0][0][:max_].reshape(max_,
        height, width, 1)
    testing_labels = mat['dataset'][0][0][1][0][0][1][:max_]
    _len = len(training_images)
    for i in range(len(training_images)):
        print('%d/%d (%.2lf%%)' % (i + 1, _len, (i + 1) / _len * 100), end='\r'
            )
        training_images[i] = rotate(training_images[i])
    print('')
    _len = len(testing_images)
    for i in range(len(testing_images)):
        print('%d/%d (%.2lf%%)' % (i + 1, _len, (i + 1) / _len * 100), end='\r'
            )
        testing_images[i] = rotate(testing_images[i])
    print('')
    training_images = training_images.astype('float32')
    testing_images = testing_images.astype('float32')
    training_images /= 255
    testing_images /= 255
    nb_classes = 62
    return (training_images, training_labels), (testing_images, testing_labels
        ), nb_classes


def build_net(training_data):
    (x_train, y_train), (x_test, y_test), nb_classes = training_data
    input_shape = height, width, 1
    nb_filters = 256
    pool_size = 2, 2
    kernel_size = 3, 3
    model = Sequential()
    model.add(Convolution2D(nb_filters, kernel_size, padding='valid',
        input_shape=input_shape, activation='relu'))
    model.add(Convolution2D(nb_filters, (4, 4), activation='relu'))
    model.add(MaxPooling2D(pool_size=pool_size))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.4))
    model.add(Dense(nb_classes, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adadelta',
        metrics=['accuracy'])
    print(model.summary())
    return model


def train(model, training_data):
    (x_train, y_train), (x_test, y_test), nb_classes = training_data
    y_train = np_utils.to_categorical(y_train, nb_classes)
    y_test = np_utils.to_categorical(y_test, nb_classes)
    tbCallBack = keras.callbacks.TensorBoard(log_dir='./Graph',
        histogram_freq=0, write_graph=True, write_images=True)
    model.fit(x_train, y_train, batch_size=256, epochs=20, verbose=1,
        validation_data=(x_test, y_test), callbacks=[tbCallBack])
    score = model.evaluate(x_test, y_test, verbose=0)
    print('Test score:', score[0])
    print('Test accuracy:', score[1])
    model_yaml = model.to_yaml()
    with open('model.yaml', 'w') as yaml_file:
        yaml_file.write(model_yaml)
    save_model(model, 'model.h5')


training_data = load_data('matlab/emnist-byclass.mat')
model = build_net(training_data)
train(model, training_data)

<mask token>
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
from keras.layers import MaxPooling2D, Convolution2D, Dropout, Dense, Flatten
from keras.models import Sequential, save_model
from keras.utils import np_utils
from scipy.io import loadmat
import keras
import numpy as np
width = 28
height = 28
verbose = True


def load_data(mat_file_path):

    def rotate(img):
        flipped = np.fliplr(img)
        return np.rot90(flipped)

    def display(img, threshold=0.5):
        render = ''
        for row in img:
            for col in row:
                if col > threshold:
                    render += '@'
                else:
                    render += '.'
            render += '\n'
        return render
    mat = loadmat(mat_file_path)
    max_ = len(mat['dataset'][0][0][0][0][0][0])
    training_images = mat['dataset'][0][0][0][0][0][0][:max_].reshape(max_,
        height, width, 1)
    training_labels = mat['dataset'][0][0][0][0][0][1][:max_]
    if max_ == None:
        max_ = len(mat['dataset'][0][0][1][0][0][0])
    else:
        max_ = int(max_ / 6)
    testing_images = mat['dataset'][0][0][1][0][0][0][:max_].reshape(max_,
        height, width, 1)
    testing_labels = mat['dataset'][0][0][1][0][0][1][:max_]
    _len = len(training_images)
    for i in range(len(training_images)):
        print('%d/%d (%.2lf%%)' % (i + 1, _len, (i + 1) / _len * 100), end='\r'
            )
        training_images[i] = rotate(training_images[i])
    print('')
    _len = len(testing_images)
    for i in range(len(testing_images)):
        print('%d/%d (%.2lf%%)' % (i + 1, _len, (i + 1) / _len * 100), end='\r'
            )
        testing_images[i] = rotate(testing_images[i])
    print('')
    training_images = training_images.astype('float32')
    testing_images = testing_images.astype('float32')
    training_images /= 255
    testing_images /= 255
    nb_classes = 62
    return (training_images, training_labels), (testing_images, testing_labels
        ), nb_classes


def build_net(training_data):
    (x_train, y_train), (x_test, y_test), nb_classes = training_data
    input_shape = height, width, 1
    nb_filters = 256
    pool_size = 2, 2
    kernel_size = 3, 3
    model = Sequential()
    model.add(Convolution2D(nb_filters, kernel_size, padding='valid',
        input_shape=input_shape, activation='relu'))
    model.add(Convolution2D(nb_filters, (4, 4), activation='relu'))
    model.add(MaxPooling2D(pool_size=pool_size))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.4))
    model.add(Dense(nb_classes, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adadelta',
        metrics=['accuracy'])
    print(model.summary())
    return model


def train(model, training_data):
    (x_train, y_train), (x_test, y_test), nb_classes = training_data
    y_train = np_utils.to_categorical(y_train, nb_classes)
    y_test = np_utils.to_categorical(y_test, nb_classes)
    tbCallBack = keras.callbacks.TensorBoard(log_dir='./Graph',
        histogram_freq=0, write_graph=True, write_images=True)
    model.fit(x_train, y_train, batch_size=256, epochs=20, verbose=1,
        validation_data=(x_test, y_test), callbacks=[tbCallBack])
    score = model.evaluate(x_test, y_test, verbose=0)
    print('Test score:', score[0])
    print('Test accuracy:', score[1])
    model_yaml = model.to_yaml()
    with open('model.yaml', 'w') as yaml_file:
        yaml_file.write(model_yaml)
    save_model(model, 'model.h5')


training_data = load_data('matlab/emnist-byclass.mat')
model = build_net(training_data)
train(model, training_data)

# -*- coding: utf-8 -*-
"""
Created on Sat Mar 31 19:21:11 2018

@author: sohai
"""

# Mute tensorflow debugging information console
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

from keras.layers import MaxPooling2D, Convolution2D, Dropout, Dense, Flatten
from keras.models import Sequential, save_model
from keras.utils import np_utils
from scipy.io import loadmat
import keras
import numpy as np

width=28
height=28
verbose=True

def load_data(mat_file_path):

    # Local functions
    def rotate(img):
        # Used to rotate images (for some reason they are transposed on read-in)
        flipped = np.fliplr(img)
        return np.rot90(flipped)

    def display(img, threshold=0.5):
        # Debugging only
        render = ''
        for row in img:
            for col in row:
                if col > threshold:
                    render += '@'
                else:
                    render += '.'
            render += '\n'
        return render

    # Load convoluted list structure form loadmat
    mat = loadmat(mat_file_path)

    max_ = len(mat['dataset'][0][0][0][0][0][0])
    training_images = mat['dataset'][0][0][0][0][0][0][:max_].reshape(max_, height, width, 1)
    training_labels = mat['dataset'][0][0][0][0][0][1][:max_]

    # Load testing data
    if max_ == None:
        max_ = len(mat['dataset'][0][0][1][0][0][0])
    else:
        max_ = int(max_ / 6)
    testing_images = mat['dataset'][0][0][1][0][0][0][:max_].reshape(max_, height, width, 1)
    testing_labels = mat['dataset'][0][0][1][0][0][1][:max_]

    # Reshape training data to be valid
    _len = len(training_images)
    for i in range(len(training_images)):
        print('%d/%d (%.2lf%%)' % (i + 1, _len, ((i + 1)/_len) * 100), end='\r')
        training_images[i] = rotate(training_images[i])
    print('')

    # Reshape testing data to be valid
    _len = len(testing_images)
    for i in range(len(testing_images)):
        print('%d/%d (%.2lf%%)' % (i + 1, _len, ((i + 1)/_len) * 100), end='\r')
        testing_images[i] = rotate(testing_images[i])
    print('')

    # Convert type to float32
    training_images = training_images.astype('float32')
    testing_images = testing_images.astype('float32')

    # Normalize to prevent issues with model
    training_images /= 255
    testing_images /= 255

    nb_classes = 62 

    return ((training_images, training_labels), (testing_images, testing_labels), nb_classes)

def build_net(training_data):
    
    # Initialize data
    (x_train, y_train), (x_test, y_test), nb_classes = training_data
    input_shape = (height, width, 1)

    # Hyperparameters
    nb_filters = 256 # number of convolutional filters to use
    pool_size = (2, 2) # size of pooling area for max pooling
    kernel_size = (3, 3) # convolution kernel size

    model = Sequential()
    model.add(Convolution2D(nb_filters,
                            kernel_size,
                            padding='valid',
                            input_shape=input_shape,
                            activation='relu'))
    model.add(Convolution2D(nb_filters,
                            (4,4),
                            activation='relu'))

    model.add(MaxPooling2D(pool_size=pool_size))
    model.add(Dropout(0.25))
    model.add(Flatten())

    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.4))
    model.add(Dense(nb_classes, activation='softmax'))

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

    print(model.summary())
    return model

def train(model, training_data):
    (x_train, y_train), (x_test, y_test), nb_classes = training_data

    # convert class vectors to binary class matrices
    y_train = np_utils.to_categorical(y_train, nb_classes)
    y_test = np_utils.to_categorical(y_test, nb_classes)

   
    tbCallBack = keras.callbacks.TensorBoard(log_dir='./Graph', histogram_freq=0, write_graph=True, write_images=True)

    model.fit(x_train, y_train,
              batch_size=256,
              epochs=20,
              verbose=1,
              validation_data=(x_test, y_test),
              callbacks=[tbCallBack])

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

    # Offload model to file
    model_yaml = model.to_yaml()
    with open("model.yaml", "w") as yaml_file:
        yaml_file.write(model_yaml)
    save_model(model, 'model.h5')

training_data = load_data('matlab/emnist-byclass.mat')
model = build_net(training_data)
train(model, training_data)
