# 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"
}
嘿懒机器人👋
我了解您正在使用 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
}
祝你好运!!