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
    num_test_videos = len(list(open(test_list_file, 'r')))
    print('Number of test videos={}'.format(num_test_videos))
    (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.0), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.04, 0.0), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.04, 0.0), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.04, 0.0), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.04, 0.0), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.04, 0.0), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.04, 0.0), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.04, 0.0), '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)
    if (not model_name.__contains__('.meta')):
        saver = tf.train.import_meta_graph((model_name + '.meta'))
    else:
        var_list = [v for v in tf.trainable_variables()]
        saver = tf.train.Saver((weights.values() + biases.values()))
    saver.restore(sess, model_name)
    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):
        start_time = time.time()
        (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)
        predict_score = norm_score.eval(session=sess, feed_dict={images_placeholder: test_images})
        duration = (time.time() - start_time)
        print(('Step %d: %.3f sec' % (step, duration)), 'next start index:', next_start_pos)
        total_testing_duration += duration
        for i in range(0, valid_len):
            true_label = (test_labels[i],)
            top1_predicted_label = np.argmax(predict_score[i])
            write_file.write('{}, {}, {}, {}\n'.format(true_label[0], predict_score[i][true_label], top1_predicted_label, predict_score[i][top1_predicted_label]))
    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')
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
    num_test_videos = len(list(open(test_list_file, 'r')))
    print('Number of test videos={}'.format(num_test_videos))
    (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.0), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.04, 0.0), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.04, 0.0), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.04, 0.0), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.04, 0.0), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.04, 0.0), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.04, 0.0), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.04, 0.0), '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)
    if (not model_name.__contains__('.meta')):
        saver = tf.train.import_meta_graph((model_name + '.meta'))
    else:
        var_list = [v for v in tf.trainable_variables()]
        saver = tf.train.Saver((weights.values() + biases.values()))
    saver.restore(sess, model_name)
    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):
        start_time = time.time()
        (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)
        predict_score = norm_score.eval(session=sess, feed_dict={images_placeholder: test_images})
        duration = (time.time() - start_time)
        print(('Step %d: %.3f sec' % (step, duration)), 'next start index:', next_start_pos)
        total_testing_duration += duration
        for i in range(0, valid_len):
            true_label = (test_labels[i],)
            top1_predicted_label = np.argmax(predict_score[i])
            write_file.write('{}, {}, {}, {}\n'.format(true_label[0], predict_score[i][true_label], top1_predicted_label, predict_score[i][top1_predicted_label]))
    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')