predictor.classify_image failed with error "Invalid iteration"

Samike 21 Reputation points

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: Invalid iteration

My code:

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,
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.
231 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,525 questions
{count} votes

Accepted answer
  1. GiftA-MSFT 11,161 Reputation points

    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(, publish_iteration_name, test_data)  
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. GiftA-MSFT 11,161 Reputation points

    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.