# Fine tuning sam

**URL:** <https://discuss.huggingface.co/t/fine-tuning-sam/45624>\
**Category:** Models\
**Created:** [July 4, 2023, 3:00pm UTC](https://discuss.huggingface.co/t/fine-tuning-sam/45624 "2023-07-04T15:00:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sushmanth](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@sushmanth](https://discuss.huggingface.co/u/sushmanth)\
**Post date:** [July 4, 2023, 3:00pm UTC](https://discuss.huggingface.co/t/fine-tuning-sam/45624/1 "2023-07-04T15:00:46Z")

</div>

I have been trying to fine-tune sam model.  
I have an image of shape [B,3,1024,1024], masks of shape [B,256,256], and bounding box of shape [B,num\_boxes,4]  
example:

 ![Screenshot 2023-07-04 at 7.49.40 PM](https://us1.discourse-cdn.com/hellohellohello/original/3X/8/4/84f4f30c0a4cb1276135a209d748721c5da056b1.png)

I don’t knew where , I was going wrong but there is problem in the training

```auto
num_epochs = 100

device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)

model.train()
for epoch in range(num_epochs):
    epoch_losses = []
    for batch in tqdm(train_loader):
        # forward pass
        outputs = model(pixel_values=image.to(device),
                        input_boxes=boxes.to(device),
                        multimask_output=False)

        # compute loss
        predicted_masks = outputs.pred_masks.squeeze(1)
        ground_truth_masks = batch["ground_truth_mask"].float().to(device)
        loss = seg_loss(predicted_masks, ground_truth_masks.unsqueeze(1))

        # backward pass (compute gradients of parameters w.r.t. loss)
        optimizer.zero_grad()
        loss.backward()

        # optimize
        optimizer.step()
        epoch_losses.append(loss.item())

        # Release GPU memory
        torch.cuda.empty_cache()

    print(f'EPOCH: {epoch}')
    print(f'Mean loss: {mean(epoch_losses)}')

```

And was encountering the following error in the training of the model  
The tutorial posted by @nielsr has the tutorial about Sam in his repo.  
@nielsr here is the error. How to get rid of this error?

 ![Screenshot 2023-07-04 at 8.27.51 PM](https://us1.discourse-cdn.com/hellohellohello/original/3X/4/3/43da8dddd072a5cda27cd879302ea6a60f0c6221.png)  
 ![Screenshot 2023-07-04 at 8.29.23 PM](https://us1.discourse-cdn.com/hellohellohello/original/3X/c/b/cbcdb1ce362072860a5f5b30ac2da8660478c685.jpeg)

---

<div class="post-metadata">

**Author:** ![sushmanth](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@sushmanth](https://discuss.huggingface.co/u/sushmanth)\
**Post date:** [July 4, 2023, 3:01pm UTC](https://discuss.huggingface.co/t/fine-tuning-sam/45624/2 "2023-07-04T15:01:28Z")

</div>

should i have more gpu for this or ??what else I should do ,to work the code out?

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](https://discuss.huggingface.co/u/nielsr)\
**Post date:** [June 3, 2024, 2:21pm UTC](https://discuss.huggingface.co/t/fine-tuning-sam/45624/3 "2024-06-03T14:21:46Z")

</div>

I’d recommend the following guide for tips and tricks regarding efficiently fine-tuning on a single GPU: [Methods and tools for efficient training on a single GPU](https://huggingface.co/docs/transformers/en/perf_train_gpu_one)
