如何在AWS Sagemaker中对TensorFlow 2.x模型运行推理?

rryofs0p  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(161)

我创建了一个MobileNetV2模型,可以将水果分为5类(苹果、胡萝卜、 cucumber 、梨和西葫芦)。我已经将.h5模型上传到AWS Sagemaker Notebook示例,并使用protobuff文件创建了文件夹层次结构。我还成功地创建了一个Sagemaker端点。然而,当尝试使用图像在此端点上运行推理时,我收到一个错误,声称在模型Graph中找不到输入Tensor名称。
我使用protobuff文件查找输入Tensor名称,它似乎是input_1:0。我不知道错误来自哪里。
笔记本的链接(如果你想看到完整的东西)在这里:https://colab.research.google.com/drive/1KqflVvWbZbXNzMQsnuqyCcV6ewJmMovC?usp=sharing

from tensorflow.keras.preprocessing import image
import numpy as np
import json
endpoint_name = 'tensorflow-inference-2023-06-15-03-24-26-323'
label_names = ['apple', 'carrot', 'cucumber', 'pear', 'zucchini']

runtime = boto3.client('runtime.sagemaker')

#photo = 'image-removebg-preview (4).png'
#img = Image.open(photo)
img = image.load_img("image-removebg-preview (4).png", target_size = (200,200))
x = image.img_to_array(img)
x = np.expand_dims(x, axis = 0)
x=x/255.0

payload = {"instances": x.tolist()}

# Invoke the endpoint
response = runtime.invoke_endpoint(EndpointName=endpoint_name, ContentType='application/json', Body=json.dumps(payload))

# Parse the response
result = json.loads(response['Body'].read().decode())
predictions = result['predictions'][0]

# Print the predicted label
predicted_label_index = np.argmax(predictions)
predicted_label = label_names[predicted_label_index]
print(predicted_label)
---------------------------------------------------------------------------
ModelError                                Traceback (most recent call last)
Cell In[6], line 19
     16 payload = {"instances": x.tolist()}
     18 # Invoke the endpoint
---> 19 response = runtime.invoke_endpoint(EndpointName=endpoint_name, ContentType='application/json', Body=json.dumps(payload))
     21 # Parse the response
     22 result = json.loads(response['Body'].read().decode())

File ~/anaconda3/envs/tensorflow2_p310/lib/python3.10/site-packages/botocore/client.py:530, in ClientCreator._create_api_method.<locals>._api_call(self, *args, **kwargs)
    526     raise TypeError(
    527         f"{py_operation_name}() only accepts keyword arguments."
    528     )
    529 # The "self" in this scope is referring to the BaseClient.
--> 530 return self._make_api_call(operation_name, kwargs)

File ~/anaconda3/envs/tensorflow2_p310/lib/python3.10/site-packages/botocore/client.py:964, in BaseClient._make_api_call(self, operation_name, api_params)
    962     error_code = parsed_response.get("Error", {}).get("Code")
    963     error_class = self.exceptions.from_code(error_code)
--> 964     raise error_class(parsed_response, operation_name)
    965 else:
    966     return parsed_response

ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (400) from primary with message "{
    "error": "Tensor input_1:0, specified in either feed_devices or fetch_devices was not found in the Graph"
}". See https://us-east-2.console.aws.amazon.com/cloudwatch/home?region=us-east-2#logEventViewer:group=/aws/sagemaker/Endpoints/tensorflow-inference-2023-06-15-03-24-26-323 in account 415514861026 for more information.
3hvapo4f

3hvapo4f1#

请测试Colab Notebook中的函数convert_model_to_aws给出的输出是否正确。遵循tensorflow最佳实践来保存和加载模型。它们的示例在Tensorflow Save and Load处给出。
一旦您验证了模型,请在train.py中编写您提供给TensorflowModel Class的处理程序函数。您可以遵循的文档是
1.从工件部署
1.处理程序函数
另外,请确保使用正确创建了model.tar.gz。它应该看起来像,

export
  └── Servo
        └── 0
            ├── saved_model.pb
            └── variables
                     └── ...

相关问题