Windows ML API를 사용하면 모델을 평가하려는 디바이스를 선택할 수 있습니다. 세션을 만들 때 디바이스를 선택합니다. 이렇게 하려면 기계 학습 모델을 평가하는 데 사용되는 디바이스를 정의하는 LearningModelDevice 클래스를 사용합니다.
여기서는 이전 앱의 파일을 변경 classifier.cs 하여 디바이스의 GPU에서 모델 평가를 실행할 수 있습니다. 이전 자습서에서는 WinML 코드 생성기에서 자동으로 생성되었으므로 해당 파일을 건드리지 않았습니다.
-
classifier.cs파일을 열고 메서드에 다음 코드를 추가합니다CreateFromStreamAsync.
// Select GPU or another DirectX device to evaluate the model.
LearningModelDevice device = new LearningModelDevice(LearningModelDeviceKind.DirectX);
다른 실행 디바이스를 선택하려면 DirectX 필드를 다른 실행 디바이스로 전환하기만 하면됩니다.
- 이제 세션을 만들 때 이 디바이스를 선택할 수 있습니다. 실행 디바이스를
LearningModelSession지정하도록 생성자를 변경합니다.
// Create the evaluation session with the model and device.
learningModel.session = new LearningModelSession(learningModel.model, device);
CreateFromStreamAsync 메서드는 다음과 같습니다.
CreateFromStreamAsync(IRandomAccessStreamReference stream)
{
classifierModel learningModel = new classifierModel();
learningModel.model = await LearningModel.LoadFromStreamAsync(stream);
// Select GPU or another DirectX device to evaluate the model.
LearningModelDevice device = new LearningModelDevice(LearningModelDeviceKind.DirectX);
// Create the evaluation session with the model and device.
learningModel.session = new LearningModelSession(learningModel.model, device);
learningModel.binding = new LearningModelBinding(learningModel.session);
return learningModel;
}
그게 전부에요! 모델 평가를 위해 GPU를 선택했습니다.
디바이스를 지정하지 않으면 시스템에서 기본값을 사용합니다. 기본값을 사용하여 시스템이 사용자 대신 선택할 수 있는 유연성을 확보하는 것이 좋습니다.
Windows AI는 호출자가 이미 만든 디바이스 사용을 지원합니다. 고급 디바이스 만들기에 대한 자세한 내용은 세션 만들기 설명서를 검토하세요.
비고
실행 디바이스를 선택하는 방법에 대해 알아보려면 LearningModelDevice 설명서를 검토하세요.