public TrainSSOMResponse GetTrainSSOM(TrainSSOMRequest request) { SSOM model = new SSOM(request.Width, request.Height, request.LearningRate, request.Epoch); IReader reader = new CSVReader(System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/Iris.csv")); model.GetData(reader); foreach (var item in request.Labels) { model.Dataset.SetLabel(item); } model.FeatureLabel = request.FeatureLabel; model.InitializeMap(); model.Train(); model.LabelNodes(); TrainSSOMResponse response = new TrainSSOMResponse() { Model = model }; return(null); }
public async Task <HttpResponseMessage> GetTrainSOM() { try { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var root = HttpContext.Current.Server.MapPath(FILE_UPLOAD_PATH); Directory.CreateDirectory(root); var provider = new MultipartFormDataStreamProvider(root); var result = await Request.Content.ReadAsMultipartAsync(provider); var jsonModel = result.FormData["model"]; if (jsonModel == null) { throw new HttpResponseException(HttpStatusCode.BadRequest); } JObject parsedModel = JObject.Parse(jsonModel); var epoch = (int)parsedModel["Epoch"]; var learningRate = (double)parsedModel["LearningRate"]; var height = (int)parsedModel["Height"]; var width = (int)parsedModel["Width"]; var kmeans = (int)parsedModel["KMeans"]; var k = (int)parsedModel["K"]; var regions = JsonConvert.DeserializeObject <List <Region> >(parsedModel["Regions"].ToString()); var csvFile = result.FileData.First(); SSOM model = new SSOM(width, height, learningRate, epoch, k); model.Regions = regions; var featureLabel = (string)parsedModel["FeatureLabel"]; var labels = ((string)parsedModel["Labels"]).Split(',').ToList(); IReader reader = new CSVReader(csvFile.LocalFileName); model.GetData(reader); foreach (var item in labels) { model.Dataset.SetLabel(item); } model.FeatureLabel = featureLabel; model.InitializeMap(); model.Train(); model.LabelNodes(); IClusterer cluster = new KMeansClustering(); var flattenedMap = ArrayHelper <Node> .FlattenMap(model.Map); var clusteredNodes = cluster.Cluster(flattenedMap, kmeans); foreach (var node in clusteredNodes) { model.Map[node.Coordinate.X, node.Coordinate.Y].ClusterGroup = node.ClusterGroup; } FileInfo fileInfo = new FileInfo(csvFile.LocalFileName); fileInfo.Delete(); TrainSOMResponse response = new TrainSOMResponse() { MapId = Guid.NewGuid(), Model = model }; var message = Request.CreateResponse(HttpStatusCode.OK, response); File.Delete(result.FileData.First().LocalFileName); return(message); } catch (Exception ex) { var message = Request.CreateResponse(HttpStatusCode.InternalServerError, ex); return(message); } }
public static void Program2(string[] args) { string filePath = @"C:\Users\Vilson\Desktop\Datasets\Kalaw-Dataset\config.json"; if (args.Length > 0) { filePath = args[0]; } var content = System.IO.File.ReadAllText(filePath); var config = ReadToObject(content); // Build the Model SSOM model = new SSOM(config.Width, config.Height, config.ConstantLearningRate, config.Epoch, config.K); model.Regions = config.Regions; // Subscribe to OnTrainingEvent model.Training += _model_Training; // Instantiate the reader IReader _reader = new CSVReader(config.Dataset); // Instantiate the clusterer IClusterer clusterer = new KMeansClustering(); model.GetData(_reader); // Get the labels string[] labels = config.Labels.Split(','); foreach (var label in labels) { model.Dataset.SetLabel(label); } // Set the feature label model.FeatureLabel = config.FeatureLabel; // Initialize the training Stopwatch stopwatch = new Stopwatch(); Console.WriteLine("Start initializing map..."); stopwatch.Start(); model.InitializeMap(); stopwatch.Stop(); Console.WriteLine("Completed initialization..."); Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", stopwatch.Elapsed); Console.WriteLine("Start training model..."); stopwatch.Restart(); model.Train(); stopwatch.Stop(); Console.WriteLine("Completed training model..."); Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", stopwatch.Elapsed); Console.WriteLine("Start labelling node..."); stopwatch.Restart(); model.LabelNodes(); stopwatch.Stop(); Console.WriteLine("Completed labelling node..."); Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", stopwatch.Elapsed); if (config.Clusters > 0) { Console.WriteLine("Start clustering nodes..."); stopwatch.Restart(); var flattenedMap = ArrayHelper <Node> .FlattenMap(model.Map); var clusteredNodes = clusterer.Cluster(flattenedMap, config.Clusters); foreach (var node in clusteredNodes) { model.Map[node.Coordinate.X, node.Coordinate.Y].ClusterGroup = node.ClusterGroup; } stopwatch.Stop(); Console.WriteLine("Completed clustering nodes..."); Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", stopwatch.Elapsed); } // Export the model Console.WriteLine("Exporting model..."); var guid = Guid.NewGuid(); model.MapId = guid; model.Dataset = null; var serializeObject = JsonConvert.SerializeObject(model, Formatting.Indented); string exportFileName = string.Format("{0}Map_{1}.json", config.Export, guid); System.IO.File.WriteAllText(exportFileName, serializeObject); Console.WriteLine("Training completed..."); Console.ReadLine(); }