File size: 5,544 Bytes
47f62d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52632d7
47f62d7
52632d7
47f62d7
 
 
52632d7
47f62d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52632d7
 
47f62d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52632d7
 
47f62d7
52632d7
47f62d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52632d7
 
47f62d7
 
 
 
 
 
 
 
 
 
 
52632d7
47f62d7
 
 
52632d7
 
47f62d7
 
 
 
52632d7
 
 
47f62d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52632d7
47f62d7
52632d7
47f62d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
---
license: mit
library_name: pytorch
pipeline_tag: image-segmentation
tags:
- semantic-segmentation
- retrieval-augmentation
- pascal-voc
- cross-attention
- unet
- mobilenet-v3
- pytorch-lightning
datasets:
- pascal_voc
- sbd
metrics:
- mean_iou
- accuracy
model-index:
- name: retrieval-augmented-unet-pascal-voc
  results:
  - task:
      type: image-segmentation
      name: Semantic Segmentation
    dataset:
      name: Pascal VOC 2012 + SBD
      type: pascal_voc
    metrics:
    - type: mean_iou
      value: 0.5648
      name: mIoU (fp32 re-eval, 21 classes)
    - type: accuracy
      value: 0.8894
      name: Pixel accuracy
---

# Retrieval-augmented semantic segmentation with a mask-similarity-trained retriever

Weights bundle for the preprint
**"Retrieval-augmented semantic segmentation with a mask-similarity-trained retriever"**
by Jianan Ren, D. A. Payunen, Ya. Matyushenko, T. M. Tatarnikova.

Code, training scripts, and paper source:
[github.com/Yeetmq/retrieval-augmented-segmentation](https://github.com/Yeetmq/retrieval-augmented-segmentation).

## Headline result

On the Pascal VOC 2012 official val split (1449 images, 21 classes),
fp32 re-evaluation on the same `best.ckpt`:

| Model            |    mIoU     |  pixel-acc  |
|------------------|:-----------:|:-----------:|
| UNet baseline    |   0.4655    |   0.8672    |
| UNet + retrieval | **0.5648**  | **0.8894**  |
| Δ                | **+0.0993** | **+0.0222** |

Improvement observed on **all 21 classes simultaneously**. Largest
per-class gains: `bird` +0.25, `bottle` +0.19, `dog` +0.16, `cow`
+0.16, `cat` +0.15.

## What's in this repo

```
baseline/
  best.ckpt              # 390 MB — UNet baseline, best epoch 48
  hparams.yaml
  training_metrics.csv   # per-epoch CSVLogger log (50 epochs)
retrieval/
  best.ckpt              # 510 MB — UNet + cross-attention fusion, epoch 49
  hparams.yaml
  training_metrics.csv
retriever/
  best.ckpt              #  39 MB — MobileNet-v3-Large + SupCon head
  memory_bank.pt         # 1.3 GB — 1587 L2-normalized support embeddings + masks
  hparams.yaml
  training_metrics.csv
eval/
  comparison.csv         # fp32 re-eval baseline vs retrieval on VOC val
  per_class_iou.csv      # per-class IoU for both models
```

Total: ~2.1 GB.

## Method (one paragraph)

Segmentation models systematically underperform on rare or atypical
cases. This project augments a plain UNet with an inference-time
**retrieval path**: a MobileNet-v3 retriever encodes the query image
into a 256-D L2-normalized embedding, the top-k nearest labeled
examples are fetched from a **memory bank** built from a held-out
support split, their masks are re-encoded by a small **MaskEncoder**,
and the resulting multi-scale features are fused into the UNet decoder
via **cross-attention** at scales /16, /8, /4. The main technical
contribution is a **mask-similarity retriever loss**
`L_ret = 0.8·L_Boundary-IoU + 0.2·L_CS-Jaccard` — a soft-target SupCon
in which pair relevance is defined by mask geometry rather than class
labels, aligning retrieval with what cross-attention actually consumes.

## Usage

```bash
# 1. Get the code
git clone https://github.com/Yeetmq/retrieval-augmented-segmentation.git
cd retrieval-augmented-segmentation
pip install -e .

# 2. Download this bundle
pip install huggingface_hub
huggingface-cli download yeetmq/retrieval-augmented-segmentation \
    --local-dir weights_release

# 3. Rebuild the runs/ layout the scripts expect
mkdir -p runs/baseline runs/retrieval runs/retriever
cp weights_release/baseline/best.ckpt      runs/baseline/best.ckpt
cp weights_release/retrieval/best.ckpt     runs/retrieval/best.ckpt
cp weights_release/retriever/best.ckpt     runs/retriever/best.ckpt
cp weights_release/retriever/memory_bank.pt runs/retriever/memory_bank.pt

# 4. Re-evaluate on Pascal VOC val (needs the dataset — see prepare_data.py)
python Experiments/pascal_voc/prepare_data.py
python Experiments/pascal_voc/build_h5.py
python Experiments/pascal_voc/build_splits.py
python Experiments/pascal_voc/compare_results.py
```

## Precision note

`training_metrics.csv` values were logged live under
`precision="16-mixed"`. The headline numbers above and in
`eval/comparison.csv` are an **fp32 re-evaluation** of the same
`best.ckpt` on the same val split. The gap is small (e.g. retrieval
mIoU 0.5648 fp32 vs 0.5634 fp16-mixed live) but non-zero due to the
precision regime.

## Training setup (short)

- **Data:** Pascal VOC 2012 + SBD (trainaug 10 582 images).
  Stratified 15 % split → support (1587) for the memory bank; the
  remaining 8995 → query_train. Official VOC val (1449) held out.
- **Baseline UNet:** 50 epochs, batch 16, AdamW (lr 3e-4, wd 1e-4),
  CosineAnnealingLR, Dice + Focal (0.5/0.5). No ImageNet pretrain.
- **Retriever:** 80 epochs on the support split, batch 32,
  MobileNet-v3-Large (ImageNet-pretrained), embedding 256-D,
  mask-similarity SupCon (τ=0.1, α=0.2, boundary erosion d=2).
- **Retrieval segmenter:** same optimization as baseline, batch 12
  (fusion memory), fusion at scales {20, 40, 80}, 4 attention heads.
  Retriever frozen; UNet + MaskEncoder + 3 fusion modules trained.
- **Hardware:** one NVIDIA RTX 5090 (32 GB). End-to-end ~24 h.

## Citation

```bibtex
@misc{ren2026retrievalseg,
  title  = {Retrieval-augmented semantic segmentation with a mask-similarity-trained retriever},
  author = {Ren, Jianan and Payunen, D. A. and Matyushenko, Ya. and Tatarnikova, T. M.},
  year   = {2026},
  note   = {Preprint},
}
```

## License

MIT.