def evaluate():
    g = tf.Graph()
    with g.as_default():
        # load data get iterator
        data_loader = data_utils_mean.DataLoader(SEQUENCE_LENGTH, BATCH_SIZE,
                                                 NUM_EPOCHS)
        iterator = data_loader.load_data(VALID_TFR_PATH, False)
        frameNo, image, label = iterator.get_next()
        # define model graph
        # VGG FACE network
        VGGFace_network = vgg_face.VGGFace(SEQUENCE_LENGTH * BATCH_SIZE)
        image_batch = tf.reshape(image, [-1, 96, 96, 3])
        VGGFace_network.setup(
            image_batch
        )  # image_batch is a tensor of shape (batch_size*seq_length,image_dim,image_dim,3)
        face_output = VGGFace_network.get_face_fc0()
        # RNN part
        rnn_in = reshape_to_rnn(face_output)
        prediction = models.get_prediction_indRNN(rnn_in)
        prediction = tf.reshape(prediction, [BATCH_SIZE, SEQUENCE_LENGTH, 2])
        label_batch = tf.reshape(label, [BATCH_SIZE, SEQUENCE_LENGTH, 2])

        # Computing MSE and Concordance values, and adding them to summary
        names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({
            'eval/mse_valence':
            slim.metrics.streaming_mean_squared_error(prediction[:, :, 0],
                                                      label_batch[:, :, 0]),
            'eval/mse_arousal':
            slim.metrics.streaming_mean_squared_error(prediction[:, :, 1],
                                                      label_batch[:, :, 1]),
        })

        summary_ops = []
        conc_total = 0
        mse_total = 0
        for i, name in enumerate(['valence', 'arousal']):
            with tf.name_scope(name) as scope:
                concordance_cc2, values, updates = metrics.concordance_cc2(
                    tf.reshape(prediction[:, :, i], [-1]),
                    tf.reshape(label_batch[:, :, i], [-1]))
                for n, v in updates.items():
                    names_to_updates[n + '/' + name] = v
            op = tf.summary.scalar('eval/concordance_' + name, concordance_cc2)
            op = tf.Print(op, [concordance_cc2], 'eval/concordance_' + name)
            summary_ops.append(op)

            mse_eval = 'eval/mse_' + name
            op = tf.summary.scalar(mse_eval, names_to_values[mse_eval])
            op = tf.Print(op, [names_to_values[mse_eval]], mse_eval)
            summary_ops.append(op)

            mse_total += names_to_values[mse_eval]
            conc_total += concordance_cc2
        conc_total = conc_total / 2
        mse_total = mse_total / 2

        op = tf.summary.scalar('eval/concordance_total', conc_total)
        op = tf.Print(op, [conc_total], 'eval/concordance_total')
        summary_ops.append(op)

        op = tf.summary.scalar('eval/mse_total', mse_total)
        op = tf.Print(op, [mse_total], 'eval/mse_total')
        summary_ops.append(op)

        num_batches = int(NUM_BATCHES)
        loggingTF.set_verbosity(1)
        if not os.path.exists(SUMMARY_PATH):
            os.makedirs(SUMMARY_PATH)
        # always check latest ckpt and wait for next.

        slim.evaluation.evaluation_loop(
            '',
            CHECK_POINT_DIR,
            SUMMARY_PATH,
            num_evals=num_batches,
            eval_op=list(names_to_updates.values()),
            summary_op=tf.summary.merge(summary_ops),
            eval_interval_secs=EVAL_INTERVAL_SECS,
        )
def evaluate():
    g = tf.Graph()
    with g.as_default():
        # load data get iterator
        data_loader = data_utils_mean.DataLoader(SEQUENCE_LENGTH, BATCH_SIZE,
                                                 NUM_EPOCHS)
        iterator = data_loader.load_data(TEST_TFR_PATH, False)
        frameNo, image, label = iterator.get_next()
        # define model graph

        image_batch = tf.reshape(image, [-1, 96, 96, 3])
        # Construct and return the model
        network_fn = nets_factory.get_network_fn(name='densenet161',
                                                 num_classes=None,
                                                 weight_decay=0.00004,
                                                 data_format='NHWC',
                                                 is_training=False)
        face_output, _ = network_fn(image_batch)
        # RNN part
        rnn_in = reshape_to_rnn(face_output)
        prediction = models.get_prediction_atten(rnn_in)
        prediction = tf.reshape(prediction, [BATCH_SIZE, SEQUENCE_LENGTH, 2])
        label_batch = tf.reshape(label, [BATCH_SIZE, SEQUENCE_LENGTH, 2])

        # Computing MSE and Concordance values, and adding them to summary
        names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({
            'eval/mse_valence':
            slim.metrics.streaming_mean_squared_error(prediction[:, :, 0],
                                                      label_batch[:, :, 0]),
            'eval/mse_arousal':
            slim.metrics.streaming_mean_squared_error(prediction[:, :, 1],
                                                      label_batch[:, :, 1]),
        })

        summary_ops = []
        conc_total = 0
        mse_total = 0
        for i, name in enumerate(['valence', 'arousal']):
            with tf.name_scope(name) as scope:
                concordance_cc2, values, updates = metrics.concordance_cc2(
                    tf.reshape(prediction[:, :, i], [-1]),
                    tf.reshape(label_batch[:, :, i], [-1]))
                for n, v in updates.items():
                    names_to_updates[n + '/' + name] = v
            op = tf.summary.scalar('eval/concordance_' + name, concordance_cc2)
            op = tf.Print(op, [concordance_cc2], 'eval/concordance_' + name)
            summary_ops.append(op)

            mse_eval = 'eval/mse_' + name
            op = tf.summary.scalar(mse_eval, names_to_values[mse_eval])
            op = tf.Print(op, [names_to_values[mse_eval]], mse_eval)
            summary_ops.append(op)

            mse_total += names_to_values[mse_eval]
            conc_total += concordance_cc2
        conc_total = conc_total / 2
        mse_total = mse_total / 2

        op = tf.summary.scalar('eval/concordance_total', conc_total)
        op = tf.Print(op, [conc_total], 'eval/concordance_total')
        summary_ops.append(op)

        op = tf.summary.scalar('eval/mse_total', mse_total)
        op = tf.Print(op, [mse_total], 'eval/mse_total')
        summary_ops.append(op)

        num_batches = int(NUM_BATCHES)
        loggingTF.set_verbosity(1)
        if not os.path.exists(SUMMARY_PATH):
            os.makedirs(SUMMARY_PATH)
        slim.evaluation.evaluate_once(
            '',
            MODEL_PATH,
            SUMMARY_PATH,
            num_evals=num_batches,
            eval_op=list(names_to_updates.values()),
            summary_op=tf.summary.merge(summary_ops),
        )
Esempio n. 3
0
def evaluate(data_folder):

    g = tf.Graph()
    with g.as_default():

        # Load dataset.
        frames, audio, ground_truth, _ = data_provider.get_split(
            data_folder, False, FLAGS.portion, FLAGS.batch_size,
            FLAGS.seq_length)

        # Define model graph.
        with slim.arg_scope([slim.batch_norm, slim.layers.dropout],
                            is_training=False):
            with slim.arg_scope(
                    slim.nets.resnet_utils.resnet_arg_scope(
                        is_training=False)):
                prediction = models.get_model(FLAGS.model)(
                    frames, audio, hidden_units=FLAGS.hidden_units)

        # Computing MSE and Concordance values, and adding them to summary
        names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({
            'eval/mse_arousal':
            slim.metrics.streaming_mean_squared_error(prediction[:, :, 0],
                                                      ground_truth[:, :, 0]),
            'eval/mse_valence':
            slim.metrics.streaming_mean_squared_error(prediction[:, :, 1],
                                                      ground_truth[:, :, 1]),
        })

        summary_ops = []

        conc_total = 0
        mse_total = 0
        for i, name in enumerate(['arousal', 'valence']):
            with tf.name_scope(name) as scope:
                concordance_cc2, values, updates = metrics.concordance_cc2(
                    tf.reshape(prediction[:, :, i], [-1]),
                    tf.reshape(ground_truth[:, :, i], [-1]))

                for n, v in updates.items():
                    names_to_updates[n + '/' + name] = v

            op = tf.summary.scalar('eval/concordance_' + name, concordance_cc2)
            op = tf.Print(op, [concordance_cc2], 'eval/concordance_' + name)
            summary_ops.append(op)

            mse_eval = 'eval/mse_' + name
            op = tf.summary.scalar(mse_eval, names_to_values[mse_eval])
            op = tf.Print(op, [names_to_values[mse_eval]], mse_eval)
            summary_ops.append(op)

            mse_total += names_to_values[mse_eval]
            conc_total += concordance_cc2

        conc_total = conc_total / 2
        mse_total = mse_total / 2

        op = tf.summary.scalar('eval/concordance_total', conc_total)
        op = tf.Print(op, [conc_total], 'eval/concordance_total')
        summary_ops.append(op)

        op = tf.summary.scalar('eval/mse_total', mse_total)
        op = tf.Print(op, [mse_total], 'eval/mse_total')
        summary_ops.append(op)

        num_examples = FLAGS.num_examples
        num_batches = int(num_examples / (FLAGS.batch_size * FLAGS.seq_length))
        logging.set_verbosity(1)

        # Setup the global step.
        eval_interval_secs = FLAGS.eval_interval_secs  # How often to run the evaluation.
        slim.evaluation.evaluation_loop(
            '',
            FLAGS.checkpoint_dir,
            FLAGS.log_dir,
            num_evals=num_batches,
            eval_op=list(names_to_updates.values()),
            summary_op=tf.summary.merge(summary_ops),
            eval_interval_secs=eval_interval_secs)