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.
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
    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.

1 additional answer

Sort by: Most helpful
  1. GiftA-MSFT 11,161 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