2

I'm trying to publish my Custom Vision Iteration after I trained it. But I always get a "Bad Request" Error.

I'm trying with the following line of code:

trainingApi.PublishIteration(ProjectID, iteration.Id, "Model", predictionResourceId);

It should Publish my Iteration but I just get an error.

I re-checked all my ID's but everything looks fine. Has the model name be something specific (start with lower letter or something)?

Edit:

I tried it now with a POST Request in Postman but now I receive:

{
    "code": "BadRequestInvalidPublishTarget",
    "message": "Invalid prediction resource id"
}

But I re-checked my Prediction Resource ID and it's correct.

Edit 2:

I thin I put the wrong thing into predictionId in the POST request, I just put in a ID but I think it should have been the /subscriptions/... part like described by microsoft. The problem now is:

{
    "code": "BadRequestInvalidPublishTarget",
    "message": "Invalid prediction id, please pass a prediction resource id."
}

For me this means it doesn't receive a prediction resource id, but I'm lost so I'm out of ideas what the problem could be.

Edit 3:

I forgot to add my POST Request:

https://xxx.cognitiveservices.azure.com/customvision/v3.0/training/projects/xxx/
iterations/xxx/publish?publishName=Model&predictionId=/subscriptions/xxx/
resourceGroups/CustomVision/providers/Microsoft.CognitiveServices/accounts/xxx
Felix Niedermann
  • 321
  • 3
  • 18

1 Answers1

1

Publish using POST request

If you want to ensure that you have the right syntax, you can check using Custom vision portal by doing the same steps.

For example when I try to publish an iteration of a project, I can see the following call in the console:

https://westeurope.api.cognitive.microsoft.com/customvision/v3.3/Training/projects/ID_OF_MY_PROJECT/iterations/ID_OF_MY_ITERATION/publish?predictionId=%2Fsubscriptions%2FID_OF_MY_AZURE_SUBSCRIPTION%2FresourceGroups%2FNAME_OF_MY_RESOURCE_GROUP%2Fproviders%2FMicrosoft.CognitiveServices%2Faccounts%2FNAME_OF_MY_CUSTOM_VISION_PREDICTION_RESOURCE&publishName=NAME_OF_MY_ITERATION

Demo: Sample from Custom Vision page

So yes, the "publicationId" value is looking like the one you mentioned but you have to encode the value of this string.

So change this:

predictionId=/subscriptions/xxx/
resourceGroups/CustomVision/providers/Microsoft.CognitiveServices/accounts/xxx

to

predictionId=%2Fsubscriptions%2Fxxx%2F
resourceGroups%2FCustomVision%2Fproviders%2FMicrosoft.CognitiveServices%2Faccounts%2Fxxx

in your call.

And be careful to use a prediction resource, not a training one.


Publish using C#

Here is a demo using C# and the official Custom Vision package hosted on Nuget (here)

using System;
using Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training;

namespace so65714960
{
    class Program
    {
        private static CustomVisionTrainingClient _trainingClient;

        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            _trainingClient = new CustomVisionTrainingClient(new ApiKeyServiceClientCredentials("PUT_YOUR_TRAINING_KEY_HERE"));
            // I'm specifying my endpoint here as I'm working on West Europe region
            _trainingClient.Endpoint = "https://westeurope.api.cognitive.microsoft.com/";

            var projectId = new Guid("4b...a5"); // Put your Project Id here
            var iterationId = new Guid("9d...e"); // Put your iteration Id here

            // Get iteration information
            var targetIteration = _trainingClient.GetIteration(projectId, iterationId);
            Console.WriteLine($"Iteration publish resource Id: '{targetIteration.OriginalPublishResourceId}'");

            // If originalPublishResourceId is not null, it is already published
            // For this demo purpose, we unpublish first to publish again after if it is already published
            if (!string.IsNullOrWhiteSpace(targetIteration.OriginalPublishResourceId))
            {
                _trainingClient.UnpublishIteration(projectId, iterationId);

                // Force status refresh
                targetIteration = _trainingClient.GetIteration(projectId, iterationId);
                Console.WriteLine($"Iteration publish resource Id after unpublish: '{targetIteration.OriginalPublishResourceId}'");
            }

            // Publish
            var publicationResourceId = "/subscriptions/7c...e8/resourceGroups/Cognitive_Demo/providers/Microsoft.CognitiveServices/accounts/NRO-Cognitive-CustomVision-WestEurope-Prediction-S0";
            var publication = _trainingClient.PublishIteration(projectId, iterationId, "Publication1", publicationResourceId);
            
            // Force status refresh
            targetIteration = _trainingClient.GetIteration(projectId, iterationId);
            Console.WriteLine($"Iteration publish resource Id after publish: '{targetIteration.OriginalPublishResourceId}'");
        }
    }
}

See my Azure resource used:

Azure Prediction resource

Nicolas R
  • 13,812
  • 2
  • 28
  • 57
  • I tried it with replacing "/" with "%2F" but that didn't work, sitll get the same error. Do you mean with "call in the console" there is a Custom Vision Console where you see the command that are executed on the page or are you just executing you command inside the Console? – Felix Niedermann Jan 18 '21 at 07:44
  • I was talking about your navigator developer tools, where you can see the network calls. Sorry for the typo in my answer: the predictionId field should point to the name of a prediction resource, not a training one! – Nicolas R Jan 18 '21 at 09:29
  • Sorry, I'm don't know muc about all this. What do you mean with Navigator Developer Tools, where can I find it? My prediction resource endpoint is a prediction resource -> xxx-prediciton. – Felix Niedermann Jan 18 '21 at 09:55
  • I've edited my answer to show the navigator developer tools. That's not linked to Azure or Cognitive Services, it's really your web browser developer tools (see the capture I've added). In addition, It will be difficult to add more help without checking your exact request and Azure resources names... – Nicolas R Jan 18 '21 at 09:58
  • But honestly, if you use C#, use the package to avoid any error in the POST request. I will try to quickly make a demo for you with a C# Console app – Nicolas R Jan 18 '21 at 10:01
  • I tried it with C# but there I also get Bad Request. – Felix Niedermann Jan 18 '21 at 10:12
  • 1
    I've added a C# demo which I tested on my side, it's working – Nicolas R Jan 18 '21 at 10:28
  • I'll test it right now thank you very much. With the HTTP Request inside the console it worked. – Felix Niedermann Jan 18 '21 at 10:29