Felix Niedermann
Felix Niedermann

Reputation: 319

Azure Custom Vision Api POST Request error, even correct ID's

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

Upvotes: 2

Views: 1005

Answers (1)

Nicolas R
Nicolas R

Reputation: 14619

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

Upvotes: 1

Related Questions