Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/examples/sam/README.md # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / examples / sam / README.md` 95 lines · 3225 bytes Raw Blame History Rendered 1 # SAM.cpp 2 3 Inference of Meta's [Segment Anything Model](https://github.com/facebookresearch/segment-anything/) in pure C/C++ 4 5 ## Description 6 7 The 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 23 Setup Python and build examples according to main README. 24 25 ```bash 26 # Download PTH model 27 wget -P examples/sam/ https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth 28 29 # Convert PTH model to ggml 30 python 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 38 You 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 46 main: seed = `1693224265` 47 main: loaded image 'img.jpg' (680 x 453) 48 sam_image_preprocess: scale = 0.664062 49 main: preprocessed image (1024 x 1024) 50 sam_model_load: loading model from 'models/sam-vit-b/ggml-model-f16.bin' - please wait ... 51 sam_model_load: n_enc_state = 768 52 sam_model_load: n_enc_layer = 12 53 sam_model_load: n_enc_head = 12 54 sam_model_load: n_enc_out_chans = 256 55 sam_model_load: n_pt_embd = 4 56 sam_model_load: ftype = 1 57 sam_model_load: qntvr = 0 58 operator(): ggml ctx size = 202.32 MB 59 sam_model_load: ...................................... done 60 sam_model_load: model size = 185.05 MB / num tensors = 304 61 embd_img 62 dims: 64 64 256 1 f32 63 First & Last 10 elements: 64 -0.05117 -0.06408 -0.07154 -0.06991 -0.07212 -0.07690 -0.07508 -0.07281 -0.07383 -0.06779 65 0.01589 0.01775 0.02250 0.01675 0.01766 0.01661 0.01811 0.02051 0.02103 0.03382 66 sum: 12736.272313 67 68 Skipping mask 0 with iou 0.705935 below threshold 0.880000 69 Skipping mask 1 with iou 0.762136 below threshold 0.880000 70 Mask 2: iou = 0.947081, stability_score = 0.955437, bbox (371, 436), (144, 168) 71 72 73 main: load time = 51.28 ms 74 main: total time = 2047.49 ms 75 76 real 0m2.068s 77 user 0m16.343s 78 sys 0m0.214s 79 ``` 80 81 Input point is (414.375, 162.796875) (currently hardcoded) 82 83 Input image: 84 85 ![llamas](https://user-images.githubusercontent.com/`8558655`/`261301565`-`37b7bf4b`-bf91-40cf-8ec1-`1532316e1612`.jpg) 86 87 Output 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)