Azure-Samples / cognitive-services-quickstart-code

Code Examples used by the Quickstarts in the Cognitive Services Documentation
MIT License
351 stars 518 forks source link

predictor.classify_image(project.id, publish_iteration_name, image_contents.read()) causes invalid iteration #223

Closed AgribotGit closed 3 years ago

AgribotGit commented 3 years ago

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

Using instructions posted at;- https://docs.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 !

'''

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 Replace with valid values ENDPOINT ="https://resource-group-name.cognitiveservices.azure.com/" training_key = "3589b< deleted from this >f5a8b95" prediction_key = "c229a2c0e82b4f" prediction_resource_id = "0261b07a074339" This value was copied from the subscription id , on the overview blade of the prediction resource this does NOT work prediction_resource_id="/subscriptions/0261b17d74339/resourceGroups/vision_group/providers/Microsoft.CognitiveServices/accounts/vicegrou-Prediction" taken from https://github.com/MicrosoftDocs/azure-docs/issues/28445 THIS SEEMS TO WORK 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) 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") Make two tags in the new project hemlock_tag = trainer.create_tag(project.id, "Hemlock") cherry_tag = trainer.create_tag(project.id, "Japanese Cherry") base_image_location = "/cognitive-services-python-sdk-samples/samples/vision/" base_image_location = "/" 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) 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!") ''' THIS IS A DUPLICATE AND CAUSES AN ERROR 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!") ''' 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)) > ### This issue is for a: (mark with an `x`) ``` - [X ] bug report -> please search issues before submitting - [ ] feature request - [ ] documentation issue or request - [ ] regression (a behavior that used to work and stopped in a new release) ``` ### Minimal steps to reproduce > using Visual Studio 2019 and using python version 3.7, on a windows 10 laptop interfacing to Azure custom vision. Using the set of example image files provided. Ran the code in visual studio de-bug mode. ### Any log messages given by the failure The line results = predictor.classify_image(project.id, publish_iteration_name, image_contents.read()) causes the reply;- invalid iteration. ### Expected/desired behavior It should run and send back the prediction results ### OS and Version? > Windows 10 ### Versions > ### Mention any other details that might be useful there is also duplicate lines of code ( a different problem ) see the comments in the above code. > --------------------------------------------------------------- > Thanks! We'll be in touch soon.
AgribotGit commented 3 years ago

I believe that I have found a defect in the azure custom vision app and I believe that the bug is the root cause of the problem I posted at

Please see the “solution ” I posted at https://stackoverflow.com/questions/66657779/predictor-classify-imageproject-id-publish-iteration-name-image-contents-read/66719954#66719954

Basically the custom vision app automatically creates an incorrect prediction resource name when it experiences a (correct ) test resource name that contains (permitted ) hyphens.

Note;-
I believe that the above app defect is not the cause of the two other problems I reported;-

) Incorrect instruction in the how-to documentation

) Duplicated lines of code in the example python code snippet.

Because I do not know where to report a product defect for the customer vision app, I posted information under the new idea portal.

The above defect really does need to be fixed.

With Regards

Agribot