Skip to content

Commit 8ec9402

Browse files
Fixed SAM example notebook small issues (#1927)
1 parent ff969fe commit 8ec9402

File tree

3 files changed

+42
-62
lines changed

3 files changed

+42
-62
lines changed

examples/vision/ipynb/sam.ipynb

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
"colab_type": "text"
77
},
88
"source": [
9-
"# Segment Anything Model with \ud83e\udd17Transformers\n",
9+
"# Segment Anything Model with 🤗Transformers\n",
1010
"\n",
1111
"**Authors:** [Merve Noyan](https://twitter.com/mervenoyann) & [Sayak Paul](https://twitter.com/RisingSayak)<br>\n",
1212
"**Date created:** 2023/07/11<br>\n",
1313
"**Last modified:** 2023/07/11<br>\n",
14-
"**Description:** Fine-tuning Segment Anything Model using Keras and \ud83e\udd17 Transformers."
14+
"**Description:** Fine-tuning Segment Anything Model using Keras and 🤗 Transformers."
1515
]
1616
},
1717
{
@@ -45,9 +45,8 @@
4545
"segmentation and edge detection. The goal of SAM is to enable all of these downstream\n",
4646
"segmentation tasks through prompting.\n",
4747
"\n",
48-
"In this example, we'll learn how to use the SAM model from \ud83e\udd17 Transformers for performing\n",
49-
"inference and fine-tuning.\n",
50-
""
48+
"In this example, we'll learn how to use the SAM model from 🤗 Transformers for performing\n",
49+
"inference and fine-tuning.\n"
5150
]
5251
},
5352
{
@@ -61,7 +60,7 @@
6160
},
6261
{
6362
"cell_type": "code",
64-
"execution_count": 0,
63+
"execution_count": null,
6564
"metadata": {
6665
"colab_type": "code"
6766
},
@@ -81,7 +80,7 @@
8180
},
8281
{
8382
"cell_type": "code",
84-
"execution_count": 0,
83+
"execution_count": null,
8584
"metadata": {
8685
"colab_type": "code"
8786
},
@@ -109,12 +108,9 @@
109108
"\n",
110109
"SAM has the following components:\n",
111110
"\n",
112-
"|\n",
113-
"![](https://imgur.com/oLfdwuB)\n",
111+
"| ![](https://imgur.com/oLfdwuB.png) |\n",
114112
"|:--:|\n",
115-
"| Image taken from the official\n",
116-
"[SAM blog post](https://ai.facebook.com/blog/segment-anything-foundation-model-image-segmentation/) |\n",
117-
"|"
113+
"| Image taken from the official [SAM blog post](https://ai.facebook.com/blog/segment-anything-foundation-model-image-segmentation/) |\n"
118114
]
119115
},
120116
{
@@ -174,7 +170,7 @@
174170
},
175171
{
176172
"cell_type": "code",
177-
"execution_count": 0,
173+
"execution_count": null,
178174
"metadata": {
179175
"colab_type": "code"
180176
},
@@ -197,7 +193,7 @@
197193
},
198194
{
199195
"cell_type": "code",
200-
"execution_count": 0,
196+
"execution_count": null,
201197
"metadata": {
202198
"colab_type": "code"
203199
},
@@ -315,8 +311,7 @@
315311
" show_mask(mask, axes[i])\n",
316312
" axes[i].title.set_text(f\"Mask {i+1}, Score: {score.numpy().item():.3f}\")\n",
317313
" axes[i].axis(\"off\")\n",
318-
" plt.show()\n",
319-
""
314+
" plt.show()\n"
320315
]
321316
},
322317
{
@@ -333,7 +328,7 @@
333328
},
334329
{
335330
"cell_type": "code",
336-
"execution_count": 0,
331+
"execution_count": null,
337332
"metadata": {
338333
"colab_type": "code"
339334
},
@@ -357,7 +352,7 @@
357352
},
358353
{
359354
"cell_type": "code",
360-
"execution_count": 0,
355+
"execution_count": null,
361356
"metadata": {
362357
"colab_type": "code"
363358
},
@@ -380,7 +375,7 @@
380375
},
381376
{
382377
"cell_type": "code",
383-
"execution_count": 0,
378+
"execution_count": null,
384379
"metadata": {
385380
"colab_type": "code"
386381
},
@@ -416,7 +411,7 @@
416411
},
417412
{
418413
"cell_type": "code",
419-
"execution_count": 0,
414+
"execution_count": null,
420415
"metadata": {
421416
"colab_type": "code"
422417
},
@@ -443,9 +438,7 @@
443438
"As can be noticed, all the masks are _valid_ masks for the point prompt we provided.\n",
444439
"\n",
445440
"SAM is flexible enough to support different visual prompts and we encourage you to check\n",
446-
"out [this\n",
447-
"notebook](https://github.com/huggingface/notebooks/blob/main/examples/segment_anything.ipy\n",
448-
"nb) to know more about them!"
441+
"out [this notebook](https://github.com/huggingface/notebooks/blob/main/examples/segment_anything.ipynb) to know more about them!"
449442
]
450443
},
451444
{
@@ -467,7 +460,7 @@
467460
},
468461
{
469462
"cell_type": "code",
470-
"execution_count": 0,
463+
"execution_count": null,
471464
"metadata": {
472465
"colab_type": "code"
473466
},
@@ -494,7 +487,7 @@
494487
},
495488
{
496489
"cell_type": "code",
497-
"execution_count": 0,
490+
"execution_count": null,
498491
"metadata": {
499492
"colab_type": "code"
500493
},
@@ -553,7 +546,7 @@
553546
},
554547
{
555548
"cell_type": "code",
556-
"execution_count": 0,
549+
"execution_count": null,
557550
"metadata": {
558551
"colab_type": "code"
559552
},
@@ -606,8 +599,7 @@
606599
" y_max = min(H, y_max + np.random.randint(0, 20))\n",
607600
" bbox = [x_min, y_min, x_max, y_max]\n",
608601
"\n",
609-
" return bbox\n",
610-
""
602+
" return bbox\n"
611603
]
612604
},
613605
{
@@ -634,7 +626,7 @@
634626
},
635627
{
636628
"cell_type": "code",
637-
"execution_count": 0,
629+
"execution_count": null,
638630
"metadata": {
639631
"colab_type": "code"
640632
},
@@ -667,7 +659,7 @@
667659
},
668660
{
669661
"cell_type": "code",
670-
"execution_count": 0,
662+
"execution_count": null,
671663
"metadata": {
672664
"colab_type": "code"
673665
},
@@ -696,7 +688,7 @@
696688
},
697689
{
698690
"cell_type": "code",
699-
"execution_count": 0,
691+
"execution_count": null,
700692
"metadata": {
701693
"colab_type": "code"
702694
},
@@ -723,12 +715,12 @@
723715
},
724716
"source": [
725717
"We will now write DICE loss. This implementation is based on\n",
726-
"[MONAI DICE loss](https://docs.monai.io/en/stable/_modules/monai/losses/dice.html#DiceLoss)."
718+
"[MONAI DICE loss](https://docs.monai.io/en/stable/losses.html#diceloss)."
727719
]
728720
},
729721
{
730722
"cell_type": "code",
731-
"execution_count": 0,
723+
"execution_count": null,
732724
"metadata": {
733725
"colab_type": "code"
734726
},
@@ -752,8 +744,7 @@
752744
" loss = 1.0 - (2.0 * intersection + 1e-5) / (denominator + 1e-5)\n",
753745
" loss = tf.reduce_mean(loss)\n",
754746
"\n",
755-
" return loss\n",
756-
""
747+
" return loss\n"
757748
]
758749
},
759750
{
@@ -762,15 +753,15 @@
762753
"colab_type": "text"
763754
},
764755
"source": [
765-
"##\u00a0Fine-tuning SAM\n",
756+
"## Fine-tuning SAM\n",
766757
"\n",
767758
"We will now fine-tune SAM's decoder part. We will freeze the vision encoder and prompt\n",
768759
"encoder layers."
769760
]
770761
},
771762
{
772763
"cell_type": "code",
773-
"execution_count": 0,
764+
"execution_count": null,
774765
"metadata": {
775766
"colab_type": "code"
776767
},
@@ -806,8 +797,7 @@
806797
" grads = tape.gradient(loss, trainable_vars)\n",
807798
" optimizer.apply_gradients(zip(grads, trainable_vars))\n",
808799
"\n",
809-
" return loss\n",
810-
""
800+
" return loss\n"
811801
]
812802
},
813803
{
@@ -822,7 +812,7 @@
822812
},
823813
{
824814
"cell_type": "code",
825-
"execution_count": 0,
815+
"execution_count": null,
826816
"metadata": {
827817
"colab_type": "code"
828818
},
@@ -859,7 +849,7 @@
859849
},
860850
{
861851
"cell_type": "code",
862-
"execution_count": 0,
852+
"execution_count": null,
863853
"metadata": {
864854
"colab_type": "code"
865855
},
@@ -880,7 +870,7 @@
880870
},
881871
{
882872
"cell_type": "code",
883-
"execution_count": 0,
873+
"execution_count": null,
884874
"metadata": {
885875
"colab_type": "code"
886876
},
@@ -906,7 +896,7 @@
906896
},
907897
{
908898
"cell_type": "code",
909-
"execution_count": 0,
899+
"execution_count": null,
910900
"metadata": {
911901
"colab_type": "code"
912902
},
@@ -947,4 +937,4 @@
947937
},
948938
"nbformat": 4,
949939
"nbformat_minor": 0
950-
}
940+
}

examples/vision/md/sam.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,9 @@ import os
8585

8686
SAM has the following components:
8787

88-
|
89-
![](https://imgur.com/oLfdwuB)
88+
| ![](https://imgur.com/oLfdwuB.png) |
9089
|:--:|
91-
| Image taken from the official
92-
[SAM blog post](https://ai.facebook.com/blog/segment-anything-foundation-model-image-segmentation/) |
93-
|
90+
| Image taken from the official [SAM blog post](https://ai.facebook.com/blog/segment-anything-foundation-model-image-segmentation/) |
9491

9592
The image encoder is responsible for computing image embeddings. When interacting with
9693
SAM, we compute the image embedding one time (as the image encoder is heavy) and then
@@ -345,9 +342,7 @@ And there we go!
345342
As can be noticed, all the masks are _valid_ masks for the point prompt we provided.
346343

347344
SAM is flexible enough to support different visual prompts and we encourage you to check
348-
out [this
349-
notebook](https://github.com/huggingface/notebooks/blob/main/examples/segment_anything.ipy
350-
nb) to know more about them!
345+
out [this notebook](https://github.com/huggingface/notebooks/blob/main/examples/segment_anything.ipynb) to know more about them!
351346

352347
---
353348
## Fine-tuning
@@ -559,7 +554,7 @@ ground_truth_mask (2, 256, 256) <dtype: 'int32'> True
559554
### Training
560555

561556
We will now write DICE loss. This implementation is based on
562-
[MONAI DICE loss](https://docs.monai.io/en/stable/_modules/monai/losses/dice.html#DiceLoss).
557+
[MONAI DICE loss](https://docs.monai.io/en/stable/losses.html#diceloss).
563558

564559

565560
```python

examples/vision/sam.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,9 @@
6767
6868
SAM has the following components:
6969
70-
|
71-
![](https://imgur.com/oLfdwuB)
70+
| ![](https://imgur.com/oLfdwuB.png) |
7271
|:--:|
73-
| Image taken from the official
74-
[SAM blog post](https://ai.facebook.com/blog/segment-anything-foundation-model-image-segmentation/) |
75-
|
72+
| Image taken from the official [SAM blog post](https://ai.facebook.com/blog/segment-anything-foundation-model-image-segmentation/) |
7673
7774
"""
7875

@@ -298,9 +295,7 @@ def show_masks_on_image(raw_image, masks, scores):
298295
As can be noticed, all the masks are _valid_ masks for the point prompt we provided.
299296
300297
SAM is flexible enough to support different visual prompts and we encourage you to check
301-
out [this
302-
notebook](https://github.com/huggingface/notebooks/blob/main/examples/segment_anything.ipy
303-
nb) to know more about them!
298+
out [this notebook](https://github.com/huggingface/notebooks/blob/main/examples/segment_anything.ipynb) to know more about them!
304299
"""
305300

306301
"""
@@ -484,7 +479,7 @@ def get_bounding_box(self, ground_truth_map):
484479

485480
"""
486481
We will now write DICE loss. This implementation is based on
487-
[MONAI DICE loss](https://docs.monai.io/en/stable/_modules/monai/losses/dice.html#DiceLoss).
482+
[MONAI DICE loss](https://docs.monai.io/en/stable/losses.html#diceloss).
488483
"""
489484

490485

0 commit comments

Comments
 (0)