中提供的python代码。
去工作。
到目前为止,我已经修复了1个代码错误(训练片段中重复的代码行),还解决了https://docs.microsoft.com/en-us/azure/cognitive-services/custom-vision-service/quickstarts/image-classification?tabs=visual-studio&pivots=programming-language-python上发布的相关说明中的1个错误信息。关于如何获取预测资源id的说明是错误的。正确的方法在https://github.com/MicrosoftDocs/azure-docs/issues/28445上有解释
然而,代码仍然有一个问题;
行results = predictor.classify_image(project.id,publish_iteration_name,image_contents.read())导致错误;-无效迭代。
在此之前的所有代码似乎都可以工作。我可以进入Azure customer vision项目,看到已经创建了一个项目,上传了图像,完成了培训,发布了迭代,一切正常
我可以看到迭代的已发布名称是正确的。此外,我可以通过web浏览器门户使用项目和迭代,还可以通过将相同的.jpg文件上传到站点来进行快速测试预测。这就像预期的一样。
如何解决python代码中的错误;-无效迭代?
背景信息;-我使用visual studio 2019作为我的IDE,python版本3.7。在Azure中,我使用的是即付即用订阅。预测资源使用定价层“Standard”
下面列出了我的代码,其中删除了敏感信息(密钥等
非常感谢
'''
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 !
'''
# <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>发布于 2021-03-18 03:28:51
发布于 2021-03-20 17:19:12
我发现了导致此错误消息的原因;- Azure自定义视觉有缺陷;-如果用户键入的资源名称中有连字符...
当azure自定义视觉应用程序自动创建两个资源(训练和预测资源)时,它无法正确复制用户在创建第二个(预测)资源时输入的连字符(训练)资源名称。
例如;- a-resource-name被不正确地处理并且创建了以下错误的名称;-aresourcename- For (用于预测资源)。
当python代码使用这对资源时,这会导致问题。
我不相信上述缺陷被https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/resource-name-rules发布的命名限制所覆盖。
https://stackoverflow.com/questions/66657779
复制相似问题