Exemplo n.º 1
0
 /// <summary>
 /// Adds the provided images to the current project iteration
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='projectId'>
 /// The project id.
 /// </param>
 /// <param name='imageData'>
 /// </param>
 /// <param name='tagIds'>
 /// The tags ids to associate with the image batch.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <CreateImageSummaryModel> CreateImagesFromDataAsync(this ITrainingApi operations, System.Guid projectId, IEnumerable <Stream> imageData, IList <Guid> tagIds = default(IList <Guid>), CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.CreateImagesFromDataWithHttpMessagesAsync(projectId, imageData, tagIds, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
        /// <summary>
        /// Read and upload images to <paramref name="project"/> based on <paramref name="options"/>
        /// </summary>
        public static async Task <CreateImageSummaryModel> ReadAndUploadImagesAsync(this ITrainingApi trainingApi,
                                                                                    Project project, TrainingOptions options, ICollection <string> allowedTagNames)
        {
            var images = ImagesLoaderGenerator.GenerateImagesLoader(options, allowedTagNames).LoadImages();

            return(await ImagesUploaderGenerator.GenerateImagesUploader(options, trainingApi).UploadImagesAsync(images, project.Id));
        }
 /// <summary>
 /// Create tags for <paramref name="projectId"/> with <paramref name="tagNames"/>
 /// </summary>
 public static async Task CreateTagsAsync(this ITrainingApi trainingApi, Guid projectId, IEnumerable <string> tagNames)
 {
     foreach (var tag in tagNames)
     {
         await trainingApi.CreateTagAsync(projectId, tag);
     }
 }
        private static ProjectModel CreateProject(ITrainingApi trainingApi)
        {
            Console.Write("Project Name: ");
            var projectName = Console.ReadLine();

            return(trainingApi.CreateProject(projectName));
        }
 /// <summary>
 /// Check if iteration exists or not.
 /// </summary>
 public static bool CheckIfIterationExists(this ITrainingApi trainingApi, Guid projectId, Guid iterationId)
 {
     try
     {
         trainingApi.GetIterationWithHttpMessagesAsync(projectId, iterationId).Wait();
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
 /// <summary>
 /// Check if the project exists or not.
 /// </summary>
 public static bool CheckIfProjectExists(this ITrainingApi trainingApi, Guid projectId)
 {
     try
     {
         trainingApi.GetProjectAsync(projectId).Wait();
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
        /// <summary>
        /// Create a project using <paramref name="project"/>
        /// </summary>
        public static async Task <ProjectInfo> CreateProjectAsync(this ITrainingApi trainingApi, ProjectInfo project)
        {
            var domains      = trainingApi.GetDomains();
            var targetDomain = domains.FirstOrDefault(x => string.Equals(x.Name, project.Domain, StringComparison.OrdinalIgnoreCase));

            if (targetDomain == null)
            {
                var supportedDomains = domains.Select(x => x.Name).Aggregate((x, y) => $"{x},{y}");
                throw new InvalidOperationException($"Unsupported domain: {project.Domain}. Must be one of {supportedDomains}");
            }

            var projectModel = await trainingApi.CreateProjectAsync(project.Name, project.Description, targetDomain.Id);

            project.Id           = projectModel.Id;
            project.ThumbnailUri = projectModel.ThumbnailUri;
            return(project);
        }
        public override bool ValidateWithTrainingApi(ITrainingApi trainingApi, out string errorMessage)
        {
            if (!base.ValidateWithTrainingApi(trainingApi, out errorMessage))
            {
                return(false);
            }

            if (AllowedTagNames != null && AllowedTagNames.Count != 0)
            {
                var existingTagNames = new HashSet <string>(trainingApi.GetTagsAsync(ProjectId, IterationId).Result.Tags.Select(x => x.Name));
                var invalidTagNames  = AllowedTagNames.Where(x => !existingTagNames.Contains(x)).ToList();
                if (invalidTagNames.Count != 0)
                {
                    errorMessage = $"{ErrorMsgPrefix}Some tag names are invalid: {invalidTagNames.Aggregate((x, y) => x + "," + y)}";
                }
            }

            return(errorMessage == null);
        }
        private static void CreateTags(ITrainingApi trainingApi, ProjectModel project)
        {
            Console.Write("Number of tags: ");

            int numberOfTags;

            while (!int.TryParse(Console.ReadLine(), out numberOfTags))
            {
                Console.Write("Invalid number. Number of tags: ");
            }

            Console.WriteLine();

            for (var i = 1; i <= numberOfTags; i++)
            {
                Console.Write("Name for tag #" + i + ": ");
                var tagName = Console.ReadLine();

                var trainingTag = trainingApi.CreateTag(project.Id, tagName);

                Console.Write("Images folder: ");
                var imagesPath = Console.ReadLine();

                // Upload the images we need for training
                Console.WriteLine("\nLoading images...");

                var images = GetLoadedImagesFromDisk(imagesPath);

                // Upload in a single batch
                var summary = trainingApi.CreateImagesFromData(project.Id, images, new List <Guid> {
                    trainingTag.Id
                });

                if (summary.IsBatchSuccessful)
                {
                    Console.WriteLine("Done!\n");
                }
                else
                {
                    Console.WriteLine("Error while uploading images, please check your images path.\n");
                }
            }
        }
        private static void TrainProject(ITrainingApi trainingApi, ProjectModel project)
        {
            // Before training images, first create tags
            CreateTags(trainingApi, project);

            // Now there are images with tags start training the project
            Console.WriteLine("Training model...");
            var iteration = trainingApi.TrainProject(project.Id);

            // The returned iteration will be in progress, and can be queried periodically to see when it has completed
            while (iteration.Status == "Training")
            {
                Thread.Sleep(1000);

                // Re-query the iteration to get it's updated status
                iteration = trainingApi.GetIteration(project.Id, iteration.Id);
            }

            // The iteration is now trained. Make it the default project endpoint
            iteration.IsDefault = true;
            trainingApi.UpdateIteration(project.Id, iteration.Id, iteration);

            Console.WriteLine("Model trained!\n");
        }
Exemplo n.º 11
0
        public virtual bool ValidateWithTrainingApi(ITrainingApi trainingApi, out string errorMessage)
        {
            errorMessage = null;
            try
            {
                trainingApi.GetProjects();
            }
            catch (HttpOperationException ex)
            {
                if (ex.Message.Contains("NotFound"))
                {
                    errorMessage = $"{ErrorMsgPrefix}{ex.Message} It is likely the base url {BaseUri} is wrong.";
                }
                else if (ex.Message.Contains("Unauthorized"))
                {
                    errorMessage = $"{ErrorMsgPrefix}{ex.Message}. It is likely the training key is wrong.";
                }
                else
                {
                    errorMessage = ex.Message;
                }

                return(false);
            }

            if (ProjectId != Guid.Empty && !trainingApi.CheckIfProjectExists(ProjectId))
            {
                errorMessage = $"{ErrorMsgPrefix}Project with id {ProjectId} does not exist.";
            }
            else if (IterationId != null && !trainingApi.CheckIfIterationExists(ProjectId, IterationId.Value))
            {
                errorMessage = $"{ErrorMsgPrefix}Iteration with id {IterationId} does not exist within project with id {ProjectId}.";
            }

            return(errorMessage == null);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Generate an instance of <see cref="IImagesUploader"/> based on <paramref name="options"/>.
        /// </summary>
        /// <returns>Requested instance of the desired implementation of <see cref="IImagesUploader"/></returns>
        public static IImagesUploader GenerateImagesUploader(TrainingOptions options, ITrainingApi trainApi)
        {
            switch (options.ImageSource)
            {
            case "url":
                return(new UrlImagesUploader(trainApi, options.UploadBatchSize));

            case "local":
                return(new LocalImagesUploader(trainApi, options.UploadBatchSize));

            default:
                throw new InvalidOperationException($"Not supported source of images: {options.ImageSource}");
            }
        }
Exemplo n.º 13
0
 /// <summary>
 /// Adds the provided images to the current project iteration
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='projectId'>
 /// The project id.
 /// </param>
 /// <param name='imageData'>
 /// </param>
 /// <param name='tagIds'>
 /// The tags ids to associate with the image batch.
 /// </param>
 public static CreateImageSummaryModel CreateImagesFromData(this ITrainingApi operations, System.Guid projectId, IEnumerable <Stream> imageData, IList <Guid> tagIds = default(IList <Guid>))
 {
     return(operations.CreateImagesFromDataAsync(projectId, imageData, tagIds).GetAwaiter().GetResult());
 }
 protected ImagesUploaderInBatches(ITrainingApi trainingApi, int batchSize)
 {
     BatchSize   = batchSize;
     TrainingApi = trainingApi;
 }
Exemplo n.º 15
0
 public ImagesLoaderFromProject(ITrainingApi trainingApi, Guid projectId, Guid?iterationId, IEnumerable <string> allowedTagNames) : base(allowedTagNames)
 {
     TrainingApi = trainingApi;
     ProjectId   = projectId;
     IterationId = iterationId;
 }
Exemplo n.º 16
0
 public LocalImagesUploader(ITrainingApi trainingApi, int batchSize) : base(trainingApi, batchSize)
 {
 }
Exemplo n.º 17
0
 /// <summary>
 /// Adds the provided images to the current project iteration
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='projectId'>
 /// The project id.
 /// </param>
 /// <param name='imageData'>
 /// </param>
 /// <param name='tagIds'>
 /// The tags ids to associate with the image batch.
 /// </param>
 /// <param name='customHeaders'>
 /// Headers that will be added to request.
 /// </param>
 public static HttpOperationResponse <CreateImageSummaryModel> CreateImagesFromDataWithHttpMessages(this ITrainingApi operations, System.Guid projectId, IEnumerable <Stream> imageData, IList <Guid> tagIds = default(IList <Guid>), Dictionary <string, List <string> > customHeaders = null)
 {
     return(operations.CreateImagesFromDataWithHttpMessagesAsync(projectId, imageData, tagIds, customHeaders, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult());
 }
Exemplo n.º 18
0
 public UrlImagesUploader(ITrainingApi trainingApi, int batchSize, int retryTimes = 3) : base(trainingApi, batchSize)
 {
     RetryTimes = retryTimes;
 }