代码之家  ›  专栏  ›  技术社区  ›  lazybot

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

  •  0
  • lazybot  · 技术社区  · 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=图像.open('test.jpg')

    导入base64

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

    predicter.predict(数据=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"
    }
    
    0 回复  |  直到 1 年前
        1
  •  1
  •   fucalost    1 年前

    lazybot

    据我所知,您正在使用SageMaker Python SDK将Hugging Face模型部署到SageMaker端点,看起来该模型根据您对PIL的使用对图像数据进行了处理。

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

    然而,这需要使用自定义 inference.py 文件( learn more here ),可能如下所示:

    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
        }
    

    祝你好运