private async Task LoadModelAsync() { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => StatusBlock.Text = $"Loading {_ourOnnxFileName} ... patience "); try { _stopwatch = Stopwatch.StartNew(); var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{_ourOnnxFileName}")); _model = await OnnxModel.CreateOnnxModel(modelFile); _stopwatch.Stop(); Debug.WriteLine($"Loaded {_ourOnnxFileName}: Elapsed time: {_stopwatch.ElapsedMilliseconds} ms"); } catch (Exception ex) { Debug.WriteLine($"error: {ex.Message}"); await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => StatusBlock.Text = $"error: {ex.Message}"); _model = null; } }
static void Main(string[] args) { var model = new OnnxModel("/Users/vladimirlisovoi/desktop/prak1/img"); model.EventResult += PredictionHandler; model.OutputEvent += OutputHandler; model.Work(); }
public static async Task <OnnxModel> CreateFromStreamAsync(IRandomAccessStreamReference stream) { OnnxModel onnxModel = new OnnxModel(); onnxModel.learningModel = await LearningModel.LoadFromStreamAsync(stream); onnxModel.session = new LearningModelSession(onnxModel.learningModel); onnxModel.binding = new LearningModelBinding(onnxModel.session); return(onnxModel); }
private void ActiveModel_SelectionChanged(object sender, SelectionChangedEventArgs e) { int selectedIdx = ActiveModel.SelectedIndex; if ((selectedIdx >= 0) && (selectedIdx < onnxFileNames.Count)) { _ourOnnxFileName = onnxFileNames[selectedIdx].Item1; _ourOnnxNumLables = onnxFileNames[selectedIdx].Item2; _model = null; // Will force reloading of model. } }
public void InsertModelFromUrl(string url) { using var webClient = new WebClient(); byte[] modelBytes = webClient.DownloadData(url); using var dbContext = new DatabaseContext(_sqlConnectionString); var query = $"insert into {MODELTABLENAME} ([description], [data]) values ('Onnx Model',?)"; var model = new OnnxModel { Description = "Onnx Model", Data = modelBytes }; dbContext.Add <OnnxModel>(model); dbContext.SaveChanges(); }
public void TestOnnxModelNotDisposal() { // Declare the path the tested ONNX model file. var modelFile = Path.Combine(Directory.GetCurrentDirectory(), "zipmap", "TestZipMapInt64.onnx"); // Create ONNX model from the model file. var onnxModel = new OnnxModel(modelFile); // Check if a temporal file is crated for storing the byte[]. Assert.True(File.Exists(onnxModel.ModelFile)); // Don't delete the temporal file! onnxModel.Dispose(); // Make sure the temporal file still exists. Assert.True(File.Exists(onnxModel.ModelFile)); }
public void TestOnnxModelDisposal() { // Create a ONNX model as a byte[]. var modelFile = Path.Combine(Directory.GetCurrentDirectory(), "zipmap", "TestZipMapInt64.onnx"); var modelInBytes = File.ReadAllBytes(modelFile); // Create ONNX model from the byte[]. var onnxModel = OnnxModel.CreateFromBytes(modelInBytes, ML); // Check if a temporal file is crated for storing the byte[]. Assert.True(File.Exists(onnxModel.ModelStream.Name)); // Delete the temporal file. onnxModel.Dispose(); // Make sure the temporal file is deleted. Assert.False(File.Exists(onnxModel.ModelStream.Name)); }
public static async Task <OnnxModel> CreateOnnxModel(StorageFile file) { LearningModelPreview learningModel = null; try { learningModel = await LearningModelPreview.LoadModelFromStorageFileAsync(file); } catch (Exception e) { var exceptionStr = e.ToString(); System.Console.WriteLine(exceptionStr); throw e; } OnnxModel model = new OnnxModel(); learningModel.InferencingOptions.PreferredDeviceKind = LearningModelDeviceKindPreview.LearningDeviceGpu; learningModel.InferencingOptions.ReclaimMemoryAfterEvaluation = true; model.learningModel = learningModel; return(model); }
private void StartCommandHandler(object sender, ExecutedRoutedEventArgs e) { this.isWorking = true; resultcollection.Clear(); imagecollection.Clear(); classcollection.Clear(); ThreadPool.QueueUserWorkItem(new WaitCallback(param => { foreach (string path in Directory.GetFiles(selected_dir, "*.jpg")) { Dispatcher.BeginInvoke(new Action(() => { imagecollection.Add(new Image(path)); })); } })); mdl = new OnnxModel(selected_dir); mdl.EventResult += OutputHandler; mdl.Work(); }