# MNIST handwritten digit classification
# Inspired from some Keras code
# Simplified version - using numerical encoding of the labels and not one-hot encoding
# Prediction and not classification
# Adapted by Jean-Pierre Briot
# 05/04/2019

# 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 training (grayscale (0, 255) -> divide by 255)
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
# Y_train = np_utils.to_categorical(y_train, number_classes)
# Y_test = np_utils.to_categorical(y_test, number_classes)

Y_train = y_train
Y_test = y_test

# building a linear stack of layers with the sequential model
model = Sequential()
model.add(Dense(512,
	input_shape = (784, )))
model.add(Activation('relu'))                            
model.add(Dropout(0.2))
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.2))
# model.add(Dense(number_classes))
# model.add(Activation('softmax'))
model.add(Dense(1))

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

# training the model and saving metrics in history
history = model.fit(X_train,
	Y_train,
	batch_size = 128,
	epochs = 1,
	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)

list_predictions = model.predict(X_test)

predicted_classes = []
for i in range(len(list_predictions)):
	predicted_classes.append(int(round(list_predictions[i][0])))

# 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")
