jepa-playground 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jepa_playground-0.0.1/.gitignore +38 -0
- jepa_playground-0.0.1/PKG-INFO +251 -0
- jepa_playground-0.0.1/README.md +240 -0
- jepa_playground-0.0.1/jepa_playground/__init__.py +1 -0
- jepa_playground-0.0.1/jepa_playground/bench.py +398 -0
- jepa_playground-0.0.1/jepa_playground/components.py +56 -0
- jepa_playground-0.0.1/jepa_playground/config.py +128 -0
- jepa_playground-0.0.1/jepa_playground/data/__init__.py +16 -0
- jepa_playground-0.0.1/jepa_playground/data/images.py +161 -0
- jepa_playground-0.0.1/jepa_playground/data/video.py +296 -0
- jepa_playground-0.0.1/jepa_playground/ema.py +41 -0
- jepa_playground-0.0.1/jepa_playground/encoder.py +405 -0
- jepa_playground-0.0.1/jepa_playground/eval/__init__.py +1 -0
- jepa_playground-0.0.1/jepa_playground/eval/__main__.py +97 -0
- jepa_playground-0.0.1/jepa_playground/eval/diagnostics.py +35 -0
- jepa_playground-0.0.1/jepa_playground/eval/features.py +160 -0
- jepa_playground-0.0.1/jepa_playground/eval/online.py +78 -0
- jepa_playground-0.0.1/jepa_playground/eval/probes.py +111 -0
- jepa_playground-0.0.1/jepa_playground/losses.py +22 -0
- jepa_playground-0.0.1/jepa_playground/masking/__init__.py +1 -0
- jepa_playground-0.0.1/jepa_playground/masking/block.py +127 -0
- jepa_playground-0.0.1/jepa_playground/masking/tube.py +117 -0
- jepa_playground-0.0.1/jepa_playground/predictor.py +91 -0
- jepa_playground-0.0.1/jepa_playground/regularizers.py +61 -0
- jepa_playground-0.0.1/jepa_playground/results.py +115 -0
- jepa_playground-0.0.1/jepa_playground/run.py +190 -0
- jepa_playground-0.0.1/jepa_playground/runtime.py +192 -0
- jepa_playground-0.0.1/jepa_playground/targets.py +22 -0
- jepa_playground-0.0.1/jepa_playground/utils/__init__.py +1 -0
- jepa_playground-0.0.1/jepa_playground/utils/training_utils.py +117 -0
- jepa_playground-0.0.1/jepa_playground/variants/__init__.py +35 -0
- jepa_playground-0.0.1/jepa_playground/variants/ijepa/__init__.py +6 -0
- jepa_playground-0.0.1/jepa_playground/variants/ijepa/configs/cifar10_IJEPA.yaml +66 -0
- jepa_playground-0.0.1/jepa_playground/variants/ijepa/model.py +118 -0
- jepa_playground-0.0.1/jepa_playground/variants/ijepa/train.py +300 -0
- jepa_playground-0.0.1/jepa_playground/variants/lejepa/__init__.py +4 -0
- jepa_playground-0.0.1/jepa_playground/variants/lejepa/configs/cifar10_LeJEPA.yaml +64 -0
- jepa_playground-0.0.1/jepa_playground/variants/lejepa/configs/imagenette_LeJEPA.yaml +64 -0
- jepa_playground-0.0.1/jepa_playground/variants/lejepa/model.py +88 -0
- jepa_playground-0.0.1/jepa_playground/variants/lejepa/train.py +302 -0
- jepa_playground-0.0.1/jepa_playground/variants/vjepa/__init__.py +6 -0
- jepa_playground-0.0.1/jepa_playground/variants/vjepa/configs/kinetics400_VJEPA.yaml +71 -0
- jepa_playground-0.0.1/jepa_playground/variants/vjepa/model.py +114 -0
- jepa_playground-0.0.1/jepa_playground/variants/vjepa/train.py +311 -0
- jepa_playground-0.0.1/jepa_playground/variants/vjepa2/__init__.py +6 -0
- jepa_playground-0.0.1/jepa_playground/variants/vjepa2/ac_predictor.py +74 -0
- jepa_playground-0.0.1/jepa_playground/variants/vjepa2/configs/kinetics400_VJEPA2.yaml +82 -0
- jepa_playground-0.0.1/jepa_playground/variants/vjepa2/configs/toy_VJEPA2_AC.yaml +60 -0
- jepa_playground-0.0.1/jepa_playground/variants/vjepa2/plan.py +182 -0
- jepa_playground-0.0.1/jepa_playground/variants/vjepa2/toy_env.py +71 -0
- jepa_playground-0.0.1/jepa_playground/variants/vjepa2/train.py +320 -0
- jepa_playground-0.0.1/jepa_playground/variants/vjepa2/train_ac.py +289 -0
- jepa_playground-0.0.1/pyproject.toml +43 -0
- jepa_playground-0.0.1/tests/__init__.py +0 -0
- jepa_playground-0.0.1/tests/conftest.py +38 -0
- jepa_playground-0.0.1/tests/test_bench.py +481 -0
- jepa_playground-0.0.1/tests/test_components.py +158 -0
- jepa_playground-0.0.1/tests/test_config.py +252 -0
- jepa_playground-0.0.1/tests/test_device.py +38 -0
- jepa_playground-0.0.1/tests/test_diagnostics.py +47 -0
- jepa_playground-0.0.1/tests/test_ema.py +75 -0
- jepa_playground-0.0.1/tests/test_encoder.py +298 -0
- jepa_playground-0.0.1/tests/test_fake_kinetics.py +127 -0
- jepa_playground-0.0.1/tests/test_features.py +163 -0
- jepa_playground-0.0.1/tests/test_images.py +305 -0
- jepa_playground-0.0.1/tests/test_lejepa.py +105 -0
- jepa_playground-0.0.1/tests/test_losses.py +263 -0
- jepa_playground-0.0.1/tests/test_masking.py +185 -0
- jepa_playground-0.0.1/tests/test_online_probe.py +31 -0
- jepa_playground-0.0.1/tests/test_position_embeddings.py +94 -0
- jepa_playground-0.0.1/tests/test_probes.py +144 -0
- jepa_playground-0.0.1/tests/test_regularizers.py +155 -0
- jepa_playground-0.0.1/tests/test_results.py +69 -0
- jepa_playground-0.0.1/tests/test_resume.py +416 -0
- jepa_playground-0.0.1/tests/test_rope.py +110 -0
- jepa_playground-0.0.1/tests/test_run.py +125 -0
- jepa_playground-0.0.1/tests/test_runtime.py +213 -0
- jepa_playground-0.0.1/tests/test_schedules.py +52 -0
- jepa_playground-0.0.1/tests/test_targets.py +110 -0
- jepa_playground-0.0.1/tests/test_toy_method.py +70 -0
- jepa_playground-0.0.1/tests/test_train_smoke.py +516 -0
- jepa_playground-0.0.1/tests/test_variants.py +40 -0
- jepa_playground-0.0.1/tests/test_video.py +187 -0
- jepa_playground-0.0.1/tests/toy_method/__init__.py +0 -0
- jepa_playground-0.0.1/tests/toy_method/config.yaml +29 -0
- jepa_playground-0.0.1/tests/toy_method/train.py +97 -0
- jepa_playground-0.0.1/uv.lock +1132 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.pyo
|
|
5
|
+
*.pyd
|
|
6
|
+
.Python
|
|
7
|
+
*.egg-info/
|
|
8
|
+
dist/
|
|
9
|
+
build/
|
|
10
|
+
.eggs/
|
|
11
|
+
|
|
12
|
+
# Virtual environments
|
|
13
|
+
.venv/
|
|
14
|
+
venv/
|
|
15
|
+
env/
|
|
16
|
+
|
|
17
|
+
# Tools
|
|
18
|
+
.ruff_cache/
|
|
19
|
+
.mypy_cache/
|
|
20
|
+
.pytest_cache/
|
|
21
|
+
|
|
22
|
+
# Jupyter
|
|
23
|
+
.ipynb_checkpoints/
|
|
24
|
+
*.ipynb
|
|
25
|
+
|
|
26
|
+
# Data / runs / local results
|
|
27
|
+
/data/
|
|
28
|
+
runs/
|
|
29
|
+
benchmarks/
|
|
30
|
+
*.pt
|
|
31
|
+
*.pth
|
|
32
|
+
*.ckpt
|
|
33
|
+
|
|
34
|
+
# Reference PDFs / papers
|
|
35
|
+
reference/*.pdf
|
|
36
|
+
|
|
37
|
+
# OS
|
|
38
|
+
.DS_Store
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: jepa-playground
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Requires-Python: >=3.11
|
|
5
|
+
Requires-Dist: av!=19.0.1,>=18.1.1
|
|
6
|
+
Requires-Dist: numpy>=1.26
|
|
7
|
+
Requires-Dist: pyyaml>=6.0.3
|
|
8
|
+
Requires-Dist: torch>=2.12
|
|
9
|
+
Requires-Dist: torchvision>=0.27
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
|
12
|
+
# jepa-playground
|
|
13
|
+
This repo serves as a playground for me to explore and implement a variety of jepa variants in an attempt to understand.
|
|
14
|
+
|
|
15
|
+
All code handwritten by refering to the papers below and their associated code bases.
|
|
16
|
+
|
|
17
|
+
## JEPA Variants
|
|
18
|
+
|
|
19
|
+
### 1. I-JEPA — Image JEPA
|
|
20
|
+
**Paper:** [Self-Supervised Learning from Images with a Joint-Embedding Predictive Architecture](https://arxiv.org/abs/2301.08243) (Assran et al., 2023)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
### 2. V-JEPA — Video JEPA
|
|
24
|
+
**Paper:** [V-JEPA: Latent Video Prediction for Visual Representation Learning](https://arxiv.org/abs/2404.08471) (Bardes et al., 2024)
|
|
25
|
+
|
|
26
|
+
### 3. V-JEPA 2
|
|
27
|
+
**Paper:** [V-JEPA 2: Self-Supervised Video Models Enable Understanding, Prediction and Planning](https://arxiv.org/abs/2506.09985) (Meta, 2025)
|
|
28
|
+
|
|
29
|
+
## Repo Layout
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
jepa-playground/
|
|
33
|
+
├── jepa_playground/
|
|
34
|
+
│ ├── encoder.py
|
|
35
|
+
│ ├── predictor.py
|
|
36
|
+
│ ├── ema.py
|
|
37
|
+
│ ├── masking/
|
|
38
|
+
│ │ ├── block.py
|
|
39
|
+
│ │ └── tube.py
|
|
40
|
+
│ └── variants/
|
|
41
|
+
│ ├── ijepa/
|
|
42
|
+
│ │ ├── model.py
|
|
43
|
+
│ │ └── train.py
|
|
44
|
+
│ ├── lejepa/
|
|
45
|
+
│ │ ├── model.py
|
|
46
|
+
│ │ └── train.py
|
|
47
|
+
│ ├── vjepa/
|
|
48
|
+
│ │ ├── model.py
|
|
49
|
+
│ │ └── train.py
|
|
50
|
+
│ └── vjepa2/
|
|
51
|
+
│ └── train.py
|
|
52
|
+
├── tests/
|
|
53
|
+
├── experiments/
|
|
54
|
+
├── reference/
|
|
55
|
+
└── pyproject.toml
|
|
56
|
+
```
|
|
57
|
+
## Shared Architecture (`jepa_playground/`)
|
|
58
|
+
|
|
59
|
+
All variants share a common skeleton.
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
jepa_playground/
|
|
63
|
+
├── encoder.py
|
|
64
|
+
├── predictor.py
|
|
65
|
+
├── ema.py
|
|
66
|
+
└── masking/
|
|
67
|
+
├── block.py
|
|
68
|
+
└── tube.py
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Setup
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
uv sync
|
|
75
|
+
uv run *
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Overrides
|
|
79
|
+
|
|
80
|
+
Every train script and `plan.py` takes a YAML config followed by `section.key=value` overrides. A key
|
|
81
|
+
must already exist in the config and the value must keep its type, so a typo stops the run with an error.
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
uv run python -m jepa_playground.variants.ijepa.train jepa_playground/variants/ijepa/configs/cifar10_IJEPA.yaml optimization.lr=3e-4 data.fake_data=true
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Runs
|
|
88
|
+
|
|
89
|
+
Each training run writes to `runs/<name>/`: `config.yaml` (the config as run), `meta.json` (git commit,
|
|
90
|
+
command line, versions, device), `metrics.jsonl` (one line per log step) and `ckpt.pt`. The name comes
|
|
91
|
+
from `run.name` and can be used once; pick another with `run.name=...`. `run.seed` seeds the run.
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
uv run python -m jepa_playground.variants.ijepa.train jepa_playground/variants/ijepa/configs/cifar10_IJEPA.yaml run.name=ijepa_lr3e-4 optimization.lr=3e-4
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
I-JEPA trains on CIFAR-10 by default; `data.dataset=cifar100` trains it on CIFAR-100, downloaded to `data/` on first use.
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
uv run python -m jepa_playground.variants.ijepa.train jepa_playground/variants/ijepa/configs/cifar10_IJEPA.yaml data.dataset=cifar100 run.name=ijepa_cifar100
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Image data
|
|
104
|
+
|
|
105
|
+
I-JEPA and LeJEPA read `data.dataset`: `cifar10`, `cifar100`, `imagenette` (all downloaded to `data.data_dir` on first
|
|
106
|
+
use) or `folder`, your own images. A folder holds one sub-folder per class under `train/` and under `test/` or `val/`.
|
|
107
|
+
Images of any size are resized and centre-cropped to `model.image_size`.
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
uv run python -m jepa_playground.variants.lejepa.train jepa_playground/variants/lejepa/configs/imagenette_LeJEPA.yaml data.dataset=folder data.data_dir=path/to/images run.name=lejepa_mine
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
`data.augmentations` lists the random transforms of each training image, or for the video variants each clip, in
|
|
114
|
+
order, with their settings. Each name is a
|
|
115
|
+
key of `AUGMENTATIONS` in `jepa_playground/data/images.py`. An override changes one setting:
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
uv run python -m jepa_playground.variants.lejepa.train jepa_playground/variants/lejepa/configs/cifar10_LeJEPA.yaml data.augmentations.gaussian_blur.p=0
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
`imagenette_LeJEPA.yaml` is the setup of the LeJEPA authors' minimal example: a ViT-S/8 on 128-pixel Imagenette.
|
|
122
|
+
|
|
123
|
+
V-JEPA 2 also saves `ckpt-pretrain.pt`. To run only the cooldown, set `pretrain_checkpoint=runs/<name>/ckpt-pretrain.pt`.
|
|
124
|
+
|
|
125
|
+
V-JEPA and V-JEPA 2 hold `data.test_fraction` (10%) of each class's Kinetics videos out of training to test on, the
|
|
126
|
+
same videos in every run. `data.test_fraction=0` trains on all of them and leaves the run nothing to evaluate on.
|
|
127
|
+
|
|
128
|
+
To continue an interrupted run from its last checkpoint, rerun the same command with `run.resume=true`. It ends with
|
|
129
|
+
the same weights and metrics as a run that was never interrupted. Only `run.dir`, `data.data_dir` and
|
|
130
|
+
`logging.log_every` may differ from the original command; any other change is refused.
|
|
131
|
+
|
|
132
|
+
### Benchmark
|
|
133
|
+
|
|
134
|
+
`jepa_playground.bench` times a config's training step and its data pipeline separately.
|
|
135
|
+
It takes the same overrides; V-JEPA 2 configs also need `--phase pretrain` or `--phase cooldown`.
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
uv run python -m jepa_playground.bench jepa_playground/variants/ijepa/configs/cifar10_IJEPA.yaml --out result.json
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Features
|
|
142
|
+
|
|
143
|
+
`jepa_playground.eval.features` runs a trained model's target encoder over its run's images or videos, without
|
|
144
|
+
augmentation and on the centre clip of each video, and caches the mean-pooled features and labels as
|
|
145
|
+
`runs/<name>/features-<split>.pt`. It prints their spread and effective rank. V-JEPA 2 is evaluated at the clip length
|
|
146
|
+
of its cooldown, the phase that saves `ckpt.pt`.
|
|
147
|
+
Training logs the same two signs of collapse at each log step as `emb_std`, `emb_std_min` and `emb_rank`.
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
uv run python -m jepa_playground.eval.features runs/ijepa_cifar10 --split test
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
A checkpoint outside a run directory works too, given the config it was trained with. Nothing is cached for it.
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
uv run python -m jepa_playground.eval.features --checkpoint old/ijepa.pt --config old/ijepa.yaml
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Probes
|
|
160
|
+
|
|
161
|
+
`jepa_playground.eval` scores a run's cached train and test features with a kNN probe and
|
|
162
|
+
a linear probe.
|
|
163
|
+
It prints each test accuracy and writes `runs/<name>/result-knn.json` and `result-linear.json`.
|
|
164
|
+
`probe=knn` can be used to select only one.
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
uv run python -m jepa_playground.eval runs/ijepa_cifar10 probe=knn,linear
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
A video run works the same way. Its kNN and linear numbers come from mean-pooled features of one clip per video, so
|
|
171
|
+
they are not the attentive-probe numbers the V-JEPA papers report.
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
uv run python -m jepa_playground.eval runs/vjepa_kinetics400
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
To watch the representation while I-JEPA, V-JEPA or V-JEPA 2 trains, `eval.online_probe=true` trains a linear head
|
|
178
|
+
on the target encoder's detached embeddings beside the main loss and logs `probe_loss` and `probe_acc` at each log
|
|
179
|
+
step. `eval.knn_every=N` logs the kNN test accuracy as `knn_acc` every N epochs (of each phase, for V-JEPA 2).
|
|
180
|
+
Neither changes the training: with the same seed, the weights and loss curve match a run without them.
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
uv run python -m jepa_playground.variants.ijepa.train jepa_playground/variants/ijepa/configs/cifar10_IJEPA.yaml eval.online_probe=true eval.knn_every=10
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Results
|
|
187
|
+
|
|
188
|
+
`jepa_playground.results` prints a table of the result files in the run directories you give it. Runs that differ
|
|
189
|
+
only in their `run` section, such as the seed, become one row with the mean, standard deviation and 95% confidence
|
|
190
|
+
half-width of each metric.
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
uv run python -m jepa_playground.results runs/*
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Add a method
|
|
197
|
+
|
|
198
|
+
A method is one folder with a train module and a config. `tests/toy_method/` is a complete one in about 90 lines, and
|
|
199
|
+
`tests/test_toy_method.py` runs it through every tool below.
|
|
200
|
+
|
|
201
|
+
1. Write a train module with three functions:
|
|
202
|
+
- `build(config, device, phase=None)` returns the model, its optimiser and the training loader.
|
|
203
|
+
- `training_step(model, batch, step)` returns a dict of scalars. `"loss"` is the one to backpropagate.
|
|
204
|
+
- `load_split(config, split, phase=None)` returns the `train` or `test` data with labels and no augmentation.
|
|
205
|
+
Leave it out if the data has no labels; the method then trains and benchmarks but cannot be probed.
|
|
206
|
+
2. Give the model an `encode(inputs)` method that returns tokens of shape `[B, N, D]` for unmasked inputs.
|
|
207
|
+
3. Write the training loop in the same module with the helpers in `jepa_playground/run.py`: `start_run`,
|
|
208
|
+
`load_checkpoint`, `seed_epoch`, `log` and `save_checkpoint`. A checkpoint holds `model`, `epoch` and `step`.
|
|
209
|
+
4. Write a config with a top-level `variant` key and the `runtime`, `run`, `data` and `optimization` sections the toy
|
|
210
|
+
config has.
|
|
211
|
+
5. Add one line to `VARIANTS` in `jepa_playground/variants/__init__.py` that maps the variant name to the train module.
|
|
212
|
+
|
|
213
|
+
The method then works with the shared tools, with no other file edited:
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
uv run python -m tests.toy_method.train tests/toy_method/config.yaml
|
|
217
|
+
uv run python -m jepa_playground.bench tests/toy_method/config.yaml
|
|
218
|
+
uv run python -m jepa_playground.eval runs/toy
|
|
219
|
+
uv run python -m jepa_playground.results runs/toy
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
These four commands need the `VARIANTS` line `"toy": "tests.toy_method.train"`, which the test adds for itself.
|
|
223
|
+
|
|
224
|
+
## Add a component
|
|
225
|
+
|
|
226
|
+
A component is a part a method picks by name in its config. The registries are in `jepa_playground/components.py`
|
|
227
|
+
and `jepa_playground/targets.py`.
|
|
228
|
+
|
|
229
|
+
| Config key | Registry | What to write |
|
|
230
|
+
|---|---|---|
|
|
231
|
+
| `model.encoder` | `ENCODERS` | a module whose `forward(x, keep_idx=None)` returns tokens `[B, N, D]`, with a `grid_size` |
|
|
232
|
+
| `model.predictor` | `PREDICTORS` | a module whose `forward(context, context_idx, target_idx)` returns `[B, M, D]` |
|
|
233
|
+
| `loss.prediction` | `PREDICTION_LOSSES` | a function of `(prediction, target)` that returns a scalar |
|
|
234
|
+
| `loss.target` | `TARGETS` | a `Target` that says whether the target encoder is a copy and whether it gets a gradient |
|
|
235
|
+
| `loss.regularizer` | `REGULARIZERS` | a function of `(embeddings [N, D], step)` that returns a scalar |
|
|
236
|
+
|
|
237
|
+
1. Write the part beside the ones that exist, for example a new function in `jepa_playground/regularizers.py`.
|
|
238
|
+
2. Add one line to its registry that maps a name to it.
|
|
239
|
+
3. Pick it with an override:
|
|
240
|
+
|
|
241
|
+
```bash
|
|
242
|
+
uv run python -m jepa_playground.variants.ijepa.train jepa_playground/variants/ijepa/configs/cifar10_IJEPA.yaml loss.target=stopgrad loss.regularizer=sigreg
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
## Other Resources
|
|
248
|
+
|
|
249
|
+
- [LeCun's paper on world models](https://openreview.net/pdf?id=BZ5a1r-kVsf)
|
|
250
|
+
- [I-JEPA blog post](https://ai.meta.com/blog/yann-lecun-ai-model-i-jepa/)
|
|
251
|
+
- [V-JEPA blog post](https://ai.meta.com/blog/v-jepa-yann-lecun-ai-model-video-joint-embedding-predictive-architecture/)
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# jepa-playground
|
|
2
|
+
This repo serves as a playground for me to explore and implement a variety of jepa variants in an attempt to understand.
|
|
3
|
+
|
|
4
|
+
All code handwritten by refering to the papers below and their associated code bases.
|
|
5
|
+
|
|
6
|
+
## JEPA Variants
|
|
7
|
+
|
|
8
|
+
### 1. I-JEPA — Image JEPA
|
|
9
|
+
**Paper:** [Self-Supervised Learning from Images with a Joint-Embedding Predictive Architecture](https://arxiv.org/abs/2301.08243) (Assran et al., 2023)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### 2. V-JEPA — Video JEPA
|
|
13
|
+
**Paper:** [V-JEPA: Latent Video Prediction for Visual Representation Learning](https://arxiv.org/abs/2404.08471) (Bardes et al., 2024)
|
|
14
|
+
|
|
15
|
+
### 3. V-JEPA 2
|
|
16
|
+
**Paper:** [V-JEPA 2: Self-Supervised Video Models Enable Understanding, Prediction and Planning](https://arxiv.org/abs/2506.09985) (Meta, 2025)
|
|
17
|
+
|
|
18
|
+
## Repo Layout
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
jepa-playground/
|
|
22
|
+
├── jepa_playground/
|
|
23
|
+
│ ├── encoder.py
|
|
24
|
+
│ ├── predictor.py
|
|
25
|
+
│ ├── ema.py
|
|
26
|
+
│ ├── masking/
|
|
27
|
+
│ │ ├── block.py
|
|
28
|
+
│ │ └── tube.py
|
|
29
|
+
│ └── variants/
|
|
30
|
+
│ ├── ijepa/
|
|
31
|
+
│ │ ├── model.py
|
|
32
|
+
│ │ └── train.py
|
|
33
|
+
│ ├── lejepa/
|
|
34
|
+
│ │ ├── model.py
|
|
35
|
+
│ │ └── train.py
|
|
36
|
+
│ ├── vjepa/
|
|
37
|
+
│ │ ├── model.py
|
|
38
|
+
│ │ └── train.py
|
|
39
|
+
│ └── vjepa2/
|
|
40
|
+
│ └── train.py
|
|
41
|
+
├── tests/
|
|
42
|
+
├── experiments/
|
|
43
|
+
├── reference/
|
|
44
|
+
└── pyproject.toml
|
|
45
|
+
```
|
|
46
|
+
## Shared Architecture (`jepa_playground/`)
|
|
47
|
+
|
|
48
|
+
All variants share a common skeleton.
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
jepa_playground/
|
|
52
|
+
├── encoder.py
|
|
53
|
+
├── predictor.py
|
|
54
|
+
├── ema.py
|
|
55
|
+
└── masking/
|
|
56
|
+
├── block.py
|
|
57
|
+
└── tube.py
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Setup
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
uv sync
|
|
64
|
+
uv run *
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Overrides
|
|
68
|
+
|
|
69
|
+
Every train script and `plan.py` takes a YAML config followed by `section.key=value` overrides. A key
|
|
70
|
+
must already exist in the config and the value must keep its type, so a typo stops the run with an error.
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
uv run python -m jepa_playground.variants.ijepa.train jepa_playground/variants/ijepa/configs/cifar10_IJEPA.yaml optimization.lr=3e-4 data.fake_data=true
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Runs
|
|
77
|
+
|
|
78
|
+
Each training run writes to `runs/<name>/`: `config.yaml` (the config as run), `meta.json` (git commit,
|
|
79
|
+
command line, versions, device), `metrics.jsonl` (one line per log step) and `ckpt.pt`. The name comes
|
|
80
|
+
from `run.name` and can be used once; pick another with `run.name=...`. `run.seed` seeds the run.
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
uv run python -m jepa_playground.variants.ijepa.train jepa_playground/variants/ijepa/configs/cifar10_IJEPA.yaml run.name=ijepa_lr3e-4 optimization.lr=3e-4
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
I-JEPA trains on CIFAR-10 by default; `data.dataset=cifar100` trains it on CIFAR-100, downloaded to `data/` on first use.
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
uv run python -m jepa_playground.variants.ijepa.train jepa_playground/variants/ijepa/configs/cifar10_IJEPA.yaml data.dataset=cifar100 run.name=ijepa_cifar100
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Image data
|
|
93
|
+
|
|
94
|
+
I-JEPA and LeJEPA read `data.dataset`: `cifar10`, `cifar100`, `imagenette` (all downloaded to `data.data_dir` on first
|
|
95
|
+
use) or `folder`, your own images. A folder holds one sub-folder per class under `train/` and under `test/` or `val/`.
|
|
96
|
+
Images of any size are resized and centre-cropped to `model.image_size`.
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
uv run python -m jepa_playground.variants.lejepa.train jepa_playground/variants/lejepa/configs/imagenette_LeJEPA.yaml data.dataset=folder data.data_dir=path/to/images run.name=lejepa_mine
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`data.augmentations` lists the random transforms of each training image, or for the video variants each clip, in
|
|
103
|
+
order, with their settings. Each name is a
|
|
104
|
+
key of `AUGMENTATIONS` in `jepa_playground/data/images.py`. An override changes one setting:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
uv run python -m jepa_playground.variants.lejepa.train jepa_playground/variants/lejepa/configs/cifar10_LeJEPA.yaml data.augmentations.gaussian_blur.p=0
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
`imagenette_LeJEPA.yaml` is the setup of the LeJEPA authors' minimal example: a ViT-S/8 on 128-pixel Imagenette.
|
|
111
|
+
|
|
112
|
+
V-JEPA 2 also saves `ckpt-pretrain.pt`. To run only the cooldown, set `pretrain_checkpoint=runs/<name>/ckpt-pretrain.pt`.
|
|
113
|
+
|
|
114
|
+
V-JEPA and V-JEPA 2 hold `data.test_fraction` (10%) of each class's Kinetics videos out of training to test on, the
|
|
115
|
+
same videos in every run. `data.test_fraction=0` trains on all of them and leaves the run nothing to evaluate on.
|
|
116
|
+
|
|
117
|
+
To continue an interrupted run from its last checkpoint, rerun the same command with `run.resume=true`. It ends with
|
|
118
|
+
the same weights and metrics as a run that was never interrupted. Only `run.dir`, `data.data_dir` and
|
|
119
|
+
`logging.log_every` may differ from the original command; any other change is refused.
|
|
120
|
+
|
|
121
|
+
### Benchmark
|
|
122
|
+
|
|
123
|
+
`jepa_playground.bench` times a config's training step and its data pipeline separately.
|
|
124
|
+
It takes the same overrides; V-JEPA 2 configs also need `--phase pretrain` or `--phase cooldown`.
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
uv run python -m jepa_playground.bench jepa_playground/variants/ijepa/configs/cifar10_IJEPA.yaml --out result.json
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Features
|
|
131
|
+
|
|
132
|
+
`jepa_playground.eval.features` runs a trained model's target encoder over its run's images or videos, without
|
|
133
|
+
augmentation and on the centre clip of each video, and caches the mean-pooled features and labels as
|
|
134
|
+
`runs/<name>/features-<split>.pt`. It prints their spread and effective rank. V-JEPA 2 is evaluated at the clip length
|
|
135
|
+
of its cooldown, the phase that saves `ckpt.pt`.
|
|
136
|
+
Training logs the same two signs of collapse at each log step as `emb_std`, `emb_std_min` and `emb_rank`.
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
uv run python -m jepa_playground.eval.features runs/ijepa_cifar10 --split test
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
A checkpoint outside a run directory works too, given the config it was trained with. Nothing is cached for it.
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
uv run python -m jepa_playground.eval.features --checkpoint old/ijepa.pt --config old/ijepa.yaml
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Probes
|
|
149
|
+
|
|
150
|
+
`jepa_playground.eval` scores a run's cached train and test features with a kNN probe and
|
|
151
|
+
a linear probe.
|
|
152
|
+
It prints each test accuracy and writes `runs/<name>/result-knn.json` and `result-linear.json`.
|
|
153
|
+
`probe=knn` can be used to select only one.
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
uv run python -m jepa_playground.eval runs/ijepa_cifar10 probe=knn,linear
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
A video run works the same way. Its kNN and linear numbers come from mean-pooled features of one clip per video, so
|
|
160
|
+
they are not the attentive-probe numbers the V-JEPA papers report.
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
uv run python -m jepa_playground.eval runs/vjepa_kinetics400
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
To watch the representation while I-JEPA, V-JEPA or V-JEPA 2 trains, `eval.online_probe=true` trains a linear head
|
|
167
|
+
on the target encoder's detached embeddings beside the main loss and logs `probe_loss` and `probe_acc` at each log
|
|
168
|
+
step. `eval.knn_every=N` logs the kNN test accuracy as `knn_acc` every N epochs (of each phase, for V-JEPA 2).
|
|
169
|
+
Neither changes the training: with the same seed, the weights and loss curve match a run without them.
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
uv run python -m jepa_playground.variants.ijepa.train jepa_playground/variants/ijepa/configs/cifar10_IJEPA.yaml eval.online_probe=true eval.knn_every=10
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Results
|
|
176
|
+
|
|
177
|
+
`jepa_playground.results` prints a table of the result files in the run directories you give it. Runs that differ
|
|
178
|
+
only in their `run` section, such as the seed, become one row with the mean, standard deviation and 95% confidence
|
|
179
|
+
half-width of each metric.
|
|
180
|
+
|
|
181
|
+
```bash
|
|
182
|
+
uv run python -m jepa_playground.results runs/*
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Add a method
|
|
186
|
+
|
|
187
|
+
A method is one folder with a train module and a config. `tests/toy_method/` is a complete one in about 90 lines, and
|
|
188
|
+
`tests/test_toy_method.py` runs it through every tool below.
|
|
189
|
+
|
|
190
|
+
1. Write a train module with three functions:
|
|
191
|
+
- `build(config, device, phase=None)` returns the model, its optimiser and the training loader.
|
|
192
|
+
- `training_step(model, batch, step)` returns a dict of scalars. `"loss"` is the one to backpropagate.
|
|
193
|
+
- `load_split(config, split, phase=None)` returns the `train` or `test` data with labels and no augmentation.
|
|
194
|
+
Leave it out if the data has no labels; the method then trains and benchmarks but cannot be probed.
|
|
195
|
+
2. Give the model an `encode(inputs)` method that returns tokens of shape `[B, N, D]` for unmasked inputs.
|
|
196
|
+
3. Write the training loop in the same module with the helpers in `jepa_playground/run.py`: `start_run`,
|
|
197
|
+
`load_checkpoint`, `seed_epoch`, `log` and `save_checkpoint`. A checkpoint holds `model`, `epoch` and `step`.
|
|
198
|
+
4. Write a config with a top-level `variant` key and the `runtime`, `run`, `data` and `optimization` sections the toy
|
|
199
|
+
config has.
|
|
200
|
+
5. Add one line to `VARIANTS` in `jepa_playground/variants/__init__.py` that maps the variant name to the train module.
|
|
201
|
+
|
|
202
|
+
The method then works with the shared tools, with no other file edited:
|
|
203
|
+
|
|
204
|
+
```bash
|
|
205
|
+
uv run python -m tests.toy_method.train tests/toy_method/config.yaml
|
|
206
|
+
uv run python -m jepa_playground.bench tests/toy_method/config.yaml
|
|
207
|
+
uv run python -m jepa_playground.eval runs/toy
|
|
208
|
+
uv run python -m jepa_playground.results runs/toy
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
These four commands need the `VARIANTS` line `"toy": "tests.toy_method.train"`, which the test adds for itself.
|
|
212
|
+
|
|
213
|
+
## Add a component
|
|
214
|
+
|
|
215
|
+
A component is a part a method picks by name in its config. The registries are in `jepa_playground/components.py`
|
|
216
|
+
and `jepa_playground/targets.py`.
|
|
217
|
+
|
|
218
|
+
| Config key | Registry | What to write |
|
|
219
|
+
|---|---|---|
|
|
220
|
+
| `model.encoder` | `ENCODERS` | a module whose `forward(x, keep_idx=None)` returns tokens `[B, N, D]`, with a `grid_size` |
|
|
221
|
+
| `model.predictor` | `PREDICTORS` | a module whose `forward(context, context_idx, target_idx)` returns `[B, M, D]` |
|
|
222
|
+
| `loss.prediction` | `PREDICTION_LOSSES` | a function of `(prediction, target)` that returns a scalar |
|
|
223
|
+
| `loss.target` | `TARGETS` | a `Target` that says whether the target encoder is a copy and whether it gets a gradient |
|
|
224
|
+
| `loss.regularizer` | `REGULARIZERS` | a function of `(embeddings [N, D], step)` that returns a scalar |
|
|
225
|
+
|
|
226
|
+
1. Write the part beside the ones that exist, for example a new function in `jepa_playground/regularizers.py`.
|
|
227
|
+
2. Add one line to its registry that maps a name to it.
|
|
228
|
+
3. Pick it with an override:
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
uv run python -m jepa_playground.variants.ijepa.train jepa_playground/variants/ijepa/configs/cifar10_IJEPA.yaml loss.target=stopgrad loss.regularizer=sigreg
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
---
|
|
235
|
+
|
|
236
|
+
## Other Resources
|
|
237
|
+
|
|
238
|
+
- [LeCun's paper on world models](https://openreview.net/pdf?id=BZ5a1r-kVsf)
|
|
239
|
+
- [I-JEPA blog post](https://ai.meta.com/blog/yann-lecun-ai-model-i-jepa/)
|
|
240
|
+
- [V-JEPA blog post](https://ai.meta.com/blog/v-jepa-yann-lecun-ai-model-video-joint-embedding-predictive-architecture/)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Common code used across all versions of JEPA."""
|