# Fine-tuning Segment Anything Model: Call up a saved model

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffine-tuning-segment-anything-model-call-up-a-saved-model%2F57634%3E%5C
**Category:** 🤗Transformers\
**Created:** [October 6, 2023, 2:42pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffine-tuning-segment-anything-model-call-up-a-saved-model%2F57634 "2023-10-06T14:42:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon69151022](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@anon69151022](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fanon69151022)%5C
**Post date:** [October 6, 2023, 2:42pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffine-tuning-segment-anything-model-call-up-a-saved-model%2F57634%2F1 "2023-10-06T14:42:39Z")

</div>

Hi all,

I have successfully trained my SAM model with the good guidance from the following blog post.

Blog Post: [Fine tune Segment Anything (SAM) for images with multiple masks](https://maxjoas.medium.com/finetune-segment-anything-sam-for-images-with-multiple-masks-34514ee811bb)

I saved the weights of the trained model using the following code:

_torch.save(model.state\_dict(), ‘model\_weights.pth’)._

However, I am now faced with the question of how to use this saved model for my code. The original version of SAM is loaded via the following code:

_from segment\_anything import SamAutomaticMaskGenerator, sam\_model\_registry_

_sam = sam\_model\_registry(vit\_h)(checkpoint=“/content/drive/MyDrive/…/SAMFineTune/sam\_vit\_h\_4b8939.pth”)_

_Note: I had to replace the square brackets at vit\_h with round ones to post it here._

Can anyone help me how to use the trained model now in my code to create masks? Unfortunately, I haven’t found anything yet to solve the problem.

---

<div class="post-metadata">

**Author:** ![Ghouloud](https://avatars.discourse-cdn.com/v4/letter/g/838e76/32.png) [@Ghouloud](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FGhouloud)%5C
**Post date:** [May 27, 2024, 1:47pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffine-tuning-segment-anything-model-call-up-a-saved-model%2F57634%2F2 "2024-05-27T13:47:54Z")

</div>

.(torch.load(model\_save\_path)) is the function to load it. Forward passes should be weighed according to your trained model now, in case this helps anybody!

---

<div class="post-metadata">

**Author:** ![JeanneL](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/jeannel/32/33546_2.png) [@JeanneL](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJeanneL)%5C
**Post date:** [October 28, 2024, 10:08am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffine-tuning-segment-anything-model-call-up-a-saved-model%2F57634%2F3 "2024-10-28T10:08:36Z")

</div>

Hello! I have the same issue with my fine-tuned weights from SAM.  
@Ghouloud I can load the weights thanks to your tip but I can’t create the sam generator.  
@anon69151022 did you find any solution ?

---

<div class="post-metadata">

**Author:** ![Ghouloud](https://avatars.discourse-cdn.com/v4/letter/g/838e76/32.png) [@Ghouloud](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FGhouloud)%5C
**Post date:** [October 28, 2024, 2:09pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffine-tuning-segment-anything-model-call-up-a-saved-model%2F57634%2F4 "2024-10-28T14:09:52Z")

</div>

Hello!

By SAM generator are you referring to the dataloader?

---

<div class="post-metadata">

**Author:** ![JeanneL](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/jeannel/32/33546_2.png) [@JeanneL](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJeanneL)%5C
**Post date:** [October 28, 2024, 2:22pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffine-tuning-segment-anything-model-call-up-a-saved-model%2F57634%2F5 "2024-10-28T14:22:50Z")

</div>

No, it’s about SamAutomaticMaskGenerator() function, as explained in Blog Post: [Fine tune Segment Anything (SAM) for images with multiple masks](https://maxjoas.medium.com/finetune-segment-anything-sam-for-images-with-multiple-masks-34514ee811bb).

I can’t run the following lines :  
sam = sam\_model\_registry(MODEL\_TYPE)(checkpoint=CHECKPOINT\_PATH).to(DEVICE)  
mask\_generator = SamAutomaticMaskGenerator(sam)
