#!/usr/bin/env python
# encoding: utf-8

"""
evaluate.py

Created by Shuailong on 2016-12-2.

Evaluate model accuracy on test set.

"""

from __future__ import print_function

from time import time
from keras.models import load_model
import os

from utils import true_accuracy
from dataset import get_data
from train import MODEL_FILE, MODEL_DIR
from train import data_generator


def main():
    start_time = time()
    print('\nGetting data...')
    data = get_data(force=False)
    X_test = data['X_test']
    X_test_feats = data['X_test_feats']
    y_test = data['y_test']
    tag_size = len(data['tag_index'])

    print('\nLoading models...')
    model = load_model(os.path.join(MODEL_DIR, MODEL_FILE), custom_objects={'true_accuracy': true_accuracy})

    print('\nEvaluating...')
    _, true_acc = model.evaluate_generator(data_generator(X_test, X_test_feats, y_test, tag_size),
                                           val_samples=len(X_test))

    print('Test accuracy: {}.'.format(true_acc))
    seconds = time() - start_time
    minutes = seconds / 60
    print('[Finished in {} seconds ({} minutes)]'.format(str(round(seconds, 1)),
                                                         str(round(minutes, 1))))

if __name__ == '__main__':
    main()

[((410, 416), 'time.time', 'time', ([], {}), '()\n', (414, 416), False, 'from time import time\n'), ((459, 480), 'dataset.get_data', 'get_data', ([], {'force': '(False)'}), '(force=False)\n', (467, 480), False, 'from dataset import get_data\n'), ((672, 707), 'os.path.join', 'os.path.join', (['MODEL_DIR', 'MODEL_FILE'], {}), '(MODEL_DIR, MODEL_FILE)\n', (684, 707), False, 'import os\n'), ((831, 885), 'train.data_generator', 'data_generator', (['X_test', 'X_test_feats', 'y_test', 'tag_size'], {}), '(X_test, X_test_feats, y_test, tag_size)\n', (845, 885), False, 'from train import data_generator\n'), ((1019, 1025), 'time.time', 'time', ([], {}), '()\n', (1023, 1025), False, 'from time import time\n')]