SAM and HQ-SAM: A New Generation of Image Segmentation Models

The Segment Anything Model (SAM) is a game-changing technique for image segmentation. SAM is a prompt able segmentation model developed by Meta AI’s FAIR team that may be utilized for a variety of tasks. This article will give a simplified overview of the model, its architecture, the challenges it answers, potential use cases, and a brief illustration of how it is implemented.

Segment Anything Model (SAM)

SAM is a model for segmenting any object in an image. It is a prompt able segmentation model, which means that it may be directed to execute certain tasks through the use of prompts. The model was created to handle a wide range of visual data, such as simulations, paintings, underwater photographs, microscopy images, driving data, stereo images, and fish-eye images.

What Issues Does SAM Address

The primary issue that SAM solves is image segmentation. Image segmentation is a critical process in computer vision that requires separating an image into many segments or sets of pixels. These segments in the image can represent various objects or sections of objects. SAM is built to accomplish this task efficiently and effectively, even in zero-shot settings when it has never seen the task before.

Structure of SAM (Segment Anything Model)

Sam And Hq-Sam
SAM and HQ-SAM: A New Generation of Image Segmentation Models 1

SAM’s structure is made up of three major components:

Image Encoder: A large component that processes the input image and generate an image embedding. This embedding can then be queried using various input prompts to generate object masks.

See also  How to Take Fantastic Artist Headshots in 2023 (7 Simple Steps)

Prompt Encoder: This component is intended to handle two types of prompts: sparse (points, boxes, text) and dense (masks). Points and boxes are represented by positional encodings combined with learnt embeddings for each prompt type. An off-the-shelf text encoder is used to handle text. Dense prompts (masks) are embedded using convolutions and element-wise summed with the picture embedding.

Mask Decoder: This component converts picture embeddings, prompt embeddings, and output tokens to masks. It employs a Transformer decoder block modification, followed by a dynamic mask prediction head. To update all embeddings, the decoder block employs prompt self-attention and cross-attention in both directions (prompt-to-image embedding and vice versa). After running two blocks, the picture embedding is unsampled, and an MLP transfers the output token to a dynamic linear classifier, which computes the mask foreground probability at each image point.

Case Studies

SAM is meant to be used for any activity that requires prompt-based segmentation. Among the use cases investigated are:

Segmenting Objects from a Point: SAM can be requested to segment specific items from a given point in an image.Edge Detection: SAM can be used for edge detection tasks, such as recognizing the borders of objects inside an image.Segmenting All Objects: Segmenting All Objects: SAM can be asked to segment all objects in an image.Segmenting Detected Objects: SAM can be used to segment recognized objects in images.Segmenting Objects from Text: Segmenting things from Text: SAM can work with other vision models to segment things based on text descriptions.

Installation command

The code requires Python>=3.8, as well as Pytorch>=1.7 and Torchvision>=0.8. Please follow the steps here to install the PyTorch and TorchVision requirements. It is strongly advised to install PyTorch and TorchVision with CUDA support.

See also  Easyerp AI: How to Create and Roleplay with AI Characters

Install Segment Anything:

pip install git+

During my tech research, I discovered that Sam has released its latest iteration known as HQ Sam. The following information outlines the details of my findings:

HQ Sam is the newest version of Sam, which has undergone significant development. This updated edition showcases a range of enhanced features and capabilities. With HQ Sam, users can expect a more advanced and efficient experience. The improvements made to HQ Sam encompass various aspects. The user interface has been revamped to provide a more intuitive and seamless interaction. Additionally, the underlying technology has been upgraded to enhance performance, responsiveness, and accuracy.

Visual comparison between SAM and HQ-SAM

Sam And Hq-Sam
SAM and HQ-SAM: A New Generation of Image Segmentation Models 2

The recently released Segment Anything Model (SAM) marks a significant advancement in scaling up segmentation models, enabling powerful zero-shot capabilities and customizable prompting. Despite having been trained with 1.1 billion masks, SAM’s mask prediction quality falls short in many circumstances, especially when dealing with objects with complex architecture. The HQ-SAM, which gives SAM the capacity to segment any object precisely while retaining SAM’s original promotable design, efficiency, and zero-shot generalizability.

Our meticulous approach reuses and retains SAM’s pre-trained model weights while introducing only a few extra parameters and computation. We design a learnable High-Quality Output Token that is injected into SAM’s mask decoder and is in charge of predicting the high-quality mask. Instead of just using it on mask-decoder features, we fuse it with early and final ViT features to improve mask details.

We create a dataset of 44K fine-grained masks from various sources to train our introduced learnable parameters. The introduced dataset of 44k masks is used to train HQ-SAM, which takes only 4 hours on 8 GPUs. We illustrate the efficacy of HQ-SAM in a set of 9 diverse segmentation datasets across several downstream tasks, 7 of which are tested in a zero-shot transfer methodology.

See also  Kundli GPT – Best AI Astrology Tool Online Free
Sam And Hq-Sam
SAM and HQ-SAM: A New Generation of Image Segmentation Models 3

Comparison between SAM and HQ-SAM

Note: For box-prompting evaluation, we input SAM and our HQ-SAM the identical image/video bounding boxes and use SAM’s single mask output mode.

Various ViT backbones on COCO

Sam And Hq-Sam
SAM and HQ-SAM: A New Generation of Image Segmentation Models 4

Note: For the COCO dataset, we employ a SOTA detector FocalNet-DINO trained on the COCO dataset as our box prompt generator.


Note: Using the ViT-L backbone. As our video boxes prompt generator, we use the SOTA detector Mask2Former trained on the YouTube VIS 2019 dataset while reusing its object association prediction.

Sam And Hq-Sam
SAM and HQ-SAM: A New Generation of Image Segmentation Models 5


Note: Using the ViT-L backbone. As our video box prompt generator, we use the SOTA model XMem while reusing its object association prediction.

Sam And Hq-Sam
SAM and HQ-SAM: A New Generation of Image Segmentation Models 6

Comparing interactive segmentation using several points

Note: Using the ViT-L backbone. On the premium COIFT (zero-shot) and DIS val sets.

Sam And Hq-Sam
SAM and HQ-SAM: A New Generation of Image Segmentation Models 7


In conclusion, the Segment Anything Model (SAM) is an innovative and transformative technique for image segmentation, offering unprecedented capabilities in accurately segmenting various objects. Furthermore, with the updated version of SAM, known as HQ-SAM (Segment Anything in High Quality), users can now achieve even higher quality segmentation results while retaining the original advantages of SAM, such as its promotable design, efficiency, and zero-shot generalizability. This advancement elevates the potential of SAM to new heights, making it an exceptional tool for image segmentation tasks requiring superior quality and precision. Please feel free to share your thoughts and feedback in the comment section below.