facebookresearch / segment-anything

The repository provides code for running inference with the SegmentAnything Model (SAM), links for downloading the trained model checkpoints, and example notebooks that show how to use the model.
Apache License 2.0
46.58k stars 5.52k forks source link

Deploying SAM on SageMaker #508

Open francisurias opened 1 year ago

francisurias commented 1 year ago

Hi,

I'm trying to deploy SAM on SageMaker but I'm not sure how to do so. I have managed to run the Automatic Mask Generator code on a Jupyter notebook, but I'd like to deploy the model as an endpoint so I can develop a web app for production. Does anyone know how I can achieve this?

I've tried using HuggingFace's SageMaker deployment for SAM, and I have managed to deploy the endpoint, but the model card doesn't contain any example input so I'm not sure what payload I should give the endpoint.

Thanks.

duckheada commented 1 year ago

an Image?

francisurias commented 1 year ago

I've tried sending it an image after deploying the model as an endpoint (using the HuggingFace's deployment):

Deploying the model as an endpoint:

import sagemaker
import boto3

try:
    role = sagemaker.get_execution_role()
except ValueError:
    iam = boto3.client('iam')
    role = iam.get_role(RoleName='sagemaker_execution_role')['Role']['Arn']

print(f"sagemaker role arn: {role}")

# Hub Model configuration. https://huggingface.co/models
hub = {
    'HF_MODEL_ID':'facebook/sam-vit-large',
    'HF_TASK':'undefined'
}

# create Hugging Face Model Class
huggingface_model = HuggingFaceModel(
    transformers_version='4.26.0',
    pytorch_version='1.13.1',
    py_version='py39',
    env=hub,
    role=role, 
)

predictor = huggingface_model.deploy(
   initial_instance_count=1,
   instance_type="ml.g4dn.xlarge",
   endpoint_name="segment-anything-model"
)

Sending an image as a request:

from sagemaker.serializers import DataSerializer

predictor.serializer = DataSerializer(content_type='image/x-image') # change to audio/x-audio for audio

with open("cats.jpg", "rb") as f:
    data = f.read()
predictor.predict(data)

I get the error:

ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (400) from primary with message "{
  "code": 400,
  "type": "InternalServerException",
  "message": "\u0027sam\u0027"
}

Any ideas on how to fix this?

an Image?

pritamrungta commented 1 year ago

have you checked cloudwatch stack trace?

francisurias commented 1 year ago

have you checked cloudwatch stack trace?

Checking the CloudWatch stack trace didn't really give any additional helpful information, it mostly just gave the same error like so:

raise KeyError(key)
KeyError: 'sam'