Models · Promptable segmentation
HQ-SAM
HQ-SAM (ETH Zurich): Segment Anything with an added head for sharper mask borders and thin structures, in sizes B, L and H.
stable Fork of SysCV/sam-hq@e696978 · backends: portable, kernels
- Paper Segment Anything in High Quality NeurIPS 2023 · first released 2 Jun 2023
Paper 2023 Added in 0.2.0 Evaluated with 0.2.0
Use it
pip install fastforks Lean install with only what this model needs: pip install fastforks-core
from fastforks import promptable_segmentation
model = promptable_segmentation.load("sam-hq-vit-b") # kernels on a supported GPU, else portable PyTorch
result = model(image, input_points=...)
result.pred_masks # also: iou_scores
result.backend # what ran: mode, device, dtype, kernels fastforks bench sam-hq-vit-b Times the model on this machine. load(…, mode="portable") or mode="kernels" picks a backend; usage guide.
Speed and memory
Every backend on one GPU (RTX 5060 Ti 16 GB) and the same input, the authors’ code included. Kernels runs fp16. Portable runs fp16 on CUDA.
| Backend | dtype | Time per call (ms) | vs original fp16 | Peak memory allocated / reserved | Load (s) | First call (s) |
|---|---|---|---|---|---|---|
| Original fp32upstream SamPredictor.set_image + predict (centre point, multimask_output=True, hq_token_only=False) | fp32 | 172 | 0.60× | 2,886 MiB/2,934 MiB | 3.4 | 0.74 |
| Original fp16torch.autocast(float16) around the call; upstream SamPredictor.set_image + predict (centre point, multimask_output=True, hq_token_only=False) | fp16 | 103 | – | 1,704 MiB/1,756 MiB | 0.8 | 0.19 |
| Portable fp32 | fp32 | not measured | – | – | – | – |
| Portable | fp16 | 49.8 | 2.1× | 375 MiB/422 MiB | 0.7 | 0.39 |
| Kernels | fp16 | 36.8 | 2.8× | 420 MiB/564 MiB | 1.0 | 0.38 |
| Portable + torch.compile | 39.4 | 2.6× | 372 MiB/422 MiB | 33 (build) | – | |
| TensorRT fp16image encoder only | fp16 | 28.0 | 3.7× | 342 MiBenginenot comparable | 39 (build) | – |
Measured on RTX 5060 Ti 16 GB, torch 2.14.1+cu130, 2026-11-10 to 2026-11-16. First call, 3 warm-up, peak reset, median of 3 rounds x median of 5. Ratios divide two
times of this table (same GPU, same input). Original fp16 is the authors’ own precision option, else torch.autocast. torch.compile and TensorRT: build time instead of load time. TensorRT memory (engine, not comparable) is the GPU memory the engine took, read from the driver because TensorRT allocates outside PyTorch, so it can't be compared with the PyTorch peaks of the other rows.
Other devices
| Device | Backend | dtype | ms / call | Peak memory | Output vs reference | Input |
|---|---|---|---|---|---|---|
| Apple M1 Pro 16 GB MPS, torch 2.14.1, 2026-11-14 | Original | fp32 | 745 | 4,909 MiB process | – | 1024x1024 |
| Portable | fp16 | 683 | 4,542 MiB process | matches | 1024x1024 | |
| Portable | fp32 | 645 | 4,374 MiB process | matches | 1024x1024 | |
| Kernels | fp16 | not implemented | – | 1024x1024 | ||
CUDA memory is torch’s peak allocated / reserved; Apple has no such counter, so the Mac rows show the process footprint (weights, Python and torch included). “Output vs reference” compares the device’s output with the stored golden output under the card’s tolerance; – = not checked on that device. Kernels “not implemented” = no kernels for this model on that backend yet: it runs portable there.
Licence and weights
Commercial use allowed
- Code
- Apache-2.0 LICENSE
- Weights
- Apache-2.0 source
- Training data
- the HQ head is trained on HQSeg-44K (six existing fine-mask datasets, per the paper; their terms were not checked) on top of SAM's frozen weights (SA-1B, under the SA-1B research licence)
Our reading of the licences, not legal advice.
Weights
sam_hq_vit_b.pth 379 MB· hosted on Hugging Facefastforks downloads and checks these files on first use (fastforks download sam-hq-vit-b).
Accuracy
The test set through the authors’ code and through ours: same checkpoint, same protocol. The distance between the original’s fp32 and fp16 runs is what half precision alone costs; our runs should stay within it.
COCO val2017
every 10th image, point prompts, mIoU best of 3 / single mask %
| Run | dtype | mIoU best of 3 % ↑ | mIoU single % ↑ | Δ mIoU best of 3 % vs fp32 original | images | ms / image |
|---|---|---|---|---|---|---|
| Original fp32 | fp32 | 52.19 | 51.05 | reference | 500 | 182 |
| Original fp16 | fp16 | 52.28 | 51.11 | +0.09 | 500 | 108 |
| Portable fp32 | fp32 | 52.18 | 51.04 | −0.01 | 500 | – |
| Portable | fp16 | 52.19 | 51.04 | ±0.00 | 500 | 60.1 |
| Kernels | fp16 | 52.19 | 51.04 | ±0.00 | 500 | 48.9 |
One point per ground-truth instance (the interior point farthest from the border); IoU of the best-scored of 3 masks / of the single mask. Original = the authors’ code, same checkpoint and protocol. Measured on original fp32, original fp16: RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-10; portable fp32: RTX 5060 Ti, torch 2.14.1+cu130, fastforks 44640884, 2026-11-18; portable, kernels: RTX 5060 Ti, torch 2.14.1+cu130, fastforks f2992269, 2026-11-10.
Closeness to the original
Beyond the test-set scores: the raw output of ours against the original’s fp32 output on the same input, pixel by pixel. CPU, float32: max |diff| <= 0.002 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.43 % and p99 <= 1.7 % of the reference's median |value| (absolute error of the output pred_masks).
| Check | Tolerance | Measured |
|---|---|---|
| CPU fp32, max |diff| | ≤ 0.002 (relative) | not recorded |
| GPU default dtypes, mean |diff| | ≤ 0.0043 (relative) | 0.00121 (relative) |
| GPU default dtypes, p99 |diff| | ≤ 0.017 (relative) | 0.00352 (relative) |
Recorded for sam-hq-vit-b on RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-10, with these kernels: dpt_fused
Details
Segment Anything in High Quality (Ke et al. 2023): SAM with one more output token and a small head that fuses the final embedding with an early ViT feature, for sharper mask borders and thin structures; SAM's weights are frozen, so prompts work the same. Each prompt gives one mask (SAM's mask plus the HQ mask, upstream's default); `multimask_output` picks the best-scored of SAM's three candidates as the SAM part. The portable `module()` is the image side only (embedding and HQ feature).
- Input / output
- Input: one image plus a point, box or text prompt. Output: masks with quality scores.
- Input size
- any H x W >= 1, resized inside to the fixed encoder input 1024x1024 (the default export size); tested up to 1080x1920 (398 MiB allocated)
- Outputs
pred_masks,iou_scores- Checkpoint
embed_dim=768,depth=12,num_heads=12- CUDA kernels
dpt_fusedfused fp16 matrix multiplies and convolutions for DINOv2 backbones and DPT depth heads
- Apple (MPS)
- fp32, fp16
Limitations
- Kernels keep CUDA graphs: their private pool and static outputs stay reserved, so peak reserved is ~125 MiB above portable (peak allocated is not); `cuda_graphs=false` or `release_graphs()` frees it.
Full reference, generated from the repository
HQ-SAM (ETH Zurich): Segment Anything with an added head for sharper mask borders and thin structures, in sizes B, L and H.
Commercial use: yes.
| variant | input | backend | time (ms) | peak memory (MiB, allocated / reserved) |
|---|---|---|---|---|
sam-hq-vit-b | synthetic (1024x1024) | original (fp32) original portable kernels portable + torch.compile TensorRT fp16 | 172 103 49.8 36.8 39.4 28.0 (image encoder only) | 2886 / 2934 1704 / 1756 375 / 422 420 / 564 372 / 422 609 / 642 |
sam-hq-vit-l | synthetic (1024x1024) | original (fp32) original portable kernels portable + torch.compile TensorRT fp16 | 412 202 118 81.9 97.0 68.3 (image encoder only) | 4545 / 4576 2969 / 3018 834 / 882 877 / 1026 822 / 902 1482 / 1538 |
sam-hq-vit-h | synthetic (1024x1024) | original (fp32) original portable kernels portable + torch.compile TensorRT fp16 | 738 311 217 143 187 125 (image encoder only) | 5848 / 5896 4266 / 4318 1488 / 1566 1528 / 1688 1476 / 1526 2790 / 2818 |
Details
- Original code: https://github.com/SysCV/sam-hq at
e696978d6035 - Code licence: Apache-2.0 (permissive); files:
LICENSE - Weights licence: Apache-2.0
- Licence evidence: https://huggingface.co/lkeab/hq-sam/blob/09b02a333b37772133eff3997bdba997867374b7/README.md (license: apache-2.0); code: https://github.com/SysCV/sam-hq/blob/e696978d60352dc9a26b12631cd91781502c6546/LICENSE
- Training data: the HQ head is trained on HQSeg-44K (six existing fine-mask datasets, per the paper; their terms were not checked) on top of SAM's frozen weights (SA-1B, under the SA-1B research licence)
- Kernel libraries:
dpt_fused - Variants:
sam-hq-vit-b,sam-hq-vit-l,sam-hq-vit-h - Outputs: pred_masks, iou_scores
- Input size: any H x W >= 1, resized inside to the fixed encoder input 1024x1024 (the default export size)
- Tested up to (portable, fp16, RTX 5060 Ti 16 GB):
sam-hq-vit-b1080x1920 (0.4 GiB peak allocated, 0.5 reserved);sam-hq-vit-l1080x1920 (0.8 GiB peak allocated, 1.1 reserved);sam-hq-vit-h1080x1920 (1.5 GiB peak allocated, 1.9 reserved) sam-hq-vit-b1024x1024, first call: portable + torch.compile 33 s (cold Inductor cache), TensorRT fp16 39 s (ONNX + engine).sam-hq-vit-l1024x1024, first call: portable + torch.compile 33 s (cold Inductor cache), TensorRT fp16 53 s (ONNX + engine).sam-hq-vit-h1024x1024, first call: portable + torch.compile 42 s (cold Inductor cache), TensorRT fp16 69 s (ONNX + engine).sam-hq-vit-boriginal (fp32) 1024x1024: upstream SamPredictor.set_image + predict (centre point, multimask_output=True, hq_token_only=False)sam-hq-vit-boriginal 1024x1024: torch.autocast(float16) around the call; upstream SamPredictor.set_image + predict (centre point, multimask_output=True, hq_token_only=False)sam-hq-vit-loriginal (fp32) 1024x1024: upstream SamPredictor.set_image + predict (centre point, multimask_output=True, hq_token_only=False)sam-hq-vit-loriginal 1024x1024: torch.autocast(float16) around the call; upstream SamPredictor.set_image + predict (centre point, multimask_output=True, hq_token_only=False)sam-hq-vit-horiginal (fp32) 1024x1024: upstream SamPredictor.set_image + predict (centre point, multimask_output=True, hq_token_only=False)sam-hq-vit-horiginal 1024x1024: torch.autocast(float16) around the call; upstream SamPredictor.set_image + predict (centre point, multimask_output=True, hq_token_only=False)sam-hq-vit-bload / first call: original 0.77 s / 0.192 s, portable 0.69 s / 0.386 s, kernels 0.97 s / 0.378 ssam-hq-vit-lload / first call: original 1.98 s / 0.205 s, portable 1.35 s / 0.432 s, kernels 1.25 s / 0.419 ssam-hq-vit-hload / first call: original 3.92 s / 0.317 s, portable 2.27 s / 0.522 s, kernels 2.01 s / 0.481 s- Protocol, first call, 3 warm-up, peak reset, median of 3 rounds x median of 5
Measured
One point per ground-truth instance (the interior point farthest from the border); IoU of the best-scored of 3 masks / of the single mask. Original = the original code named under Details, same checkpoint and protocol.
| variant | dataset | original fp32 | original fp16 | portable | portable fp32 | kernels | note |
|---|---|---|---|---|---|---|---|
sam-hq-vit-b | COCO val2017, every 10th image, point prompts, mIoU best of 3 / single mask % | 52.19 / 51.05 | 52.28 / 51.11 | 52.19 / 51.04 | 52.18 / 51.04 | 52.19 / 51.04 | |
sam-hq-vit-l | COCO val2017, every 10th image, point prompts, mIoU best of 3 / single mask % | 56.04 / 52.52 | 56.00 / 52.53 | 56.09 / 52.52 | 56.08 / 52.52 | ||
sam-hq-vit-h | COCO val2017, every 10th image, point prompts, mIoU best of 3 / single mask % | 56.15 / 53.43 | 56.09 / 53.45 | 56.16 / 53.43 | 56.14 / 53.43 |
Measured on portable: NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks f2992269, 2026-11-10; portable32: NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks 44640884, 2026-11-18; kernels: NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks f2992269, 2026-11-10.
Parity
CPU, float32: max |diff| <= 0.002 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.43 % and p99 <= 1.7 % of the reference's median |value| (absolute error of the output pred_masks).
| variant | kernels error vs original, mean / p99 | GPU, torch | kernels used |
|---|---|---|---|
sam-hq-vit-b | 0.00121 / 0.00352 | RTX 5060 Ti, torch 2.14.1+cu130 | dpt_fused@48e0065bb7cf:sm_120 |
sam-hq-vit-l | 0.00286 / 0.0111 | RTX 5060 Ti, torch 2.14.1+cu130 | dpt_fused@48e0065bb7cf:sm_120 |
sam-hq-vit-h | 0.00141 / 0.00561 | RTX 5060 Ti, torch 2.14.1+cu130 | dpt_fused@48e0065bb7cf:sm_120 |
Limitations
- Kernels keep CUDA graphs: their private pool and static outputs stay reserved, so peak reserved is ~125 MiB above portable (peak allocated is not);
cuda_graphs=falseorrelease_graphs()frees it.
Example output
One fixed input through this checkpoint, the same for every model of the task (kernels · float16 · NVIDIA GeForce RTX 5060 Ti). Prompt: one foreground point.
Input: A cup of coffee, Rachel Michetti (CC0).