將值系結至模型的輸入和輸出之後,您就可以評估模型的輸入並取得其預測。
若要執行模型,您可以在 LearningModelSession 上呼叫任何 Evaluate* 方法。 您可以使用 LearningModelEvaluationResult 來查看輸出功能。
範例
在下列範例中,我們會執行會話評估,傳入綁定和唯一關聯識別碼。 然後,我們會將輸出剖析為機率清單、將其與模型可辨識之不同專案的標籤清單相符,並將結果寫入主控台:
// 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}");
}
}
裝置移除
如果裝置變成無法使用,或您想要使用不同的裝置,您必須關閉工作階段並建立新的工作階段。
在某些情況下,圖形裝置可能需要卸除和重載,如 DirectX 檔中所述。
使用 Windows ML 時,您必須偵測這個情況並關閉工作階段。 若要從裝置移除或重新初始化復原,您將建立新的會話,以觸發裝置選取邏輯再次執行。
最常見的情況是 LearningModelSession.Evaluate 期間,您會看到此錯誤。 在裝置移除或重設的情況下, LearningModelEvaluationResult.ErrorStatus 將會 DXGI_ERROR_DEVICE_REMOVED 或 DXGI_ERROR_DEVICE_RESET。
另請參閱
- 上一個 綁定模型
備註
使用以下資源以獲得 Windows ML 的協助。
- 若要詢問或回答有關 Windows ML 的技術問題,請使用 Stack Overflow 上的 windows-machine-learning 標籤。
- 若要回報錯誤,請在 GitHub 上提出問題。