Skip to content

Commit b7d02ce

Browse files
authored
Update segment_anything_in_keras_hub.md (#1967)
Removed extraneous output
1 parent ac3bdf7 commit b7d02ce

File tree

1 file changed

+2
-194
lines changed

1 file changed

+2
-194
lines changed

guides/md/keras_hub/segment_anything_in_keras_hub.md

Lines changed: 2 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Segment Anything in KerasHub!
1+
# Segment Anything in KerasHub
22

33
**Author:** Tirth Patel, Ian Stenbit, Divyashree Sreepathihalli<br><br>
44
**Date created:** 2024/10/1<br><br>
@@ -37,14 +37,7 @@ First, let's get all our dependencies and images for our demo.
3737
```python
3838
!!wget -q https://raw.githubusercontent.com/facebookresearch/segment-anything/main/notebooks/images/truck.jpg
3939
```
40-
<div class="k-default-codeblock">
41-
```
42-
[]
43-
44-
[]
4540

46-
```
47-
</div>
4841
---
4942
## Choose your backend
5043

@@ -158,26 +151,6 @@ masks).
158151
model = keras_hub.models.SAMImageSegmenter.from_preset("sam_huge_sa1b")
159152
```
160153

161-
<div class="k-default-codeblock">
162-
```
163-
Downloading from https://www.kaggle.com/api/v1/models/kerashub/sam/keras/sam_huge_sa1b/2/download/config.json...
164-
165-
100%|████████████████████████████████████████████████████| 3.06k/3.06k [00:00<00:00, 6.08MB/s]
166-
167-
Downloading from https://www.kaggle.com/api/v1/models/kerashub/sam/keras/sam_huge_sa1b/2/download/task.json...
168-
169-
100%|████████████████████████████████████████████████████| 5.76k/5.76k [00:00<00:00, 11.0MB/s]
170-
171-
Downloading from https://www.kaggle.com/api/v1/models/kerashub/sam/keras/sam_huge_sa1b/2/download/task.weights.h5...
172-
173-
100%|████████████████████████████████████████████████████| 2.39G/2.39G [00:26<00:00, 95.7MB/s]
174-
175-
Downloading from https://www.kaggle.com/api/v1/models/kerashub/sam/keras/sam_huge_sa1b/2/download/model.weights.h5...
176-
177-
100%|████████████████████████████████████████████████████| 2.39G/2.39G [00:32<00:00, 79.7MB/s]
178-
179-
```
180-
</div>
181154
---
182155
## Understanding Prompts
183156

@@ -289,14 +262,6 @@ outputs = model.predict(
289262
)
290263
```
291264

292-
<div class="k-default-codeblock">
293-
```
294-
Could not load symbol cuFuncGetName. Error: /usr/lib64-nvidia/libcuda.so.1: undefined symbol: cuFuncGetName
295-
296-
1/1 ━━━━━━━━━━━━━━━━━━━━ 24s 24s/step
297-
298-
```
299-
</div>
300265
`SegmentAnythingModel.predict` returns two outputs. First are logits (segmentation masks)
301266
of shape `(1, 4, 256, 256)` and the other are the IoU confidence scores (of shape `(1,
302267
4)`) for each mask predicted. The pretrained SAM model predicts four masks: the first is
@@ -388,12 +353,6 @@ plt.axis("off")
388353
plt.show()
389354
```
390355

391-
<div class="k-default-codeblock">
392-
```
393-
1/1 ━━━━━━━━━━━━━━━━━━━━ 10s 10s/step
394-
395-
```
396-
</div>
397356

398357
![png](/img/guides/segment_anything_in_keras_hub/segment_anything_in_keras_hub_23_1.png)
399358

@@ -435,12 +394,6 @@ plt.axis("off")
435394
plt.show()
436395
```
437396

438-
<div class="k-default-codeblock">
439-
```
440-
1/1 ━━━━━━━━━━━━━━━━━━━━ 14s 14s/step
441-
442-
```
443-
</div>
444397

445398
![png](/img/guides/segment_anything_in_keras_hub/segment_anything_in_keras_hub_25_1.png)
446399

@@ -477,13 +430,11 @@ Then, we can install the pretrained model's weights and config:
477430
!!pip install -U git+https://github.com/IDEA-Research/GroundingDINO.git
478431
```
479432

480-
481-
482-
483433
```python
484434
!!wget -q https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth
485435
!!wget -q https://raw.githubusercontent.com/IDEA-Research/GroundingDINO/v0.1.0-alpha2/groundingdino/config/GroundingDINO_SwinT_OGC.py
486436
```
437+
487438
```python
488439
from groundingdino.util.inference import Model as GroundingDINO
489440

@@ -492,113 +443,7 @@ WEIGHTS_PATH = "groundingdino_swint_ogc.pth"
492443

493444
grounding_dino = GroundingDINO(CONFIG_PATH, WEIGHTS_PATH)
494445
```
495-
<div class="k-default-codeblock">
496-
```
497-
['Collecting git+https://github.com/IDEA-Research/GroundingDINO.git',
498-
' Cloning https://github.com/IDEA-Research/GroundingDINO.git to /tmp/pip-req-build-m_hhz04_',
499-
' Running command git clone --filter=blob:none --quiet https://github.com/IDEA-Research/GroundingDINO.git /tmp/pip-req-build-m_hhz04_',
500-
' Resolved https://github.com/IDEA-Research/GroundingDINO.git to commit 856dde20aee659246248e20734ef9ba5214f5e44',
501-
' Preparing metadata (setup.py) ... \x1b[?25l\x1b[?25hdone',
502-
'Requirement already satisfied: torch in /usr/local/lib/python3.10/dist-packages (from groundingdino==0.1.0) (2.4.1+cu121)',
503-
'Requirement already satisfied: torchvision in /usr/local/lib/python3.10/dist-packages (from groundingdino==0.1.0) (0.19.1+cu121)',
504-
'Requirement already satisfied: transformers in /usr/local/lib/python3.10/dist-packages (from groundingdino==0.1.0) (4.44.2)',
505-
'Collecting addict (from groundingdino==0.1.0)',
506-
' Downloading addict-2.4.0-py3-none-any.whl.metadata (1.0 kB)',
507-
'Collecting yapf (from groundingdino==0.1.0)',
508-
' Downloading yapf-0.40.2-py3-none-any.whl.metadata (45 kB)',
509-
'\x1b[?25l \x1b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m \x1b[32m0.0/45.4 kB\x1b[0m \x1b[31m?\x1b[0m eta \x1b[36m-:--:--\x1b[0m',
510-
'\x1b[2K \x1b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m \x1b[32m45.4/45.4 kB\x1b[0m \x1b[31m1.8 MB/s\x1b[0m eta \x1b[36m0:00:00\x1b[0m',
511-
'\x1b[?25hCollecting timm (from groundingdino==0.1.0)',
512-
' Downloading timm-1.0.9-py3-none-any.whl.metadata (42 kB)',
513-
'\x1b[?25l \x1b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m \x1b[32m0.0/42.4 kB\x1b[0m \x1b[31m?\x1b[0m eta \x1b[36m-:--:--\x1b[0m',
514-
'\x1b[2K \x1b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m \x1b[32m42.4/42.4 kB\x1b[0m \x1b[31m1.8 MB/s\x1b[0m eta \x1b[36m0:00:00\x1b[0m',
515-
'\x1b[?25hRequirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from groundingdino==0.1.0) (1.26.4)',
516-
'Requirement already satisfied: opencv-python in /usr/local/lib/python3.10/dist-packages (from groundingdino==0.1.0) (4.10.0.84)',
517-
'Collecting supervision>=0.22.0 (from groundingdino==0.1.0)',
518-
' Downloading supervision-0.23.0-py3-none-any.whl.metadata (14 kB)',
519-
'Requirement already satisfied: pycocotools in /usr/local/lib/python3.10/dist-packages (from groundingdino==0.1.0) (2.0.8)',
520-
'Requirement already satisfied: defusedxml<0.8.0,>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from supervision>=0.22.0->groundingdino==0.1.0) (0.7.1)',
521-
'Requirement already satisfied: matplotlib>=3.6.0 in /usr/local/lib/python3.10/dist-packages (from supervision>=0.22.0->groundingdino==0.1.0) (3.7.1)',
522-
'Requirement already satisfied: opencv-python-headless>=4.5.5.64 in /usr/local/lib/python3.10/dist-packages (from supervision>=0.22.0->groundingdino==0.1.0) (4.10.0.84)',
523-
'Requirement already satisfied: pillow>=9.4 in /usr/local/lib/python3.10/dist-packages (from supervision>=0.22.0->groundingdino==0.1.0) (10.4.0)',
524-
'Requirement already satisfied: pyyaml>=5.3 in /usr/local/lib/python3.10/dist-packages (from supervision>=0.22.0->groundingdino==0.1.0) (6.0.2)',
525-
'Requirement already satisfied: scipy<2.0.0,>=1.10.0 in /usr/local/lib/python3.10/dist-packages (from supervision>=0.22.0->groundingdino==0.1.0) (1.13.1)',
526-
'Requirement already satisfied: huggingface_hub in /usr/local/lib/python3.10/dist-packages (from timm->groundingdino==0.1.0) (0.24.7)',
527-
'Requirement already satisfied: safetensors in /usr/local/lib/python3.10/dist-packages (from timm->groundingdino==0.1.0) (0.4.5)',
528-
'Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from torch->groundingdino==0.1.0) (3.16.1)',
529-
'Requirement already satisfied: typing-extensions>=4.8.0 in /usr/local/lib/python3.10/dist-packages (from torch->groundingdino==0.1.0) (4.12.2)',
530-
'Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch->groundingdino==0.1.0) (1.13.3)',
531-
'Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch->groundingdino==0.1.0) (3.3)',
532-
'Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch->groundingdino==0.1.0) (3.1.4)',
533-
'Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from torch->groundingdino==0.1.0) (2024.6.1)',
534-
'Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from transformers->groundingdino==0.1.0) (24.1)',
535-
'Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.10/dist-packages (from transformers->groundingdino==0.1.0) (2024.9.11)',
536-
'Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from transformers->groundingdino==0.1.0) (2.32.3)',
537-
'Requirement already satisfied: tokenizers<0.20,>=0.19 in /usr/local/lib/python3.10/dist-packages (from transformers->groundingdino==0.1.0) (0.19.1)',
538-
'Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.10/dist-packages (from transformers->groundingdino==0.1.0) (4.66.5)',
539-
'Requirement already satisfied: importlib-metadata>=6.6.0 in /usr/local/lib/python3.10/dist-packages (from yapf->groundingdino==0.1.0) (8.4.0)',
540-
'Requirement already satisfied: platformdirs>=3.5.1 in /usr/local/lib/python3.10/dist-packages (from yapf->groundingdino==0.1.0) (4.3.6)',
541-
'Requirement already satisfied: tomli>=2.0.1 in /usr/local/lib/python3.10/dist-packages (from yapf->groundingdino==0.1.0) (2.0.1)',
542-
'Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.10/dist-packages (from importlib-metadata>=6.6.0->yapf->groundingdino==0.1.0) (3.20.2)',
543-
'Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.6.0->supervision>=0.22.0->groundingdino==0.1.0) (1.3.0)',
544-
'Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.6.0->supervision>=0.22.0->groundingdino==0.1.0) (0.12.1)',
545-
'Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.6.0->supervision>=0.22.0->groundingdino==0.1.0) (4.54.1)',
546-
'Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.6.0->supervision>=0.22.0->groundingdino==0.1.0) (1.4.7)',
547-
'Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.6.0->supervision>=0.22.0->groundingdino==0.1.0) (3.1.4)',
548-
'Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.6.0->supervision>=0.22.0->groundingdino==0.1.0) (2.8.2)',
549-
'Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch->groundingdino==0.1.0) (2.1.5)',
550-
'Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->transformers->groundingdino==0.1.0) (3.3.2)',
551-
'Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->transformers->groundingdino==0.1.0) (3.10)',
552-
'Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->transformers->groundingdino==0.1.0) (2.2.3)',
553-
'Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->transformers->groundingdino==0.1.0) (2024.8.30)',
554-
'Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from sympy->torch->groundingdino==0.1.0) (1.3.0)',
555-
'Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib>=3.6.0->supervision>=0.22.0->groundingdino==0.1.0) (1.16.0)',
556-
'Downloading supervision-0.23.0-py3-none-any.whl (151 kB)',
557-
'\x1b[?25l \x1b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m \x1b[32m0.0/151.5 kB\x1b[0m \x1b[31m?\x1b[0m eta \x1b[36m-:--:--\x1b[0m',
558-
'\x1b[2K \x1b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m \x1b[32m151.5/151.5 kB\x1b[0m \x1b[31m6.0 MB/s\x1b[0m eta \x1b[36m0:00:00\x1b[0m',
559-
'\x1b[?25hDownloading addict-2.4.0-py3-none-any.whl (3.8 kB)',
560-
'Downloading timm-1.0.9-py3-none-any.whl (2.3 MB)',
561-
'\x1b[?25l \x1b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m \x1b[32m0.0/2.3 MB\x1b[0m \x1b[31m?\x1b[0m eta \x1b[36m-:--:--\x1b[0m',
562-
'\x1b[2K \x1b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m\x1b[90m╺\x1b[0m\x1b[90m━━━━━━━\x1b[0m \x1b[32m1.9/2.3 MB\x1b[0m \x1b[31m55.9 MB/s\x1b[0m eta \x1b[36m0:00:01\x1b[0m',
563-
'\x1b[2K \x1b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m \x1b[32m2.3/2.3 MB\x1b[0m \x1b[31m42.4 MB/s\x1b[0m eta \x1b[36m0:00:00\x1b[0m',
564-
'\x1b[?25hDownloading yapf-0.40.2-py3-none-any.whl (254 kB)',
565-
'\x1b[?25l \x1b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m \x1b[32m0.0/254.7 kB\x1b[0m \x1b[31m?\x1b[0m eta \x1b[36m-:--:--\x1b[0m',
566-
'\x1b[2K \x1b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m \x1b[32m254.7/254.7 kB\x1b[0m \x1b[31m18.3 MB/s\x1b[0m eta \x1b[36m0:00:00\x1b[0m',
567-
'\x1b[?25hBuilding wheels for collected packages: groundingdino',
568-
' Building wheel for groundingdino (setup.py) ... \x1b[?25l\x1b[?25hdone',
569-
' Created wheel for groundingdino: filename=groundingdino-0.1.0-cp310-cp310-linux_x86_64.whl size=3038498 sha256=1e7306dfa5ebd4bebb340bfe814e13026800708bbc0223d37ae8963e90145fb2',
570-
' Stored in directory: /tmp/pip-ephem-wheel-cache-multbs74/wheels/6b/06/d7/b57f601a4df56af41d262a5b1b496359b13c323bf5ef0434b2',
571-
'Successfully built groundingdino',
572-
'Installing collected packages: addict, yapf, supervision, timm, groundingdino',
573-
'Successfully installed addict-2.4.0 groundingdino-0.1.0 supervision-0.23.0 timm-1.0.9 yapf-0.40.2']
574-
575-
[]
576-
577-
UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:3609.)
578-
579-
final text_encoder_type: bert-base-uncased
580-
581-
UserWarning:
582-
Error while fetching `HF_TOKEN` secret value from your vault: 'Requesting secret HF_TOKEN timed out. Secrets can only be fetched when running from the Colab UI.'.
583-
You are not authenticated with the Hugging Face Hub in this notebook.
584-
If the error persists, please let us know by opening an issue on GitHub (https://github.com/huggingface/huggingface_hub/issues/new).
585-
586-
tokenizer_config.json: 0%| | 0.00/48.0 [00:00<?, ?B/s]
587-
588-
config.json: 0%| | 0.00/570 [00:00<?, ?B/s]
589-
590-
vocab.txt: 0%| | 0.00/232k [00:00<?, ?B/s]
591-
592-
tokenizer.json: 0%| | 0.00/466k [00:00<?, ?B/s]
593-
594-
FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be depracted in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884
595-
596-
model.safetensors: 0%| | 0.00/440M [00:00<?, ?B/s]
597-
598-
FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
599446

600-
```
601-
</div>
602447
Let's load an image of a dog for this part!
603448

604449

@@ -615,15 +460,6 @@ plt.axis("on")
615460
plt.show()
616461
```
617462

618-
<div class="k-default-codeblock">
619-
```
620-
Downloading data from https://storage.googleapis.com/keras-cv/test-images/mountain-dog.jpeg
621-
1236492/1236492 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step
622-
623-
WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
624-
625-
```
626-
</div>
627463

628464
![png](/img/guides/segment_anything_in_keras_hub/segment_anything_in_keras_hub_31_2.png)
629465

@@ -651,17 +487,6 @@ outputs = model.predict(
651487
)
652488
```
653489

654-
<div class="k-default-codeblock">
655-
```
656-
FutureWarning: The `device` argument is deprecated and will be removed in v5 of Transformers.
657-
UserWarning: torch.utils.checkpoint: the use_reentrant parameter should be passed explicitly. In version 2.4 we will raise an exception if use_reentrant is not passed. use_reentrant=False is recommended, but if you need to preserve the current default behavior, you can pass use_reentrant=True. Refer to docs for more details on the differences between the two variants.
658-
UserWarning: None of the inputs have requires_grad=True. Gradients will be None
659-
FutureWarning: `torch.cuda.amp.autocast(args...)` is deprecated. Please use `torch.amp.autocast('cuda', args...)` instead.
660-
661-
1/1 ━━━━━━━━━━━━━━━━━━━━ 10s 10s/step
662-
663-
```
664-
</div>
665490
And that's it! We got a segmentation mask for our text prompt using the combination of
666491
Gounding DINO + SAM! This is a very powerful technique to combine different models to
667492
expand the applications!
@@ -683,12 +508,6 @@ plt.axis("off")
683508
plt.show()
684509
```
685510

686-
<div class="k-default-codeblock">
687-
```
688-
WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
689-
690-
```
691-
</div>
692511

693512
![png](/img/guides/segment_anything_in_keras_hub/segment_anything_in_keras_hub_35_1.png)
694513

@@ -732,17 +551,6 @@ time_taken = timeit.repeat(
732551
print(f"Time taken with float16 dtype: {min(time_taken) / 3:.10f}s")
733552
```
734553

735-
<div class="k-default-codeblock">
736-
```
737-
Time taken with float32 dtype: 0.2298811787s
738-
739-
UserWarning: Skipping variable loading for optimizer 'loss_scale_optimizer', because it has 4 variables whereas the saved optimizer has 2 variables.
740-
UserWarning: Skipping variable loading for optimizer 'adam', because it has 2 variables whereas the saved optimizer has 0 variables.
741-
742-
Time taken with float16 dtype: 0.2068303013s
743-
744-
```
745-
</div>
746554
Here's a comparison of KerasHub's implementation with the original PyTorch
747555
implementation!
748556

0 commit comments

Comments
 (0)