import os
import sys
from keras.applications.vgg16 import VGG16
from keras.models import Sequential, Model
from keras.layers import Input, Activation, Dropout, Flatten, Dense
from keras.preprocessing import image
import numpy as np

# if len(sys.argv) != 2:
#     print("usage: python predict.py [filename]")
#     sys.exit(1)
#
# filename = sys.argv[1]
# print('input:', filename)

result_dir = 'results'

img_height, img_width = 150, 150
channels = 3

input_tensor = Input(shape=(img_height, img_width, channels))
vgg16_model = VGG16(include_top=False, weights='imagenet', input_tensor=input_tensor)

top_model = Sequential()
top_model.add(Flatten(input_shape=vgg16_model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(1, activation='sigmoid'))

model = Model(input=vgg16_model.input, output=top_model(vgg16_model.output))

model.load_weights(os.path.join(result_dir, 'finetuning.h5'))

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


def predict(filename):
    img = image.load_img(filename, target_size=(img_height, img_width))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)

    x = x / 255.0

    pred = model.predict(x)[0]
    return pred

num = 0
count = 0
error_list = []
for i in range(1,84):
    filename = './test_dog/dog%04d.jpg' % i
    pred = predict(filename)
    num += 1
    if pred >= 0.5:
        count += 1
    else:
        error_list.append(i)
print(count, num, count/num)
print(error_list)
