# -*- coding: utf-8 -*-
"""
Created on Sat May  4 15:21:30 2019

@author: Administrator
"""


from keras.models import Sequential
from sklearn.externals import joblib
from keras.layers import Convolution2D
from keras.layers.core import Dropout
from keras.layers import MaxPooling2D
from keras.utils import to_categorical
from keras.optimizers import Adam
from keras.layers import Flatten

from keras.layers import Dense
from keras.preprocessing.image import ImageDataGenerator
from keras.layers.normalization import BatchNormalization
model = Sequential()
model.add(Convolution2D(64, kernel_size=(3, 3), strides=(1, 1),padding="same",
                input_shape=(32,32,3),  activation='relu', data_format='channels_last'
                 ))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(4, 4)))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dense(units =25,activation='softmax'))

model.add(BatchNormalization(axis=1))

model.add(Dropout(0.25))
EPOCHS = 75
INIT_LR = 1e-3


model.compile(loss='binary_crossentropy',optimizer='sgd',metrics=['accuracy'])

train_datagen = ImageDataGenerator(rescale = 1./255,

                                   shear_range = 0.2,
                                   zoom_range = 0.5,
                                 horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('C:\\Users\\Administrator\\training',
                                                  target_size = (32, 32),
                                                  batch_size = 256,

                                                  class_mode = 'categorical')


test_set = test_datagen.flow_from_directory('C:\\Users\\Administrator\\test',
                                              target_size = (32, 32),

                                                batch_size = 256,

                                                class_mode = 'categorical')


model=model.fit_generator(training_set,

                                           samples_per_epoch = 7022,

                                           nb_epoch = 50,

                                           validation_data = test_set,

                                           nb_val_samples = 2342)
filename = 'finalized_model2.sav'
joblib.dump(model, filename)

# load the model from disk
model = joblib.load(open(filename, 'rb'))


#ploat the map
import matplotlib.pyplot as plt

train_loss=model.history['loss']
val_loss=model.history['val_loss']
train_acc=model.history['acc']
val_acc=model.history['val_acc']
xc=range(50)

plt.figure(1,figsize=(7,5))
plt.plot(xc,train_loss)
plt.plot(xc,val_loss)
plt.xlabel('num of Epochs')
plt.ylabel('loss')
plt.title('train_loss vs val_loss')
plt.grid(True)
plt.legend(['train','val'])

plt.style.use(['classic'])

plt.figure(2,figsize=(7,5))
plt.plot(xc,train_acc)
plt.plot(xc,val_acc)
plt.xlabel('num of Epochs')
plt.ylabel('accuracy')
plt.title('train_acc vs val_acc')
plt.grid(True)
plt.legend(['train','val'],loc=4)
#print plt.style.available # use bmh, classic,ggplot for big pictures
plt.style.use(['classic'])
