# MESAM: Enhancing SAM2 for Few-Shot Segmentation through Semantic-Visual Memory Generation and Refinement

Text-based SAM2 memory improves few-shot segmentation

Tianyu Zou, Shengwu Xiong, Rui Wu, Ruilin Yao, Yi Rong · arXiv · 2026-11-6

- **Problem** SAM2 memory built from support images fails when support and query objects look different. The memory carries only visible parts of the object, so the query mask misses the rest.
- **Method** MESAM builds SAM2 memory from a CLIP text-based semantic prior instead of support pixels. It then refines that memory with visual details from DINOv2 features and attention.
- **Results** On COCO-20i 1-shot, MESAM scores 67.3 mIoU, compared to 62.3 for FSSAM.

## Why support-built SAM2 memory breaks down

Few-shot segmentation (FSS) asks a model to segment novel categories from only a few annotated support samples. SAM2 has shown strong foreground-to-foreground matching across video frames, which makes it attractive for FSS. Consecutive video frames look alike, so that matching works well on video. Support and query objects in FSS often differ a lot in appearance, though.

When SAM2's memory comes from a support image that looks different from the query, the matching breaks down and accuracy drops.

Figure 1 shows the failure when the support view is only partial. A support image may show only the hull of a boat or the shoulder straps of a backpack. In that case FSSAM segments only the query parts that resemble the support, and it misses the rest of the object. Earlier work tries to shrink the visual gap through intermediate samples, query-oriented prompts, or feature adaptation.

The authors argue that this gap remains unsolved when SAM2 is applied to FSS.

![Figure 1: Illustration of the large support-query appearance divergence challenge. With only partial visual cues provided by the support image, existing method…](https://refivu.com/papers/52161752-7e0a-4bad-98ea-54f74bf885d7/figures/figure-e04913f086b8cced9a82b17745e9e658.png?thumb=512)

**Figure 1** : Illustration of the large support-query appearance divergence challenge. With only partial visual cues provided by the support image, existing methods segment only visu- ally similar parts in the query, instead of the entire object. In contrast, with the refined semantic memory, our MESAM method yields better segmentation results. The orange and blue circles indicate foreground (FG) misses and background (BG) misactivations, respectively.

[Original paper](https://arxiv.org/abs/2611.08943v1) · [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)

The authors trace this failure to one design choice: current methods build the SAM2 memory from visual cues alone. Since support and query differ visually, a support image can't supply the evidence needed for an accurate query mask. The memory therefore carries only the visible parts of the object that the support image shows.

## Why a text-based memory might suit SAM2

The key idea is to build the memory from a text description of the target class instead of from support pixels. A CLIP text classifier with a foreground and a background prompt gives a class score, and GradCAM turns that score into a spatial map. That map depends on the category rather than on the look of one support image, so the authors expect it to hold up under appearance change.

The first test compares the semantic memory with the FG and DISC visual memories that FSSAM builds. On PASCAL-5𝑖 the semantic memory scored 82.8 mIoU, compared to 81.1 for the FG memory and 78.1 for DISC. On COCO-20𝑖 it scored 58.9, compared to 55.9 for FG and 56.0 for DISC. The visual memories fail differently: their priors follow visual similarity to support prototypes, so they light up look-alike background such as a dog or a horse.

They also miss foreground with large appearance changes, such as the stop sign.

![Figure 2: The priors and the segmentation results of two visual memories (i.e., FG, DISC) and our semantic memory](https://refivu.com/papers/52161752-7e0a-4bad-98ea-54f74bf885d7/figures/figure-f267ee0c90cfbd07a727d14600fd1b5a.png?thumb=512)

**Figure 2** : The priors and the segmentation results of two visual memories (i.e., FG, DISC) and our semantic memory.

[Original paper](https://arxiv.org/abs/2611.08943v1) · [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) · Tianyu Zou, Shengwu Xiong, Rui Wu, Ruilin Yao, Yi Rong · MESAM: Enhancing SAM2 for Few-Shot Segmentation through Semantic-Visual Memory Generation and Refinement · Original paper figure · Extracted the figure area from page 3 of the original PDF.

To explain the gap, the authors threshold each prior at 0.5 and count false positives and false negatives. The visual priors have higher FP and lower FN rates than the semantic prior. The semantic prior runs the other way: on PASCAL-5𝑖 its FP is 0.0139 and its FN is 0.1207. The authors read this as the semantic prior covering only confident foreground pixels, which trades some missed foreground for fewer false alarms.

The CLIP-based explanation of why the semantic memory suits SAM2 is an interpretation, and the paper does not test it on its own. The authors also argue that the text-based cue is classification-oriented and shared across the whole category.

## Toy test of background noise versus lost foreground

The authors run the toy test on the ground-truth masks of query samples, not on real priors. Dilating these masks imitates visual priors, which cover more FG pixels and also over-segment some BG areas. Eroding them imitates the semantic prior, which covers only reliable pixels that are strongly identified as FG. Beyond the standard vanilla erosion, they add a skeleton-erosion that keeps the target object's skeleton unchanged.

The skeleton-erosion memory consistently outperforms the dilation memory across the tested ratios in Figure 3. So extra background noise seems to hurt SAM2 more than losing some foreground information. The vanilla-erosion memory, which drops the skeleton, falls below both the skeleton-erosion memory and the dilation memory. That drop shows that the skeleton, which holds key FG features, matters for precise segmentation.

![Figure 3: Performance comparison between different memo- ries obtained under varying dilation and erosion ratios](https://refivu.com/papers/52161752-7e0a-4bad-98ea-54f74bf885d7/figures/figure-24f5571717655a626db5d481db4bea77.png?thumb=512)

**Figure 3** : Performance comparison between different memo- ries obtained under varying dilation and erosion ratios.

[Original paper](https://arxiv.org/abs/2611.08943v1) · [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)

The authors read these results as support for the semantic memory's compatibility with SAM2. The CLIP classifier provides classification-oriented global information shared across the whole target category. Because this information is less likely to add BG noise, it can capture the representative object features that classification needs. They don't test the semantic memory with an imperfect prior that keeps the skeleton intact.

## Three priors and three SAM2 memories

SVMG begins with CLIP's image and text encoders. The query's CLIP feature map is average-pooled into one global embedding, and a softmax over temperature-scaled cosine similarities gives the FG and BG scores.

GradCAM then differentiates the FG score with respect to each CLIP feature map. Each weight \\(w\_m\\) is the spatial average of that gradient, and the prior is \\(P\_{sq}=\\mathrm{ReLU}(\\sum\_m w\_m F\_{clip\_q,m})\\). The ReLU drops negative responses, so the prior keeps only regions that push the image toward the FG class.

For the visual priors, DINOv2 features of the support and query images are computed. Masked average pooling over the support mask \\(M\_s\\) gives the support FG prototype \\(Pro^{fg}\_s\\). Cosine similarity with that prototype, min-max normalized, gives the query FG visual prior \\(P\_{vq}=\\mathrm{Norm}(\\mathrm{Cos}(F\_{dino\_q},Pro^{fg}\_s))\\).

The support adaptive prior starts from a query FG prototype, computed by masked average pooling with \\(P\_{vq}\\). The prose then compares that query prototype with the support features, but the printed Eq. (5) writes the cosine between the query features and the support prototype, so the exact comparison is unclear from the text. Either way, multiplying by \\(M\_s\\) removes responses that fall in the support background.

All three priors are interpolated to the input size and passed through SAM2's memory encoder. The semantic and query FG visual memories both use the query features \\(F\_{sam\_q}\\), while the support adaptive memory uses the support features \\(F\_{sam\_s}\\). The three memories share the same size, \\(h\_m w\_m \\times d\_m\\).

## Refining the semantic memory with cross-attention

ISMR projects the three memories into a common \\(d\_c\\)-dimensional space. Queries come from the semantic memory, keys and values come from the query FG visual memory, and a separate key comes from the support adaptive memory. The scaled dot product \\(A\_{qq}=Q K^\\top/\\sqrt{d\_c}\\) gives the attention between the two query memories.

The support FG prototype is formed by masked average pooling of \\(K\_s\\) with \\(P\_{vs}\\). Cosine similarity between that prototype and each position of \\(Q\\) gives \\(A\_{sq}\\) in \[-1, 1\], where a low value marks likely background. The authors use \\(Q\\) rather than \\(K\\), because they judge the semantic memory to be the more reliable source.

Both \\(A\_{qq}\\) and \\(A\_{sq}\\) are normalized to \[0, 1\], and the calibration term is \\(\\min\[\\mathrm{Norm}(A\_{qq})+(\\mathrm{Norm}(A\_{sq})-1),0\]\\). The min keeps only negative values, so the term lowers attention entries and never raises them. The calibrated scores are \\(A''\_{qq}=A\_{qq}+\\alpha A'\_{qq}\\) with \\(\\alpha=10\\), and the refined memory is \\(Mem'\_{sq}=Mem\_{sq}+\\mathrm{Linear}\_O(\\mathrm{Softmax}(A''\_{qq})V)\\), with Softmax taken along the column.

The refined memory goes with the query features through SAM2's memory attention and mask decoder. Training uses binary cross-entropy between the predicted and ground-truth masks. AdamW trains the ISMR module together with SAM2's memory encoder, memory attention and mask decoder, while the other components stay frozen. The initial learning rate is 0.001 with polynomial decay, and training uses 512 × 512 patches.

For k shots, each support sample gives its own FG prototype and visual prior, and the query priors are averaged. The similarity maps from the support samples are averaged to give the final \\(A\_{sq}\\).

## Standard-setting results against SAM2 baselines

The authors evaluate the standard setting by splitting PASCAL-5𝑖 and COCO-20𝑖 into four disjoint folds. They train on three folds and test on the remaining one, sampling 1,000 episodes. Performance is measured with mIoU, which averages IoU over foreground classes, and with FB-IoU, which balances foreground and background IoU.

The FB-IoU gap over FSSAM is 2.2% at 1-shot and 0.9% at 5-shot on PASCAL-5𝑖.

On COCO-20𝑖 with 1-shot, MESAM scores 67.3 mIoU, compared to 62.3 for FSSAM. At 5-shot, the mean is 70.2 for MESAM and 69.3 for FSSAM, a smaller margin. The FB-IoU gains on COCO-20𝑖 are 4.0% at 1-shot and 1.2% at 5-shot. The authors say these COCO-20𝑖 FB-IoU gains are more pronounced than the PASCAL-5𝑖 gains.

The authors credit foundation models with more transferable prior knowledge from large-scale pre-training. PAHNet, the best traditional ResNet50 model, still performs worse than most SAM, DINOv2 and SAM2 approaches. The authors link these gains to the semantic memory, which other SAM2 models lack.

## Transfer from COCO-20𝑖 to LVIS-92𝑖

The transfer test starts from a MESAM model trained on COCO-20𝑖 under the standard setting. That 1-shot model is evaluated directly on each LVIS-92𝑖 fold, with 2,300 episodes sampled. LVIS-92𝑖 is built on LVIS and contains 920 fine-grained object categories, which means higher visual complexity. Each fold covers 92 classes, and the reported mean is taken across all ten folds.

MESAM averages 41.9 mIoU across the ten folds, compared to 33.0 for Matcher and 31.2 for SINE. That margin is 8.9 points over Matcher and 10.7 points over SINE. FSSAM averages 37.3 on the same folds, so MESAM adds 4.6 points over it. MESAM scores higher than FSSAM on every one of the ten folds, with fold 920 at 38.0 against 34.7.

MESAM reaches an FB-IoU of 70.9, above 68.4 for FSSAM. Matcher and SINE reach 66.2 and 63.5 FB-IoU, so MESAM leads on that metric too. On LVIS-92𝑖, the support shows a camera lens, and FSSAM segments it incompletely while firing on a cat's eyes.

This test uses one training source and one shot count, so it shows transfer from COCO-20𝑖 to LVIS-92𝑖 only.

## Which memories matter

All ablations in this section run on COCO-20𝑖 under the 1-shot setup, so the memory designs share one footing. Setting A uses only the query semantic memory and scores 58.9 mIoU, the lowest of the four settings in Table 6. The semantic memory alone cannot represent every foreground feature in each query image, which explains the low score.

Setting B adds the query FG visual memory and reaches 65.8 mIoU. Setting B refines the semantic memory only through cross-attention with that visual memory.

Setting C calibrates this refinement with the support memory built from the support mask, which serves directly as the prior. Setting C scores 66.2 mIoU, while Setting D, the full MESAM model, scores 67.3 mIoU. Setting D surpasses Setting C by a margin of 1.1%, and the authors credit the support adaptive visual prior mask for this gain. The support adaptive visual prior mask identifies query-oriented foreground features inside the support image.

Both Setting C and Setting D beat Setting B, so suppressing background responses in the attention scores helps the refinement.

On fold 200, Setting A reaches 51.2 mIoU while Setting D reaches 63.9. On fold 201, Setting D reaches 70.4, while Setting A reaches 67.1. Setting D uses pure foreground information from the support memory to calibrate the attention scores. That calibration reduces the influence of misactivated background areas, which improves the segmentation.

Figure 6 compares the cross-attention weights for a selected FG pixel under Settings B and D, without and with the support calibration. They didn't run these memory ablations on the LVIS-92𝑖 folds.

## Which ISMR choices matter

The ISMR design ablation checks whether the chosen operations beat their alternatives on COCO-20𝑖 with 1-shot. Computing the BG-suppression similarity over K instead of Q lowers the overall mean from 67.3% to 66.4%. The authors argue that Q is more effective at finding background regions because it comes from the more reliable semantic memory.

Here, Eq. (9) is the cosine similarity that scans the spatial locations of Q to find likely background pixels.

That variant, which the authors call FG Enhancement, leaves the negative impact of BG noise unmitigated. The result is a 0.6% decrease in segmentation accuracy from 67.3%. This is a 0.5% degradation, which supports treating the semantic memory as the foundation and enhancing it with visual information.

Each variant changes one design choice while the rest of ISMR stays the same. The drops are consistent across these variants, so each removed choice contributes to the full model's score. The authors didn't test these variants on PASCAL-5𝑖 or LVIS-92𝑖.

## Where FSSAM misses and MESAM recovers

Figure 5 sets FSSAM against MESAM on PASCAL-5𝑖, COCO-20𝑖 and LVIS-92𝑖 under the 1-shot setting. On PASCAL-5𝑖, the support image shows a bottle from the front, while the query shows bottle openings from above. That viewpoint gap leads FSSAM to misidentify the target objects. On COCO-20𝑖, the support image shows only the handle of a pair of scissors.

FSSAM misses the scissor blades in the query image.

![Figure 5: Qualitative comparisons between FSSAM and MESAM on PASCAL-5 𝑖 , COCO-20𝑖 and LVIS-92𝑖 under the 1-shot setting](https://refivu.com/papers/52161752-7e0a-4bad-98ea-54f74bf885d7/figures/figure-acca94936ed4b718dc64f4912cfb8c44.png?thumb=512)

**Figure 5** : Qualitative comparisons between FSSAM and MESAM on PASCAL-5 𝑖 , COCO-20𝑖 and LVIS-92𝑖 under the 1-shot setting.

[Original paper](https://arxiv.org/abs/2611.08943v1) · [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)

On LVIS-92𝑖, the support camera lens looks very different from the camera lens in the query. FSSAM produces an incomplete segmentation there. FSSAM also activates on the eyes of a cat, which is a background error. They attribute this to MESAM handling substantial support-query appearance divergence. The paper reports no per-case scores for these qualitative examples.

The ablations use COCO-20𝑖 under the 1-shot setup, the same setting the authors use for this analysis. The semantic memory alone reaches a mean of 58.9, while the full MESAM model reaches 67.3.

## Limitations of the evidence

The ablation studies run only on COCO-20𝑖 under the 1-shot setup, so the effect of each memory is measured on one dataset. The cross-dataset test on LVIS-92𝑖 uses the 1-shot model trained on COCO-20𝑖, which leaves the other training combinations untested. The experiments cover only PASCAL-5𝑖, COCO-20𝑖 and LVIS-92𝑖, so other FSS benchmarks aren't part of the evidence.

The claim that the CLIP classifier gives semantic information shared across the target category rests on interpretation. The paper tests this reasoning only through the toy erosion experiment and the FP and FN rates in Table 2. The FP and FN comparison uses a threshold of 0.5 on the prior masks, so a different threshold could shift those rates.

The k-shot extension is described in the method, but the ablation tables cover 1-shot only. Numbers for the k-shot setting aren't reported in the ablations, so its gains remain unmeasured.

## Paper info

- **Authors**: Tianyu Zou, Shengwu Xiong, Rui Wu, Ruilin Yao, Yi Rong
- **Venue**: arXiv · Preprint
- **First published**: 2026-11-6
- **Added**: 2026-11-9
- **Updated**: 2026-11-6
- **Version**: v1
- **arXiv**: 2611.08943
- **Category**: cs.CV
- **Original paper terms**: [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)
- **What we show**: Paper info, commentary and permitted figures from the original paper

[Original paper ↗](https://arxiv.org/abs/2611.08943v1) · [PDF ↗](https://arxiv.org/pdf/2611.08943v1)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Abstract

This paper investigates how to leverage the powerful capabilities of the Segment Anything Model 2 (SAM2) to tackle few-shot segmentation (FSS) tasks. One major challenge lies in the substantial appearance differences between the support and query images, which may diminish the effectiveness of the memory-matching process in SAM2. Through systematic analysis, we find that the memory generated based on the categorical semantic information is robust to this appearance divergence and is more compatible with SAM2. Based on these observations, we propose a new Memory-Enhanced SAM2 (MESAM) method, which first introduces a Semantic-Visual Memory Generation (SVMG) module to separately construct a query semantic memory, a query foreground visual memory, and a support adaptive visual memory. Then, in our designed Interactive Semantic Memory Refinement (ISMR) module, we further enrich the obtained semantic memory with fine-grained visual details, and suppress its misatcivated background responses. Extensive experiments on the PASCAL-$5^i$, COCO-$20^i$, and LVIS-$92^i$ datasets demonstrate that MESAM consistently outperforms existing FSS methods, achieving new state-of-the-art segmentation performance. Our source code is available at: \\href{https://github.com/tianyu-zou/MESAM}{https://github.com/tianyu-zou/MESAM}.

## Quote from the paper

Tianyu Zou, Shengwu Xiong, Rui Wu, Ruilin Yao, Yi Rong (2026). MESAM: Enhancing SAM2 for Few-Shot Segmentation through Semantic-Visual Memory Generation and Refinement. arXiv:2611.08943v1. https://arxiv.org/abs/2611.08943v1

**BibTeX**

```
@misc{zou2026mesam,
  title = {{MESAM: Enhancing SAM2 for Few-Shot Segmentation through Semantic-Visual Memory Generation and Refinement}},
  author = {Tianyu Zou and Shengwu Xiong and Rui Wu and Ruilin Yao and Yi Rong},
  year = {2026},
  eprint = {2611.08943},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.08943v1}
}
```

## Other paper commentary

- [From Geometry to Function: A Multi-Task LiDAR Benchmark Dataset for Semantic and Functional Parsing of Urban Facades](https://refivu.com/en/papers/f3fa21ca-cb31-43d7-a2a8-a3f7ae14b67a-from-geometry-to-function-a-multi-task-lidar.md)
- [SSD: Single Shot MultiBox Detector](https://refivu.com/en/papers/1889995f-3f80-4a7d-b734-c549e845a707-ssd-single-shot-multibox-detector.md)
- [BinsFormer: Revisiting Adaptive Bins for Monocular Depth Estimation](https://refivu.com/en/papers/7fc81cb2-3782-4326-9ffa-5af4ea6fe25b-binsformer-revisiting-adaptive-bins-for-monocular-depth.md)
- [Unifying Flow, Stereo and Depth Estimation](https://refivu.com/en/papers/a17a6b8c-5864-4598-9183-8a1c09180428-unifying-flow-stereo-and-depth-estimation.md)
- [Deep Gradient Learning for Efficient Camouflaged Object Detection](https://refivu.com/en/papers/ab2734b0-aadf-4a7a-9878-ec87828236f4-deep-gradient-learning-for-efficient-camouflaged-object.md)
- [Video Salient Object Detection via Fully Convolutional Networks](https://refivu.com/en/papers/bc9ebd0c-9bab-4b48-a6aa-5dc69dbeadea-video-salient-object-detection-via-fully-convolutional.md)
- [Aspect-wise Pairwise Tournaments for Test-Time Selection](https://refivu.com/en/papers/1c1b6d7a-3b53-497b-a00d-ef382ac91c72-aspect-wise-pairwise-tournaments-for-test-time-selection.md)
- [Q-OIL: Optimistic Exploration in Learning from Human Interventions](https://refivu.com/en/papers/693c23ec-3e49-41aa-ade3-dcb14c772476-q-oil-optimistic-exploration-in-learning-from-human.md)
- [Training Stability under Adaptive Curricula](https://refivu.com/en/papers/8dfc5dca-17c3-43e0-be3d-554290933796-training-stability-under-adaptive-curricula.md)
- [ChainFlow: Few-Step Latent Flow Matching for Speculative Decoding](https://refivu.com/en/papers/93c02290-232b-4fea-be96-dcb416a7416b-chainflow-few-step-latent-flow-matching-for-speculative.md)
- [On Differentially Private Mechanisms for Linear Regression](https://refivu.com/en/papers/a8e2597e-f4e0-4024-8f09-3e7b34a34de1-on-differentially-private-mechanisms-for-linear-regression.md)
- [AtariBench: Evaluating Spatiotemporal Reasoning of Vision-Language Models in Atari Gameplay](https://refivu.com/en/papers/f06f628b-d075-4ff5-aef2-b7e03d1a833f-ataribench-evaluating-spatiotemporal-reasoning-of-vision.md)

## Terms in this paper

- [mIoU](https://refivu.com/en/terms/901dcc5384c2-miou)
- [CLIP](https://refivu.com/en/terms/b5b5b48d5f4e-clip)
- [DINOv2](https://refivu.com/en/terms/15de7b44cbeb-dinov2)
- [few-shot segmentation](https://refivu.com/en/terms/194cff59d20d-few-shot-segmentation)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/52161752-7e0a-4bad-98ea-54f74bf885d7-mesam-enhancing-sam2-for-few-shot-segmentation-through)
