0

I’m trying to get the python code provided at

https://github.com/Azure-Samples/cognitive-services-quickstart-code/blob/master/python/CustomVision/ImageClassification/CustomVisionQuickstart.py

to work.

So far I’ve fixed 1 code error (duplicated lines of code in the training snippet) and also I resolved 1 incorrect piece of information in the associated instructions posted on https://learn.microsoft.com/en-us/azure/cognitive-services/custom-vision-service/quickstarts/image-classification?tabs=visual-studio&pivots=programming-language-python . The instruction about how to get the prediction resource id is wrong. The correct way is explained at https://github.com/MicrosoftDocs/azure-docs/issues/28445

However, the code still has a problem;-

The line results = predictor.classify_image(project.id, publish_iteration_name, image_contents.read()) causes an error ;- invalid iteration.

All the code before that seems to work. I can go into the Azure customer vision project and I see that a project has been created, images uploaded, training completed and the iteration published, all OK

I can see that the published name of the iteration is correct. Also I can use the project and the iteration via the web browser portal and I can do a quick-test prediction by uploading the same .jpg file to the site. That works as expected.

How can I resolve the error in the python code ;- invalid iteration ?

Background info;- I’m using visual studio 2019 as my IDE, with python version 3.7. In Azure I’m using a pay-as-you-go subscription. The prediction resource uses pricing tier “Standard”

My code is listed below , with the sensitive information ( keys, etc ) deleted

Many thanks

'''
Code taken from https://github.com/Azure-Samples/cognitive-services-quickstart-code/blob/master/python/CustomVision/ImageClassification/CustomVisionQuickstart.py

Using instructions posted at;-  https://learn.microsoft.com/en-us/azure/cognitive-services/custom-vision-service/quickstarts/image-classification?tabs=visual-studio&pivots=programming-language-python

Note;- both the above contain errors !

'''

# <snippet_imports>
from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
from azure.cognitiveservices.vision.customvision.training.models import ImageFileCreateBatch, ImageFileCreateEntry, Region
from msrest.authentication import ApiKeyCredentials
import time
# </snippet_imports>

# <snippet_creds>
# Replace with valid values
ENDPOINT ="https://resource-group-name.cognitiveservices.azure.com/"  

training_key = "3589b< deleted from this >f5a8b95"
prediction_key = "c229a2<deleted from this >c0e82b4f"

#prediction_resource_id = "0261b<does not work>07a074339" This value was copied from the subscription id , on the overview blade of the prediction resource 
# this does NOT work

prediction_resource_id="/subscriptions/0261b17d<deleted from this >74339/resourceGroups/vision_group/providers/Microsoft.CognitiveServices/accounts/vi<deleted from this >cegrou-Prediction"
# taken from https://github.com/MicrosoftDocs/azure-docs/issues/28445  THIS SEEMS TO WORK

# </snippet_creds>

# <snippet_auth>
credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(ENDPOINT, credentials)
prediction_credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
predictor = CustomVisionPredictionClient(ENDPOINT, prediction_credentials)
# </snippet_auth>


# <snippet_create>
publish_iteration_name = "classifyModel"


credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})


trainer = CustomVisionTrainingClient(ENDPOINT, credentials)

# Create a new project
print ("Creating project...")
project = trainer.create_project("MyProject")
# </snippet_create>

# <snippet_tags>
# Make two tags in the new project
hemlock_tag = trainer.create_tag(project.id, "Hemlock")
cherry_tag = trainer.create_tag(project.id, "Japanese Cherry")
# </snippet_tags>



# <snippet_upload>
#base_image_location = "<path to repo directory>/cognitive-services-python-sdk-samples/samples/vision/"
base_image_location = "<path deleted from this >/"
print("Adding images...")

image_list = []

for image_num in range(1, 11):
    file_name = "hemlock_{}.jpg".format(image_num)
    with open(base_image_location + "images/Hemlock/" + file_name, "rb") as image_contents:
        image_list.append(ImageFileCreateEntry(name=file_name, contents=image_contents.read(), tag_ids=[hemlock_tag.id]))

for image_num in range(1, 11):
    file_name = "japanese_cherry_{}.jpg".format(image_num)
    with open(base_image_location + "images/Japanese Cherry/" + file_name, "rb") as image_contents:
        image_list.append(ImageFileCreateEntry(name=file_name, contents=image_contents.read(), tag_ids=[cherry_tag.id]))

upload_result = trainer.create_images_from_files(project.id, ImageFileCreateBatch(images=image_list))
if not upload_result.is_batch_successful:
    print("Image batch upload failed.")
    for image in upload_result.images:
        print("Image status: ", image.status)
    exit(-1)
# </snippet_upload>

# <snippet_train>
print ("Training...")
iteration = trainer.train_project(project.id)
while (iteration.status != "Completed"):
    iteration = trainer.get_iteration(project.id, iteration.id)
    print ("Training status: " + iteration.status)
    time.sleep(1)


# The iteration is now trained. Publish it to the project endpoint
trainer.publish_iteration(project.id, iteration.id, publish_iteration_name, prediction_resource_id)
print ("Done!")
# </snippet_train>


'''
THIS IS A DUPLICATE AND CAUSES AN ERROR

# <snippet_publish>
# The iteration is now trained. Publish it to the project endpoint
trainer.publish_iteration(project.id, iteration.id, publish_iteration_name, prediction_resource_id) 
print ("Done!")
# </snippet_publish>
'''

# <snippet_test>
# Now there is a trained endpoint that can be used to make a prediction
prediction_credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
predictor = CustomVisionPredictionClient(ENDPOINT, prediction_credentials)

with open(base_image_location + "images/Test/test_image_2.jpg", "rb") as image_contents:

    results = predictor.classify_image(
        project.id, publish_iteration_name, image_contents.read())
# The above line does NOT work. It causes ... Invalid iteration  error !!

    # Display the results.
    for prediction in results.predictions:
        print("\t" + prediction.tag_name +
              ": {0:.2f}%".format(prediction.probability * 100))
# </snippet_test>
Name
  • 31
  • 2

2 Answers2

0

This error is likely to occur when the end point & key combination of the training and prediction resources are incorrect.

Alsoe, ensure the endpoints are correct for both Training and prediction resources.

Reference to a similar discussed thread.

Satya V
  • 3,811
  • 1
  • 6
  • 9
0

I have discovered what caused this error message;- Azure custom vision has a defect;- If a resource name typed in by a user has a hyphen in it ...

When the azure custom vision app automatically creates both resources ( a training and a prediction resource), it fails to correctly copy across the hyphenated (training) resource name that is entered by the user across when creating the 2nd (prediction) resource.

For example;- a-resource-name is incorrectly processed and the following wrong name is created;- aresourcename-Prediction (for the prediction resource).

That later causes a problem when python code uses the pair of resources.

I do NOT believe that the above defect is covered by the naming restrictions posted at [https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/resource-name-rules]

Name
  • 31
  • 2