# Segment Anything Model (SAM) inference

**URL:** <https://discuss.huggingface.co/t/segment-anything-model-sam-inference/49114>\
**Category:** Amazon SageMaker\
**Created:** [August 2, 2023, 9:40am UTC](https://discuss.huggingface.co/t/segment-anything-model-sam-inference/49114 "2023-08-02T09:40:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![francisurias](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@francisurias](https://discuss.huggingface.co/u/francisurias)\
**Post date:** [August 2, 2023, 9:40am UTC](https://discuss.huggingface.co/t/segment-anything-model-sam-inference/49114/1 "2023-08-02T09:40:47Z")

</div>

Hi,

I’m working on a project that uses SAM to do image segmentation. I’m trying to deploy the “sam-vit-large” model (found here: [facebook/sam-vit-large · Hugging Face](https://huggingface.co/facebook/sam-vit-large)) on AWS SageMaker using the code given in the deployment section. I have managed to deploy the endpoint on SageMaker, but I’m not sure what the payload/input I need to give the endpoint is; would it be in json format? If so, what would be the properties?

Thanks!

---

<div class="post-metadata">

**Author:** ![harryneal](https://avatars.discourse-cdn.com/v4/letter/h/f9ae1b/32.png) [@harryneal](https://discuss.huggingface.co/u/harryneal)\
**Post date:** [October 16, 2023, 2:43pm UTC](https://discuss.huggingface.co/t/segment-anything-model-sam-inference/49114/2 "2023-10-16T14:43:35Z")

</div>

Did you find an answer to this? I would love to know what you found out if so.

Thanks!

---

<div class="post-metadata">

**Author:** ![radames](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/radames/32/28246_2.png) [@radames](https://discuss.huggingface.co/u/radames)\
**Post date:** [October 19, 2023, 4:29am UTC](https://discuss.huggingface.co/t/segment-anything-model-sam-inference/49114/3 "2023-10-19T04:29:50Z")

</div>

hi it looks like the HuggingFace sagemaker needs to update the transformers images, currently the latest version support is `4.28.1` and SAM is only integrated on transformers `4.29.0` cc @philschmid for an update. Also my guess is SAM my need a custom inference handler, please look [https://github.com/huggingface/notebooks/blob/main/sagemaker/17\_custom\_inference\_script/sagemaker-notebook.ipynb](https://github.com/huggingface/notebooks/blob/main/sagemaker/17_custom_inference_script/sagemaker-notebook.ipynb)

---

<div class="post-metadata">

**Author:** ![payamahv](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@payamahv](https://discuss.huggingface.co/u/payamahv)\
**Post date:** [October 26, 2023, 6:41pm UTC](https://discuss.huggingface.co/t/segment-anything-model-sam-inference/49114/5 "2023-10-26T18:41:57Z")

</div>

I am trying to pass an model to this model like this in sagemaker

with open(“/root/aws-sam/02.JPG”, “rb”) as f:  
data = f.read()  
#raw\_image = Image.open(data).convert(“RGB”)  
response = runtime.invoke\_endpoint(EndpointName=ENDPOINT\_NAME,  
ContentType=‘image/jpeg’, #image/jpeg image/x-image’  
Body=data)

I am getting this error

2023-10-26T09:42:33.354-07:00

Copy  
2023-10-26T16:42:33,265 [INFO] W-9000-facebook\_\_sam-vit-large-stdout com.amazonaws.ml.mms.wlm.WorkerLifeCycle - raise KeyError(key)  
2023-10-26T16:42:33,265 [INFO] W-9000-facebook\_\_sam-vit-large-stdout com.amazonaws.ml.mms.wlm.WorkerLifeCycle - raise KeyError(key) AllTraffic/i-0dad3e9d16762a67f

2023-10-26T09:42:33.354-07:00

Copy  
2023-10-26T16:42:33,265 [INFO] W-9000-facebook\_\_sam-vit-large-stdout com.amazonaws.ml.mms.wlm.WorkerLifeCycle - KeyError: ‘sam’  
2023-10-26T16:42:33,265 [INFO] W-9000-facebook\_\_sam-vit-large-stdout com.amazonaws.ml.mms.wlm.WorkerLifeCycle - KeyError: ‘sam’ AllTraffic/i-0dad3e9d16762a67f

2023-10-26T09:42:37.363-07:00

Copy  
2023-10-26T16:42:33,265 [INFO] W-9000-facebook\_\_sam-vit-large-stdout com.amazonaws.ml.mms.wlm.WorkerLifeCycle - Model facebook\_\_sam-vit-large loaded io\_fd=966296fffe2e23f7-00000015-00000000-a9415483c6ddb70e-6cd401cc  
2023-10-26T16:42:33,265 [INFO] W-9000-facebook\_\_sam-vit-large-stdout com.amazonaws.ml.mms.wlm.WorkerLifeCycle - Model facebook\_\_sam-vit-large loaded io\_fd=966296fffe2e23f7-00000015-00000000-a9415483c6ddb70e-6cd401cc
