Пример #1
0
        public bool StopTensorboard(string id)
        {
            bool   result       = false;
            string ResourceName = $"{id}.pmml";
            string ResourceNameWithoutExtension = id;
            string ResourcePath = DirectoryHelper.GetModelDirectoryPath() + ResourceName;
            string Message      = "Error";

            System.Console.WriteLine("Resource Path " + ResourcePath);
            Dictionary <string, string> Result = new Dictionary <string, string>();

            try
            {
                TensorBoard TBTool         = this.tbClient.GetTensorBoardTool();
                ITask       TBNoteBookTask = TBTool.FindTask(ResourcePath);
                if (TBNoteBookTask.IsEmpty())
                {
                    Message = "Error : There is no such task running";
                }
                else
                {
                    TBTool.StopTask(ResourcePath);
                    result = true;
                    InstancePayload.Delete(id);
                    Message = "Tensorboard successfully stop";
                }
            }
            catch (Exception ex)
            {
                Message = ex.Message;
            }

            return(result);
        }
Пример #2
0
 public UnitTests()
 {
     if (TensorBoardTool == null)
     {
         string LogDirectory = System.Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + "logs";
         if (!Directory.Exists(LogDirectory))
         {
             Directory.CreateDirectory(LogDirectory);
         }
         TensorBoardTool = new TensorBoard("http://localhost", LogDirectory);
     }
 }
Пример #3
0
        int Train(List <string[]>[] filesByExtension)
        {
            var random = this.Seed is null ? new Random() : new Random(this.Seed.Value);

            if (this.Seed != null)
            {
                tf.set_random_seed(this.Seed.Value);
            }

            var test = Split(random, filesByExtension, this.TestSplit, out filesByExtension);

            byte[] trainData = Sample(random, filesByExtension, CSharpOrNot.Size,
                                      count: this.TrainingSamples / SamplePart, out int[] trainValues);
            byte[] testData = Sample(random, test, CSharpOrNot.Size,
                                     count: this.TestSamples / SamplePart, out int[] testValues);

            //var checkpoint = new ModelCheckpoint(
            //    filepath: Path.Combine(Environment.CurrentDirectory, "weights.{epoch:02d}-{val_loss:.4f}.hdf5"),
            //    save_best_only: true, save_weights_only: true);
            var checkpointBest = new ModelCheckpoint(
                filepath: Path.Combine(Environment.CurrentDirectory, "weights.best.hdf5"),
                save_best_only: true, save_weights_only: true);

            // not present in 1.10, but present in 1.14
            ((dynamic)checkpointBest).save_freq = "epoch";

            ndarray <float> @in = GreyscaleImageBytesToNumPy(trainData, imageCount: this.TrainingSamples / SamplePart,
                                                             width: Width, height: Height);
            ndarray <int> expectedOut = OutputToNumPy(trainValues);

            string runID  = DateTime.Now.ToString("s").Replace(':', '-');
            string logDir = Path.Combine(".", "logs", runID);

            Directory.CreateDirectory(logDir);
            var tensorboard = new TensorBoard(log_dir: logDir);

            var model = CreateModel(classCount: IncludeExtensions.Length);

            model.compile(
                optimizer: new Adam(),
                loss: tf.keras.losses.sparse_categorical_crossentropy_fn,
                metrics: new dynamic[] { "accuracy" });
            model.build(input_shape: new TensorShape(null, Height, Width, 1));
            model.summary();

            tf.io.write_graph(
                tf.keras.backend.get_session().graph_def,
                Path.GetFullPath("logs"),
                name: "model.pbtxt",
                as_text: true);

            GC.Collect();

            var validationInputs = GreyscaleImageBytesToNumPy(testData,
                                                              imageCount: this.TestSamples / SamplePart,
                                                              width: Width, height: Height);
            var validationOutputs = OutputToNumPy(testValues);

            model.fit(@in, expectedOut,
                      batchSize: this.BatchSize, epochs: this.Epochs,
                      callbacks: new ICallback[] {
                checkpointBest,
                tensorboard,
            }, validationInput: validationInputs, validationTarget: validationOutputs);

            model.save_weights(Path.GetFullPath("weights.final.hdf5"));

            var fromCheckpoint = CreateModel(classCount: IncludeExtensions.Length);

            fromCheckpoint.build(new TensorShape(null, Height, Width, 1));
            fromCheckpoint.load_weights(Path.GetFullPath("weights.best.hdf5"));

            var evaluationResults = fromCheckpoint.evaluate(@in, (ndarray)expectedOut);

            Console.WriteLine($"reloaded: loss: {evaluationResults[0]} acc: {evaluationResults[1]}");

            evaluationResults = model.evaluate(@in, (ndarray)expectedOut);
            Console.WriteLine($"original: loss: {evaluationResults[0]} acc: {evaluationResults[1]}");

            return(0);
        }
Пример #4
0
 public PyTensorServiceClient(string HostURL, string RoutePrefix, int[] PortRangeInUse, string ContentDir)
 {
     TensorBoardTool = new TensorBoard(HostURL, RoutePrefix, PortRangeInUse, ContentDir + System.IO.Path.DirectorySeparatorChar + "data");
 }
 public PyTensorServiceClient(string HostURL, string ContentDir)
 {
     TensorBoardTool = new TensorBoard(HostURL, ContentDir + System.IO.Path.DirectorySeparatorChar + "data");
 }
Пример #6
0
        public async Task <IActionResult> PostModelTrainAsync(string id)
        {
            string  response    = string.Empty;
            string  dirFullpath = DirectoryHelper.GetDataDirectoryPath();//create new folder in data folder with the name of the file
            string  reqBody     = string.Empty;
            string  filePath    = string.Empty;
            string  dataFolder  = string.Empty;
            JObject jObjOrig    = new JObject();

            using (var reader = new StreamReader(Request.Body))
            {
                var body = reader.ReadToEnd();
                reqBody = body.ToString();
            }
            try
            {
                //get file name
                if (responseData.Count > 0)
                {
                    foreach (var record in responseData)
                    {
                        if (record.Id.ToString() == id)
                        {
                            //set the filePath
                            filePath = record.FilePath;
                            //create dir with filename in data folder
                            if (!Directory.Exists(dirFullpath + record.Name.Replace(".pmml", string.Empty)))
                            {
                                Directory.CreateDirectory(dirFullpath + record.Name.Replace(".pmml", string.Empty));
                                dataFolder = dirFullpath + record.Name.Replace(".pmml", string.Empty);
                            }
                        }
                    }
                }

                //json merge
                //Add TensorBoard Info
                if (!string.IsNullOrEmpty(reqBody))
                {
                    jObjOrig = JObject.Parse(reqBody);
                }
                if (!string.IsNullOrEmpty(filePath))
                {
                    string ResourcePath         = filePath;
                    string TensorBoardLink      = string.Empty;
                    string TensorboardLogFolder = string.Empty;

                    var obj = new
                    {
                        base_url     = "/",
                        ResourcePath = $"{ResourcePath}"
                    };
                    try
                    {
                        var             portRegex       = new Regex(@"(?<![^/]/[^/]*):\d+");//to remove port number
                        TensorBoard     TBTool          = this.tbClient.GetTensorBoardTool();
                        ZMM.Tasks.ITask TensorBoardTask = TBTool.FindTask(ResourcePath);
                        if (TensorBoardTask.IsEmpty())
                        {
                            TBTool.StartTaskAsync((int)TaskTypes.Start, ResourcePath, (JObject)JObject.FromObject(obj));
                        }
                        TensorBoardLink = TBTool.GetResourceLink(ResourcePath, out TensorboardLogFolder);
                        //
                        //TB redirection
                        string tbLink = "";
                        if (TensorBoardLink.Contains("6006"))
                        {
                            tbLink = TensorBoardLink.Replace(":6006", "/tb1");
                        }
                        else if (TensorBoardLink.Contains("6007"))
                        {
                            tbLink = TensorBoardLink.Replace(":6007", "/tb2");
                        }
                        else if (TensorBoardLink.Contains("6008"))
                        {
                            tbLink = TensorBoardLink.Replace(":6008", "/tb3");
                        }
                        //
                        jObjOrig.Add("filePath", ResourcePath);
                        jObjOrig.Add("tensorboardLogFolder", TensorboardLogFolder);
                        jObjOrig.Add("tensorboardUrl", tbLink);
                        Logger.LogInformation("PostModelTrainAsync", jObjOrig.ToString());
                        //for asset
                        // int sIdx =  TensorBoardLink.IndexOf(":6");
                        // var tbInst = new List<InstanceProperty>();
                        // tbInst.Add(new InstanceProperty(){ key = "port", value = TensorBoardLink.Substring(sIdx,6)});

                        //
                        var objJNBInst = new InstanceResponse()
                        {
                            Id   = id,
                            Name = $"{id}",
                            Type = "TB"
                        };
                        InstancePayload.Create(objJNBInst);
                        //
                    }
                    catch (Exception ex)
                    {
                        Logger.LogCritical("PostModelTrainAsync", ex.Message);
                        return(BadRequest(new { user = CURRENT_USER, id = id, message = ex.Message }));
                    }
                }
                if (!string.IsNullOrEmpty(dataFolder))
                {
                    jObjOrig.Add("dataFolder", dataFolder);
                }

                response = await nnclient.TrainModel(jObjOrig.ToString());

                return(Json(response));
            }
            catch (Exception ex)
            {
                //TO DO: ILogger
                string _ex = ex.Message;
                return(BadRequest());
            }
        }