如何在Tensorflow C ++中使用3D-CNN为视频分类提供输入?

时间:2019-07-08 09:36:39

标签: c++ tensorflow

我想使用3DCNN在tensorflow c ++ API中编写代码。我已经用python训练了模型并冻结了模型。现在我想用C ++进行预测。我找不到在C ++中提供视频输入的任何示例。

用于预测的python代码为:

import tensorflow as tf
import single_input_data
import single_c3d_model
import time
import numpy as np

flags = tf.app.flags
flags.DEFINE_integer('batch_size', 10 , 'Batch size.')
FLAGS = flags.FLAGS

def placeholder_inputs(batch_size):
  images_placeholder = tf.placeholder(tf.float32, shape=(batch_size,
                                                         single_c3d_model.NUM_FRAMES_PER_CLIP,
                                                         single_c3d_model.CROP_SIZE,
                                                         single_c3d_model.CROP_SIZE,
                                                         single_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: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():
  frozen_graph = "./pbmodels/3dcnn_frozen.pb"
   test_list_file = './test.list'
  num_test_videos = len(list(open(test_list_file,'r')))
  all_steps = int((num_test_videos - 1) / (FLAGS.batch_size) + 1)
  print("Number of test videos={}".format(num_test_videos))
  start_time=0
  end_time=0

    graph_def = tf.GraphDef()
    with tf.gfile.GFile(frozen_graph, "rb") as f:
      graph_def.ParseFromString(f.read())

    images_placeholder, labels_placeholder = placeholder_inputs(FLAGS.batch_size)
    tf.import_graph_def(graph_def,{"images_placeholder":images_placeholder},name="")
    #tf.import_graph_def(graph_def,name="",input_map={images_placeholder:test_images,labels_placeholder: test_labels})

    output_tt = tf.get_default_graph().get_tensor_by_name("sm_outt:0")
    print(output_tt)

    bufsize = 0
    write_file = open("predict_pb.txt", "w+", bufsize)
    next_start_pos = 0
    correct_pred_counter=0

    start_time = time.time()
    print("Start time: ",start_time)
    for step in xrange(all_steps):
      test_images, test_labels, next_start_pos, _, valid_len = \
                single_input_data.read_clip_and_label(
                        test_list_file,
                        FLAGS.batch_size,
                        start_pos=next_start_pos
 )
      print(test_images.shape, test_images.dtype)
      print(images_placeholder.get_shape(), images_placeholder.dtype)

      predict_score = sess.run(output_tt, feed_dict={images_placeholder:test_images,labels_placeholder: test_labels})
      avg_predict_score_true=0.0
      avg_predict_score_predicted_label=0.0

      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]))
        if(str(true_label[0])==str(top1_predicted_label)):
          correct_pred_counter=correct_pred_counter+1
      end_time=time.time()
  write_file.close()
  print("total time taken: ", end_time-start_time)
  print("done")


def main(_):
  run_test()

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


single_C3d_model.py是神经网络模型。我不知道如何在C ++ API中提供这些输入。有人,请告诉我如何在tensorflow C ++ API中输入一系列帧。

0 个答案:

没有答案