简体   繁体   中英

Calling invoke_endpoint from AWS Lambda for a semantic segmentation model returning application/x-recordio-protobuf instead of image (Python)

I'm calling a semantic segmentation model from AWS Lamba with the invoke_endpoint function but instead of getting an image response I get a application/x-recordio-protobuf which I'm unable to convert to an image for further processing. The semantic segmentation model works as expected in my sagemaker notebook, the problem is when calling this from AWS Lambda. I need to call it from AWS Lambda or a similar service so I can connect it with an application that would pass an image imput through a rest service.

Here is the relevant part of my code:

runtime= boto3.client('runtime.sagemaker')
response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                                  ContentType='image/jpeg',
                                  Body=imageBody,
                                  Accept='image/jpeg')

I haven't been able to find good examples of how to properly invoke an endpoint specifically for semantic segmentation models from AWS Lambda. I also tried importing sagemaker, but AWS Lambda doesn't come with that module and and I couldn't find a good layer for sagemaker.

I would appreciate if anyone has an idea on how to be able to call a semantic segmentation model from AWS, pass it an image, and get an image back as a response.

I assume when you state it works in your notebook you are using.predict() - SageMaker Python SDK.

If so, you can make use of the boto3 client (which I see you have showcased). You would just need to serialize the imageBody as bytes the same way the SDK does and set the correct AcceptType and ContentType .

To query a trained model that is deployed to an endpoint, you need to provide an image and an AcceptType that denotes the type of output required. The endpoint takes JPEG images with an image/jpeg content type. If you request an AcceptType of image/png, the algorithm outputs a PNG file with a segmentation mask in the same format as the labels themselves. If you request an accept type ofapplication/x-recordio-protobuf, the algorithm returns class probabilities encoded in recordio-protobuf format.

https://docs.aws.amazon.com/sagemaker/latest/dg/semantic-segmentation.html#semantic-segmentation-inputoutput-inference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM