Back to paper library
arXiv 2023ICCV 2023SegmentationPaper notes

SegGPT

Copy the Example

Original paper

SegGPT: Segmenting Everything In Context

Paper authors

Xinlong Wang · Xiaosong Zhang · Yue Cao · Wen Wang · Chunhua Shen · Tiejun Huang

Publication
ICCV 2023Conference
First submitted to arXiv
arXiv:2304.03284
Cite this paper · BibTeX

The citation identifies the linked source version. It cites the original paper and its authors.

@misc{segmentationseggpt2023,
  title = {{SegGPT: Segmenting Everything In Context}},
  author = {Xinlong Wang and Xiaosong Zhang and Yue Cao and Wen Wang and Chunhua Shen and Tiejun Huang},
  year = {2023},
  eprint = {2304.03284},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2304.03284}
}
On this page

GPT-3 made the term "in-context learning" popular. No retraining needed; put a few examples in the input and the model knows what you want it to do.

Xinlong Wang, Xiaosong Zhang, Yue Cao and colleagues at the Beijing Academy of Artificial Intelligence (BAAI) wanted to bring the same thing to segmentation. Show the model an example image and its mask, and it segments a new image following what the example means. If the example paints "all the balls," it segments every ball in the new image; if the example paints "shadows," it segments shadows.

SegGPT came out the same week as Meta's SAM. SAM wants a point or a box; SegGPT wants an example.

Defining the Problem

Most segmentation models are specialists that segment only certain classes, at a certain granularity, on a certain kind of data. To segment a new concept, or to move from images to video, you have to annotate and train all over again.

The authors want to train a generalist model that can segment anything, which raises two difficulties:

  1. The data formats differ too much. Part segmentation, semantic segmentation, instance segmentation, panoptic segmentation, human parts, medical images, aerial images; each has its own annotation format and its own class definitions.
  2. It has to handle unseen tasks. Traditional multi-task learning fixes a list of tasks in advance, with one output head per task. The authors want task definitions flexible enough to cover even tasks never seen in training.

SegGPT builds on Painter, an earlier model from the same team. Painter draws the outputs of all vision tasks as "images." A depth map is an image, a segmentation result is a colored image, and keypoints are an image too. That way every task becomes the same job, painting in the corresponding output image for a given input image.

Painter has a problem, though. It fixes the colors for each task in advance; in semantic segmentation each class has a fixed color, with "person" always a particular red, for example. So what the model learns is "when you see an example in these colors, do semantic segmentation." It's really recognizing the task by its colors, which slides back into multi-task learning.

Solving the Problem

Random Coloring

Original Figure 2: SegGPT's training framework. On the left, various kinds of segmentation data (part, semantic, instance, panoptic, human, medical, aerial) are converted into in-context samples: each column holds several images that share the same context, along with their masks, and within a column the same color means the same class or the same instance. On the right is the training method: images and masks are stitched together, some patches are masked out, and the result goes into a Vision Transformer, which has to fill in the masked-out masks (in-context coloring).
Original Figure 2, Wang et al., from PDF page 3, CC BY 4.0.

SegGPT's key change to Painter is to make the colors random.

Each training sample is produced like this:

  1. Take an image and its segmentation annotation.
  2. Find another image that "shares context," for example one containing the same class, or another view of the same instance.
  3. Pick a few colors from the annotation and swap each for another color at random, applying the same mapping to both images.

The resulting pair of images is called an in-context pair. In this pair "red" might mean a person; in the next sample red might mean sky. The model can no longer cheat with "red means person" and has to look at the example instead. Whatever regions are painted the same color in the example image, the corresponding regions in the new image get painted that same color.

The authors call this in-context coloring. Different kinds of data only change how the context is picked. Semantic segmentation picks a few classes at random, instance segmentation picks a few instances at random, and differently augmented versions of the same image can serve as each other's context. There's also a mix-context training method that stitches together several images sharing the same color mapping and then randomly crops and resizes them, making the model focus even more on context.

The model itself is entirely unchanged. It's a standard ViT-L (307 million parameters). The example image, the example mask, the new image and the mask to be filled in are stitched into one large image, the part to be predicted is masked out, and a smooth-ℓ1 loss asks the model to fill the pixel values back in, much as MAE masks patches and reconstructs them.

Every segmentation dataset is handled the same way, with no hand-merging of classes. The training data includes ADE20K, COCO, PASCAL VOC, Cityscapes, LIP for human parts, PACO for object parts, four retinal-vessel datasets, and the aerial iSAID and LoveDA.

Inference: Just Give an Example

Original Figure 1: SegGPT uses a single model to do different segmentation tasks from different examples. In each group, the orange box on the left holds the example image and its mask, and the blue box on the right holds the new image and the model's output (the bright regions). Given different examples, the same image full of geometric solids yields "that big red ball," "all the balls," "shadows," "top surfaces" and "the outlines of all the balls"; other examples include rainbows, parts of the Statue of Liberty, objects in a video, and ADE20K semantic segmentation with a learned prompt.
Original Figure 1, Wang et al., from PDF page 1, CC BY 4.0.

Once trained, it's used by stitching an example image and its mask together with the new image to be segmented and letting the model fill in the new image's mask.

The groups in the top half show best what this model does. With the same table of geometric solids, if the example paints only one big red ball, it segments only that ball; if the example paints all the balls, it segments all the balls; if the example paints shadows, top surfaces or outlines, it follows along. None of these "tasks" are predefined classes; the example tells it on the spot. The example mask can also use several colors, segmenting several classes or objects at once.

Using Multiple Examples

Original Figure 3: two ways of combining multiple examples. At the top, Spatial Ensemble stitches several examples into a grid and shrinks it to the size of a single example before feeding it to SegGPT; at the bottom, Feature Ensemble computes the examples separately and averages the new image's features after each attention layer.
Original Figure 3, Wang et al., from PDF page 4, CC BY 4.0.

Sometimes one example isn't enough. To segment a class, for instance, several different examples are more reliable, and in a video all the earlier frames can serve as examples. The authors designed two ways to combine them:

  • Spatial ensemble. n×nn \times n examples are stitched into a grid and shrunk to the size of one example. This adds almost no compute, but every example gets shrunk.
  • Feature ensemble. Each example is stitched together with the new image and computed separately along the batch dimension; after each attention layer, the features of the new-image half are averaged. The new image can thus draw on all the examples at once.

Tuning a Prompt Image for a Specific Use

The last trick is called in-context tuning. The whole model is frozen, a learnable "image" is initialized as the example, and only this image is trained, with the same loss.

The trained image becomes a plug-and-play key. You can tune one for a fixed-class dataset like ADE20K, or one for "your apartment" or for "Bert's face" from Sesame Street. The model itself is never touched, so its original general ability is unaffected.

Discussion

Training starts from Painter's weights, with a batch size of 2,048 and only 9,000 iterations; a single image is 448×448. Importantly, every evaluation uses the same model.

Few-Shot Segmentation

In example-based semantic segmentation, the model is given one example image (one-shot) or a few (few-shot) and segments the same class (Table 1 of the paper, mIoU):

MethodCOCO-20i 1-shotCOCO-20i few-shotPASCAL-5i 1-shotPASCAL-5i few-shot
HSNet41.249.566.270.4
FPTrans47.058.968.878.0
FPTrans (test classes in training)56.565.577.783.2
Painter32.832.664.564.6
SegGPT56.167.983.289.8

The comparison conditions need care here. The standard COCO-20i and PASCAL-5i remove the test classes from the training data, but SegGPT's training data already includes COCO and PASCAL VOC, so it has seen every test class. The authors are aware of this, so they also re-evaluated the specialized models with the test classes included in training. Under the same conditions, SegGPT is about level with FPTrans on COCO-20i (56.1 vs. 56.5) and clearly better on PASCAL-5i. Also, FPTrans trains a separate model for each number of shots; SegGPT has just one.

A cleaner comparison is FSS-1000 (Table 2 of the paper), which SegGPT was never trained on. It scores 85.6 with one example and 89.3 with a few. Among specialized models trained on FSS-1000, the best, DACM, gets 90.8 and 91.7, and HSNet 86.5 and 88.5. Without ever seeing this dataset, SegGPT still comes close to the specialized models.

Video Object Segmentation

SegGPT's training data contains no video at all, but give it the masks of the first few frames as examples and it follows the same object through the video (Table 3 of the paper):

MethodVideo trainingYouTube-VOS 2018DAVIS 2017MOSE
XMemYes86.187.757.6
PainterNo24.134.614.5
SegGPTNo74.775.645.1

It still trails XMem, which was designed for video and trained on it, by more than ten points, but it's two to three times better than Painter, which hasn't seen video either.

How multiple examples are combined matters here (Table 4 of the paper). On the low-resolution FSS-1000 (224×224), spatial ensemble does well, but on the high-resolution DAVIS (640×480), shrinking 4 examples into one loses too much detail, and J&F drops from 70.0 with a single example to 61.9. Feature ensemble has no such problem, with 74.7 for 4 examples and 75.6 for 8. With even more example frames, 12 or 16, results dip slightly.

Worse on Large In-Domain Datasets

The authors also honestly list where SegGPT does poorly. On ADE20K semantic segmentation, after tuning a prompt image with in-context tuning, it gets 39.6 mIoU, 10.3 below the same team's Painter (49.9); on COCO panoptic segmentation its PQ is 34.4, 9.0 below Painter (43.4). The specialized Mask2Former gets 57.7 and 57.8 on the two, an even larger gap.

The authors' explanation is that random coloring keeps the model from using color as a shortcut, which makes training harder; with plenty of annotations and fixed tasks, as in ADE20K and COCO, Painter's fixed-color "cheating" actually works in its favor. In other words, SegGPT traded in-domain scores for the flexibility to handle new tasks. The authors also state plainly that beating every specialized model on particular benchmarks was never this paper's goal.

Conclusion

SegGPT turns segmentation into a problem of "coloring by example." During training each sample's colors are randomly reassigned, so the model can only work out from the example which things should be painted the same color. What it learns is therefore not a fixed set of classes but the ability to segment according to an example. The result is a single model that segments objects, parts, shadows and outlines, and can even do video object segmentation, which it never saw in training.

The price is that on large fixed-class datasets it falls clearly short of specialized models, and even of its predecessor Painter.

If you have a few annotated examples and want to quickly segment the same kind of thing across a batch of new images, say a new part on a production line or a particular structure in medical images, SegGPT's approach fits well, with no fine-tuning needed. To top the scores on a fixed-class benchmark like ADE20K, you're better off just training a specialized model.