Пример #1
0
def run_training():
    # Get the sets of images and labels for training, validation, and
    # Tell TensorFlow that the model will be built into the default Graph.
    pre_model_save_dir = "./models/rgb_imagenet_10000_6_64_0.0001_decay"
    test_list_file = '../../list/hmdb_list/test_flow.list'
    file = list(open(test_list_file, 'r'))
    num_test_videos = len(file)
    print("Number of test videos={}".format(num_test_videos))
    with tf.Graph().as_default():
        rgb_images_placeholder, _, labels_placeholder, is_training = placeholder_inputs(
            FLAGS.batch_size * gpu_num, FLAGS.num_frame_per_clib,
            FLAGS.crop_size, FLAGS.rgb_channels)

        with tf.variable_scope('RGB'):
            logit, _ = InceptionI3d(num_classes=FLAGS.classics,
                                    spatial_squeeze=True,
                                    final_endpoint='Logits',
                                    name='inception_i3d')(
                                        rgb_images_placeholder, is_training)
        norm_score = tf.nn.softmax(logit)

        # Create a saver for writing training checkpoints.
        saver = tf.train.Saver()
        init = tf.global_variables_initializer()

        # Create a session for running Ops on the Graph.
        sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
        sess.run(init)

    ckpt = tf.train.get_checkpoint_state(pre_model_save_dir)
    if ckpt and ckpt.model_checkpoint_path:
        print("loading checkpoint %s,waiting......" %
              ckpt.model_checkpoint_path)
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("load complete!")

    all_steps = num_test_videos
    top1_list = []
    for step in xrange(all_steps):
        start_time = time.time()
        s_index = 0
        predicts = []
        top1 = False
        while True:
            val_images, _, val_labels, s_index, is_end = input_test.read_clip_and_label(
                filename=file[step],
                batch_size=FLAGS.batch_size * gpu_num,
                s_index=s_index,
                num_frames_per_clip=FLAGS.num_frame_per_clib,
                crop_size=FLAGS.crop_size,
            )
            predict = sess.run(norm_score,
                               feed_dict={
                                   rgb_images_placeholder: val_images,
                                   labels_placeholder: val_labels,
                                   is_training: False
                               })
            predicts.append(
                np.array(predict).astype(np.float32).reshape(FLAGS.classics))
            if is_end:
                avg_pre = np.mean(predicts, axis=0).tolist()
                top1 = (avg_pre.index(max(avg_pre)) == val_labels)
                top1_list.append(top1)
                break
        duration = time.time() - start_time
        print('TOP_1_ACC in test: %f , time use: %.3f' % (top1, duration))
    print(len(top1_list))
    print('TOP_1_ACC in test: %f' % np.mean(top1_list))
    print("done")
def run_training():
    # Get the sets of images and labels for training, validation, and
    # Tell TensorFlow that the model will be built into the default Graph.
    rgb_pre_model_save_dir = "/media/senilab/DATA/Master/I3D-Tensorflow/experiments/ucf-101/models/rgb_imagenet_30000_101_5_64_0.0001_decay"
    flow_pre_model_save_dir = "/media/senilab/DATA/Master/I3D-Tensorflow/experiments/ucf-101/models/flow_imagenet_101_20000_5_64_0.0001_decay"
    test_list_file = '/media/senilab/DATA/Master/I3D-Tensorflow/list/ucf_list/test.list'
    file = list(open(test_list_file, 'r'))
    num_test_videos = len(file)
    print("Number of test videos={}".format(num_test_videos))
    with tf.Graph().as_default():
        rgb_images_placeholder, flow_images_placeholder, labels_placeholder, is_training = placeholder_inputs(
            FLAGS.batch_size * gpu_num, FLAGS.num_frame_per_clib,
            FLAGS.crop_size, FLAGS.rgb_channels)

        with tf.variable_scope('RGB'):
            rgb_logit, _ = InceptionI3d(
                num_classes=FLAGS.classics,
                spatial_squeeze=True,
                final_endpoint='Logits',
                name='inception_i3d')(rgb_images_placeholder, is_training)
        with tf.variable_scope('Flow'):
            flow_logit, _ = InceptionI3d(
                num_classes=FLAGS.classics,
                spatial_squeeze=True,
                final_endpoint='Logits',
                name='inception_i3d')(flow_images_placeholder, is_training)
        norm_score = tf.nn.softmax(tf.add(rgb_logit, flow_logit))

        # Create a saver for writing training checkpoints.
        rgb_variable_map = {}
        flow_variable_map = {}
        for variable in tf.global_variables():
            if variable.name.split('/')[
                    0] == 'RGB' and 'Adam' not in variable.name.split('/')[-1]:
                rgb_variable_map[variable.name.replace(':0', '')] = variable
        rgb_saver = tf.train.Saver(var_list=rgb_variable_map, reshape=True)

        for variable in tf.global_variables():
            if variable.name.split(
                    '/')[0] == 'Flow' and 'Adam' not in variable.name.split(
                        '/')[-1]:
                flow_variable_map[variable.name.replace(':0', '')] = variable
        flow_saver = tf.train.Saver(var_list=flow_variable_map, reshape=True)
        saver = tf.train.Saver()
        init = tf.global_variables_initializer()

        # Create a session for running Ops on the Graph
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        sess = tf.Session(config=config)
        sess.run(init)

    # load pre_train models
    ckpt = tf.train.get_checkpoint_state(rgb_pre_model_save_dir)
    if ckpt and ckpt.model_checkpoint_path:
        print("loading checkpoint %s,waiting......" %
              ckpt.model_checkpoint_path)
        rgb_saver.restore(sess, ckpt.model_checkpoint_path)
        print("load complete!")
    ckpt = tf.train.get_checkpoint_state(flow_pre_model_save_dir)
    if ckpt and ckpt.model_checkpoint_path:
        print("loading checkpoint %s,waiting......" %
              ckpt.model_checkpoint_path)
        flow_saver.restore(sess, ckpt.model_checkpoint_path)
        print("load complete!")

    all_steps = num_test_videos
    top1_list = []
    start_time_all = time.time()
    for step in xrange(all_steps):
        start_time = time.time()
        s_index = 0
        predicts = []
        top1 = False
        while True:
            rgb_images, flow_images, val_labels, s_index, is_end = input_test.read_clip_and_label(
                filename=file[step],
                batch_size=FLAGS.batch_size * gpu_num,
                s_index=s_index,
                num_frames_per_clip=FLAGS.num_frame_per_clib,
                crop_size=FLAGS.crop_size,
            )
            predict = sess.run(norm_score,
                               feed_dict={
                                   rgb_images_placeholder: rgb_images,
                                   flow_images_placeholder: flow_images,
                                   labels_placeholder: val_labels,
                                   is_training: False
                               })
            predicts.append(
                np.array(predict).astype(np.float32).reshape(FLAGS.classics))
            # print ('predict', predict)
            if is_end:
                avg_pre = np.mean(predicts, axis=0).tolist()
                # print ('avg_pred',avg_pre)
                print(avg_pre.index(max(avg_pre)))
                print('val_label', val_labels)
                top1 = (avg_pre.index(max(avg_pre)) == val_labels)
                top1_list.append(top1)
                break
        duration = time.time() - start_time
        print('TOP_1_ACC in test: %f , time use: %.3f' % (top1, duration))
    print(len(top1_list))
    dur_time_all = time.time() - start_time_all
    print('TOP_1_ACC in test_all: %f, time use: %.3f' %
          (np.mean(top1_list), dur_time_all))
    print("done")