In [3]:
from sklearn.cross_validation import train_test_split
from sklearn.metrics import classification_report
from sklearn.externals import joblib
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn import datasets
from nolearn.dbn import DBN
from skimage import io
from skimage import data, segmentation, filters, color, img_as_float, img_as_ubyte, exposure, feature, measure, morphology
from skimage.color import rgb2gray
from skimage.morphology import square
from skimage.feature import hog
import cv2
import numpy as np
import cv2
from time import time
from glob import glob
import os
In [ ]:
### Plan2 ###
In [217]:
features = joblib.load("./mldata/features_1200.mat")
labels = joblib.load("./mldata/lables_1200.mat")
features = np.array(features, 'int16')
labels = np.array(labels, 'int')
In [218]:
t0 = time()
def scale(X, eps = 0.001):
# scale the data points s.t the columns of the feature space
# (i.e the predictors) are within the range [0, 1]
return (X - np.min(X, axis = 0)) / (np.max(X, axis = 0) + eps)
features = features.astype("float32")
features = scale(features)
print "escape time : ", round(time()-t0, 3), "s"
In [219]:
# scale the data to the range [0, 1] and then construct the training
# and testing splits
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.1)
print "the shape of training set %s rows, %s columns" %(trainX.shape[0], trainX.shape[1])
print "the shape of test set %s rows, %s columns" %(testX.shape[0], testX.shape[1])
print "the range of training set : %s ~ %s" %(trainX.min(),trainX.max())
print "the range of test set : %s ~ %s" %(testX.min(),testX.max())
In [220]:
# train the Deep Belief Network with 784 input units (the flattened,
# 28x28 grayscale image), 500 hidden units, 9 output units (one for
# each possible output classification, which are the digits 0-8)
t0 = time()
dbn = DBN(
[trainX.shape[1], 500, 9],
learn_rates = 0.009,
learn_rate_decays = 0.9,
epochs = 10,
verbose = 1)
dbn.fit(trainX, trainY)
print "escape time : ", round(time()-t0, 3), "s"
In [221]:
# compute the predictions for the test data and show a classification report
preds = dbn.predict(testX)
print "accuracy score : %s" %(accuracy_score(testY, preds))
print "classification report : "
print classification_report(testY, preds)
print "confusion matrix : "
print confusion_matrix(testY, preds)
In [1]:
# MNIST TEST
In [4]:
dataset = datasets.fetch_mldata("MNIST Original")
# scale the data to the range [0, 1] and then construct the training
# and testing splits
(trainX, testX, trainY, testY) = train_test_split(dataset.data / 255.0, dataset.target.astype("int0"), test_size = 0.33)
In [16]:
# train the Deep Belief Network with 784 input units (the flattened,
# 28x28 grayscale image), 300 hidden units, 10 output units (one for
# each possible output classification, which are the digits 1-10)
dbn = DBN(
[trainX.shape[1], 300, 10],
learn_rates = 0.01,
learn_rate_decays = 0.9,
epochs = 1000,
verbose = 1)
dbn.fit(trainX, trainY)
# compute the predictions for the test data and show a classification report
preds = dbn.predict(testX)
print "accuracy score : %s" %(accuracy_score(testY, preds))
print "classification report : "
print classification_report(testY, preds)
print "confusion matrix : "
print confusion_matrix(testY, preds)
dbn = DBN( [trainX.shape[1], 300, 10], learn_rates = 0.3, learn_rate_decays = 0.9, epochs = 10, verbose = 1) accuracy score : 0.977142857143
dbn = DBN( [trainX.shape[1], 300, 10], learn_rates = 0.3, learn_rate_decays = 0.9, epochs = 20, verbose = 1) accuracy score : 0.980779220779
dbn = DBN( [trainX.shape[1], 300, 10], learn_rates = 0.3, learn_rate_decays = 0.9, epochs = 30, verbose = 1) accuracy score : 0.981082251082
dbn = DBN( [trainX.shape[1], 300, 10], learn_rates = 0.4, learn_rate_decays = 0.9, epochs = 20, verbose = 1) accuracy score : 0.979047619048
dbn = DBN( [trainX.shape[1], 300, 10], learn_rates = 0.2, learn_rate_decays = 0.9, epochs = 20, verbose = 1) accuracy score : 0.981038961039
dbn = DBN( [trainX.shape[1], 300, 10], learn_rates = 0.5, learn_rate_decays = 0.9, epochs = 20, verbose = 1) accuracy score : 0.971601731602
dbn = DBN( [trainX.shape[1], 400, 10], learn_rates = 0.3, learn_rate_decays = 0.9, epochs = 20, verbose = 1) accuracy score : 0.981948051948
dbn = DBN( [trainX.shape[1], 500, 10], learn_rates = 0.3, learn_rate_decays = 0.9, epochs = 20, verbose = 1) accuracy score : 0.981255411255
dbn = DBN( [trainX.shape[1], 300, 10], learn_rates = 0.001, learn_rate_decays = 0.9, epochs = 20, verbose = 1) accuracy score : 0.923722943723
dbn = DBN( [trainX.shape[1], 300, 10], learn_rates = 0.01, learn_rate_decays = 0.9, epochs = 100, verbose = 1) accuracy score : 0.973246753247
dbn = DBN( [trainX.shape[1], 300, 10], learn_rates = 0.01, learn_rate_decays = 0.9, epochs = 1000, verbose = 1) accuracy score : 0.97354978355