predictor.classify_image failed with error "Invalid iteration"

Samike 21 Reputation points
2021-02-26T09:15:21.627+00:00

I've created and trained an image classification model using Azure Custom Vision (Cognitive Services) and published the model with API. Now, I've written a simple code in Python which takes an image from my local file and calls the API. However, I'm still getting this error even though the Iteration surely exists:

azure.cognitiveservices.vision.customvision.prediction.models._models_py3.CustomVisionErrorException: Invalid iteration

My code:

ENDPOINT = "https://northeurope.api.cognitive.microsoft.com/"
PROJECT_ID = "my project id"
prediction_key = "my prediction key"

prediction_credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
predictor = CustomVisionPredictionClient(ENDPOINT, prediction_credentials)

image_location = "/tmp/123.jpg"
publish_iteration_name = "Iteration2"

with open(image_location, "rb") as image_contents:
    results = predictor.classify_image(
        PROJECT_ID, publish_iteration_name, image_contents.read())
Azure AI Custom Vision
Azure AI Custom Vision
An Azure artificial intelligence service and end-to-end platform for applying computer vision to specific domains.
251 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,955 questions
{count} votes

Accepted answer
  1. GiftA-MSFT 11,171 Reputation points
    2021-03-10T22:05:49.007+00:00

    The classificationType shows null, meaning that you created an object detection model. Please follow this example instead to test your model. It should be predictor.detect_image() instead of predictor.classify_image(). Hope this helps!

    # Open the sample image and get back the prediction results.  
    with open(base_image_location + "images/Test/test_od_image.jpg", mode="rb") as test_data:  
        results = predictor.detect_image(project.id, publish_iteration_name, test_data)  
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. GiftA-MSFT 11,171 Reputation points
    2021-03-08T21:48:20.113+00:00

    Hi, the error has to do with the publish name. Ensure that you are entering a valid publish name that you specified when publishing the model. You can retrieve the publish name using the GetIterations API. If you're following this example, the publish name would be publish_iteration_name = "classifyModel". Hope this helps.

    75576-image.png


  2. Zhuohong Xie 0 Reputation points
    2024-11-30T20:53:58.8966667+00:00

    I had a similar issue with Microsoft Learn training. Later I found that I did not use the correct model name as in the tutorial, when publishing the model. Once the model name matched the one in the .env python file, the code ran smoothly. User's image

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.