private static async Task TrainProject(TrainingApi trainingApi, Guid projectId)
        {
            var iteration = await trainingApi.TrainProjectAsync(projectId);

            while (iteration.Status == "Training")
            {
                Console.Clear();
                Console.WriteLine("Training in progress...");

                Thread.Sleep(1000);

                iteration = await trainingApi.GetIterationAsync(projectId, iteration.Id);
            }

            iteration.IsDefault = true;
            trainingApi.UpdateIteration(projectId, iteration.Id, iteration);

            Console.WriteLine();
            Console.WriteLine("Project successfully trained... Press any key to continue");
            Console.ReadLine();
        }
        private async Task TrainProjectsAsync()
        {
            this.progressControl.IsActive = true;

            bool trainingSucceeded = true;

            try
            {
                Iteration iterationModel = await trainingApi.TrainProjectAsync(this.CurrentProject.Id);

                while (true)
                {
                    iterationModel = await trainingApi.GetIterationAsync(this.CurrentProject.Id, iterationModel.Id);

                    if (iterationModel.Status != "Training")
                    {
                        if (iterationModel.Status == "Failed")
                        {
                            trainingSucceeded = false;
                        }
                        break;
                    }
                    await Task.Delay(500);
                }

                this.needsTraining = false;
            }
            catch (Exception ex)
            {
                await Util.GenericApiCallExceptionHandler(ex, "Failure requesting training");
            }

            this.progressControl.IsActive = false;

            if (!trainingSucceeded)
            {
                await new MessageDialog("Training failed.").ShowAsync();
            }
        }
示例#3
0
        private async void TriggerActiveLearningButtonClicked(object sender, RoutedEventArgs e)
        {
            this.activeLearningFlyout.Hide();

            var currentProject = ((ProjectViewModel)this.projectsComboBox.SelectedValue).Model;

            try
            {
                var tags = this.PredictionDataForRetraining.Where(d => d.HasTag).Select(d => d.TagId).ToList();

                if (tags.Any())
                {
                    var test = await this.userProvidedTrainingApi.CreateImagesFromPredictionsAsync(currentProject.Id,
                                                                                                   new ImageIdCreateBatch
                    {
                        TagIds = tags,
                        Images = new List <ImageIdCreateEntry>(new ImageIdCreateEntry[] { new ImageIdCreateEntry(this.PredictionDataForRetraining.First().PredictionResultId) })
                    });
                }
                else
                {
                    await new MessageDialog("You need to select at least one Tag in order to save and re-train.").ShowAsync();
                    return;
                }
            }
            catch (Exception ex)
            {
                await Util.GenericApiCallExceptionHandler(ex, "Failure adding image to the training set");

                return;
            }

            this.progressRing.IsActive = true;

            bool trainingSucceeded = true;

            try
            {
                Iteration iterationModel = await userProvidedTrainingApi.TrainProjectAsync(currentProject.Id);

                while (true)
                {
                    iterationModel = await userProvidedTrainingApi.GetIterationAsync(currentProject.Id, iterationModel.Id);

                    if (iterationModel.Status != "Training")
                    {
                        if (iterationModel.Status == "Failed")
                        {
                            trainingSucceeded = false;
                        }
                        break;
                    }
                    await Task.Delay(500);
                }
            }
            catch (Exception ex)
            {
                await Util.GenericApiCallExceptionHandler(ex, "The image was added to the training set, but re-training failed. You can try re-training later via the Custom Vision Setup page.");
            }

            if (!trainingSucceeded)
            {
                await new MessageDialog("The image was added to the training set, but re-training failed. You can try re-training later via the Custom Vision Setup page.").ShowAsync();
            }

            this.progressRing.IsActive = false;
        }
        private static async Task <bool> MainAsync(string[] args)
        {
            try
            {
                trainingApi.ApiKey = CustomVisionTrainingApiKey;

                // Create project
                var projects = await trainingApi.GetProjectsAsync();

                var project = projects.Where(x => x.Name == projectName).FirstOrDefault();

                if (project == null)
                {
                    Console.WriteLine($"\nCreating project '{projectName}'");
                    project = await trainingApi.CreateProjectAsync(projectName);
                }

                // Retrieve all tags
                var tagsList = await trainingApi.GetTagsAsync(project.Id);

                #region Create Object tag
                var objectTag = tagsList.Tags.Where(x => x.Name == objectTagName).FirstOrDefault();

                if (objectTag == null)
                {
                    Console.WriteLine($"\nCreating tag '{objectTagName}'");
                    objectTag = await trainingApi.CreateTagAsync(project.Id, objectTagName);
                }

                // add images to tag
                Console.WriteLine($"\nAdding images to tag '{objectTagName}'");
                List <ImageFileCreateEntry> imageFiles = (Directory.GetFiles(objectTrainImagesPath)).Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList();
                await trainingApi.CreateImagesFromFilesAsync(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>()
                {
                    objectTag.Id
                }));

                #endregion

                #region Create Ocean tag
                var oceanTag = tagsList.Tags.Where(x => x.Name == oceanTagName).FirstOrDefault();

                if (oceanTag == null)
                {
                    Console.WriteLine($"\nCreating tag '{oceanTagName}'");
                    oceanTag = await trainingApi.CreateTagAsync(project.Id, oceanTagName);
                }

                // add images
                Console.WriteLine($"\nAdding images to tag '{oceanTagName}'");
                imageFiles = (Directory.GetFiles(noObjectTrainImagesPath)).Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList();
                await trainingApi.CreateImagesFromFilesAsync(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>()
                {
                    oceanTag.Id
                }));

                #endregion

                #region Train the classifier
                Console.WriteLine("\nTraining");
                var iteration = await trainingApi.TrainProjectAsync(project.Id);

                do
                {
                    Thread.Sleep(1000);
                    iteration = await trainingApi.GetIterationAsync(project.Id, iteration.Id);
                }while (string.Equals("training", iteration.Status, StringComparison.OrdinalIgnoreCase));

                if (!string.Equals(iteration.Status, "completed", StringComparison.OrdinalIgnoreCase))
                {
                    throw new Exception($"An error occurred training the classifier. Iteration status is {iteration.Status}");
                }

                iteration.IsDefault = true;
                await trainingApi.UpdateIterationAsync(project.Id, iteration.Id, iteration);

                #endregion

                Console.WriteLine(
                    $@"\n
Your custom vision project ID is {project.Id.ToString()}.

Copy this Guid and add it to your application settings under the name 'CustomVisionProjectId'
"
                    );

                Console.WriteLine("\nFinished. Press Enter to exit");
                Console.Read();

                return(true);
            }
            catch (Exception e)
            {
                Console.Write(e);
                Console.WriteLine("\nPress Enter to exit");
                Console.Read();

                return(false);
            }
        }