# coding:utf-8
__author__ = 'fz'
__date__ = '2017-05-23 17:35'

# coding: utf-8
# Simple CNN
import numpy
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Convolution2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from PIL import Image
from keras import backend as K

seed = 7
numpy.random.seed(seed)

# 加载数据
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# reshape to be [samples][channels][width][height]
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')

# normalize inputs from 0-255 to 0-1
X_train = X_train / 255
X_test = X_test / 255

# one hot encode outputs
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]

def load_testData():
    testImg = Image.open('testNum0.jpg')
    imgArr = numpy.asarray(testImg, dtype="float32")
    testData = numpy.empty((1, 1, 28, 28), dtype="float32")
    testData[0, :, :, :] = imgArr
    return testData

# define a simple CNN model
def baseline_model():
    # create model
    model = Sequential()
    K.set_image_dim_ordering('th')
    model.add(Convolution2D(32, 5, 5, border_mode='valid', input_shape=(1, 28, 28), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dense(num_classes, activation='softmax'))
    # Compile model
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model


# build the model
model = baseline_model()

# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), nb_epoch=1, batch_size=128, verbose=2)

testData = load_testData()
print(model.predict(testData))

# # Final evaluation of the model
# scores = model.evaluate(X_test, y_test, verbose=0)
# print("CNN Error: %.2f%%" % (100 - scores[1] * 100))