Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 7c604a4

Browse files
committed
Release
0 parents  commit 7c604a4

36 files changed

+4751
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.pt filter=lfs diff=lfs merge=lfs -text
2+
*.png filter=lfs diff=lfs merge=lfs -text

.github/arch.svg

Lines changed: 1 addition & 0 deletions
Loading

.gitignore

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
.DS_Store
2+
3+
# Byte-compiled / optimized / DLL files
4+
__pycache__/
5+
*.py[cod]
6+
7+
# Vim
8+
*.swp
9+
10+
# Distribution / packaging
11+
*.egg-info/
12+
dist
13+
build
14+
.venv/
15+
*/_version.py
16+
17+
# Unit test / coverage reports
18+
.coverage
19+
.pytest_cache
20+
htmlcov
21+
22+
# VScode IDE
23+
.vscode/
24+
.env
25+
26+
# Local data and scratch
27+
.scratch
28+
dataset/algonauts_2023_challenge_data
29+
dataset/derived_splits
30+
dataset/processed
31+
projections
32+
figures
33+
results
34+
35+
# Jupyter
36+
.ipynb_checkpoints
37+
38+
# SLURM
39+
slurm-*
40+
job.txt
41+
42+
# Wandb
43+
wandb/

.pre-commit-config.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
exclude: 'build|third-party|.github'
2+
3+
default_language_version:
4+
python: python3
5+
6+
repos:
7+
- repo: https://github.com/pre-commit/pre-commit-hooks
8+
rev: v4.3.0
9+
hooks:
10+
- id: trailing-whitespace
11+
- id: check-ast
12+
- id: check-merge-conflict
13+
# - id: no-commit-to-branch
14+
# args: ['--branch=main']
15+
- id: end-of-file-fixer
16+
17+
- repo: https://github.com/psf/black
18+
rev: 22.10.0 # Replace by any tag/version: https://github.com/psf/black/tags
19+
hooks:
20+
- id: black
21+
language_version: python3 # Should be a command that runs python3.6+
22+
23+
- repo: https://github.com/pycqa/flake8
24+
rev: 5.0.4
25+
hooks:
26+
- id: flake8
27+
28+
- repo: https://github.com/pycqa/isort
29+
rev: 5.12.0
30+
hooks:
31+
- id: isort
32+
name: isort (python)

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Child Mind Institute
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Algonauts 2023
2+
3+
Code for the [CMI-DAIR submission](https://arxiv.org/abs/2308.02351) to the [Algonauts 2023 Challenge](http://algonauts.csail.mit.edu/) (team "BlobGPT").
4+
5+
<p align="center">
6+
<img src=".github/arch.svg" alt="model architecture" width="360">
7+
</p>
8+
9+
Our model consists of a multi-subject linear encoding head attached to a pretrained trunk model. The multi-subject head has three components: (1) a shared multi-layer feature projection, (2) shared plus subject-specific low-dimension linear transformations, and (3) a shared frozen PCA embedding. The feature projection is "factorized" as a 1x1 convolution followed by learned depthwise spatial pooling.
10+
11+
Our submission model used an `eva02_base_patch14_224.mim_in22k` trunk from [timm](https://github.com/huggingface/pytorch-image-models). We first trained the encoding head only with the trunk frozen (phase 1). Then we unfroze the trunk's attention blocks and fine-tuned the model end-to-end (phase 2). See [our report](https://arxiv.org/abs/2308.02351) for more details.
12+
13+
## Results
14+
15+
| Model | Val score | Test score | Config | Weights |
16+
| --- | --- | --- | --- | --- |
17+
| GroupLin-P1 | 20.4% | 58.8% | [config](config/phase1_head_only.yaml) | [weights](https://github.com/cmi-dair/algonauts23/releases/download/v0.1.0/grouplin_phase1.pt) |
18+
| GroupLin-P2 | 20.9% | 60.3% | [config](config/phase2_finetune.yaml) | [weights](https://github.com/cmi-dair/algonauts23/releases/download/v0.1.0/grouplin_phase2.pt) |
19+
20+
**Val score**: median R<sup>2</sup> on our validation set
21+
22+
**Test score**: mean noise-normalized R<sup>2</sup> on official challenge test set
23+
24+
## Installation
25+
26+
Clone the repository.
27+
28+
```bash
29+
git clone https://github.com/cmi-dair/algonauts23.git
30+
```
31+
32+
Create a new environment.
33+
34+
```bash
35+
cd algonauts23
36+
python3.10 -m venv --prompt algonauts23 .venv
37+
source .venv/bin/activate
38+
pip install -U pip
39+
```
40+
41+
Install the dependencies
42+
43+
```bash
44+
pip install -r requirements.txt
45+
```
46+
47+
Install the package
48+
49+
```bash
50+
pip install .
51+
```
52+
53+
## Dataset preparation
54+
55+
Follow the steps [here](dataset/) to download and prepare the data for training.
56+
57+
## Training
58+
59+
### WandB setup (optional)
60+
61+
We used [WandB](https://wandb.ai/) for experiment tracking. To set up WandB, create an account if you don't have one already and get your [API key](https://docs.wandb.ai/quickstart#common-questions). Then include these commands before launching training.
62+
63+
```bash
64+
export WANDB_API_KEY="XXXXXXX"
65+
wandb login
66+
opts="--wandb"
67+
```
68+
69+
### Download PCA weights
70+
71+
Our model uses a frozen group PCA embedding. You can download the weights [here](https://github.com/cmi-dair/algonauts23/releases/download/v0.1.0/group_pca_d-2048.pt).
72+
73+
You can also re-run the group PCA using [`scripts/fit_group_pca.py`](scripts/fit_group_pca.py).
74+
75+
### Phase 1: Multi-subject linear head only
76+
77+
In the first phase of training, we only train the multi-subject linear head with the trunk model frozen.
78+
79+
```bash
80+
python scripts/train_group_encoder.py config/phase1_head_only.yaml \
81+
--out_dir results --workers 4 $opts
82+
```
83+
84+
### Phase 2: Partial fine-tuning
85+
86+
Next, we partially fine-tune the full model starting from the best checkpoint from the first phase.
87+
88+
```bash
89+
# Path to checkpoint
90+
ckpt_run="PHASE1_RUN_NAME"
91+
ckpt="results/algonauts23-group-encoder/${ckpt_run}/checkpoints/ckpt-best.pt"
92+
93+
python scripts/train_group_encoder.py config/phase2_finetune.yaml \
94+
--out_dir results --ckpt $ckpt --workers 4 $opts
95+
```
96+
97+
## Submission
98+
99+
Run the [`zip_submission.sh`](scripts/zip_submission.sh) to prepare a zip file for submission to the [leaderboard](https://codalab.lisn.upsaclay.fr/competitions/9304).
100+
101+
```bash
102+
./scripts/zip_submission.sh RESULT_DIR
103+
```
104+
105+
## Acknowledgements
106+
107+
This code was built with elements and inspiration from [timm](https://github.com/huggingface/pytorch-image-models).
108+
109+
## Citation
110+
If you find this repository helpful, please consider citing:
111+
112+
```
113+
@article{lane2023algonauts,
114+
author = {Connor Lane and Gregory Kiar},
115+
title = {A Parameter-efficient Multi-subject Model for Predicting fMRI Activity},
116+
journal = {arXiv preprint arXiv:2308.02351},
117+
year = {2023},
118+
}
119+
```

algonauts23/__init__.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
from pathlib import Path
3+
4+
ALGONAUTS_DIR = Path(
5+
os.environ.get("ALGONAUTS_DIR", Path(__file__).parent.parent.absolute())
6+
)
7+
8+
ALGONAUTS_DATA_DIR = Path(
9+
os.environ.get("ALGONAUTS_DATA_DIR", ALGONAUTS_DIR / "dataset")
10+
)
11+
12+
ALGONAUTS_RAW_DIR = Path(
13+
os.environ.get(
14+
"ALGONAUTS_RAW_DIR",
15+
ALGONAUTS_DATA_DIR / "algonauts_2023_challenge_data",
16+
)
17+
)
18+
19+
SUBS = tuple([f"subj{ii:02d}" for ii in range(1, 9)])
20+
NUM_SUBS = len(SUBS)
21+
FMRI_DIM = 39548
22+
23+
ROI_GROUPS = {
24+
"streams": "Anatomical streams",
25+
"prf-visualrois": "Early retinotopic",
26+
"floc-bodies": "Body-selective",
27+
"floc-faces": "Face-selective",
28+
"floc-places": "Place-selective",
29+
"floc-words": "Word-selective",
30+
}

0 commit comments

Comments
 (0)