interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / examples / sam / README.md

95 lines · 3225 bytes Raw Blame History Rendered
1# SAM.cpp
2
3Inference of Meta's [Segment Anything Model](https://github.com/facebookresearch/segment-anything/) in pure C/C++
4
5## Description
6
7The example currently supports only the [ViT-B SAM model checkpoint](https://huggingface.co/facebook/sam-vit-base).
8
9## Next steps
10
11- [X] Reduce memory usage by utilizing the new ggml-alloc
12- [X] Remove redundant graph nodes
13- [ ] Make inference faster
14- [X] Fix the difference in output masks compared to the PyTorch implementation
15- [X] Filter masks based on stability score
16- [ ] Add support for user input
17- [ ] Support F16 for heavy F32 ops
18- [ ] Test quantization
19- [X] Support bigger model checkpoints
20- [ ] GPU support
21
22## Quick start
23Setup Python and build examples according to main README.
24
25```bash
26# Download PTH model
27wget -P examples/sam/ https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth
28
29# Convert PTH model to ggml
30python examples/sam/convert-pth-to-ggml.py examples/sam/sam_vit_b_01ec64.pth examples/sam/ 1
31
32# run inference
33./bin/sam -t 16 -i ../examples/sam/example.jpg -m ../examples/sam/ggml-model-f16.bin
34```
35
36## Downloading and converting the model checkpoints
37
38You can download a [model checkpoint](https://github.com/facebookresearch/segment-anything/tree/main#model-checkpoints) and convert it to `ggml` format using the script `convert-pth-to-ggml.py`:
39
40## Example output on M2 Ultra
41```
42 $ ▶ make -j sam && time ./bin/sam -t 8 -i img.jpg
43[ 28%] Built target common
44[ 71%] Built target ggml
45[100%] Built target sam
46main: seed = 1693224265
47main: loaded image 'img.jpg' (680 x 453)
48sam_image_preprocess: scale = 0.664062
49main: preprocessed image (1024 x 1024)
50sam_model_load: loading model from 'models/sam-vit-b/ggml-model-f16.bin' - please wait ...
51sam_model_load: n_enc_state = 768
52sam_model_load: n_enc_layer = 12
53sam_model_load: n_enc_head = 12
54sam_model_load: n_enc_out_chans = 256
55sam_model_load: n_pt_embd = 4
56sam_model_load: ftype = 1
57sam_model_load: qntvr = 0
58operator(): ggml ctx size = 202.32 MB
59sam_model_load: ...................................... done
60sam_model_load: model size = 185.05 MB / num tensors = 304
61embd_img
62dims: 64 64 256 1 f32
63First & Last 10 elements:
64-0.05117 -0.06408 -0.07154 -0.06991 -0.07212 -0.07690 -0.07508 -0.07281 -0.07383 -0.06779
650.01589 0.01775 0.02250 0.01675 0.01766 0.01661 0.01811 0.02051 0.02103 0.03382
66sum: 12736.272313
67
68Skipping mask 0 with iou 0.705935 below threshold 0.880000
69Skipping mask 1 with iou 0.762136 below threshold 0.880000
70Mask 2: iou = 0.947081, stability_score = 0.955437, bbox (371, 436), (144, 168)
71
72
73main: load time = 51.28 ms
74main: total time = 2047.49 ms
75
76real 0m2.068s
77user 0m16.343s
78sys 0m0.214s
79```
80
81Input point is (414.375, 162.796875) (currently hardcoded)
82
83Input image:
84
85![llamas](https://user-images.githubusercontent.com/8558655/261301565-37b7bf4b-bf91-40cf-8ec1-1532316e1612.jpg)
86
87Output mask (mask_out_2.png in build folder):
88
89![mask_glasses](https://user-images.githubusercontent.com/8558655/263706800-47eeea30-1457-4c87-938b-8f11536c5aa7.png)
90
91## References
92
93- [ggml](https://github.com/ggerganov/ggml)
94- [SAM](https://segment-anything.com/)
95- [SAM demo](https://segment-anything.com/demo)