# MNIST handwritten digit classification
# Inspired from some Keras code
# Simplified version (without convolutions)
# Adapted by Jean-Pierre Briot
# 28/08/2018

# #2 Version
# Only one hidden layer
# No regularization
# Shorter training

# Patching the ssl certificate error...
import requests
requests.packages.urllib3.disable_warnings()

import ssl

try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    # Legacy Python that doesn't verify HTTPS certificates by default
    pass
else:
    # Handle target environment that doesn't support HTTPS verification
    ssl._create_default_https_context = _create_unverified_https_context

import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.utils import np_utils

import matplotlib.cm as cm
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
from matplotlib.path import Path
from matplotlib.patches import PathPatch

# Dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# fig, ax = plt.subplots()
# im = ax.imshow(X_train[0])
# plt.show()

# reshaping data into vectors of 28x28 (= 784) pixels vectors
X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')

# scaling the data to help with the training (divide by 255, because pixel = 1 byte = 2**8 = 256)
X_train /= 255
X_test /= 255

# number of labels/classes
number_classes = len(np.unique(y_train))

# one-hot encoding using keras' numpy-related utilities
n_classes = 10
Y_train = np_utils.to_categorical(y_train, n_classes)
Y_test = np_utils.to_categorical(y_test, n_classes)

# building a linear stack of layers with the sequential model
model = Sequential()
model.add(Dense(100,
	input_shape = (784, )))
model.add(Activation('sigmoid'))                            
model.add(Dense(number_classes))
model.add(Activation('softmax'))

# compiling the sequential model
model.compile(loss = 'categorical_crossentropy',
	metrics = ['accuracy'],
	optimizer = 'sgd')

# training the model and saving metrics in history
history = model.fit(X_train,
	Y_train,
	batch_size = 10,
	epochs = 5,
	verbose = 2,
	validation_data = (X_test, Y_test))

loss_and_metrics = model.evaluate(X_test, Y_test, verbose=2)

print("Test Loss", loss_and_metrics[0])
print("Test Accuracy", loss_and_metrics[1])

predicted_classes = model.predict_classes(X_test)

# see which we predicted correctly and which not
correct_indices = np.nonzero(predicted_classes == y_test)[0]
incorrect_indices = np.nonzero(predicted_classes != y_test)[0]
print()
print(len(correct_indices)," classified correctly")
print(len(incorrect_indices)," classified incorrectly")

