Model Introspection

Understanding Model Metadata, Inputs, and Outputs

Models contain information about metadata and feature types. This data is useful for debugging and writing custom predictors.

Model Metadata

Models often contain metadata information, including the model author, name, and so on. The MLModel class acts like any dictionary in C#. A model can be queried for specific metadata values by indexing it with a provided key:

// Check the model description
var description = model["Description"];

You can use the System.Linq extensions on the model for enhanced querying operations.

Metadata can be enumerated using a standard foreach loop:

foreach (KeyValuePair<string, string> metadata in model)
Debug.Log($"{metadata.Key}: {metadata.Value}");

Model Feature Types

In order to properly use an MLModel to make predictions, we need to understand what types of features the model takes as inputs and produces as outputs. The model exposes its input and output types:

// Fetch model input types
MLFeatureType[] inputTypes = model.inputs;
MLFeatureType[] outputTypes = model.outputs;

The MLFeatureType is an abstract base type. NatML includes several concrete feature types:

if (model.inputs[0] is MLImageType imageType)
Debug.Log($"Model expects image with size {imageType.width}x{imageType.height}");