Model Evaluation API Examples

This demonstrates how to use the evaluate_model API.

Refer to the Model Evaluation guide for more details.

NOTES:

  • Click here: Open In Colab to run this example interactively in your browser

  • Refer to the Notebook Examples Guide for how to run this example locally in VSCode

Install MLTK Python Package

# Install the MLTK Python package (if necessary)
!pip install --upgrade silabs-mltk

Import Python Packages

# Import the necessary MLTK APIs
from mltk.core import evaluate_model

Example 1: Evaluate Classification .h5 Model

The following example evaluates the image_example1 model using the .h5 (i.e. non-quantized, float32 weights) model file.
This is a “classification” model, i.e. given an input, the model predicts to which “class” the input belongs.

The model archive is updated with the evaluation results.
Additionally, the show option displays interactive diagrams.

# Evaluate the .h5 image_example1 model and display the results
evaluation_results = evaluate_model('image_example1', show=True)
print(f'{evaluation_results}')
../../_images/e318fd96ab105b71353352cda490c75fe7309b560f24cd55ddfe24c925860f62.png ../../_images/70459d4b0929ead22e7734a304272ef09308ae6bfb6730bcbe1fb3c90a3a27d3.png ../../_images/caf76d05e1f2592e9133d2f28edf277bdc6650b7ef5cff771eeb58755d82fbc2.png ../../_images/91f870de0de3e5f2ce1935e620ae6a70f5c7a36606bc2aff410c04dd9e5e0215.png ../../_images/601e5b7f63091b274ffd16d2102ce1c619631e257d21dcd647a742f082622ebf.png ../../_images/93241ef2a244caa4139d639c4a3d4615f11567c8ede818c74382594d472524ed.png
Name: image_example1
Model Type: classification
Overall accuracy: 97.266%
Class accuracies:
- rock = 100.000%
- scissor = 97.674%
- paper = 93.151%
Average ROC AUC: 99.919%
Class ROC AUC:
- rock = 99.987%
- scissor = 99.891%
- paper = 99.880%

Example 2: Evaluate Classification .tflite Model

The following example evaluates the image_example1 model using the .tflite (i.e. quantized, int8 weights) model file.
This is a “classification” model, i.e. given an input, the model predicts to which “class” the input belongs.

The model archive is updated with the evaluation results.
Additionally, the show option displays interactive diagrams.

# Evaluate the .tflite image_example1 model and display the results
evaluation_results = evaluate_model('image_example1', tflite=True, show=True)
print(f'{evaluation_results}')
../../_images/00a9fd8285fa963f8fd702748f046df7fb51df91844c26e07a7d28304f407ac0.png ../../_images/c85bf5a1a5191d5bbe02f80ba85ac22cb5d59307807b9253a73df4a0fe720396.png ../../_images/64904128ae39e9df4a14d05f98c29168ca358a5cd3bca38110f6c6dbb4ed54fa.png ../../_images/b4af566bcfa8e710c8dbb061fa297b8d2de762b3351d2646afb6d8bd55e78500.png ../../_images/7a297eb51a81f23f9f979a1b6adafe6cfbc31d7775ba8c5afe42863bda391d45.png ../../_images/b235e89dc7d3d5b0d5ad2ebc5cfcde075c6bf2bfc99e17855e4e5529c8271d22.png
Name: image_example1
Model Type: classification
Overall accuracy: 96.484%
Class accuracies:
- rock = 100.000%
- scissor = 97.674%
- paper = 90.411%
Average ROC AUC: 99.893%
Class ROC AUC:
- rock = 99.981%
- paper = 99.865%
- scissor = 99.832%

Example 3: Evaluate Auto-Encoder .h5 Model

The following example evaluates the anomaly_detection model using the .h5 (i.e. non-quantized, float32 weights) model file.
This is an “auto-encoder” model, i.e. given an input, the model attempts to reconstruct the same input. The worse the reconstruction, the further the input is from what the model was trained against, this indicates that an anomaly may be detected.

The model archive is updated with the evaluation results.
Additionally, the show option displays interactive diagrams and the dump option dumps a comparison image between the input and reconstructed image.

# Evaluate the .h5 anomaly_detection model and display the results
evaluation_results = evaluate_model('anomaly_detection', show=True, dump=True)
print(f'{evaluation_results}')
../../_images/8a6da68e3200dc7ce25ab39a7f995cae7fb5febaad7b49aa9e7de19a55ab6faa.png ../../_images/77c32d7a713f4576e0703aaab6ebd49aa5822560dd6500ba3cede9f4a2e382e2.png ../../_images/72d75da220eb238447f18ff61081fc445303bcfded387a8bdf1c89e0694033f5.png ../../_images/409333b854db48cb1fe13965cd4d0da4761756260b72a6b5a8a126d747914ee0.png
Name: anomaly_detection
Model Type: auto_encoder
Overall accuracy: 83.636%
Precision/recall accuracy: 62.308%
Overall ROC AUC: 77.726%