#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar  8 21:34:22 2020

@author: john
"""
# import the necessary packages
 


from keras.preprocessing.image import ImageDataGenerator
from keras.applications import VGG16
from keras.layers.core import Dropout
from keras.layers.core import Flatten
from keras.layers.core import Dense
from keras.layers import Input
from keras import models
from keras.optimizers import SGD
from tensorflow.python.keras.layers import Lambda
import numpy as np
import pandas as pd
#from keras.applications.vgg16 import VGG16
vgg_conv = VGG16(weights='imagenet', include_top=False, input_shape=(224,224,3))
for layer in vgg_conv.layers[:-4]:
    layer.trainable = False
 
# Check the trainable status of the individual layers
for layer in vgg_conv.layers:
    print(layer, layer.trainable)

# place the head FC model on top of the base model (this will become
# the actual model we will train)
# Create the model
model = models.Sequential()

# Add the vgg convolutional base model
model.add(vgg_conv)
 
# Add new layers
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(40, activation='softmax'))
model.load_weights('Single_frame_weights_10_epochs.h5')
print(model.summary())


sgd = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)

## (4) Compile 
model.compile(loss='mean_squared_error', optimizer=sgd,\
 metrics=['accuracy'])

test_dir = '/home/Test/'

test_datagen = ImageDataGenerator(rescale=1./255)

test_generator = test_datagen.flow_from_directory(directory=test_dir,
    target_size=(224, 224),
    color_mode="rgb",
    batch_size=32,
    class_mode="categorical",
    shuffle=False)

STEP_SIZE_TEST=test_generator.n//test_generator.batch_size

# tow ways for testing the network

# mehod 1
pred=model.predict_generator(test_generator,
steps=STEP_SIZE_TEST,
verbose=1)
predicted_class_indices=np.argmax(pred,axis=1)
labels = (train_generator.class_indices)
labels = dict((v,k) for k,v in labels.items())
predictions = [labels[k] for k in predicted_class_indices]

#method 2
loss, acc = model.evaluate_generator(test_generator, steps=STEP_SIZE_TEST, verbose=0)
print(acc)
print(loss)

