yinguobing / cnn-facial-landmark

Training code for facial landmark detection based on deep convolutional neural network.
MIT License
626 stars 182 forks source link

Change the tf.metric.accuracy #85

Open abdou31 opened 4 years ago

abdou31 commented 4 years ago

Hello Yin, I have your old project before that you have updated it. I would like to evaluate my model but I find a problem that the accuracy doesn't meet the real accuracy and this due to function used tf.metrics.accuracy that should be used only with classification problem, and for this regression linear problem ( output gives landmarks), I should use the mse ( Mean squard error).

I see that you have updated the code and you change this function, Unfortunately, I don't have the last update ( last project), I have the old and I can't change it for now due to some reasons of studies.

I would like to tell me , how can I change the accuracy calculation from tf.metrics.accuracy to tf.metrics.root_mean_squard_error

I have tried changing some lines in the old script landmark.py but I have get some errors:

 # Create a metric.
    rmse_metrics = tf.metrics.root_mean_squared_error(
        labels=label_tensor,
        predictions=logits)
    metrics = {'eval_mse': rmse_metrics}

    # A tensor for metric logging
    tf.identity(rmse_metrics[1], name='root_mean_squared_error')
    tf.summary.scalar('root_mean_squared_error', rmse_metrics[1])

    # Generate a summary node for the images
    tf.summary.image('images', features, max_outputs=6)

    return tf.estimator.EstimatorSpec(
        mode=mode,
        predictions=logits,
        loss=loss,
        train_op=train_op,
        eval_metric_ops=metrics
    )

"""
    # Add evaluation metrics (for EVAL mode)
    eval_metric_ops = {
        "accuracy": tf.metrics.accuracy(
            labels=label_tensor,
            predictions=logits)}
    return tf.estimator.EstimatorSpec(
        mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)

"""
def _eval_input_fn():
    """Function for evaluating."""
    return input_fn(
        record_file="./validiris.record",
        batch_size=2,
        num_epochs=1,
        shuffle=False)

def main(unused_argv):
    """MAIN"""
    # Create the Estimator
    estimator = tf.estimator.Estimator(
        model_fn=cnn_model_fn, model_dir="./irismodel")

    # Choose mode between Train, Evaluate and Predict
    mode_dict = {
        'train': tf.estimator.ModeKeys.TRAIN,
        'eval': tf.estimator.ModeKeys.EVAL,
        'predict': tf.estimator.ModeKeys.PREDICT
    }

    mode = mode_dict['eval']

    if mode == tf.estimator.ModeKeys.TRAIN:
        estimator.train(input_fn=_train_input_fn, steps=200000)

        # Export result as SavedModel.
        estimator.export_savedmodel('./saved_model', serving_input_receiver_fn)

    elif mode == tf.estimator.ModeKeys.EVAL:
        evaluation = estimator.evaluate(input_fn=_eval_input_fn)
        print(evaluation)

That was the errors that I got:

AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 562, in make_tensor_proto "supported type." % (type(values), values)) TypeError: Failed to convert object of type <class 'dict'> to Tensor. Contents: {'x': <tf.Tensor 'IteratorGetNext:1' shape=(?, 112, 112, 3) dtype=uint8>, 'name': <tf.Tensor 'IteratorGetNext:0' shape=(?,) dtype=string>}. Consider casting elements to a supported type.

How can I solve those errors? How can I get the real accuracy of my trained model?

yinguobing commented 4 years ago

The old project has some issues and that's the why we should update to the latest.

abdou31 commented 4 years ago

I have tested your new project that force you to train your model to evaluate it. I get other value of accuracy but also is too bad.