# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Trains and Evaluates the MNIST network using a feed dictionary."""
# pylint: disable=missing-docstring
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os.path
import time
from six.moves import xrange    # pylint: disable=redefined-builtin
import tensorflow as tf
import input_data
import c3d_model
import numpy as np

# Basic model parameters as external flags.
flags = tf.app.flags
gpu_num = 1


def placeholder_inputs(batch_size):
    """Generate placeholder variables to represent the input tensors.
    These placeholders are used as inputs by the rest of the model building
    code and will be fed from the downloaded data in the .run() loop, below.
    Args:
        batch_size: The batch size will be baked into both placeholders.
    Returns:
        images_placeholder: Images placeholder.
        labels_placeholder: Labels placeholder.
    """
    # Note that the shapes of the placeholders match the shapes of the full
    # image and label tensors, except the first dimension is now batch_size
    # rather than the full size of the train or test data sets.
    images_placeholder = tf.placeholder(tf.float32, shape=(batch_size,c3d_model.NUM_FRAMES_PER_CLIP,c3d_model.CROP_SIZE,
                                                           c3d_model.CROP_SIZE,c3d_model.CHANNELS))

    labels_placeholder = tf.placeholder(tf.int64, shape=(batch_size))
    return images_placeholder, labels_placeholder

def _variable_on_cpu(name, shape, initializer):
    #with tf.device('/cpu:%d' % cpu_id):
    with tf.device('/cpu:0'):
        var = tf.get_variable(name, shape, initializer=initializer)
    return var

def _variable_with_weight_decay(name, shape, stddev, wd):
    var = _variable_on_cpu(name, shape, tf.truncated_normal_initializer(stddev=stddev))
    if wd is not None:
        weight_decay = tf.nn.l2_loss(var) * wd
        tf.add_to_collection('losses', weight_decay)
    return var

def run_test(ds_dir, mean_file, model_name, test_list_file, batch_size):
    tf.reset_default_graph()
    try:
        FLAGS = flags.FLAGS
        FLAGS.batch_size = batch_size
    except:
        flags.DEFINE_integer('batch_size', batch_size, 'Batch size.')
        FLAGS = flags.FLAGS

    #model_name = "./models-5sec/c3d_ucf_model-4999"
    #model_name = "./models.5sec/c3d_ucf_model-75450"
    #model_name = "./models-1sec/c3d_ucf_model-4999"
    #model_name = "./models.5sec.summarized.1sec/c3d_ucf_model-4999"
    #model_name = "./models-multi-5sec-5sec_sum_1/c3d_ucf_model-4999"
    #model_name = "./models-multi-5-5sum1/c3d_ucf_model-9999"

    num_test_videos = len(list(open(test_list_file,'r')))
    print("Number of test videos={}".format(num_test_videos))

    # max_bt_sz = -1;min
    #
    # for factor in range(1, 31):
    #         if num_test_videos%factor==0:
    #                 max_bt_sz=factor
    # if max_bt_sz == 1:
    #         print("no good batchsize available, setting to 25")
    #         max_bt_sz = 20

    # FLAGS.batch_size = max_bt_sz
    # print("batch size:", FLAGS.batch_size)

    # Get the sets of images and labels for testing
    images_placeholder, labels_placeholder = placeholder_inputs(FLAGS.batch_size * gpu_num)

    with tf.variable_scope('var_name') as var_scope:
        weights = {
                        'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.04, 0.00),
                        'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.04, 0.00),
                        'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.04, 0.00),
                        'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.04, 0.00),
                        'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.04, 0.00),
                        'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.04, 0.00),
                        'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.04, 0.00),
                        'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.04, 0.00),
                        'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.04, 0.001),
                        'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.04, 0.002),
                        'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.04, 0.005)
                        }
        biases = {
                        'bc1': _variable_with_weight_decay('bc1', [64], 0.04, 0.0),
                        'bc2': _variable_with_weight_decay('bc2', [128], 0.04, 0.0),
                        'bc3a': _variable_with_weight_decay('bc3a', [256], 0.04, 0.0),
                        'bc3b': _variable_with_weight_decay('bc3b', [256], 0.04, 0.0),
                        'bc4a': _variable_with_weight_decay('bc4a', [512], 0.04, 0.0),
                        'bc4b': _variable_with_weight_decay('bc4b', [512], 0.04, 0.0),
                        'bc5a': _variable_with_weight_decay('bc5a', [512], 0.04, 0.0),
                        'bc5b': _variable_with_weight_decay('bc5b', [512], 0.04, 0.0),
                        'bd1': _variable_with_weight_decay('bd1', [4096], 0.04, 0.0),
                        'bd2': _variable_with_weight_decay('bd2', [4096], 0.04, 0.0),
                        'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.04, 0.0),
                        }

    logits = []

    for gpu_index in range(0, gpu_num):
        with tf.device('/gpu:%d' % gpu_index):
            logit = c3d_model.inference_c3d(images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1)
                                                                                                                                                                * FLAGS.batch_size,:,:,:,:],
                                                                            0,
                                                                            FLAGS.batch_size,
                                                                            weights,
                                                                            biases)
            logits.append(logit)

    logits = tf.concat(logits,0)
    norm_score = tf.nn.softmax(logits)

    saver = tf.train.Saver()
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))

    init = tf.global_variables_initializer()
    sess.run(init)

    # Restoring a saved model.
    if not model_name.__contains__(".meta"):
        saver = tf.train.import_meta_graph(model_name+'.meta')
    else:
        # saver = tf.train.import_meta_graph(model_name)
        var_list = [v for v in tf.trainable_variables()]
        saver = tf.train.Saver(weights.values() + biases.values())

    saver.restore(sess, model_name)

    # And then after everything is built, start the testing loop.
    bufsize = 0
    write_file = open("predict_ret.txt", "w+", bufsize)
    next_start_pos = 0
    all_steps = int((num_test_videos - 1) / (FLAGS.batch_size * gpu_num) + 1)

    print ("num_test_videos, batch_size, gpu_num,all steps", num_test_videos, FLAGS.batch_size, gpu_num, all_steps)

    total_testing_duration = 0

    for step in range(all_steps):
        # Fill a feed dictionary with the actual set of images and labels
        # for this particular testing step.
        start_time = time.time()
# try:
        test_images, test_labels, next_start_pos, _, valid_len = \
                        input_data.read_clip_and_label(
                                        ds_dir,
                                        mean_file,
                                        test_list_file,
                                        FLAGS.batch_size * gpu_num,
                                        start_pos=next_start_pos,
                                        num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP
                                        )
# except:
#         print("exception occured loading at step:", step)
        # try:
        predict_score = norm_score.eval(
                        session=sess,
                        feed_dict={images_placeholder: test_images}
                        )
        # except:
                # print("exception occured prediction at step:", step)

        duration = time.time() - start_time
        print('Step %d: %.3f sec' % (step, duration), 'next start index:', next_start_pos)
        total_testing_duration += duration

# try:
        for i in range(0, valid_len):
            true_label = test_labels[i],
            top1_predicted_label = np.argmax(predict_score[i])

            # Write results: true label, class prob for true label, predicted label, class prob for predicted label
            write_file.write('{}, {}, {}, {}\n'.format(
                            true_label[0],
                            predict_score[i][true_label],
                            top1_predicted_label,
                            predict_score[i][top1_predicted_label]))
# except:
#         print ("exception occured saving predictions at step:", step)
        # break # test only 1 batch

    print('Prediction time taken =', total_testing_duration)

    import datetime
    now = datetime.datetime.now()

    with open('stats.txt', 'a') as f:
        f.write(now.strftime("%Y-%m-%d %H:%M\n"))
        f.write(" testing time:"+ str(total_testing_duration) + "\n")

    write_file.close()
    print("done")

import sys

def main(_):
    # run_test(sys.argv[1])
    ds_dir = "/home/bassel/data/office-actions/office_actions_19/short_clips/resized_frms"
    mean_file = "../c3d_data_preprocessing/oa_kinetics_calculated_mean.npy"
    model_name = "c3d_ucf_model-14698"
    testing_file = ""
    TESTING_BATCH_SIZE = 16
    run_test(ds_dir, mean_file, "model/" + model_name, testing_file, TESTING_BATCH_SIZE)


if __name__ == '__main__':
    tf.app.run()
