File size: 3,047 Bytes
506b36e
 
28b4298
 
 
506b36e
28b4298
506b36e
28b4298
 
506b36e
 
28b4298
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Next Shift
emoji: 🔀
colorFrom: green
colorTo: gray
sdk: static
app_file: index.html
pinned: false
license: cc-by-4.0
short_description: Few-shot adaptation to a new visual environment, live
---

# Next Shift

Next Shift adapts a digit recogniser trained on clean scans to a **new visual environment** (grey, low-contrast, textured,
rotated, blurred captures) using only a handful of labeled target images. It first re-estimates BatchNorm statistics
on unlabeled target images (AdaBN), then fine-tunes the top MLP on the labels. The target environment is a **synthetic**
transform of real UCI digits.

**Model:** https://huggingface.co/thelamapi/Next-Shift — label a few target images; the top layers fine-tune in your browser and predictions update.

## Results: target test accuracy (297 images, 5 seeds, mean ± std)

| Method | k=0 | k=1 | k=5 | k=10 (labels per class) |
|---|---:|---:|---:|---:|
| Next Shift (AdaBN + fine-tune top) | 0.394 ± 0.000 | 0.489 ± 0.033 | 0.588 ± 0.021 | 0.661 ± 0.010 |
| Ablation: fine-tune top, no AdaBN | 0.401 ± 0.000 | 0.459 ± 0.042 | 0.589 ± 0.014 | 0.673 ± 0.009 |
| Frozen source features + linear head | 0.401 ± 0.000 | 0.358 ± 0.031 | 0.558 ± 0.019 | 0.613 ± 0.022 |
| Random init, same labels and steps | 0.099 ± 0.023 | 0.211 ± 0.022 | 0.467 ± 0.026 | 0.636 ± 0.050 |

Source-only accuracy on the source validation split: 0.973; on the target test split: 0.401.
For each k and seed, every method sees the same labeled images. Steps and learning rates were chosen per method on a
separate 100-image target development split (seeds 100–102), never on the test split.

![Accuracy vs labels](preview.png)

## Method
* Encoder: 2 conv blocks (16, 32 channels, BatchNorm, ReLU, max-pool) → 512 features → fc 64 → 10 classes; pre-trained on 900 source images.
* Next Shift: AdaBN on 350 unlabeled target pool images, then 50 full-batch GD steps (lr 0.05) on the top MLP.
* Frozen representation: linear head on fixed 64-d source features (200 steps, lr 0.1).
* Random init: the whole network from scratch with Adam (100 steps, lr 0.001).
* Ablation without AdaBN: same fine-tuning on source-normalised features.
* Browser: conv stacks run through `nlrt.js` and the fine-tuning is a line-by-line port; Node parity max abs difference 5.412337245047638e-16.

## Usage
```python
from inference import load, adapt, predict
spec = load()                               # NumPy only
top = adapt(spec, target_images, labels)    # images: (N,16,16) in [0,1]
probs = predict(spec, new_images, top)
```
`source_model.safetensors` holds the PyTorch source weights (architecture in the notebook).

## Limitations
* The domain shift is synthetic and the images are tiny; real shifts (sensors, lighting, populations) can be harder.
* AdaBN needs a batch of unlabeled target images; the released `conv_target` was estimated on the target pool.
* 96 of 297 target test images are still wrong for Next Shift at k=10 (seed 0); see `evaluation.json`.
* Trained on NVIDIA L4; profile full.