private async void btnTrain_Click(object sender, RoutedEventArgs e) { //btnTrain.IsEnabled = false; //var someTask = Task.Factory.StartNew(() => Train()); //await someTask; //btnTrain.IsEnabled = true; // Now there are images with tags start training the project Log("Training"); var iteration = await trainingApi.TrainProjectAsync(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); Log("Done Training!"); }
private void buttonTrain_Click(object sender, EventArgs e) { if (trainingApi.GetIterations(project.Id).Count < 10) { labelStatus.Text = "TRAINING"; 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 != "Completed") { Thread.Sleep(1000); // Re-query the iteration to get its 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); labelStatus.Text = "Done!"; } else { MessageBox.Show("The number of iterations is 10"); } }
private static void TrainTheModel(Project project) { try { var trainingKey = ConfigurationManager.AppSettings["CustomVision_TrainingKey"]; var trainingApi = new TrainingApi() { ApiKey = trainingKey }; var iteration = trainingApi.TrainProject(project.Id); Console.WriteLine($"\tWaiting for training process finishes"); // The returned iteration will be in progress, and can be queried periodically to see when it has completed while (iteration.Status == "Training") { Console.WriteLine($"\t..."); Thread.Sleep(1000); // Re-query the iteration to get it's updated status iteration = trainingApi.GetIteration(project.Id, iteration.Id); } Console.WriteLine($"\tUpdating default iteration"); // The iteration is now trained. Make it the default project endpoint iteration.IsDefault = true; trainingApi.UpdateIteration(project.Id, iteration.Id, iteration); } catch (Exception e) { Console.WriteLine(e); } }
/// <summary> /// Train a Model given a set of trainings /// </summary> /// <param name="project"></param> /// <param name="trainings"></param> /// <param name="token"></param> /// <returns></returns> public void TrainModel(ref ProjectModel project, IList <Models.Training> trainings) { // Save all Training Tasks to be performed //var tagCreationTasks = (from training in trainings // from tag in training.Tags // select TrainingApi.CreateTagAsync(project.Id, tag.ToString(), cancellationToken: token)). // Cast<Task>().ToList(); var tagNames = trainings.Select(t => t.TagName).Distinct(); var tagNameToId = new Dictionary <string, Guid>(); foreach (var tagName in tagNames) { var tagModel = TrainingApi.CreateTag(project.Id, tagName); tagNameToId.Add(tagName, tagModel.Id); } // Execute Trainings //await Task.WhenAny(tagCreationTasks); foreach (var training in trainings) { TrainingApi.CreateImagesFromData(project.Id, training.ImageFileStream, new List <string> { tagNameToId[training.TagName].ToString() }); } //var imageCreationTasks = (from training in trainings // from tag in training.Tags // select TrainingApi.CreateImagesFromDataAsync(project.Id, training.ImageFileStream, new List<string> { tag.ToString() }, cancellationToken: token)).Cast<Task>().ToList(); //await Task.WhenAny(imageCreationTasks); var iterationModel = TrainingApi.TrainProject(project.Id); // The returned iteration will be in progress, and can be queried periodically to see when it has completed while (iterationModel.Status == "Training") { Thread.Sleep(1000); // Re-query the iteration to get it's updated status iterationModel = TrainingApi.GetIteration(project.Id, iterationModel.Id); } iterationModel.IsDefault = true; // Complete Training TrainingApi.UpdateIteration(project.Id, iterationModel.Id, iterationModel); }
private static void TrainClassifier(Guid projectId, TrainingApi trainingApi) { Console.WriteLine("Training classifier"); var iteration = trainingApi.TrainProject(projectId); while (iteration.Status == "Training") { Thread.Sleep(1000); iteration = trainingApi.GetIteration(projectId, iteration.Id); } // Make the newly trained version the default for RESTful API requests iteration.IsDefault = true; trainingApi.UpdateIteration(projectId, iteration.Id, iteration); Console.WriteLine("Training complete. Press any key to exit."); }
private static void Train() { string trainingKey = "fdf8998652a44b5ea1380439f25d71ca"; string projectKey = "INat-ImageClassifier"; string trainPath = @"c:\data\Images\train\"; TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey); TrainingApi trainingApi = new TrainingApi(trainingCredentials); var project = trainingApi.CreateProject(projectKey); var files = new DirectoryInfo(trainPath).GetFiles("*", SearchOption.AllDirectories); foreach (var image in files) { if (tagCount >= 50) { break; } List <string> tags = new List <string>(); string tagName = image.Directory.Name.Replace("_", " "); var tagdata = trainingApi.GetTags(project.Id); CreateTags(trainingApi, project, tags, tagName, tagdata); var imagestream = new MemoryStream(File.ReadAllBytes(image.FullName)); Console.WriteLine("Sending image " + image.Name + " To Custom Vision AI for training..."); trainingApi.CreateImagesFromData(project.Id, imagestream, tags); } var iteration = trainingApi.TrainProject(project.Id); while (iteration.Status == "Training") { Thread.Sleep(100); iteration = trainingApi.GetIteration(project.Id, iteration.Id); Console.WriteLine("Training..."); } iteration.IsDefault = true; trainingApi.UpdateIteration(project.Id, iteration.Id, iteration); }
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(); }
public int Execute() { TrainingApi trainingApi = new TrainingApi() { ApiKey = _trainingKey }; Console.Write("\tTraining"); Iteration iteration = trainingApi.TrainProject(_projectId); while (iteration.Status == "Training") { Thread.Sleep(1000); Console.Write("."); iteration = trainingApi.GetIteration(_projectId, iteration.Id); } iteration.IsDefault = true; trainingApi.UpdateIteration(_projectId, iteration.Id, iteration); Console.WriteLine("done!"); return(0); }
static void Main(string[] args) { // Add your training & prediction key from the settings page of the portal string trainingKey = "<your training key here>"; string predictionKey = "<your prediction key here>"; // Create the Api, passing in the training key TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Find the object detection domain var domains = trainingApi.GetDomains(); var objDetectionDomain = domains.FirstOrDefault(d => d.Type == "ObjectDetection"); // Create a new project Console.WriteLine("Creating new project:"); var project = trainingApi.CreateProject("My New Project", null, objDetectionDomain.Id); // Make two tags in the new project var forkTag = trainingApi.CreateTag(project.Id, "fork"); var scissorsTag = trainingApi.CreateTag(project.Id, "scissors"); Dictionary <string, double[]> fileToRegionMap = new Dictionary <string, double[]>() { // FileName, Left, Top, Width, Height { "scissors_1", new double[] { 0.4007353, 0.194068655, 0.259803921, 0.6617647 } }, { "scissors_2", new double[] { 0.426470578, 0.185898721, 0.172794119, 0.5539216 } }, { "scissors_3", new double[] { 0.289215684, 0.259428144, 0.403186262, 0.421568632 } }, { "scissors_4", new double[] { 0.343137264, 0.105833367, 0.332107842, 0.8055556 } }, { "scissors_5", new double[] { 0.3125, 0.09766343, 0.435049027, 0.71405226 } }, { "scissors_6", new double[] { 0.379901975, 0.24308826, 0.32107842, 0.5718954 } }, { "scissors_7", new double[] { 0.341911763, 0.20714055, 0.3137255, 0.6356209 } }, { "scissors_8", new double[] { 0.231617644, 0.08459154, 0.504901946, 0.8480392 } }, { "scissors_9", new double[] { 0.170343131, 0.332957536, 0.767156839, 0.403594762 } }, { "scissors_10", new double[] { 0.204656869, 0.120539248, 0.5245098, 0.743464053 } }, { "scissors_11", new double[] { 0.05514706, 0.159754932, 0.799019635, 0.730392158 } }, { "scissors_12", new double[] { 0.265931368, 0.169558853, 0.5061275, 0.606209159 } }, { "scissors_13", new double[] { 0.241421565, 0.184264734, 0.448529422, 0.6830065 } }, { "scissors_14", new double[] { 0.05759804, 0.05027781, 0.75, 0.882352948 } }, { "scissors_15", new double[] { 0.191176474, 0.169558853, 0.6936275, 0.6748366 } }, { "scissors_16", new double[] { 0.1004902, 0.279036, 0.6911765, 0.477124184 } }, { "scissors_17", new double[] { 0.2720588, 0.131977156, 0.4987745, 0.6911765 } }, { "scissors_18", new double[] { 0.180147052, 0.112369314, 0.6262255, 0.6666667 } }, { "scissors_19", new double[] { 0.333333343, 0.0274019931, 0.443627447, 0.852941155 } }, { "scissors_20", new double[] { 0.158088237, 0.04047389, 0.6691176, 0.843137264 } }, { "fork_1", new double[] { 0.145833328, 0.3509314, 0.5894608, 0.238562092 } }, { "fork_2", new double[] { 0.294117659, 0.216944471, 0.534313738, 0.5980392 } }, { "fork_3", new double[] { 0.09191177, 0.0682516545, 0.757352948, 0.6143791 } }, { "fork_4", new double[] { 0.254901975, 0.185898721, 0.5232843, 0.594771266 } }, { "fork_5", new double[] { 0.2365196, 0.128709182, 0.5845588, 0.71405226 } }, { "fork_6", new double[] { 0.115196079, 0.133611143, 0.676470637, 0.6993464 } }, { "fork_7", new double[] { 0.164215669, 0.31008172, 0.767156839, 0.410130739 } }, { "fork_8", new double[] { 0.118872553, 0.318251669, 0.817401946, 0.225490168 } }, { "fork_9", new double[] { 0.18259804, 0.2136765, 0.6335784, 0.643790841 } }, { "fork_10", new double[] { 0.05269608, 0.282303959, 0.8088235, 0.452614367 } }, { "fork_11", new double[] { 0.05759804, 0.0894935, 0.9007353, 0.3251634 } }, { "fork_12", new double[] { 0.3345588, 0.07315363, 0.375, 0.9150327 } }, { "fork_13", new double[] { 0.269607842, 0.194068655, 0.4093137, 0.6732026 } }, { "fork_14", new double[] { 0.143382356, 0.218578458, 0.7977941, 0.295751631 } }, { "fork_15", new double[] { 0.19240196, 0.0633497, 0.5710784, 0.8398692 } }, { "fork_16", new double[] { 0.140931368, 0.480016381, 0.6838235, 0.240196079 } }, { "fork_17", new double[] { 0.305147052, 0.2512582, 0.4791667, 0.5408496 } }, { "fork_18", new double[] { 0.234068632, 0.445702642, 0.6127451, 0.344771236 } }, { "fork_19", new double[] { 0.219362751, 0.141781077, 0.5919118, 0.6683006 } }, { "fork_20", new double[] { 0.180147052, 0.239820287, 0.6887255, 0.235294119 } } }; // Add all images for fork var imagePath = Path.Combine("Images", "fork"); var imageFileEntries = new List <ImageFileCreateEntry>(); foreach (var fileName in Directory.EnumerateFiles(imagePath)) { var region = fileToRegionMap[Path.GetFileNameWithoutExtension(fileName)]; imageFileEntries.Add(new ImageFileCreateEntry(fileName, File.ReadAllBytes(fileName), null, new List <Region>(new Region[] { new Region(forkTag.Id, region[0], region[1], region[2], region[3]) }))); } trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFileEntries)); // Add all images for scissors imagePath = Path.Combine("Images", "scissors"); imageFileEntries = new List <ImageFileCreateEntry>(); foreach (var fileName in Directory.EnumerateFiles(imagePath)) { var region = fileToRegionMap[Path.GetFileNameWithoutExtension(fileName)]; imageFileEntries.Add(new ImageFileCreateEntry(fileName, File.ReadAllBytes(fileName), null, new List <Region>(new Region[] { new Region(scissorsTag.Id, region[0], region[1], region[2], region[3]) }))); } trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFileEntries)); // Now there are images with tags start training the project Console.WriteLine("\tTraining"); 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 its 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("Done!\n"); // Now there is a trained endpoint, it can be used to make a prediction // Create a prediction endpoint, passing in the obtained prediction key PredictionEndpoint endpoint = new PredictionEndpoint() { ApiKey = predictionKey }; // Make a prediction against the new project Console.WriteLine("Making a prediction:"); var imageFile = Path.Combine("Images", "test", "test_image.jpg"); using (var stream = File.OpenRead(imageFile)) { var result = endpoint.PredictImage(project.Id, File.OpenRead(imageFile)); // Loop over each prediction and write out the results foreach (var c in result.Predictions) { Console.WriteLine($"\t{c.TagName}: {c.Probability:P1} [ {c.BoundingBox.Left}, {c.BoundingBox.Top}, {c.BoundingBox.Width}, {c.BoundingBox.Height} ]"); } } Console.ReadKey(); }
public static async Task TrainAsync(ParsingOptions options) { // Create the Api, passing in the training key var trainingApi = new TrainingApi { ApiKey = options.TrainingKey }; if (options.Delete) { try { await DeleteImagesAndTagsAsync(options, trainingApi); Console.WriteLine("Images and tags successfully deleted."); } catch { } return; } var fullFolder = Path.GetFullPath(options.Folder); if (!Directory.Exists(fullFolder)) { Console.WriteLine($"Error: folder \"{fullFolder}\" does not exist."); Console.WriteLine(string.Empty); return; } try { await DeleteImagesAndTagsAsync(options, trainingApi); // Creates the string for resized images. string resizeString = null; if (options.Width.GetValueOrDefault() > 0) { resizeString += $"width={options.Width}&"; } if (options.Height.GetValueOrDefault() > 0) { resizeString += $"height={options.Height}&"; } if (!string.IsNullOrWhiteSpace(resizeString)) { resizeString += "crop=auto&scale=both"; } foreach (var dir in Directory.EnumerateDirectories(fullFolder).Where(f => !Path.GetFileName(f).StartsWith("!"))) { var tagName = Path.GetFileName(dir).ToLower(); Console.WriteLine($"\nCreating tag '{tagName}'..."); var tag = await trainingApi.CreateTagAsync(options.ProjectId, tagName); var images = Directory.EnumerateFiles(dir, "*.*", SearchOption.AllDirectories) .Where(s => s.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase) || s.EndsWith(".jpeg", StringComparison.InvariantCultureIgnoreCase) || s.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase) || s.EndsWith(".bmp", StringComparison.InvariantCultureIgnoreCase)); foreach (var image in images) { var imageName = Path.GetFileName(image); Console.WriteLine($"Uploading image {imageName}..."); // Resizes the image before sending it to the service. using (var input = new MemoryStream(File.ReadAllBytes(image))) { if (!string.IsNullOrWhiteSpace(resizeString)) { using (var output = new MemoryStream()) { ImageBuilder.Current.Build(input, output, new ResizeSettings(resizeString)); output.Position = 0; await trainingApi.CreateImagesFromDataAsync(options.ProjectId, output, new List <string>() { tag.Id.ToString() }); } } else { await trainingApi.CreateImagesFromDataAsync(options.ProjectId, input, new List <string>() { tag.Id.ToString() }); } } } } // Now there are images with tags start training the project Console.WriteLine("\nTraining..."); var iteration = await trainingApi.TrainProjectAsync(options.ProjectId); // The returned iteration will be in progress, and can be queried periodically to see when it has completed while (iteration.Status == "Training") { await Task.Delay(1000); // Re-query the iteration to get it's updated status iteration = trainingApi.GetIteration(options.ProjectId, iteration.Id); } // The iteration is now trained. Make it the default project endpoint iteration.IsDefault = true; trainingApi.UpdateIteration(options.ProjectId, iteration.Id, iteration); Console.WriteLine("Training completed.\n"); } catch (Exception ex) { Console.WriteLine($"\nUnexpected error: {ex.GetBaseException()?.Message}.\n"); } }
public static async Task TrainAsync(ParsingOptions options) { // Create the Api, passing in the training key var trainingApi = new TrainingApi { ApiKey = options.TrainingKey }; CognitiveServiceTrainerStorage storageImages = new CognitiveServiceTrainerStorage(); if (options.Delete) { try { await DeleteImagesAndTagsAsync(options, trainingApi); storageImages.DeleteDB(); Console.WriteLine("Images and tags successfully deleted."); } catch { } if (string.IsNullOrEmpty(options.Folder)) { return; } } var fullFolder = Path.GetFullPath(options.Folder); if (!Directory.Exists(fullFolder)) { Console.WriteLine($"Error: folder \"{fullFolder}\" does not exist."); Console.WriteLine(string.Empty); return; } try { //await DeleteImagesAndTagsAsync(options, trainingApi); foreach (var dir in Directory.EnumerateDirectories(fullFolder).Where(f => !Path.GetFileName(f).StartsWith("!"))) { var tagName = Path.GetFileName(dir).ToLower(); Console.WriteLine($"\nCheck latest images uploaded '{tagName}'..."); IList <Microsoft.Cognitive.CustomVision.Training.Models.Image> imagesAlreadyExists = new List <Microsoft.Cognitive.CustomVision.Training.Models.Image>(); IList <Microsoft.Cognitive.CustomVision.Training.Models.Image> imagesAlreadyExistsTmp = new List <Microsoft.Cognitive.CustomVision.Training.Models.Image>(); int skip = 0; while ((imagesAlreadyExistsTmp = await trainingApi.GetTaggedImagesAsync(options.ProjectId, tagIds: new[] { tagName }, take: 50, skip: skip)).Any()) { skip += 50; foreach (var item in imagesAlreadyExistsTmp) { imagesAlreadyExists.Add(item); } } Console.WriteLine($"\nCreating tag '{tagName}'..."); var tagExist = storageImages.FindTag(tagName, options.ProjectId); Tag tag = null; if (tagExist == null) { tag = await trainingApi.CreateTagAsync(options.ProjectId, tagName); storageImages.InsertTag(new Storage.Collections.StorageTag { IdCustomVision = tag.Id, TagName = tag.Name, ProjectId = options.ProjectId }); } else { if (trainingApi.GetTag(tagExist.ProjectId, tagExist.IdCustomVision) == null) { await trainingApi.DeleteTagAsync(options.ProjectId, tagExist.IdCustomVision); tag = await trainingApi.CreateTagAsync(options.ProjectId, tagName); storageImages.InsertTag(new Storage.Collections.StorageTag { IdCustomVision = tag.Id, TagName = tag.Name, ProjectId = options.ProjectId }); } else { tag = new Tag(tagExist.IdCustomVision, tagName); } } var images = Directory.EnumerateFiles(dir, "*.*", SearchOption.AllDirectories) .Where(s => s.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase) || s.EndsWith(".jpeg", StringComparison.InvariantCultureIgnoreCase) || s.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase) || s.EndsWith(".bmp", StringComparison.InvariantCultureIgnoreCase)).ToList(); List <ImageDto> tempImages = new List <ImageDto>(); for (int i = 0; i < images.Count; i++) { Stream imageToUpload = null; string image = images.ElementAt(i); var imageName = Path.GetFileName(image); var storageImage = storageImages.FindImage(image, options.ProjectId); if (storageImage == null || !imagesAlreadyExists.Any(x => x.Id == storageImage.IdCustomVision)) { // Resizes the image before sending it to the service. using (var input = new MemoryStream(File.ReadAllBytes(image))) { if (options.Width.GetValueOrDefault() > 0 || options.Height.GetValueOrDefault() > 0) { imageToUpload = await ResizeImageAsync(input, options.Width.GetValueOrDefault(), options.Height.GetValueOrDefault()); } else { imageToUpload = input; } tempImages.Add(new ImageDto { FullName = image, FileName = imageName, Content = imageToUpload.ToByteArray(), Tag = tag }); imageToUpload.Dispose(); } } else { Console.WriteLine($"Image already exist {imageName}..."); } //Persist batch images if (tempImages.Count % 32 == 0 && tempImages.Any() || (i == images.Count - 1)) { await UploadImagesAsync(tempImages); tempImages.Clear(); tempImages.Capacity = 0; } } } // Now there are images with tags start training the project Console.WriteLine("\nTraining..."); var iteration = await trainingApi.TrainProjectAsync(options.ProjectId); // The returned iteration will be in progress, and can be queried periodically to see when it has completed while (iteration.Status == "Training") { await Task.Delay(1000); // Re-query the iteration to get it's updated status iteration = trainingApi.GetIteration(options.ProjectId, iteration.Id); } // The iteration is now trained. Make it the default project endpoint iteration.IsDefault = true; trainingApi.UpdateIteration(options.ProjectId, iteration.Id, iteration); Console.WriteLine("Training completed.\n"); } catch (Exception ex) { Console.WriteLine($"\nUnexpected error: {ex.GetBaseException()?.Message}.\n"); } async Task UploadImagesAsync(List <ImageDto> images) { ImageFileCreateBatch imageFileCreateBatch = new ImageFileCreateBatch(); imageFileCreateBatch.Images = new List <ImageFileCreateEntry>(); foreach (var img in images) { imageFileCreateBatch.Images.Add(new ImageFileCreateEntry { Name = img.FullName, TagIds = new [] { img.Tag.Id }, Contents = img.Content }); Console.WriteLine($"Uploading image {img.FileName}..."); } await Policy .Handle <Exception>() .WaitAndRetryAsync(retries) .ExecuteAsync(async() => { ImageCreateSummary reponseCognitiveService = await trainingApi.CreateImagesFromFilesAsync(options.ProjectId, imageFileCreateBatch); if (reponseCognitiveService.Images != null) { for (int i = 0; i < reponseCognitiveService.Images.Count; i++) { var img = reponseCognitiveService.Images.ElementAt(i); // https://docs.microsoft.com/en-us/rest/api/cognitiveservices/customvisiontraining/createimagesfrompredictions/createimagesfrompredictions if ((img.Status == "OK" || img.Status == "OKDuplicate") && img.Image != null) { var uploadedImage = img.Image; var tagsToStore = uploadedImage.Tags? .Select(x => new Storage.Collections.StorageImageTag() { Created = x.Created, TagId = x.TagId }).ToList(); storageImages.InsertImage(new Storage.Collections.StorageImage() { ProjectId = options.ProjectId, FullFileName = imageFileCreateBatch.Images.ElementAt(i).Name, IdCustomVision = uploadedImage.Id, ImageUri = uploadedImage.ImageUri, Created = uploadedImage.Created, Height = uploadedImage.Height, Tags = tagsToStore, ThumbnailUri = uploadedImage.ThumbnailUri, Width = uploadedImage.Width }); } else { Console.WriteLine($"API bad response: {img.Status }"); throw new InvalidOperationException($"API bad response: {img.Status }"); } } } }); await Task.Delay(500); } }
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = nameof(TrainClassifier))] HttpRequestMessage req, TraceWriter log) { using (var analytic = new AnalyticService(new RequestTelemetry { Name = nameof(TrainClassifier) })) { try { var allTags = new List <string>(); var json = req.Content.ReadAsStringAsync().Result; var j = JObject.Parse(json); var gameId = (string)j["gameId"]; var imageUrls = j["imageUrls"].ToObject <List <string> >(); var tags = (JArray)j["tags"]; var game = CosmosDataService.Instance.GetItemAsync <Game>(gameId).Result; var api = new TrainingApi(new TrainingApiCredentials(ConfigManager.Instance.CustomVisionTrainingKey)); ProjectModel project = null; //Get the existing project for this game if there is one if (!string.IsNullOrEmpty(game.CustomVisionProjectId)) { try { project = api.GetProject(Guid.Parse(game.CustomVisionProjectId)); } catch (Exception) { } } //Otherwise create a new project and associate it with the game if (project == null) { project = api.CreateProject(game.Name, game.Id); game.CustomVisionProjectId = project.Id.ToString(); CosmosDataService.Instance.UpdateItemAsync <Game>(game).Wait(); } //Generate tag models for training var tagModels = new List <ImageTagModel>(); foreach (string tag in tags) { var model = api.CreateTag(project.Id, tag.Trim()); tagModels.Add(model); } //Batch the image urls that were sent up from Azure Storage (blob) var batch = new ImageUrlCreateBatch(tagModels.Select(m => m.Id).ToList(), imageUrls); var summary = api.CreateImagesFromUrls(project.Id, batch); if (!summary.IsBatchSuccessful) { return(req.CreateErrorResponse(HttpStatusCode.BadRequest, "Image batch was unsuccessful")); } //Traing the classifier and generate a new iteration, that we'll set as the default var iteration = api.TrainProject(project.Id); while (iteration.Status == "Training") { Thread.Sleep(1000); iteration = api.GetIteration(project.Id, iteration.Id); } iteration.IsDefault = true; api.UpdateIteration(project.Id, iteration.Id, iteration); return(req.CreateResponse(HttpStatusCode.OK, true)); } catch (Exception e) { analytic.TrackException(e); return(req.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } } }
static void Main(string[] args) { // Agregue su clave de entrenamiento desde la página de configuración del portale string trainingKey = "8f15a89a49a44979bf478d9391a45cd6"; // Crea el Api, pasando la clave de entrenamiento TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Create a new project Console.WriteLine("Creating new project:"); var project = trainingApi.CreateProject("My New Project"); // Crea un nuevo proyecto var hemlockTag = trainingApi.CreateTag(project.Id, "Hemlock"); var japaneseCherryTag = trainingApi.CreateTag(project.Id, "Japanese Cherry"); // Add some images to the tags Console.WriteLine("\tUploading images"); LoadImagesFromDisk(); // Las imágenes se pueden cargar de a una por vez foreach (var image in hemlockImages) { using (var stream = new MemoryStream(File.ReadAllBytes(image))) { trainingApi.CreateImagesFromData(project.Id, stream, new List <string>() { hemlockTag.Id.ToString() }); } } // O subido en un solo lote var imageFiles = japaneseCherryImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>() { japaneseCherryTag.Id })); // Ahora hay imágenes con etiquetas que comienzan a entrenar el proyecto Console.WriteLine("\tTraining"); var iteration = trainingApi.TrainProject(project.Id); // La iteración devuelta estará en progreso, y se puede consultar periódicamente para ver cuándo se completó while (iteration.Status == "Training") { Thread.Sleep(1000); // Vuelva a consultar la iteración para obtener su estado actualizado iteration = trainingApi.GetIteration(project.Id, iteration.Id); } // La iteración ahora está entrenada. Convertirlo en el punto final predeterminado del proyecto iteration.IsDefault = true; trainingApi.UpdateIteration(project.Id, iteration.Id, iteration); Console.WriteLine("Done!\n"); // Ahora hay un punto final entrenado, se puede usar para hacer una predicción // Agregue su clave de predicción desde la página de configuración del portal // La clave de predicción se usa en lugar de la clave de entrenamiento cuando se hacen predicciones string predictionKey = "559018cc3d434cef8095da2e8b8dd30c"; // Crear un punto final de predicción, pasando la clave de predicción obtenida PredictionEndpoint endpoint = new PredictionEndpoint() { ApiKey = predictionKey }; // Hacer una predicción contra el nuevo proyecto Console.WriteLine("Making a prediction:"); var result = endpoint.PredictImage(project.Id, testImage); // Pasa el cursor sobre cada predicción y escribe los resultados foreach (var c in result.Predictions) { Console.WriteLine($"\t{c.Tag}: {c.Probability:P1}"); } Console.ReadKey(); }
/// <summary> /// 更新Iteration信息 /// </summary> /// <param name="projectId"></param> /// <param name="iterationId"></param> /// <param name="iteration"></param> public void UpdateIteration(Guid projectId, Guid iterationId, IterationModel iteration) { trainingApi.UpdateIteration(projectId, iterationId, iteration); }
async public static Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = nameof(TrainClassifier))] HttpRequestMessage req, TraceWriter log) { using (var analytic = new AnalyticService(new RequestTelemetry { Name = nameof(TrainClassifier) })) { try { var allTags = new List <string>(); var json = req.Content.ReadAsStringAsync().Result; var j = JObject.Parse(json); var gameId = (string)j["gameId"]; var imageUrls = j["imageUrls"].ToObject <List <string> >(); var tags = (JArray)j["tags"]; var game = CosmosDataService.Instance.GetItemAsync <Game>(gameId).Result; TrainingApi api = new TrainingApi { ApiKey = ConfigManager.Instance.CustomVisionTrainingKey }; Project project = null; //Get the existing project for this game if there is one if (!string.IsNullOrEmpty(game.CustomVisionProjectId)) { try { project = api.GetProject(Guid.Parse(game.CustomVisionProjectId)); } catch (Exception) { } } //Otherwise create a new project and associate it with the game if (project == null) { project = api.CreateProject($"{game.Name}_{DateTime.Now.ToString()}_{Guid.NewGuid().ToString()}", game.Id); game.CustomVisionProjectId = project.Id.ToString(); CosmosDataService.Instance.UpdateItemAsync <Game>(game).Wait(); } var tagItems = tags.Select(t => api.CreateTag(project.Id, t.ToString().Trim())); var entries = imageUrls.Select(u => new ImageUrlCreateEntry(u)).ToList(); //Batch the image urls that were sent up from Azure Storage (blob) var batch = new ImageUrlCreateBatch(entries, tagItems.Select(t => t.Id).ToList()); var summary = api.CreateImagesFromUrls(project.Id, batch); //if(!summary.IsBatchSuccessful) // return req.CreateErrorResponse(HttpStatusCode.BadRequest, "Image batch was unsuccessful"); //Traing the classifier and generate a new iteration, that we'll set as the default var iteration = api.TrainProject(project.Id); while (iteration.Status == "Training") { Thread.Sleep(1000); iteration = api.GetIteration(project.Id, iteration.Id); } iteration.IsDefault = true; api.UpdateIteration(project.Id, iteration.Id, iteration); var data = new Event("Training classifier"); data.Add("project", project.Name); data.Add("iteration", iteration.Id); await EventHubService.Instance.SendEvent(data); return(req.CreateResponse(HttpStatusCode.OK, true)); } catch (Exception e) { analytic.TrackException(e); var baseException = e.GetBaseException(); var operationException = baseException as HttpOperationException; var reason = baseException.Message; if (operationException != null) { var jobj = JObject.Parse(operationException.Response.Content); var code = jobj.GetValue("Code"); if (code != null && !string.IsNullOrWhiteSpace(code.ToString())) { reason = code.ToString(); } } return(req.CreateErrorResponse(HttpStatusCode.BadRequest, reason)); } } }
static void Main(string[] args) { string trainingKey = GetTrainingKey(configTrainingKey, args); TrainingApi trainingApi = new TrainingApi { ApiKey = trainingKey }; Console.WriteLine("Creating new project:"); var project = trainingApi.CreateProject("Bike Type"); var MbikesTag = trainingApi.CreateTag(project.Id, "Mountain"); var RbikesTag = trainingApi.CreateTag(project.Id, "Racing"); Console.WriteLine("\tUploading images"); LoadImages(); foreach (var image in bikesImages) { trainingApi.CreateImagesFromData(project.Id, image, new List <string>() { MbikesTag.Id.ToString() }); } foreach (var image in rBikesImages) { trainingApi.CreateImagesFromData(project.Id, image, new List <string>() { RbikesTag.Id.ToString() }); } trainingApi.CreateImagesFromData(project.Id, testImage, new List <string>() { MbikesTag.Id.ToString() }); Console.WriteLine("\tTraining"); var iteration = trainingApi.TrainProject(project.Id); while (iteration.Status.Equals("Training")) { Thread.Sleep(1000); iteration = trainingApi.GetIteration(project.Id, iteration.Id); } iteration.IsDefault = true; trainingApi.UpdateIteration(project.Id, iteration.Id, iteration); Console.WriteLine("Done!\n"); var predictionKey = GetPredictionKey(configPredictionKey, args); PredictionEndpoint endpoint = new PredictionEndpoint { ApiKey = predictionKey }; Console.WriteLine("Making a prediction:"); var result = endpoint.PredictImage(project.Id, testImage); foreach (var c in result.Predictions) { Console.WriteLine($"\t{c.Tag}: {c.Probability:P1}"); } Console.ReadKey(); }
static void Main(string[] args) { // Add your training key from the settings page of the portal string trainingKey = "d34ef84918894544889d7136f32d4e67"; // Create the Api, passing in the training key TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Getting Project or create project Console.WriteLine("Getting project"); var project = trainingApi.GetProject(new Guid("06d4aeff-fb9d-453f-8912-5b2e89d1f1d4")); //var project = trainingApi.CreateProject("MurdersWeapons"); // Make two tags in the new project var batTag = trainingApi.CreateTag(project.Id, "Bat"); var candleTag = trainingApi.CreateTag(project.Id, "Candle"); var flashlightTag = trainingApi.CreateTag(project.Id, "Flashlight"); var gitarTag = trainingApi.CreateTag(project.Id, "Gitar"); var hammerTag = trainingApi.CreateTag(project.Id, "Hammer"); var screwdriverTag = trainingApi.CreateTag(project.Id, "Screwdriver"); // Add some images to the tags Console.WriteLine("\tUploading images"); LoadImagesFromDisk(); var imageFiles = batImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>() { batTag.Id })); imageFiles = candleImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>() { candleTag.Id })); imageFiles = flashlightImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>() { flashlightTag.Id })); imageFiles = gitarImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>() { gitarTag.Id })); imageFiles = hammerImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>() { hammerTag.Id })); imageFiles = screwdriverImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>() { screwdriverTag.Id })); // Now there are images with tags start training the project Console.WriteLine("\tTraining"); 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("Done!\n"); Console.ReadKey(); }
public void Main(string[] args) { // Add your training & prediction key from the settings page of the portal string trainingKey = "d7ba782c8051443c8557ff464418949f"; //string predictionKey = "<your prediction key here>"; // Create the Api, passing in the training key TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Create a new project Console.WriteLine("Creating new project:"); //var project = trainingApi.CreateProject("My New Project"); // Make two tags in the new project var hemlockTag = trainingApi.CreateTag(project.Id, "Hemlock"); var japaneseCherryTag = trainingApi.CreateTag(project.Id, "Japanese Cherry"); // Add some images to the tags Console.WriteLine("\tUploading images"); LoadImagesFromDisk(); // Images can be uploaded one at a time foreach (var image in hemlockImages) { using (var stream = new MemoryStream(File.ReadAllBytes(image))) { trainingApi.CreateImagesFromData(project.Id, stream, new List <string>() { hemlockTag.Id.ToString() }); } } // Or uploaded in a single batch var imageFiles = japaneseCherryImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFiles, new List <Guid>() { japaneseCherryTag.Id })); // Now there are images with tags start training the project Console.WriteLine("\tTraining"); 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("Done!\n"); // Now there is a trained endpoint, it can be used to make a prediction // Create a prediction endpoint, passing in obtained prediction key PredictionEndpoint endpoint = new PredictionEndpoint() { ApiKey = predictionKey }; // Make a prediction against the new project Console.WriteLine("Making a prediction:"); var result = endpoint.PredictImage(project.Id, testImage); // Loop over each prediction and write out the results foreach (var c in result.Predictions) { Console.WriteLine($"\t{c.TagName}: {c.Probability:P1}"); } Console.ReadKey(); }
static void Main(string[] args) { string chaveClassificacao = "<Chave de treinamento>"; string idProjeto = "<ID Projeto>"; // Crio o cliente para o Azure Custom View TrainingApi trainingApi = new TrainingApi() { ApiKey = chaveClassificacao }; Console.WriteLine("Iniciando as tags."); // Crio a tag shitzu Tag shitzuTag = trainingApi.CreateTag(new Guid(idProjeto), "Shitzu"); // Crio a tag poodle Tag poodleTag = trainingApi.CreateTag(new Guid(idProjeto), "Poodle"); // Carrego as imagens de shitzu string[] imagensShitzu = Directory.GetFiles(Path.Combine("Amostras", "Shitzu")).ToArray(); // Carrego as imagens de poodle string[] imagensPoodle = Directory.GetFiles(Path.Combine("Amostras", "Poodle")).ToArray(); Console.WriteLine("Cadastrando as imagens de shitzu."); // Cadastro das imagens de shitzu foreach (var imagem in imagensShitzu) { using (var stream = new MemoryStream(File.ReadAllBytes(imagem))) { trainingApi.CreateImagesFromData(new Guid(idProjeto), stream, new List <string>() { shitzuTag.Id.ToString() }); } } Console.WriteLine("Cadastrando as imagens de poodle."); // Cadastro das imagens de poodle foreach (var imagem in imagensPoodle) { using (var stream = new MemoryStream(File.ReadAllBytes(imagem))) { trainingApi.CreateImagesFromData(new Guid(idProjeto), stream, new List <string>() { poodleTag.Id.ToString() }); } } // Inicio o treinamento com as imagens cadastradas. Iteration interacao = trainingApi.TrainProject(new Guid(idProjeto)); Console.WriteLine("Treinando."); // Verifico periodicamente até concluir while (interacao.Status == "Training") { Thread.Sleep(1000); // Verifico novamente o status interacao = trainingApi.GetIteration(new Guid(idProjeto), interacao.Id); } // Agora que o treinamento está concluído, configuro como o endpoint padrão. interacao.IsDefault = true; trainingApi.UpdateIteration(new Guid(idProjeto), interacao.Id, interacao); Console.WriteLine("Finalizado."); }
static void Main(string[] args) { // You can either add your training key here, pass it on the command line, or type it in when the program runs string trainingKey = GetTrainingKey("<your key here>", args); // Create the Api, passing in a credentials object that contains the training key TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey); TrainingApi trainingApi = new TrainingApi(trainingCredentials); // Create a new project Console.WriteLine("Creating new project:"); var project = trainingApi.CreateProject("Car Assessment"); // Make two tags in the new project var WriteOffTag = trainingApi.CreateTag(project.Id, "WriteOff"); var DentTag = trainingApi.CreateTag(project.Id, "Dent"); // Add some images to the tags Console.WriteLine("\\tUploading images"); LoadImagesFromDisk(); // Images can be uploaded one at a time foreach (var image in WriteOffImages) { trainingApi.CreateImagesFromData(project.Id, image, new List <string> () { WriteOffTag.Id.ToString() }); } // Or uploaded in a single batch trainingApi.CreateImagesFromData(project.Id, DentImages, new List <Guid> () { DentTag.Id }); // Now there are images with tags start training the project Console.WriteLine("\\tTraining"); 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("Done!\\n"); // Now there is a trained endpoint, it can be used to make a prediction // Get the prediction key, which is used in place of the training key when making predictions var account = trainingApi.GetAccountInfo(); var predictionKey = account.Keys.PredictionKeys.PrimaryKey; // Create a prediction endpoint, passing in a prediction credentials object that contains the obtained prediction key PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey); PredictionEndpoint endpoint = new PredictionEndpoint(predictionEndpointCredentials); // Make a prediction against the new project Console.WriteLine("Making a prediction:"); var result = endpoint.PredictImage(project.Id, testImage); // Loop over each prediction and write out the results foreach (var c in result.Predictions) { Console.WriteLine($"\\t{c.Tag}: {c.Probability:P1}"); } Console.ReadKey(); }
public async Task Main() { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); TrainingApi trainingApi = new TrainingApi() { ApiKey = CustomVisionAPIKey }; trainingApi.HttpClient.Timeout = TimeSpan.FromMinutes(TimeoutMins); if (!QuickTest) { Console.WriteLine($"Creating Custom Vision Project: {ProjectName}"); var project = trainingApi.CreateProject(ProjectName); Console.WriteLine($"Scanning subfolders within: {ImagePath}"); List <Tag> allTags = new List <Tag>(); foreach (var folder in Directory.EnumerateDirectories(ImagePath)) { string folderName = Path.GetFileName(folder); var tagNames = folderName.Contains(",") ? folderName.Split(',').Select(t => t.Trim()).ToArray() : new string[] { folderName }; // Create tag for each comma separated value in subfolder name foreach (var tag in tagNames) { // Check we've not already created this tag from another subfolder if (!allTags.Any(t => t.Name.Equals(tag))) { Console.WriteLine($"Creating Tag: {tag}"); var imageTag = trainingApi.CreateTag(project.Id, tag); allTags.Add(imageTag); } } } foreach (var currentFolder in Directory.EnumerateDirectories(ImagePath)) { string folderName = Path.GetFileName(currentFolder); var tagNames = folderName.Contains(",") ? folderName.Split(',').Select(t => t.Trim()).ToArray() : new string[] { folderName }; try { // Load the images to be uploaded from disk into memory Console.WriteLine($"Uploading: {ImagePath}\\{folderName} images..."); var images = Directory.GetFiles(currentFolder).ToList(); var folderTags = allTags.Where(t => tagNames.Contains(t.Name)).Select(t => t.Id).ToList(); var imageFiles = images.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img), folderTags)).ToList(); var imageBatch = new ImageFileCreateBatch(imageFiles); var summary = await trainingApi.CreateImagesFromFilesAsync(project.Id, new ImageFileCreateBatch(imageFiles)); // List any images that didn't make it foreach (var imageResult in summary.Images.Where(i => !i.Status.Equals("OK"))) { Console.WriteLine($"{ImagePath}\\{folderName}\\{imageResult.SourceUrl}: {imageResult.Status}"); } Console.WriteLine($"Uploaded {summary.Images.Where(i => i.Status.Equals("OK")).Count()}/{images.Count()} images successfully from {ImagePath}\\{folderName}"); } catch (Exception exp) { Console.WriteLine($"Error processing {currentFolder}: {exp.Source}:{exp.Message}"); } } try { // Train CV model and set iteration to the default Console.WriteLine($"Training model"); var iteration = trainingApi.TrainProject(project.Id); while (iteration.Status.Equals("Training")) { Thread.Sleep(1000); iteration = trainingApi.GetIteration(project.Id, iteration.Id); Console.WriteLine($"Model status: {iteration.Status}"); } if (iteration.Status.Equals("Completed")) { iteration.IsDefault = true; trainingApi.UpdateIteration(project.Id, iteration.Id, iteration); Console.WriteLine($"Iteration: {iteration.Id} set as default"); } else { Console.WriteLine($"Iteration status: {iteration.Status}"); } } catch (Exception exp) { Console.WriteLine($"Error training model (check you have at least 5 images per tag and 2 tags)"); Console.WriteLine($"Error {exp.Source}: {exp.Message}"); } } else { // Quick test existing (trained) model Console.WriteLine($"Custom Vision Quick test: {ProjectName} with image {ImagePath}"); // Retrieve CV project var projects = trainingApi.GetProjects(); var project = projects.Where(p => p.Name.Equals(ProjectName)).FirstOrDefault(); if (project == null) { Console.WriteLine($"Can't find Custom Vision Project: {ProjectName}"); return; } // Read test image if (!File.Exists(ImagePath)) { Console.WriteLine($"Can't find image: {ImagePath}"); return; } var image = new MemoryStream(File.ReadAllBytes(ImagePath)); // Get the default iteration to test against and check results var iterations = trainingApi.GetIterations(project.Id); var defaultIteration = iterations.Where(i => i.IsDefault == true).FirstOrDefault(); if (defaultIteration == null) { Console.WriteLine($"No default iteration has been set"); return; } var result = trainingApi.QuickTestImage(project.Id, image, defaultIteration.Id); foreach (var prediction in result.Predictions) { Console.WriteLine($"Tag: {prediction.Tag} Probability: {prediction.Probability}"); } } // fin stopwatch.Stop(); Console.WriteLine($"Done."); Console.WriteLine($"Total time: {stopwatch.Elapsed}"); }
static void Main(string[] args) { // Add your training key from the settings page of the portal string trainingKey = "YOUR TRAINING KEY"; // Create the Api, passing in the training key TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Find the object detection domain var domains = trainingApi.GetDomains(); var objDetectionDomain = domains.FirstOrDefault(d => d.Type == "ObjectDetection"); // Create a new project Console.WriteLine("Creating new project:"); var project = trainingApi.CreateProject("CSharp Office", null, objDetectionDomain.Id); //var project = trainingApi.GetProject(); using (StreamReader r = new StreamReader("Office.json")) { string json = r.ReadToEnd(); Console.Write(json); var data = JObject.Parse(json); //Export Tags from code var inputTags = data["inputTags"].ToString().Split(','); foreach (string t in inputTags) { Console.WriteLine(t); trainingApi.CreateTag(project.Id, t); } // Get all tagsIDs created on custom vision and add into a dictionary Dictionary <string, Guid> imgtags = new Dictionary <string, Guid>(); foreach (Tag t in trainingApi.GetTags(project.Id)) { Console.WriteLine(t.Name + " - " + t.Id); imgtags.Add(t.Name, t.Id); } // Create Image TagIDs with normalized points Dictionary <string, double[]> imgtagdic = new Dictionary <string, double[]>(); foreach (var a in data["visitedFrames"]) { Console.WriteLine(a); try { foreach (var key in data["frames"][a.ToString()]) { double x1 = Convert.ToDouble(key["x1"].ToString()); double y1 = Convert.ToDouble(key["y1"].ToString()); double x2 = Convert.ToDouble(key["x2"].ToString()); double y2 = Convert.ToDouble(key["y2"].ToString()); int h = Convert.ToInt32(key["height"].ToString()); int w = Convert.ToInt32(key["width"].ToString()); double tleft = (double)x1 / (double)w; double ttop = (double)y1 / (double)h; double twidth = (double)(x2 - x1) / (double)w; double theight = (double)(y2 - y1) / (double)h; try { string tag = key["tags"][0].ToString(); // Defining UniqueID per tags in photo below imgtagdic.Add(imgtags[tag].ToString() + "," + a.ToString() + ',' + key["name"].ToString() + tag, new double[] { tleft, ttop, twidth, theight }); } catch { Console.WriteLine("An Error occured on imtagdic"); } } } catch { Console.WriteLine("An Error occured on json parsing"); } } // Add all images for fork var imagePath = Path.Combine("", "Office"); string[] allphotos = Directory.GetFiles(imagePath); var imageFileEntries = new List <ImageFileCreateEntry>(); foreach (var key in imgtagdic) { Guid tagguid = Guid.Parse(key.Key.Split(',')[0]); var fileName = allphotos[Convert.ToInt32(key.Key.Split(',')[1])]; imageFileEntries.Add(new ImageFileCreateEntry(fileName, File.ReadAllBytes(fileName), null, new List <Region>(new Region[] { new Region(tagguid, key.Value[0], key.Value[1], key.Value[2], key.Value[3]) }))); // //Tried the add list of tags //List<Guid> listtags = new List<Guid> { tagguid }; //imageFileEntries.Add(new ImageFileCreateEntry(fileName, File.ReadAllBytes(fileName), listtags, new List<Region>(new Region[] { new Region(tagguid, key.Value[0], key.Value[1], key.Value[2], key.Value[3]) }))); } Console.WriteLine("\tUpload has started!"); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFileEntries)); Console.WriteLine("\tUpload is done!"); // Now there are images with tags start training the project Console.WriteLine("\tTraining"); 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 != "Completed") { Thread.Sleep(1000); // Re-query the iteration to get its 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("Done!\n"); } }
public Boolean Train(string tagName, string description = null) { // Add your training & prediction key from the settings page of the portal string trainingKey = "6308b3b62b344e3f8e4170c4728deed2"; // Create the Api, passing in the training key TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; var project = trainingApi.GetProjects().First(f => f.Name == "WA-SE-AI"); // Make two tags in the new project Tag trainTag; try { trainTag = trainingApi.GetTags(project.Id).First(f => f.Name == tagName); } catch (Exception) { trainTag = trainingApi.CreateTag(project.Id, tagName, description); } // Add some images to the tags Console.WriteLine("Start load image into memory"); List <string> TrainImages = LoadImagesFromDisk(tagName); if (TrainImages != null) { Console.WriteLine("Uploading " + TrainImages.Count + "images"); var trainImageFiles = TrainImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(trainImageFiles, new List <Guid>() { trainTag.Id })); // Now there are images with tags start training the project Console.WriteLine("\tTraining"); 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("Training Done!\n"); return(true); } else { Console.WriteLine("No image found!\n"); return(true); } }
static void Main(string[] args) { // You can either add your training key here, pass it on the command line, or type it in when the program runs string trainingKey = GetTrainingKey(trainingKeyString, args); // Create the Api, passing in a credentials object that contains the training key TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey); TrainingApi trainingApi = new TrainingApi(trainingCredentials); // Create a new project Console.WriteLine("Creating new project:"); var project = trainingApi.CreateProject(projectName); // Create some tags, you need at least two var tag1 = trainingApi.CreateTag(project.Id, "tag1"); var tag2 = trainingApi.CreateTag(project.Id, "tag2"); // Add some images to the tags Console.Write("\n\tProcessing images"); // Upload using the path to the images, a reference to the training API, a ference to your project and a tag UploadImages(@"..\..\..\Images\1", trainingApi, project, new List <string>() { tag1.Id.ToString() }); UploadImages(@"..\..\..\Images\1", trainingApi, project, new List <string>() { tag2.Id.ToString() }); // Or uploaded in a single batch //trainingApi.CreateImagesFromData(project.Id, japaneseCherryImages, new List<Guid>() { japaneseCherryTag.Id }); // Now there are images with tags start training the project Console.WriteLine("\tStarting training"); IterationModel iteration = null; try { iteration = trainingApi.TrainProject(project.Id); } catch (Exception e) { Console.WriteLine($"Trainig could not be completed. Error: {e.Message}"); } if (iteration != null) { // 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); } Console.WriteLine($"\tFinished training iteration {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("Done!\n"); // Now there is a trained endpoint, it can be used to make a prediction // Get the prediction key, which is used in place of the training key when making predictions //var account = trainingApi.GetAccountInfo(); //var predictionKey = account.Keys.PredictionKeys.PrimaryKey; //// Create a prediction endpoint, passing in a prediction credentials object that contains the obtained prediction key //PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey); //PredictionEndpoint endpoint = new PredictionEndpoint(predictionEndpointCredentials); //// Make a prediction against the new project //Console.WriteLine("Making a prediction:"); //var result = endpoint.PredictImage(project.Id, testImage); //// Loop over each prediction and write out the results //foreach (var c in result.Predictions) //{ // Console.WriteLine($"\t{c.Tag}: {c.Probability:P1}"); //} } Console.ReadKey(); }
static void Main(string[] args) { // Add your training and prediction key from the settings page of the portal string trainingKey = "<add your training key here>"; string predictionKey = "<add your prediction key here>"; // Create the Api, passing in the training key TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Create a new project Console.WriteLine("Creating new project:"); var project = trainingApi.CreateProject("My First Project"); // Make two tags in the new project var hemlockTag = trainingApi.CreateTag(project.Id, "Hemlock"); var japaneseCherryTag = trainingApi.CreateTag(project.Id, "Japanese Cherry"); // Add some images to the tags Console.WriteLine("\\tUploading images"); LoadImagesFromDisk(); // Images are then uploaded foreach (var image in hemlockImages) { trainingApi.CreateImagesFromData(project.Id, image, new List <string>() { hemlockTag.Id.ToString() }); } foreach (var image in japaneseCherryImages) { trainingApi.CreateImagesFromData(project.Id, image, new List <string>() { japaneseCherryTag.Id.ToString() }); } // Now there are images with tags start training the project Console.WriteLine("\\tTraining"); 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("Done!\n"); // Now there is a trained endpoint, it can be used to make a prediction PredictionEndpoint endpoint = new PredictionEndpoint() { ApiKey = predictionKey }; // Make a prediction against the new project Console.WriteLine("Making a prediction:"); var result = endpoint.PredictImage(project.Id, testImage); // Loop over each prediction and write out the results foreach (var c in result.Predictions) { Console.WriteLine($"\t{c.TagName}: {c.Probability:P1}"); } Console.ReadKey(); }
static void Main(string[] args) { // Add your training & prediction key from the settings page of the portal string trainingKey = "d7ba782c8051443c8557ff464418949f"; // Create the Api, passing in the training key TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; // Create a new project Console.WriteLine("Creating new project:"); var project = trainingApi.GetProject(new Guid("65d4860d-036d-4e2a-97af-50c3afaec972")); var sourceDirectory = System.Configuration.ConfigurationSettings.AppSettings["ImageSourece"]; var imageDirectories = Directory.GetDirectories(sourceDirectory).ToList(); foreach (var directory in imageDirectories) { var diseaseTags = directory.Split(new string[] { "___" }, StringSplitOptions.None); if (diseaseTags.Count() < 2) { continue; } var diseaseTag = diseaseTags[1].Replace('_', ' '); if (diseaseTag == "healthy") { continue; } var plantName = diseaseTags[0].Substring(diseaseTags[0].LastIndexOf('\\') + 1).Replace('_', ' '); var imagefiles = Directory.GetFiles(Path.Combine(sourceDirectory, directory)).ToList(); var imagePerTag = 190; var tagName = string.Format("{0} {1}", plantName, diseaseTag); if (trainingApi.GetTags(project.Id).Any(t => t.Name == tagName)) { continue; } var tag = trainingApi.CreateTag(project.Id, tagName); var randomFiles = GetRandomFiles(imagefiles, imagePerTag); //max upload size 64 in a batch var batchStartIndex = 0; var batchsize = 64; while (batchStartIndex < imagePerTag) { var itemsCountToTake = (imagePerTag - batchStartIndex >= batchsize) ? batchsize : imagePerTag - batchStartIndex; var batchImages = randomFiles.Skip(batchStartIndex).Take(itemsCountToTake); var imageFilesToUpload = batchImages.Select(img => new ImageFileCreateEntry(Path.GetFileName(img), File.ReadAllBytes(img))).ToList(); trainingApi.CreateImagesFromFiles(project.Id, new ImageFileCreateBatch(imageFilesToUpload, new List <Guid>() { tag.Id })); batchStartIndex += itemsCountToTake; } } // Now there are images with tags start training the project Console.WriteLine("\tTraining"); 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") { System.Threading.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("Done!\n"); Console.ReadKey(); }
static void Main(string[] args) { var keys = GetApiKeys(); var trainingApi = new TrainingApi { ApiKey = keys.TrainingKey }; var predictionEndpoint = new PredictionEndpoint { ApiKey = keys.PredictionKey }; var projects = trainingApi.GetProjects(); var herbProject = projects.FirstOrDefault(p => p.Name == "Herbs"); Console.WriteLine("Press 1 to predict and 2 to train:"); var pathChoice = Console.ReadLine(); if ("1".Equals(pathChoice)) { Console.WriteLine("Press 1 to predict on a URL or 2 to predict on a local file:"); var predictType = Console.ReadLine(); if ("1".Equals(predictType)) { Console.WriteLine("Input the URL to an image to test:"); var imageUrl = Console.ReadLine(); if (herbProject != null) { var result = predictionEndpoint.PredictImageUrl(herbProject.Id, new Microsoft.Cognitive.CustomVision.Prediction.Models.ImageUrl(imageUrl)); PrintResults(result); } } else { Console.WriteLine("Input path to image to test:"); var imagePath = Console.ReadLine(); if (!File.Exists(imagePath)) { Console.WriteLine("File does not exist. Press enter to exit."); Console.ReadLine(); return; } Console.WriteLine("Image predictions:"); var imageFile = File.OpenRead(imagePath); if (herbProject != null) { var result = predictionEndpoint.PredictImage(herbProject.Id, imageFile); PrintResults(result); } else { Console.WriteLine("Project doesn't exist."); } } Console.ReadLine(); } else { Console.WriteLine("Input path to image to train model with:"); var imagePath = Console.ReadLine(); Console.WriteLine("What tag would you give this image? Rosemary, cilantro, or basil?"); var imageTag = Console.ReadLine(); var capitilizedTag = char.ToUpper(imageTag.First()) + imageTag.Substring(1).ToLower(); if (!File.Exists(imagePath)) { Console.WriteLine("File does not exist. Press enter to exit."); Console.ReadLine(); return; } var imageFile = File.OpenRead(imagePath); var tags = trainingApi.GetTags(herbProject.Id); var matchedTag = tags.Tags.FirstOrDefault(t => t.Name == capitilizedTag); var memoryStream = new MemoryStream(); imageFile.CopyTo(memoryStream); var fileCreateEntry = new ImageFileCreateEntry(imageFile.Name, memoryStream.ToArray()); var fileCreateBatch = new ImageFileCreateBatch { Images = new List <ImageFileCreateEntry> { fileCreateEntry }, TagIds = new List <Guid> { matchedTag.Id } }; var result = trainingApi.CreateImagesFromFiles(herbProject.Id, fileCreateBatch); var resultImage = result.Images.FirstOrDefault(); switch (resultImage.Status) { case "OKDuplicate": Console.WriteLine("Image is already used for training. Please use another to train with"); Console.ReadLine(); break; default: break; } var iteration = trainingApi.TrainProject(herbProject.Id); while (iteration.Status != "Completed") { System.Threading.Thread.Sleep(1000); iteration = trainingApi.GetIteration(herbProject.Id, iteration.Id); } iteration.IsDefault = true; trainingApi.UpdateIteration(herbProject.Id, iteration.Id, iteration); Console.WriteLine("Done training!"); Console.ReadLine(); } }
public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, TraceWriter log) { try { var allTags = new List <string>(); var json = req.Content.ReadAsStringAsync().Result; var jobj = JObject.Parse(json); var tags = (JArray)jobj["tags"]; var term = jobj["term"].ToString(); var projectId = jobj["projectId"].ToString(); var trainingKey = jobj["trainingKey"].ToString(); var offset = 0; if (jobj["offset"] != null) { offset = (int)jobj["offset"]; } var imageUrls = await SearchForImages(term, offset); var api = new TrainingApi(new TrainingApiCredentials(trainingKey)); var project = api.GetProject(Guid.Parse(projectId)); var tagModels = new List <ImageTagModel>(); var existingTags = api.GetTags(project.Id); foreach (string tag in tags) { ImageTagModel model = existingTags.Tags.SingleOrDefault(t => t.Name == tag); if (model == null) { model = api.CreateTag(project.Id, tag.Trim()); } tagModels.Add(model); } var batch = new ImageUrlCreateBatch(tagModels.Select(m => m.Id).ToList(), imageUrls); var summary = api.CreateImagesFromUrls(project.Id, batch); //if(!summary.IsBatchSuccessful) // return req.CreateErrorResponse(HttpStatusCode.BadRequest, "Image batch was unsuccessful"); //Traing the classifier and generate a new iteration, that we'll set as the default var iteration = api.TrainProject(project.Id); while (iteration.Status == "Training") { Thread.Sleep(1000); iteration = api.GetIteration(project.Id, iteration.Id); } iteration.IsDefault = true; api.UpdateIteration(project.Id, iteration.Id, iteration); return(req.CreateResponse(HttpStatusCode.OK, iteration.Id)); } catch (Exception e) { var exception = e.GetBaseException(); return(req.CreateErrorResponse(HttpStatusCode.BadRequest, exception.Message)); } async Task <List <string> > SearchForImages(string term, int offset) { var client = new HttpClient(); client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "c2adf0e5c057447ea9e0f50cc5202251"); var uri = $"https://api.cognitive.microsoft.com/bing/v7.0/images/search?count=50&q={term}&offset={offset}"; var json = await client.GetStringAsync(uri); var jobj = JObject.Parse(json); var arr = (JArray)jobj["value"]; var list = new List <string>(); foreach (var result in arr) { list.Add(result["contentUrl"].ToString()); } return(list); } }