Sagemaker-调用 InvokeEndpoint 操作时发生错误 (ModelError):从主服务器收到带有消息的客户端错误 (400)

问题描述 投票:0回答:1
# deploy model to SageMaker Inference
predictor = huggingface_model.deploy(
    initial_instance_count=1, # number of instances
    instance_type='ml.m5.xlarge', # ec2 instance type
    serializer=JSONSerializer(),
    deserializer=JSONDeserializer(),
)

从 PIL 导入图像

# 打开图像文件

test1 = Image.open('test.jpg')

导入base64

img_base64 = base64.b64encode(test1.tobytes()).decode('utf-8')

预测器.预测(数据=img_base64)


返回以下错误:

ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (400) from primary with message "{
  "code": 400,
  "type": "InternalServerException",
  "message": "\u0027str\u0027 object has no attribute \u0027pop\u0027"
}
deployment huggingface-transformers amazon-sagemaker endpoint
1个回答
0
投票

懒机器人👋

我了解您正在使用 SageMaker Python SDK 将 Hugging Face 模型部署到 SageMaker 端点,并且看起来该模型根据您对 PIL 的使用,对图像数据做了一些操作。

考虑到您对图像的使用,

JSONSerializer
在这里似乎是错误的选择。如果您使用 DataSerializer,图像文件的原始片段可以发送到您的端点。

但是,这需要使用自定义

inference.py
文件(在此处了解更多信息),可能如下所示:

import io
import torch
from PIL import Image
from transformers import AutoModel, AutoTokenizer

DEVICE = "cuda:0" if torch.cuda.is_available() else "cpu"

def model_fn(model_dir: str)
    model = AutoModel.from_pretrained(model_dir)
    tokenizer = AutoTokenizer.from_pretrained(model_dir)
    model = model.to(DEVICE)

def predict_fn(data):
    # serialize bytes to PIL.Image
    image_data = ... # byte values of the image
    image = Image.open(io.BytesIO(image_data))
    # ... your model inference code ...
    return {
        "outputs": ...,
        "device": DEVICE
    }

祝你好运!!

© www.soinside.com 2019 - 2024. All rights reserved.