Created
January 20, 2018 20:38
-
-
Save anujb/ef23827939f17902b63e3ccaaa1d4599 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async Task CompileModelAsync() | |
{ | |
//Define remote URL | |
var model_remote_url = ""; | |
var model_name = "FruitModel"; | |
var model_file_extension = "mlodel"; | |
string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal); | |
string model_path = Path.Combine(path, $"{model_name + model_file_extension}"); | |
//Retry up to 3 times, on success break | |
foreach (var i in Enumerable.Range(0, 2)) | |
{ | |
var success = false; | |
try | |
{ | |
using (var client = new HttpClient()) | |
{ | |
using (var response = await client.GetAsync(model_remote_url, HttpCompletionOption.ResponseHeadersRead)) | |
{ | |
using (var stream = await response.Content.ReadAsStreamAsync()) | |
{ | |
using (Stream streamToWriteTo = File.Open(model_path, FileMode.Create)) | |
{ | |
await stream.CopyToAsync(streamToWriteTo); | |
success = true; | |
} | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
success = false; | |
} | |
if (success) break; | |
} | |
try | |
{ | |
var bundle = NSBundle.MainBundle; | |
var model_local_url = bundle.GetUrlForResource(model_name, model_file_extension); //need to handle error if file is corrupt/locked | |
var compiled_model = MLModel.CompileModel(model_local_url, out var error); | |
if(error == null) | |
{ | |
Debug.WriteLine($"Compiled model is located at: {model_local_url.AbsoluteUrl}"); | |
var model = MLModel.Create(compiled_model, out error); | |
if(error == null) | |
{ | |
var vision_model = VNCoreMLModel.FromMLModel(model, out error); | |
if(error == null) | |
{ | |
//do requests and stuff with vision model | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment