You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

向SageMaker端点传入图片推理时SSL验证失败求助

SageMaker端点调用SSL验证失败(真实图片触发,空白图片正常)

问题描述

按照AWS博客训练并部署自定义YOLOv5模型到SageMaker端点,部署代码如下:

model = TensorFlowModel(model_data=model_data,
                        framework_version='2.8', role=role)

predictor = model.deploy(initial_instance_count=1,
                            instance_type=INSTANCE_TYPE,
                            endpoint_name=ENDPOINT_NAME)

调用端点时,使用真实图片处理后的payload会触发SSL验证失败错误,但传入空白图片的payload则正常工作。已确认笔记本、boto3客户端和SageMaker端点区域均为ap-southeast-2。

真实图片处理及调用代码:

# 图片处理
s3 = boto3.client('s3')
response = s3.get_object(Bucket=bucket_name, Key=key)
image_data = response['Body'].read()

image = Image.open(io.BytesIO(image_data))
image = image.resize((640, 640))
image_array = np.array(image)

image_data = np.array(image_array.astype(np.float32)/255.)
image_payload = json.dumps([image_data.tolist()])

# 端点调用
import boto3
from botocore.config import Config

ENDPOINT_NAME = 'yolov5-inference-test-new'

client = boto3.client('sagemaker-runtime', region_name = 'ap-southeast-2')

response = client.invoke_endpoint(EndpointName=ENDPOINT_NAME,
ContentType='application/json',
Body=image_payload)

空白图片处理代码:

blank_image = np.zeros((modelHeight,modelWidth,3), np.uint8)

blank_data = np.array(blank_image.astype(np.float32)/255.)
blank_payload = json.dumps([blank_data.tolist()])

问题分析

这种真实图片触发SSL错误、空白图片正常的情况,大概率不是真正的SSL证书问题,而是:

  • 真实图片序列化后的JSON payload体积远大于空白图片(像素值多样导致序列化字符串更长),引发请求超时,被客户端误判为SSL验证失败。
  • 小规格实例处理大payload时出现资源瓶颈,导致响应缓慢触发超时。

解决方案

1. 优化Payload格式,大幅减小体积

避免将图片数组转为list再JSON序列化,改用二进制格式传递:

# 图片处理改为直接输出JPEG二进制
from PIL import Image
import io

image = Image.open(io.BytesIO(image_data)).resize((640,640))
buffer = io.BytesIO()
image.save(buffer, format='JPEG')
image_payload = buffer.getvalue()

# 调用时修改ContentType为image/jpeg
response = client.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                                  ContentType='image/jpeg',
                                  Body=image_payload)

同时需要修改SageMaker端点的inference.py脚本,支持解析JPEG输入:

def input_fn(request_body, request_content_type):
    if request_content_type == 'image/jpeg':
        # 解码JPEG并转为模型所需格式
        image = Image.open(io.BytesIO(request_body))
        image_np = np.array(image.resize((640,640))).astype(np.float32)/255.
        return np.expand_dims(image_np, axis=0)  # 增加batch维度
    elif request_content_type == 'application/json':
        # 保留原有JSON格式支持(可选)
        data = json.loads(request_body)
        return np.array(data).astype(np.float32)
    else:
        raise ValueError(f"Unsupported content type: {request_content_type}")

2. 调整boto3客户端超时配置(针对JSON格式场景)

若坚持使用JSON格式,通过Config增加超时时间避免请求被中断:

config = Config(
    connect_timeout=30,
    read_timeout=30,
    retries={'max_attempts': 3}
)
client = boto3.client('sagemaker-runtime', region_name='ap-southeast-2', config=config)

3. 升级端点实例规格

如果使用t2.micro等小规格实例,处理大payload时可能出现CPU/内存瓶颈,建议升级到ml.t2.medium或更高规格的实例。

4. 验证SSL证书(排除证书问题)

通过curl直接测试端点,确认证书有效性:

curl -v https://runtime.sagemaker.ap-southeast-2.amazonaws.com/endpoints/yolov5-inference-test-new/invocations

若curl返回证书正常,则可确认问题根源为payload大小或超时。

内容的提问来源于stack exchange,提问作者InvisibleEcho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 18:13:22