The default use of trained machine learning model in UWP apps is to add onnx file to your solution and leave Visual Studio to generate the corresponding class and load the file directly in the solution, but in some case can be useful to load the file from other sources, like the filesystem.
This is not a problem, the CreateXXXXXModel generate take a StorageFile so you can use a FilePicker to take a file, but if you try a code like this
FileOpenPicker fileOpenPicker = new FileOpenPicker(); fileOpenPicker.FileTypeFilter.Add(".onnx"); StorageFile selectedStorageFile = await fileOpenPicker.PickSingleFileAsync(); try { _model = await CustomVisionModel.CreateCustomVisionModel(selectedStorageFile); }catch(Exception ex) { new MessageDialog(ex.StackTrace,ex.Message).ShowAsync(); }
You can see that an exception is thown in the calling of method LearningModelPreview.LoadModelFromStorageFileAsync in the generated class
public static async Task CreateCustomVisionModel(StorageFile file) { LearningModelPreview learningModel = await LearningModelPreview.LoadModelFromStorageFileAsync(file); CustomVisionModel model = new CustomVisionModel(); model.learningModel = learningModel; return model; }
The exception does not give us more info about the problem
Exception from HRESULT: 0x88900103
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at WinMLTester.CustomVisionModel.d__1.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at WinMLTester.Views.MainPage.d__6.MoveNext()
it seems that file doesn’t have some permission for made WinML works, the solution is quite simple, before creating the model we need to copy the file in the app local folder and then use this file instead of the other.
We need only few lines of codes for do this
FileOpenPicker fileOpenPicker = new FileOpenPicker(); fileOpenPicker.FileTypeFilter.Add(".onnx"); StorageFile selectedStorageFile = await fileOpenPicker.PickSingleFileAsync(); //Fix for loading file StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFile sf2 = await selectedStorageFile.CopyAsync(storageFolder, selectedStorageFile.Name, NameCollisionOption.ReplaceExisting); try { _model = await CustomVisionModel.CreateCustomVisionModel(sf2); }catch(Exception ex) { new MessageDialog(ex.StackTrace,ex.Message); }
Now your code work without problem
I have also created a new project on github for help to test your machine learning model created with Custom Vision service available on https://github.com/a-iafrate/WinMLTester that can be used to test this fix
happy coding!!
Commenti recenti