tfx icon indicating copy to clipboard operation
tfx copied to clipboard

Clarification on TFMA metric: multiClassConfusionMatrixAtThresholds

Open PunyPony opened this issue 6 years ago • 3 comments

First of all thank you guys for your amazing work ! I'm currently making a TFX pipeline that generate a model to deals with a multiclass image classification problem and I struggle to understand the MultiClassConfusionMatrixAtThresholds metric (especially the result) when used within the Evaluator component. I regret the lack of clear documentation about it.

What I did is that :

metrics = [
    tfma.metrics.MultiClassConfusionMatrixAtThresholds(name="multiClassConfusionMatrixAtThresholds")
]

metrics_specs = tfma.metrics.specs_from_metrics(metrics)

eval_config = tfma.EvalConfig(
    model_specs=[tfma.ModelSpec(label_key='label_xf', model_type='tf_lite')],
    slicing_specs=[tfma.SlicingSpec()],
    metrics_specs=metrics_specs
)

Evaluator(
    examples=transform.outputs['transformed_examples'],
    model=trainer.outputs['model'],
    baseline_model=model_resolver.outputs['model'],
    eval_config=eval_config,
)

My test set has 6 classes each classe have 12 pictures in it : Can you explain this result ? I may miss use your tool.

{'multiClassConfusionMatrixAtThresholds': {'matrices': [{'entries': [ {'numWeightedExamples': 66.0}, {'predictedClassId': 3, 'numWeightedExamples': 6.0}, {'actualClassId': 1, 'numWeightedExamples': 12.0}, {'actualClassId': 1, 'predictedClassId': 1, 'numWeightedExamples': 54.0}, {'actualClassId': 1, 'predictedClassId': 2, 'numWeightedExamples': 6.0}, {'actualClassId': 2, 'numWeightedExamples': 12.0}, {'actualClassId': 2, 'predictedClassId': 2, 'numWeightedExamples': 60.0}, {'actualClassId': 3, 'numWeightedExamples': 6.0}, {'actualClassId': 3, 'predictedClassId': 2, 'numWeightedExamples': 12.0}, {'actualClassId': 3, 'predictedClassId': 3, 'numWeightedExamples': 42.0}, {'actualClassId': 3, 'predictedClassId': 5, 'numWeightedExamples': 12.0}, {'actualClassId': 4, 'numWeightedExamples': 6.0}, {'actualClassId': 4, 'predictedClassId': 2, 'numWeightedExamples': 6.0}, {'actualClassId': 4, 'predictedClassId': 3, 'numWeightedExamples': 12.0}, {'actualClassId': 4, 'predictedClassId': 4, 'numWeightedExamples': 48.0}, {'actualClassId': 5, 'numWeightedExamples': 6.0}, {'actualClassId': 5, 'predictedClassId': 1, 'numWeightedExamples': 6.0}, {'actualClassId': 5, 'predictedClassId': 2, 'numWeightedExamples': 18.0}, {'actualClassId': 5, 'predictedClassId': 3, 'numWeightedExamples': 24.0}, {'actualClassId': 5, 'predictedClassId': 5, 'numWeightedExamples': 18.0} ]}]}}

use full links : https://www.tensorflow.org/tfx/model_analysis/api_docs/python/tfma/metrics/MultiClassConfusionMatrixAtThresholds https://github.com/tensorflow/tfx/blob/master/tfx/components/evaluator/executor.py

Thank you !

PunyPony avatar Nov 19 '20 09:11 PunyPony

We don't have display support in the metrics view for this metric yet, so you may be better off using tfma.metrics.MultiClassConfusionMatrixPlot and rendering with tfma.view.render_plot(...). The matrix tends to be used by advanced users that want to process the raw data. Basically it gives you the (weighted) count of examples that had the given combinations of the specified actual class and predicted class. A few notes:

  • There seems to be a bug in the code where it says it defaults to a threshold of 0.0 vs the 0.5 specified in the comments (you can fix this by passing a thresholds parameter - e.g. MultiClassConfusionMatrixAtThresholds(thresholds=[0.5], ...)
  • If the 'predictedClassId' or 'actualClassId' are 0, then they won't be displayed (it's just how proto3 to JSON works with "zero" values), so the first entry with numWeightedExamples set to 66 is for predictedClassId=0 and actualClassId=0.

mdreves avatar Nov 30 '20 02:11 mdreves

Ok, I was missing your last bullet point.

To sum it up juste to be sure :

The matrix is contructed as expected but the id 0 is missing, when rows do not sum to my_exemple_nb (72 in my case) in my case, add the missing quantity to the zero classe. In the case of the classe zero take into account : {'numWeightedExamples': 66.0}, {'predictedClassId': 3, 'numWeightedExamples': 6.0}

Big thanks your answer helped me a lot.

PunyPony avatar Nov 30 '20 12:11 PunyPony

Are you satisfied with the resolution of your issue? Yes No

google-ml-butler[bot] avatar Mar 04 '21 20:03 google-ml-butler[bot]