Abstract
Segment anything model (SAM) is a promising prompt-guided vision foundation model to segment objects of interest. However, the extensive computational requirements of SAM have limited its applicability in resource-constraint edge devices. Post-training quantization (PTQ) is an effective potential for fast-deploying SAM. Nevertheless, SAM’s billion-scale pretraining creates a highly asymmetric activation distribution with detrimental outliers in excessive channels, resulting in significant performance degradation of the low-bit PTQ. In this paper, we propose PQ-SAM, the first PTQ method customized for SAM. To achieve a quantization-friendly tensor-wise distribution, PQ-SAM incorporates a novel grouped activation distribution transformation (GADT) based on a two-stage outlier hierarchical clustering (OHC) scheme to scale and shift each channel. Firstly, OHC identifies and truncates extreme outliers to reduce the scale variance of different channels. Secondly, OHC iteratively allocates learnable shifting and scaling sizes to each group of channels with similar distributions, reducing the number of learnable parameters and easing the optimization difficulty. These shifting and scaling sizes are used to adjust activation channels, and jointly optimized with quantization step sizes for optimal results. Extensive experiments demonstrate that PQ-SAM outperforms existing PTQ methods on nine zero-shot datasets, and pushes the 4-bit PTQ of SAM to a usable level.
The Challenge: Quantizing a Foundation Model
The Segment Anything Model (SAM) represents a significant leap in computer vision, yet its massive Vision Transformer (ViT) architecture poses a major barrier for deployment on edge devices. While Post-Training Quantization (PTQ) is a standard technique to reduce model size by converting 32-bit floating-point numbers into lower-precision integers (e.g., 4-bit or 8-bit), it fails significantly when applied to SAM.
As shown in the figure above, SAM's activations exhibit a highly asymmetric distribution with extreme outliers concentrated in specific channels—a direct consequence of its billion-scale pretraining. These outliers "blind" standard quantization algorithms, leading to catastrophic accuracy drops that existing PTQ methods for standard ViTs cannot mitigate.
The PQ-SAM Framework
PQ-SAM introduces two primary innovations to transform SAM's activations into a "quantization-friendly" format without requiring massive retraining.
Grouped Activation Distribution Transformation (GADT)
Instead of quantizing raw activations, PQ-SAM learns to shift and scale them. These transformations are mathematically reparameterized, allowing the model to maintain channel-wise precision during training while benefiting from the hardware efficiency of per-tensor quantization during deployment.
Outlier Hierarchical Clustering (OHC)
To manage the complexity of optimizing thousands of channels, the researchers developed OHC, a two-stage process:
- Tensor-wise Truncating: Using the Interquartile Range (IQR), the method identifies and clips extreme outliers across the entire layer to reduce variance.
- Channel-wise Grouping: Channels with similar distributions are iteratively grouped. Each group shares a single pair of learnable shifting and scaling parameters, which simplifies optimization and prevents overfitting.
Finally, the quantization step sizes and GADT parameters are jointly optimized using a small calibration set of just 100 unlabeled images from the SA1B dataset.
Experimental Results and Insights
The researchers evaluated PQ-SAM across nine zero-shot datasets, covering point prompts, automatic edge detection, and bounding box modes. The results demonstrate a significant breakthrough in low-bit quantization:
- 4-bit Usability: PQ-SAM achieves a usable 4-bit (W4A4) quantization level. In medical imaging tests (BBBC038V1), it outperformed the previous state-of-the-art method, RepQ-ViT, by over 40% in Intersection over Union (IoU).
- Near-Lossless 6-bit: At 6-bit precision (W6A6), the performance gap compared to the full-precision 32-bit model is less than 0.04 IoU across most benchmarks.
- Robustness: Even in extreme 2-bit settings, the OHC scheme provided a 35% improvement in Dice score over existing methods on specific datasets.
Conclusion and Future Work
PQ-SAM successfully preserves SAM's zero-shot capabilities even when compressed to 4 bits. While there remains a performance gap in highly complex scenes at 4-bit precision, the method's efficiency—requiring no fine-tuning on the original massive dataset—makes it a practical solution for developers. Future work may involve combining PQ-SAM with structural pruning or knowledge distillation to enable SAM on even smaller, low-power sensors.
Summary updated