/* * Utility method to check if the Model Guid is valid * */ public bool IsValidModelId(Guid guid) { using (var mlapictx = new MLAPIEntities()) { try { var models = mlapictx.Models; var newModel = new Model { Id = guid }; if (mlapictx.Models.Any(x => x.Id == guid)) { return(true); } else { return(false); } } catch (Exception ex) { //Log the exception var res = string.Format($"Error ocuurred while checking model validity"); return(false); } } }
private void SaveBestResults(MLApiAccuracyParameters parameters) { Dictionary <string, object> dict = new Dictionary <string, object>(); using (var mlapictx = new MLAPIEntities()) { try { var accparams = mlapictx.AccuracyParamters; var data = mlapictx.AccuracyParamters.FirstOrDefault(x => x.ModelId == parameters.ModelId); if (data != null) { data.Accuracy = parameters.Accuracy; data.NumberOfLayers = parameters.NumberOfLayers; data.Steps = parameters.Steps; data.LearningRate = parameters.LearningRate; mlapictx.Entry(data).State = System.Data.Entity.EntityState.Modified; mlapictx.SaveChanges(); } else { var newModel = new MLAPI.Domain.AccuracyParamter { ModelId = parameters.ModelId, Steps = parameters.Steps, NumberOfLayers = parameters.NumberOfLayers, Accuracy = parameters.Accuracy, LearningRate = parameters.LearningRate }; mlapictx.AccuracyParamters.Add(newModel); mlapictx.SaveChanges(); } } catch (Exception ex) { //Log the exception var res = string.Format($"Error occurred while saving the best results : {ex.Message}"); } } }
public HttpResponseMessage GetBestAccuracy() { Dictionary <string, object> dict = new Dictionary <string, object>(); ModelService modelService = new ModelService(); var httpRequest = HttpContext.Current.Request; //Getting the GUID from the request var guid = httpRequest.Params["ModelId"]; var accparams = new MLApiAccuracyParameters(); try { if (!modelService.IsValidModelId(Guid.Parse(guid))) { var message = string.Format("The model is not available in the datamodel"); dict.Add("status", "failure"); dict.Add("error", message); return(Request.CreateResponse(HttpStatusCode.NotFound, dict)); } accparams.ModelId = Guid.Parse(guid); } catch (Exception ex) { var message = string.Format("The GUID format is incorrect"); dict.Add("status", "failure"); dict.Add("error", message); return(Request.CreateResponse(HttpStatusCode.BadRequest, dict)); } using (var mlapictx = new MLAPIEntities()) { try { var accparamsmodel = mlapictx.AccuracyParamters; var data = mlapictx.AccuracyParamters.FirstOrDefault(x => x.ModelId == accparams.ModelId); if (data == null) { dict.Add("status", "failure"); dict.Add("error", $"Accuracy paramters for the given modelid {accparams.ModelId} are not available"); return(Request.CreateResponse(HttpStatusCode.OK, dict)); } else { dict.Add("status", "success"); dict.Add("result", new { ModelId = data.ModelId, Accuracy = data.Accuracy, Steps = data.Steps, LearningRate = data.LearningRate, Layers = data.NumberOfLayers }); return(Request.CreateResponse(HttpStatusCode.OK, dict)); } } catch (Exception ex) { dict.Clear(); dict.Add("status", "failure"); dict.Add("error", $"Error occurred while getting the best accuracy results : {ex.Message}"); return(Request.CreateResponse(HttpStatusCode.InternalServerError, dict)); } } }
public IEnumerable <MLApiImage> AcessDB() { using (var x = new MLAPIEntities()) { var res = x.Images.Select(z => new MLApiImage { Id = z.Id, ImagePath = z.ImagePath, ModelId = z.ModelId }).ToList(); return(res); } }
/* * Method to add the image path to the persistanct storage. * */ public HttpResponseMessage SaveImage(MLApiImage image) { Dictionary <string, object> dict = new Dictionary <string, object>(); if (string.IsNullOrEmpty(image.ImagePath)) { dict.Add("status", "failure"); dict.Add("error", "Image path is not added"); } else { using (var mlapictx = new MLAPIEntities()) { try { var images = mlapictx.Images; var newModel = new Image { ModelId = image.ModelId, ImagePath = image.ImagePath }; mlapictx.Images.Add(newModel); mlapictx.SaveChanges(); var responseMessage = string.Format("Image uploaded successfully."); dict.Add("status", "success"); dict.Add("message", responseMessage); return(Request.CreateResponse(HttpStatusCode.OK, dict));; } catch (Exception ex) { var res = string.Format($"Error Ocuurred while uploading the image : {ex.Message}"); dict.Clear(); dict.Add("status", "failure"); dict.Add("error", res); return(Request.CreateResponse(HttpStatusCode.InternalServerError, dict)); } } } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.NoContent, dict); return(response); }
public HttpResponseMessage CreateModel(MLApiModel model) { Dictionary <string, object> dict = new Dictionary <string, object>(); if (string.IsNullOrEmpty(model.ModelName)) { dict.Add("error", "Model name has to be given"); } else { using (var mlapictx = new MLAPIEntities()) { try { var models = mlapictx.Models; var newModel = new Model { ModelName = model.ModelName }; Guid obj = Guid.NewGuid(); newModel.Id = obj; mlapictx.Models.Add(newModel); mlapictx.SaveChanges(); var responseMessage = string.Format("Model created successfully."); dict.Add("guid", newModel.Id); return(Request.CreateResponse(HttpStatusCode.OK, dict));; } catch (Exception ex) { var res = string.Format($"Error Ocuurred while creating the model : {ex.Message}"); dict.Clear(); dict.Add("error", res); return(Request.CreateResponse(HttpStatusCode.InternalServerError, dict)); } } } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.NoContent, dict); return(response); }
public HttpResponseMessage GenerateExperiments(MLApiExperiment experiment) { ModelService modelservice = new ModelService(); Dictionary <string, object> dict = new Dictionary <string, object>(); if (experiment.ModelId == null || experiment.ModelId == Guid.Empty || !modelservice.IsValidModelId(experiment.ModelId ?? Guid.Empty)) { dict.Add("status", "failure"); dict.Add("error", "Valid model id is required"); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.NoContent, dict); return(response); } else { double[] learningRates = new double[] { 0.001, 0.01, 0.1 }; double[] stepsArr = new double[] { 1000, 2000, 4000 }; double[] noOfLayers = new double[] { 1, 2, 4 }; double maxlr = 0, maxsteps = 0, maxlayers = 0, maxacc = 0; List <String> experimentErrors = new List <String>(); using (var mlapictx = new MLAPIEntities()) { for (int i = 0; i < learningRates.Length; i++) { for (int j = 0; j < stepsArr.Length; j++) { for (int k = 0; k < noOfLayers.Length; k++) { double acc = TrainModelAndReturnAccuracy(k, j, i); if (acc > maxacc) { maxlayers = noOfLayers[k]; maxlr = learningRates[i]; maxsteps = stepsArr[j]; maxacc = acc; } try { var models = mlapictx.Experiments; var newModel = new Experiment { ModelId = experiment.ModelId, Accuracy = (decimal)acc, LearningRate = (decimal?)learningRates[i], Steps = (decimal?)stepsArr[j], NumberOfLayers = (decimal?)noOfLayers[k] }; mlapictx.Experiments.Add(newModel); mlapictx.SaveChanges(); } catch (Exception ex) { var res = string.Format($"Error occurred for learning rate = ${i} , steps = ${j} , layers: ${k} , message = {ex.Message}"); experimentErrors.Add(res); //The errors while training for a specfic paramters combination should be logged in splunk } } } } } //Sending the error response only if all the 27 experiments are unsuccessful , even if some of them are successful sending //The response will be given with the best accuracy , steps , learning rate , no.of layers if (experimentErrors.Count != 27) { dict.Add("status", "success"); dict.Add("Accuracy", maxacc); dict.Add("Steps", maxsteps); dict.Add("LearningRate", maxlr); dict.Add("Layers", maxlayers); var accparam = new MLApiAccuracyParameters { ModelId = experiment.ModelId, Accuracy = (decimal?)maxacc, Steps = (decimal?)maxsteps, LearningRate = (decimal?)maxlr, NumberOfLayers = (decimal?)maxlayers }; SaveBestResults(accparam); return(Request.CreateResponse(HttpStatusCode.OK, dict)); } dict.Add("status", "failure"); dict.Add("error", experimentErrors); return(Request.CreateResponse(HttpStatusCode.InternalServerError, dict)); } }
public double GetAccuracyForModel(Guid guid) { double steps = 0, learningrate = 0, layers = 0; using (var mlapictx = new MLAPIEntities()) { try { var accparams = mlapictx.AccuracyParamters; var data = mlapictx.AccuracyParamters.FirstOrDefault(x => x.ModelId == guid); if (data != null) { layers = (double)data.NumberOfLayers; steps = (double)data.Steps; learningrate = (double)data.LearningRate; } else { //Log that the guid is not present in the accuracy table return(-1); } } catch (Exception ex) { //Log the exception var res = string.Format($"Error occurred while getting the best results : {ex.Message}"); return(-1); } } ProcessStartInfo start = new ProcessStartInfo(); //start.FileName = @"C:\Users\Jayakrishna Alwar\AppData\Local\Programs\Python\Python36\python.exe"; start.FileName = @"D:\Python34\python.exe"; var basePath = AppDomain.CurrentDomain.BaseDirectory; var scriptPath = basePath + @"Scripts\test.py"; //The below is the path of the folder in which the images are stored before training //That path is given to the train.py file , the train.py file will read the images from that location and does the processing/training. //Ideally these images can be stored in a S3/Storage account and can be given to the model on demand for training by getting them into a folder //and that folder with images will be used for training the model.S var im = basePath + @"UserImages\"; var argv = $"\"--i\" \"{learningrate}\" \"--j\" \"{layers}\" \"--k\" \"{steps}\" \"--images\" \"{im}\"".Split(' '); start.Arguments = $"\"{scriptPath}\" \"{argv[0]}\" \"{argv[1]}\" \"{argv[2]}\" \"{argv[3]}\" \"{argv[4]}\" \"{argv[5]}\" \"{argv[6]}\" \"{argv[7]}\""; //args is path to .py file and any cm line args start.UseShellExecute = false; start.RedirectStandardOutput = true; start.CreateNoWindow = true; start.RedirectStandardError = true; var errors = ""; var results = ""; using (Process process = Process.Start(start)) { errors = process.StandardError.ReadToEnd(); results = process.StandardOutput.ReadToEnd(); } var final = JsonConvert.DeserializeObject <dynamic>(results); var accuracy = final.accuracy.Value; return(Convert.ToDouble(accuracy)); }