Once deployed, I have an S3 bucket where I am uploading .jpg images too. This S3 bucket triggers a Lambda function to invoke the SageMaker endpoint.
However, I am running into difficulty getting this endpoint working! Playing around, I have seen the endpoint can handle a maximum file size of 5 MB, which I have accounted for.
I have tried calling the endpoint with the image/content type set to ‘image/jpeg’ and passing the file as is from S3. I have tried converting the image file to bytes also. Nothing I have attempted is allowing me to successfully call the endpoint and return the classification.
Any help in this regard would be greatly appreciated!
Hm, ok, I probably need some more info. What transformers version are you using, what does your inference script look like, how do you invoke the endpoint, what serializer are you using, etc. Anything than can help
You can see that this notebook uses a DataSerializer instead:
# create a serializer for the data
image_serializer = DataSerializer(content_type='image/x-image') # using x-image to support multiple image formats
# deploy model to SageMaker Inference
predictor = huggingface_model.deploy(
initial_instance_count=1, # number of instances
instance_type='ml.g4dn.xlarge', # ec2 instance type
serializer=image_serializer, # serializer for image data.
)
Hopefully that fixes your issue. You can learn more about Sagemaker Serializers here
Here is the code for deploying the wildifre model in SageMaker Notebook that I am using.
import base64
import json
import sagemaker
# Role
role = sagemaker.get_execution_role()
print(role)
# SageMaker
from sagemaker.huggingface import HuggingFaceModel
from sagemaker.serializers import DataSerializer
role = sagemaker.get_execution_role()
# Hub Model configuration. https://huggingface.co/models
hub = {
'HF_MODEL_ID':'EdBianchi/vit-fire-detection',
'HF_TASK':'image-classification'
}
# create Hugging Face Model Class
huggingface_model = HuggingFaceModel(
transformers_version='4.17.0',
pytorch_version='1.10.2',
py_version='py38',
env=hub,
role=role,
)
# create a serializer for the data
image_serializer = DataSerializer(content_type='image/x-image') # using x-image to support multiple image formats
# 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=image_serializer, # serializer for image data.
)
# download the image
!wget https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/raw/main/ADE_val_00000001.jpg
# File path
image_path = "ADE_val_00000001.jpg"
# Call the endpoint
with open(image_path, "rb") as data_file:
image_data = data_file.read()
res = predictor.predict(data=image_data)
print(res)
I run into this error…
ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (400) from primary with message "{
"code": 400,
"type": "InternalServerException",
"message": "an integer is required (got type str)"
}
I’m having the same issue - I’ve tried appending “data:image/jpeg;base64,” and just sending the raw B64 image - however no approach works - did you find a way around this?
Make sure your image not only meets the maximum file size, but is also encoded correctly. Make sure you are using the correct image format when calling the SageMaker endpoint. It’s also worth checking your S3 permissions and Lambda settings to make sure they’re configured correctly for file access.