Amazon web services 使用自定义推断脚本调用sagemaker端点

Amazon web services 使用自定义推断脚本调用sagemaker端点,amazon-web-services,amazon-sagemaker,Amazon Web Services,Amazon Sagemaker,我使用以下代码部署了sagemaker端点: from sagemaker.pytorch import PyTorchModel from sagemaker import get_execution_role, Session sess = Session() role = get_execution_role() model = PyTorchModel(model_data=my_trained_model_location, role=ro

我使用以下代码部署了sagemaker端点:

from sagemaker.pytorch import PyTorchModel
from sagemaker import get_execution_role, Session

sess = Session()
role = get_execution_role()

model = PyTorchModel(model_data=my_trained_model_location,
                     role=role,
                     sagemaker_session=sess,
                     framework_version='1.5.0',
                     entry_point='inference.py',
                     source_dir='.')

predictor = model.deploy(initial_instance_count=1, 
                         instance_type='ml.m4.xlarge',
                         endpoint_name='my_endpoint')
如果我跑步:

import numpy as np

pseudo_data = [np.random.randn(1, 300), np.random.randn(6, 300), np.random.randn(3, 300), np.random.randn(7, 300), np.random.randn(5, 300)] # input data is a list of 2D numpy arrays with variable first dimension and fixed second dimension
result = predictor.predict(pseudo_data)
我可以生成没有错误的结果。但是,如果要调用端点并通过运行进行预测:

from sagemaker.predictor import RealTimePredictor

predictor = RealTimePredictor(endpoint='my_endpoint')
result = predictor.predict(pseudo_data)
我会得到一个错误:

Traceback (most recent call last):
  File "default_local.py", line 77, in <module>
    score = predictor.predict(input_data)
  File "/home/biggytruck/.local/lib/python3.6/site-packages/sagemaker/predictor.py", line 113, in predict
    response = self.sagemaker_session.sagemaker_runtime_client.invoke_endpoint(**request_args)
  File "/home/biggytruck/.local/lib/python3.6/site-packages/botocore/client.py", line 316, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/home/biggytruck/.local/lib/python3.6/site-packages/botocore/client.py", line 608, in _make_api_call
    api_params, operation_model, context=request_context)
  File "/home/biggytruck/.local/lib/python3.6/site-packages/botocore/client.py", line 656, in _convert_to_request_dict
    api_params, operation_model)
  File "/home/biggytruck/.local/lib/python3.6/site-packages/botocore/validate.py", line 297, in serialize_to_request
    raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter Body
回溯(最近一次呼叫最后一次):
文件“default_local.py”,第77行,在
分数=预测值。预测值(输入数据)
文件“/home/biggytrack/.local/lib/python3.6/site packages/sagemaker/predictor.py”,预测中第113行
response=self.sagemaker\u session.sagemaker\u runtime\u client.invoke\u endpoint(**请求参数)
文件“/home/biggytrack/.local/lib/python3.6/site packages/botocore/client.py”,第316行,在api调用中
返回self.\u make\u api\u调用(操作名称,kwargs)
文件“/home/biggyctruck/.local/lib/python3.6/site packages/botocore/client.py”,第608行,在make\u api\u调用中
api参数,操作模型,上下文=请求(上下文)
文件“/home/biggytrack/.local/lib/python3.6/site packages/botocore/client.py”,第656行,在“转换为请求”目录中
api(参数、操作(模型)
文件“/home/biggytrack/.local/lib/python3.6/site packages/botocore/validate.py”,第297行,在序列化请求中
raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError:参数验证失败:
参数体的类型无效

据我所知,发生错误的原因是我没有将
interference.py
作为入口点文件传入,这是处理输入所必需的,因为它不是Sagemaker支持的标准格式。但是,
sagemaker.predictor.realtimeprodictor
不允许我定义入口点文件。如何解决此问题?

您看到的错误来自客户端SageMaker Python SDK库,而不是您发布的远程端点

以下是
数据
参数的文档(在您的例子中,这是
伪_数据

data(object)–希望模型为其提供推断的输入数据。如果在创建RealTimePredictor时指定了序列化程序,则序列化程序的结果将作为输入数据发送。否则,数据必须是字节序列,然后predict方法按原样发送请求正文中的字节。

资料来源:


我的猜测是,
pseudo_数据
不是SageMaker Python SDK所期望的类型,它是一个
字节序列

,如我的代码所示,pseudo_数据基本上是一个numpy数组列表。我知道这不是Sagemaker Python SDK所期望的数据类型,因此我需要interference.py,其中定义了一个自定义输入函数,以正确处理我看到的情况。在将numpy数组列表发送到端点之前,您可以尝试将其转储为JSON字符串表示形式吗?