
In a previous tech blog we described how to get started with using the Segment-Anything Model (SAM). In this blog we will continue working with SAM and show how to implement the segmentation of an entire image with Zero-shot learning (ZSL). The method presented with more implementation details is also available on github, to learn more please visit the link.
What is Zero-shot learning (ZSL)?
Zero-shot learning is a machine learning technique that trains a model to perform a task without giving concrete examples of the task. Instead of giving specific examples, it learns from different but related tasks. For example, when changing the object detection target, the detection can be done simply by providing the text of the class name of the detection target (car or traffic light) without having to prepare a new dataset to retrain with. This approach is useful when labelled data for a task is not available or when data acquisition is costly. If you’d like to learn more, visit this blog (Japanese only).
How can I segment an entire image in SAM?
SAM is built on the assumption that information about a specific object is given as a point (click point) or bounding box prompt. This makes it difficult to apply SAM directly when segmentation is to be performed on the entire image.
On the other hand, SAM has high zero-shot learning performance and can obtain accurate segmentation for unseen objects. So, coarse segmentation into individual objects with a model with high zero-shot performance for another task (Grounding DINO or CLIPSeg) can be combined with SAM to produce a high-quality segmentation for the whole image.
Entire image segmentation algorithm using SAM
In addition to SAM, Grounding DINO, an object detection model with high zero-shot performance, and CLIPSeg, a segmentation model, are used here.
Procedure
- Perform object detection in Grounding DINO and input the resulting bounding box to SAM to create an object mask
- Perform segmentation in CLIPSeg and remove overlaps with the mask obtained in step 1
- Input the sampled points from the mask obtained in step 2 as prompts to the SAM to create a stuff mask
- Combine the thing mask from step 1 and the stuff mask from step 3The above steps are represented in the diagram below:

Let’s now look at each step of the procedure using a running example.
In this article, we will continue to use the following images from the previous article:

Step 1: GroundingDINO + SAM (creation of thing masks).
First, object detection of ‘sushi’ is carried out using GroundingDINO.
# use dino_detection function to perform object(thing) detection on the image
thing_boxes, thing_category_ids, visualization = dino_detection(
dino_model,
image, # Image.open("test_img/sushi.jpg").convert("RGB"),
image_array, # np.asarray(image),
thing_category_names, # ["sushi"]
category_name_to_id, # {"sushi": 0},
dino_box_threshold, # 0.35,
dino_text_threshold, # 0.25,
device, # "cpu",
visualize=True,
)

All sushi in the image have been assigned a bounding box. The pickled ginger is also detected as sushi, but if you look at the confidence score in the upper left corner of the bounding box, you can see that the score for the pickled ginger is 0.41, which is smaller than the other sushi.
In this case, the goal is to segment the entire image, so all these bounding boxes are used to create a thing mask. 6 bounding boxes were each entered into SAM as prompts, resulting in the following.






You can see that each sushi (or ginger) is segmented with fairly good accuracy.
Step 2: Segmentation using CLIPSeg
CLIPSeg is used to segment objects other than the main object (sushi). Here, we set objects other than sushi, such as ‘wood’ and ‘table’, as detection targets.
# use the clipseg_segmentation function to perform object(stuff) detection
clipseg_preds, clipseg_semantic_inds = clipseg_segmentation(
clipseg_processor,
clipseg_model,
image, # Image.open("test_img/sushi.jpg").convert("RGB"),
stuff_category_names, # ["wood", "table"],
segmentation_background_threshold, # 0.1,
device, # "cpu",
)


The CLIPSeg results show that the sushi plate is segmented as ‘wood’ and the table as ‘table’, and although the borders are slightly distorted, the segmentation The result of CLIPSeg shows that the segmentation is successful, although the boundaries are a little distorted.
Next, the thing mask area obtained in step 1 has already been segmented, so the overlapping parts are removed.

Step 3: CLIPSeg + SAM (creation of stuff masks)
From the segmentation results obtained in step 2, each point is sampled (see below, red X) and entered into SAM as a prompt. The result of the SAM for the table is shown in the diagram below: whereas the contours of ‘wood’ and ‘table’ were distorted using CLIPSeg alone, a cleaner contour was obtained by using SAM. The SAM results are shown in the diagram below.


Step 4: Combine thing mask and stuff mask
Finally, the result is a combination of the thing mask obtained with Grounding Dino + SAM in step 1 and the stuff mask obtained with CLIPSeg + SAM in step 3.

Conclusion
As described above, by combining several models with excellent zero-shot performance, semi-automatic (only specifying the detection targets ‘sushi’, ‘wood’, ‘table ‘ as detection targets), high-quality segmentation results for the entire image can be obtained.
While the conventional approach in AI has been to improve performance by collecting and re-training training data for the desired task, this new approach, of combining multiple strong models to solve a new task, is becoming increasingly common. The number of ways to combine models is enormous, so know-how on how to combine AI models is likely to become important in the future.