Update README.md
Browse files
README.md
CHANGED
|
@@ -1,199 +1,205 @@
|
|
| 1 |
---
|
| 2 |
library_name: transformers
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
---
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
|
| 16 |
-
<!-- Provide a longer summary of what this model is. -->
|
| 17 |
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
- **Funded by [optional]:** [More Information Needed]
|
| 22 |
-
- **Shared by [optional]:** [More Information Needed]
|
| 23 |
-
- **Model type:** [More Information Needed]
|
| 24 |
-
- **Language(s) (NLP):** [More Information Needed]
|
| 25 |
-
- **License:** [More Information Needed]
|
| 26 |
-
- **Finetuned from model [optional]:** [More Information Needed]
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
- **Paper [optional]:** [More Information Needed]
|
| 34 |
-
- **Demo [optional]:** [More Information Needed]
|
| 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 |
-
### Recommendations
|
| 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 |
-
<!-- Relevant interpretability work for the model goes here -->
|
| 138 |
-
|
| 139 |
-
[More Information Needed]
|
| 140 |
-
|
| 141 |
-
## Environmental Impact
|
| 142 |
-
|
| 143 |
-
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
| 144 |
-
|
| 145 |
-
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
| 146 |
-
|
| 147 |
-
- **Hardware Type:** [More Information Needed]
|
| 148 |
-
- **Hours used:** [More Information Needed]
|
| 149 |
-
- **Cloud Provider:** [More Information Needed]
|
| 150 |
-
- **Compute Region:** [More Information Needed]
|
| 151 |
-
- **Carbon Emitted:** [More Information Needed]
|
| 152 |
-
|
| 153 |
-
## Technical Specifications [optional]
|
| 154 |
-
|
| 155 |
-
### Model Architecture and Objective
|
| 156 |
-
|
| 157 |
-
[More Information Needed]
|
| 158 |
-
|
| 159 |
-
### Compute Infrastructure
|
| 160 |
-
|
| 161 |
-
[More Information Needed]
|
| 162 |
-
|
| 163 |
-
#### Hardware
|
| 164 |
-
|
| 165 |
-
[More Information Needed]
|
| 166 |
-
|
| 167 |
-
#### Software
|
| 168 |
-
|
| 169 |
-
[More Information Needed]
|
| 170 |
-
|
| 171 |
-
## Citation [optional]
|
| 172 |
-
|
| 173 |
-
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
| 174 |
-
|
| 175 |
-
**BibTeX:**
|
| 176 |
-
|
| 177 |
-
[More Information Needed]
|
| 178 |
-
|
| 179 |
-
**APA:**
|
| 180 |
-
|
| 181 |
-
[More Information Needed]
|
| 182 |
-
|
| 183 |
-
## Glossary [optional]
|
| 184 |
-
|
| 185 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
| 186 |
-
|
| 187 |
-
[More Information Needed]
|
| 188 |
-
|
| 189 |
-
## More Information [optional]
|
| 190 |
-
|
| 191 |
-
[More Information Needed]
|
| 192 |
-
|
| 193 |
-
## Model Card Authors [optional]
|
| 194 |
-
|
| 195 |
-
[More Information Needed]
|
| 196 |
-
|
| 197 |
-
## Model Card Contact
|
| 198 |
-
|
| 199 |
-
[More Information Needed]
|
|
|
|
| 1 |
---
|
| 2 |
library_name: transformers
|
| 3 |
+
license: apache-2.0
|
| 4 |
+
datasets:
|
| 5 |
+
- ILSVRC/imagenet-1k
|
| 6 |
---
|
| 7 |
+
# NEPA: Next-Embedding Predictive Architectures Are Strong Vision Learners
|
| 8 |
|
| 9 |
+
[]()
|
| 10 |
+
[]()
|
| 11 |
+
[]()
|
| 12 |
|
| 13 |
+
This is a PyTorch/GPU re-implementation of Next-Embedding Predictive Architectures Are Strong Vision Learners.
|
| 14 |
|
| 15 |
+
<p align="center">
|
| 16 |
+
<img src="https://github.com/user-attachments/assets/760a7ca4-1aa4-43d9-8731-ab8b33e463d5" width="350">
|
| 17 |
+
</p>
|
| 18 |
|
| 19 |
+
The Next-Embedding Predictive Architecture. An image is split into patches and embedded into a sequence. An autoregressive model predicts the next embedding from previous ones.
|
| 20 |
|
| 21 |
+
```
|
| 22 |
+
@article{six2025nepa,
|
| 23 |
+
title={Next-Embedding Predictive Architectures Are Strong Vision Learners},
|
| 24 |
+
author = {Sihan Xu and Ziqiao Ma and Wenhao Chai and Xuweiyi Chen and Weiyang Jin and Joyce Chai and Saining Xie and Stella X. Yu},
|
| 25 |
+
journal={arXiv preprint arXiv:},
|
| 26 |
+
year={2025}
|
| 27 |
+
}
|
| 28 |
|
| 29 |
+
```
|
| 30 |
|
|
|
|
| 31 |
|
| 32 |
+
## Environment
|
| 33 |
|
| 34 |
+
The codebase has been tested with the following environment:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
- Python 3.10
|
| 37 |
+
- PyTorch 2.8.0
|
| 38 |
+
- Transformers 4.56.2
|
| 39 |
|
| 40 |
+
### Installation
|
| 41 |
|
| 42 |
+
First, clone the repository:
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
```bash
|
| 45 |
+
git clone https://github.com/SihanXU/nepa
|
| 46 |
+
cd nepa
|
| 47 |
+
```
|
| 48 |
|
| 49 |
+
Then, create a conda environment and install dependencies:
|
| 50 |
|
| 51 |
+
```bash
|
| 52 |
+
conda env create -f environment.yml
|
| 53 |
+
conda activate nepa
|
| 54 |
+
```
|
| 55 |
|
| 56 |
+
Alternatively, you can install the dependencies manually:
|
| 57 |
|
| 58 |
+
```bash
|
| 59 |
+
pip install -r requirements.txt
|
| 60 |
+
```
|
| 61 |
|
| 62 |
+
## Quick Start
|
| 63 |
|
| 64 |
+
Here's a simple example to run inference with a pretrained NEPA model:
|
| 65 |
|
| 66 |
+
```python
|
| 67 |
+
from transformers import AutoImageProcessor
|
| 68 |
+
from models.vit_nepa import ViTNepaForImageClassification
|
| 69 |
+
from PIL import Image
|
| 70 |
+
import requests
|
| 71 |
|
| 72 |
+
url = 'https://raw.githubusercontent.com/pytorch/hub/master/images/dog.jpg'
|
| 73 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
| 74 |
|
| 75 |
+
processor = AutoImageProcessor.from_pretrained('SixAILab/nepa-large-patch14-224-sft')
|
| 76 |
+
model = ViTNepaForImageClassification.from_pretrained('SixAILab/nepa-large-patch14-224-sft')
|
| 77 |
|
| 78 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 79 |
+
outputs = model(**inputs)
|
| 80 |
+
logits = outputs.logits
|
| 81 |
+
# model predicts one of the 1000 ImageNet classes
|
| 82 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 83 |
+
print("Predicted class:", model.config.id2label[predicted_class_idx])
|
| 84 |
+
```
|
| 85 |
|
| 86 |
+
## Setup Huggingface Token
|
| 87 |
|
| 88 |
+
To download pretrained models from Hugging Face Hub, you need to authenticate with your Hugging Face account:
|
| 89 |
|
| 90 |
+
```bash
|
| 91 |
+
hf auth login
|
| 92 |
+
```
|
| 93 |
|
|
|
|
| 94 |
|
| 95 |
+
## [Optional] Setup Wandb Token
|
| 96 |
|
| 97 |
+
We use Wandb to track experiments. You may want to use [Weights & Biases](https://wandb.ai/) to log and track your experiments:
|
| 98 |
|
| 99 |
+
```bash
|
| 100 |
+
pip install wandb
|
| 101 |
+
wandb login
|
| 102 |
+
```
|
| 103 |
|
| 104 |
+
## Prepare ImageNet-1k Dataset
|
| 105 |
|
| 106 |
+
We use the ImageNet-1k dataset for training and evaluation. To download the dataset via Hugging Face Datasets:
|
| 107 |
|
| 108 |
+
```bash
|
| 109 |
+
python download_dataset.py
|
| 110 |
+
```
|
| 111 |
|
| 112 |
+
This script will download and prepare the ImageNet-1k dataset. Note that this requires approximately 150GB of disk space. You may need to accept the dataset terms on [Hugging Face](https://huggingface.co/datasets/ILSVRC/imagenet-1k) before downloading.
|
| 113 |
|
| 114 |
+
## Evaluate Nepa for Image Classification
|
| 115 |
|
| 116 |
+
We provide pretrained checkpoints for NEPA models. The following table compares our reproduced results with the paper:
|
| 117 |
|
| 118 |
+
| Model | SwiGLU (paper) | GeLU (reproduce) |
|
| 119 |
+
|--------|---------------:|-----------------:|
|
| 120 |
+
| Nepa-B | 83.8 | 83.75 |
|
| 121 |
+
| Nepa-L | 85.3 | 85.40 |
|
| 122 |
|
| 123 |
+
To evaluate the base model on ImageNet-1k validation set:
|
| 124 |
|
| 125 |
+
```bash
|
| 126 |
+
bash scripts/eval/nepa_b_sft_eval.sh
|
| 127 |
+
```
|
| 128 |
|
| 129 |
+
This should give:
|
| 130 |
+
```
|
| 131 |
+
***** eval metrics *****
|
| 132 |
+
eval_accuracy = 0.8375
|
| 133 |
+
eval_loss = 0.7169
|
| 134 |
+
```
|
| 135 |
|
| 136 |
+
To evaluate the large model:
|
| 137 |
|
| 138 |
+
```bash
|
| 139 |
+
bash scripts/eval/nepa_l_sft_eval.sh
|
| 140 |
+
```
|
| 141 |
|
| 142 |
+
This should give:
|
| 143 |
+
```
|
| 144 |
+
***** eval metrics *****
|
| 145 |
+
eval_accuracy = 0.854
|
| 146 |
+
eval_loss = 0.6371
|
| 147 |
+
```
|
| 148 |
|
| 149 |
+
## Fine-tune
|
| 150 |
|
| 151 |
+
To fine-tune a pretrained NEPA model on ImageNet-1k for image classification:
|
| 152 |
|
| 153 |
+
For the base model:
|
| 154 |
|
| 155 |
+
```bash
|
| 156 |
+
bash scripts/finetune/nepa_b_sft.sh
|
| 157 |
+
```
|
| 158 |
|
| 159 |
+
For the large model:
|
| 160 |
|
| 161 |
+
```bash
|
| 162 |
+
bash scripts/finetune/nepa_l_sft.sh
|
| 163 |
+
```
|
| 164 |
|
| 165 |
+
You can modify the training hyperparameters (learning rate, batch size, epochs, etc.) in the corresponding script files.
|
| 166 |
|
| 167 |
+
## Pretrain
|
| 168 |
|
| 169 |
+
To pretrain NEPA from scratch on ImageNet-1k:
|
| 170 |
|
| 171 |
+
For the base model:
|
| 172 |
|
| 173 |
+
```bash
|
| 174 |
+
bash scripts/pretrain/nepa_b.sh
|
| 175 |
+
```
|
| 176 |
|
| 177 |
+
For the large model:
|
| 178 |
|
| 179 |
+
```bash
|
| 180 |
+
bash scripts/pretrain/nepa_l.sh
|
| 181 |
+
```
|
| 182 |
|
| 183 |
+
Pretraining typically requires multiple GPUs. We recommend using at least 8 A100 GPUs for the large model.
|
| 184 |
|
| 185 |
+
## Convert a Pretrained Model to Classification Model
|
| 186 |
|
| 187 |
+
After pretraining, you can convert the pretrained model to a classification model by initializing a classification head. Use the `init_nepa_cls_from_pretrain.py` script:
|
| 188 |
|
| 189 |
+
Here is an example:
|
| 190 |
+
```
|
| 191 |
+
python init_nepa_cls_from_pretrain.py \
|
| 192 |
+
--pretrained_model_id SixAILab/nepa-base-patch14-224 \
|
| 193 |
+
--config_model_id configs/finetune/nepa-base-patch14-224-sft \
|
| 194 |
+
--pretrained_revision main \
|
| 195 |
+
--save_local \
|
| 196 |
+
--local_dir ./nepa-base-patch14-224-sft
|
| 197 |
+
```
|
| 198 |
|
| 199 |
+
## Acknowledgements
|
| 200 |
|
| 201 |
+
We gratefully acknowledge the developers of [Transformers](https://github.com/huggingface/transformers), [Evaluate](https://github.com/huggingface/evaluate), and [timm](https://github.com/huggingface/pytorch-image-models) for their excellent open-source contributions.
|
| 202 |
|
| 203 |
+
## Contact
|
| 204 |
|
| 205 |
+
Feel free to contact me through email (sihanxu@umich.edu). Enjoy!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|