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

Checkpoint

Paper 2023 Added in 0.2.0 Evaluated with 0.2.0

Kernels, RTX 5060 Ti 16 GB 36.8 ms 1024x1024, fp16
vs the faster original 2.8× original fp16: 103 ms
Peak memory 420 MiB original: 1,704 MiB
Accuracy vs original fp32 ±0.00 kernels mIoU best of 3 %, COCO val2017 (52.19 original)

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.

BackenddtypeTime per call (ms)vs original fp16Peak memory
allocated / reserved
Load (s)First call (s)
Original fp32upstream SamPredictor.set_image + predict (centre point, multimask_output=True, hq_token_only=False)fp32 1720.60×2,886 MiB/2,934 MiB3.40.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 MiB0.80.19
Portable fp32fp32not measured ––––
Portablefp16 49.82.1×375 MiB/422 MiB0.70.39
Kernelsfp16 36.82.8×420 MiB/564 MiB1.00.38
Portable + torch.compile 39.42.6×372 MiB/422 MiB33 (build)–
TensorRT fp16image encoder onlyfp16 28.03.7×342 MiBenginenot comparable39 (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

DeviceBackenddtypems / callPeak memoryOutput vs referenceInput
Apple M1 Pro 16 GB MPS, torch 2.14.1, 2026-11-14Originalfp32745 4,909 MiB process–1024x1024
Portablefp16683 4,542 MiB processmatches1024x1024
Portablefp32645 4,374 MiB processmatches1024x1024
Kernelsfp16not 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 Face

fastforks 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 %

RundtypemIoU best of 3 % ↑mIoU single % ↑Δ mIoU best of 3 % vs fp32 originalimagesms / image
Original fp32fp3252.1951.05 reference 500 182
Original fp16fp1652.2851.11 +0.09 500 108
Portable fp32fp3252.1851.04 −0.01 500 –
Portablefp1652.1951.04 ±0.00 500 60.1
Kernelsfp1652.1951.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).

CheckToleranceMeasured
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.

variantinputbackendtime (ms)peak memory (MiB, allocated / reserved)
sam-hq-vit-bsynthetic (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-lsynthetic (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-hsynthetic (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-b 1080x1920 (0.4 GiB peak allocated, 0.5 reserved); sam-hq-vit-l 1080x1920 (0.8 GiB peak allocated, 1.1 reserved); sam-hq-vit-h 1080x1920 (1.5 GiB peak allocated, 1.9 reserved)
  • sam-hq-vit-b 1024x1024, first call: portable + torch.compile 33 s (cold Inductor cache), TensorRT fp16 39 s (ONNX + engine).
  • sam-hq-vit-l 1024x1024, first call: portable + torch.compile 33 s (cold Inductor cache), TensorRT fp16 53 s (ONNX + engine).
  • sam-hq-vit-h 1024x1024, first call: portable + torch.compile 42 s (cold Inductor cache), TensorRT fp16 69 s (ONNX + engine).
  • sam-hq-vit-b original (fp32) 1024x1024: upstream SamPredictor.set_image + predict (centre point, multimask_output=True, hq_token_only=False)
  • sam-hq-vit-b original 1024x1024: torch.autocast(float16) around the call; upstream SamPredictor.set_image + predict (centre point, multimask_output=True, hq_token_only=False)
  • sam-hq-vit-l original (fp32) 1024x1024: upstream SamPredictor.set_image + predict (centre point, multimask_output=True, hq_token_only=False)
  • sam-hq-vit-l original 1024x1024: torch.autocast(float16) around the call; upstream SamPredictor.set_image + predict (centre point, multimask_output=True, hq_token_only=False)
  • sam-hq-vit-h original (fp32) 1024x1024: upstream SamPredictor.set_image + predict (centre point, multimask_output=True, hq_token_only=False)
  • sam-hq-vit-h original 1024x1024: torch.autocast(float16) around the call; upstream SamPredictor.set_image + predict (centre point, multimask_output=True, hq_token_only=False)
  • sam-hq-vit-b load / first call: original 0.77 s / 0.192 s, portable 0.69 s / 0.386 s, kernels 0.97 s / 0.378 s
  • sam-hq-vit-l load / first call: original 1.98 s / 0.205 s, portable 1.35 s / 0.432 s, kernels 1.25 s / 0.419 s
  • sam-hq-vit-h load / 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.

variantdatasetoriginal fp32original fp16portableportable fp32kernelsnote
sam-hq-vit-bCOCO val2017, every 10th image, point prompts, mIoU best of 3 / single mask %52.19 / 51.0552.28 / 51.1152.19 / 51.0452.18 / 51.0452.19 / 51.04
sam-hq-vit-lCOCO val2017, every 10th image, point prompts, mIoU best of 3 / single mask %56.04 / 52.5256.00 / 52.5356.09 / 52.5256.08 / 52.52
sam-hq-vit-hCOCO val2017, every 10th image, point prompts, mIoU best of 3 / single mask %56.15 / 53.4356.09 / 53.4556.16 / 53.4356.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).

variantkernels error vs original, mean / p99GPU, torchkernels used
sam-hq-vit-b0.00121 / 0.00352RTX 5060 Ti, torch 2.14.1+cu130dpt_fused@48e0065bb7cf:sm_120
sam-hq-vit-l0.00286 / 0.0111RTX 5060 Ti, torch 2.14.1+cu130dpt_fused@48e0065bb7cf:sm_120
sam-hq-vit-h0.00141 / 0.00561RTX 5060 Ti, torch 2.14.1+cu130dpt_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=false or release_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
Output
A cup of coffee, input
Input · 100%

Input: A cup of coffee, Rachel Michetti (CC0).