Evaluate the model inputs
Once you have bound values to a model's inputs and outputs, you are ready to evaluate the model's inputs and get its predictions.
To run the model, you call any of the Evaluate* methods on your LearningModelSession. You can use the LearningModelEvaluationResult to look at the output features.
Example
In the following example, we run an evaluation on the session, passing in the binding and a unique correlation ID. Then we parse the output as a list of probabilities, match it to a list of labels for the different things our model can recognize, and write the results to the console:
// How many times an evaluation has been run
private int runCount = 0;
private void EvaluateModel(
LearningModelSession session,
LearningModelBinding binding,
string outputName,
List<string> labels)
{
// Process the frame with the model
var results =
await session.EvaluateAsync(binding, $"Run {++runCount}");
// Retrieve the results of evaluation
var resultTensor = results.Outputs[outputName] as TensorFloat;
var resultVector = resultTensor.GetAsVectorView();
// Find the top 3 probabilities
List<(int index, float probability)> indexedResults = new List<(int, float)>();
for (int i = 0; i < resultVector.Count; i++)
{
indexedResults.Add((index: i, probability: resultVector.ElementAt(i)));
}
// Sort the results in order of highest probability
indexedResults.Sort((a, b) =>
{
if (a.probability < b.probability)
{
return 1;
}
else if (a.probability > b.probability)
{
return -1;
}
else
{
return 0;
}
});
// Display the results
for (int i = 0; i < 3; i++)
{
Debug.WriteLine(
$"\"{labels[indexedResults[i].index]}\" with confidence of {indexedResults[i].probability}");
}
}
Device removal
If the device becomes unavailable, or if you'd like to use a different device, you must close the session and create a new session.
In some cases, graphics devices might need to be unloaded and reloaded, as explained in the DirectX documentation.
When using Windows ML, you'll need to detect this case and close the session. To recover from a device removal or re-initialization, you'll create a new session, which triggers the device selection logic to run again.
The most common case where you will see this error is during LearningModelSession.Evaluate. In the case of device removal or reset, LearningModelEvaluationResult.ErrorStatus will be DXGI_ERROR_DEVICE_REMOVED or DXGI_ERROR_DEVICE_RESET.
See also
- Previous: Bind a model
Note
Use the following resources for help with Windows ML:
- To ask or answer technical questions about Windows ML, please use the windows-machine-learning tag on Stack Overflow.
- To report a bug, please file an issue on our GitHub.