private async void predict_predict_Click(object sender, RoutedEventArgs e) { List <float> predictions = new List <float>(); int deviceId = int.Parse(train_device.Text); string s = $"Count: {predictData.Count()}\n"; var task = Task.Run(() => { MLModel model = new MLModel($"{deviceId}.mlm"); for (int i = 0; i < predictData.Count; i++) { var prediction = model.Predict(predictData[i]); predictions.Add(prediction); s += $"{predictData[i].Timestamp.ToString("HH")}: {predictData[i].Volume:f1} -> {prediction:f1}" + $" ({((predictData[i].Volume != 0) ? 100 - predictData[i].Volume / prediction * 100:0):f2}%)\n"; } }); await Process(task); DrawChild(predictions, "Prediction"); Log(s); }
private async void train_test_Click(object sender, RoutedEventArgs e) { List <float> predictions = new List <float>(); MLModel.Metrics metrics = null; var task = Task.Run(() => { for (int i = 0; i < trainData.Count; i++) { var prediction = _mlModel.Predict(trainData[i]); predictions.Add(prediction); } metrics = _mlModel.Validate(); }); await Process(task); DrawChild(predictions, "Prediction"); Log($"ML model metrics:\n" + $"Mean absolute error: {metrics.MeanAbsoluteError:f2}\n" + $"Mean squared error: {metrics.MeanSquaredError:f2}"); }
public async Task PredictAll() { _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Working, ""); try { Status = "starting ml model..."; //load config var confDir = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "lacmus"); var configPath = Path.Join(confDir, "appConfig.json"); _appConfig = await AppConfig.Create(configPath); var config = _appConfig.MlModelConfig; using (var model = new MLModel(config)) { await model.Init(); var count = 0; var objectCount = 0; Status = "processing..."; foreach (var photoViewModel in _photos.Items) { try { photoViewModel.Annotation.Objects = await model.Predict(photoViewModel); photoViewModel.BoundBoxes = photoViewModel.GetBoundingBoxes(); if (photoViewModel.BoundBoxes.Any()) { photoViewModel.Photo.Attribute = Attribute.WithObject; photoViewModel.IsHasObject = true; } objectCount += photoViewModel.BoundBoxes.Count(); count++; PredictProgress = (double)count / _photos.Items.Count() * 100; PredictTextProgress = $"{Convert.ToInt32(PredictProgress)} %"; _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Working, $"Working | {(int)((double) count / _photos.Items.Count() * 100)} %, [{count} of {_photos.Items.Count()}]"); Console.WriteLine($"\tProgress: {(double) count / _photos.Items.Count() * 100} %"); } catch (Exception e) { Log.Error(e, $"Unable to process file {photoViewModel.Path}. Slipped."); } } Status = "stopping ml model..."; await model.Stop(); PredictTextProgress = $"predict {_photos.Count} photos."; Log.Information($"Successfully predict {_photos.Count} photos. Find {objectCount} objects."); } } catch (Exception e) { Status = "error."; Log.Error(e, "Unable to get prediction."); } _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready, ""); }
public async Task GetPrediction(float[] image) { var digit = MinstDigit.FromList(image, false); var prediction = ml.Predict(digit); prediction.Label = prediction.Score.Select((value, index) => new { Value = value, Index = index }).Aggregate((a, b) => (a.Value > b.Value) ? a : b).Index; await Clients.Caller.SendAsync("receivePrediction", image, prediction); await Clients.All.SendAsync("receiveBackground", image, prediction); }
// Attempt AI Prediction: public JsonResult OnGetAIPrediction() { // Get the query string: var queryStringObject = Request.QueryString; string queryString = queryStringObject.ToString(); // Parse the query string and extract parameters: string[] queryParams = queryString.Split("&"); string imageURL = queryParams[1].Replace("imageURL=", ""); // Attempt to return the response: return(new JsonResult(MLModel.Predict(imageURL).Prediction)); }
/// <summary> /// This is the starting point of the server program. Here we assign the app a configuration and initialize Machine Learning engines. /// </summary> /// <param name="configuration">An IConfiguration object</param> public Startup(IConfiguration configuration) { Configuration = configuration; // Set Firebase URL: RestfulDBConnection.FIREBASE_URL = Configuration["FirebaseURL"]; // Initialize Machine Learning Components: MLModel.CreatePredictionEngine(); GroundTruth.CreateGroundTruthLookup(); // Get the Machine Learning Warmed Up: MLModel.Predict("https://firebasestorage.googleapis.com/v0/b/agricultureai-15ce0.appspot.com/o/DSC00025.JPG?alt=media"); }