Delete README.md
Browse files
README.md
DELETED
|
@@ -1,283 +0,0 @@
|
|
| 1 |
-
<div align=center>
|
| 2 |
-
|
| 3 |
-
# [NeurIPS 2024 Spotlight] Bridging The Gap between Low-rank and Orthogonal Adaptation via Householder Reflection Adaptation
|
| 4 |
-
|
| 5 |
-
[](https://arxiv.org/pdf/2405.17484)
|
| 6 |
-
[](https://huggingface.co/docs/peft/en/package_reference/hra)
|
| 7 |
-
|
| 8 |
-
</div>
|
| 9 |
-
|
| 10 |
-
<div align="center">
|
| 11 |
-
<img src="assets/OHRFT_scheme.png" width="1100"/>
|
| 12 |
-
</div>
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
## Introduction
|
| 17 |
-
|
| 18 |
-
This repository includes the official implementation of [HRA](https://arxiv.org/pdf/2405.17484).
|
| 19 |
-
We propose a simple yet effective adapter-based orthogonal fine-tuning method, HRA.
|
| 20 |
-
Given a pre-trained model, our method fine-tunes its layers by multiplying each frozen weight matrix with an orthogonal matrix constructed by a chain of learnable Householder reflections (HRs).
|
| 21 |
-
|
| 22 |
-
## Usage
|
| 23 |
-
|
| 24 |
-
### Subject-driven Generation
|
| 25 |
-
|
| 26 |
-
<div align="center">
|
| 27 |
-
<img src="assets/subject.png" width="600"/>
|
| 28 |
-
</div>
|
| 29 |
-
|
| 30 |
-
Given several images of a specific subject and a textual prompt, subject-driven generation aims to generate images of the same subject in a context aligning with the prompt.
|
| 31 |
-
|
| 32 |
-
#### Environment Setup
|
| 33 |
-
|
| 34 |
-
```bash
|
| 35 |
-
cd generation
|
| 36 |
-
conda env create -f env.yml
|
| 37 |
-
```
|
| 38 |
-
|
| 39 |
-
#### Prepare Dataset
|
| 40 |
-
|
| 41 |
-
Download [dreambooth](https://github.com/google/dreambooth) dataset by running this script.
|
| 42 |
-
|
| 43 |
-
```bash
|
| 44 |
-
cd subject
|
| 45 |
-
bash download_dreambooth.sh
|
| 46 |
-
```
|
| 47 |
-
|
| 48 |
-
After downloading the data, your directory structure should look like this:
|
| 49 |
-
|
| 50 |
-
```
|
| 51 |
-
dreambooth
|
| 52 |
-
βββ dataset
|
| 53 |
-
β βββ backpack
|
| 54 |
-
β βββ backpack_dog
|
| 55 |
-
β ...
|
| 56 |
-
```
|
| 57 |
-
|
| 58 |
-
You can also put your custom images into `dreambooth/dataset`.
|
| 59 |
-
|
| 60 |
-
#### Finetune
|
| 61 |
-
|
| 62 |
-
```bash
|
| 63 |
-
prompt_idx=0
|
| 64 |
-
class_idx=0
|
| 65 |
-
./train_dreambooth.sh $prompt_idx $class_idx
|
| 66 |
-
```
|
| 67 |
-
|
| 68 |
-
where the `$prompt_idx` corresponds to different prompts ranging from 0 to 24 and the `$class_idx` corresponds to different subjects ranging from 0 to 29.
|
| 69 |
-
|
| 70 |
-
Launch the training script with `accelerate` and pass hyperparameters, as well as LoRa-specific arguments to it such as:
|
| 71 |
-
|
| 72 |
-
- `use_hra`: Enables HRA in the training script.
|
| 73 |
-
- `hra_r`: the number of HRs (i.e., r) across different layers, expressed in `int`.
|
| 74 |
-
As r increases, the number of trainable parameters increases, which generally leads to improved performance.
|
| 75 |
-
However, this also results in higher memory consumption and longer computation times.
|
| 76 |
-
Therefore, r is usually set to 8.
|
| 77 |
-
**Note**, please set r to an even number to avoid potential issues during initialization.
|
| 78 |
-
- `hra_apply_GS`: Applys Gram-Schmidt orthogonalization. Default is `false`.
|
| 79 |
-
- `hra_bias`: specify if the `bias` paramteres should be traind. Can be `none`, `all` or `hra_only`.
|
| 80 |
-
|
| 81 |
-
#### Evaluation
|
| 82 |
-
|
| 83 |
-
```bash
|
| 84 |
-
python evaluate.py
|
| 85 |
-
python get_result.py
|
| 86 |
-
```
|
| 87 |
-
|
| 88 |
-
### Controllable Generation
|
| 89 |
-
|
| 90 |
-
<div align="center">
|
| 91 |
-
<img src="assets/control.png" width="650"/>
|
| 92 |
-
</div>
|
| 93 |
-
|
| 94 |
-
Controllable generation aims to generate images aligning with a textual prompt and additional control signals (such as facial landmark annotations, canny edges, and segmentation maps).
|
| 95 |
-
|
| 96 |
-
#### Prepare Dataset
|
| 97 |
-
|
| 98 |
-
Download ADE20K and CelebA-HQ datasets by running this script.
|
| 99 |
-
|
| 100 |
-
```bash
|
| 101 |
-
cd control
|
| 102 |
-
bash download_ade20k.sh
|
| 103 |
-
bash download_celebhq.sh
|
| 104 |
-
```
|
| 105 |
-
|
| 106 |
-
For COCO dataset, we follow [OFT](https://github.com/Zeju1997/oft) to download and preprocess it.
|
| 107 |
-
|
| 108 |
-
After downloading the data, your directory structure should look like this:
|
| 109 |
-
|
| 110 |
-
```
|
| 111 |
-
data
|
| 112 |
-
βββ ADE20K
|
| 113 |
-
β βββ train
|
| 114 |
-
β β βββ color
|
| 115 |
-
β β βββ segm
|
| 116 |
-
β β βββ prompt_train_blip.json
|
| 117 |
-
β βββ val
|
| 118 |
-
β β βββ color
|
| 119 |
-
β β βββ segm
|
| 120 |
-
β β βββ prompt_val_blip.json
|
| 121 |
-
βββ COCO
|
| 122 |
-
β βββ train
|
| 123 |
-
β β βββ color
|
| 124 |
-
β β βββ depth
|
| 125 |
-
...
|
| 126 |
-
```
|
| 127 |
-
|
| 128 |
-
#### Prepare pre-trained model
|
| 129 |
-
|
| 130 |
-
Download the pre-trained model weights [v1-5-pruned.ckpt](https://huggingface.co/runwayml/stable-diffusion-v1-5/tree/main) and save it in the `models` directory.
|
| 131 |
-
|
| 132 |
-
#### Fine-tuning
|
| 133 |
-
|
| 134 |
-
1. Create the model with additional **HRA** parameters:
|
| 135 |
-
```bash
|
| 136 |
-
python tool_add_hra.py \
|
| 137 |
-
--input_path=./models/v1-5-pruned.ckpt \
|
| 138 |
-
--output_path=./models/hra_r_8.ckpt \
|
| 139 |
-
--r=8
|
| 140 |
-
```
|
| 141 |
-
2. Specify the control signal and dataset. Train the model specify the same hyperparameters as above:
|
| 142 |
-
```bash
|
| 143 |
-
python train.py \
|
| 144 |
-
--r=8 \
|
| 145 |
-
--control=segm
|
| 146 |
-
```
|
| 147 |
-
|
| 148 |
-
#### Generation
|
| 149 |
-
1. After finetuning with **HRA**, run inference to generate images based on control signal. Because the inference takes some time, to perform large scale evaluation, we split the dataset into different sub-datasets and run inference on multiple gpus:
|
| 150 |
-
```bash
|
| 151 |
-
python generation.py
|
| 152 |
-
--r=8 \
|
| 153 |
-
--control=segm
|
| 154 |
-
```
|
| 155 |
-
1. To evaluate **HRA** results on the three tasks: canny edge to image (C2I) on the COCO dataset, landmark to face (L2F) on the CelebA-HQ dataset, and segmentation map to image (S2I) on the ADE20K dataset, run the following scripts on the generated images.
|
| 156 |
-
```bash
|
| 157 |
-
python eval_landmark.py
|
| 158 |
-
```
|
| 159 |
-
```bash
|
| 160 |
-
python eval_canny.py
|
| 161 |
-
```
|
| 162 |
-
Note, for evaluating the segmentation map-to-image (S2I) task, please install the [Segformer](https://github.com/NVlabs/SegFormer) repository. Run the following testing command on both the original and generated images.
|
| 163 |
-
```bash
|
| 164 |
-
python tools/test.py local_configs/segformer/B4/segformer.b4.512x512.ade.160k.py ./weights/segformer.b4.512x512.ade.160k.pth
|
| 165 |
-
```
|
| 166 |
-
|
| 167 |
-
### Natural Language Understanding
|
| 168 |
-
|
| 169 |
-
<div align="center">
|
| 170 |
-
<img src="assets/figure_nlp.png" width="300"/>
|
| 171 |
-
</div>
|
| 172 |
-
|
| 173 |
-
We adapt [DeBERTaV3-base](https://arxiv.org/abs/2111.09543) and test the performance of the adapted models on [General Language Understanding Evaluation (GLUE) benchmark](https://gluebenchmark.com/).
|
| 174 |
-
|
| 175 |
-
#### Environment Setup
|
| 176 |
-
|
| 177 |
-
```bash
|
| 178 |
-
cd nlu
|
| 179 |
-
conda env create -f env.yml
|
| 180 |
-
```
|
| 181 |
-
|
| 182 |
-
Before fine-tuning, you need to install the dependencies.
|
| 183 |
-
|
| 184 |
-
```bash
|
| 185 |
-
python setup.py install
|
| 186 |
-
```
|
| 187 |
-
|
| 188 |
-
#### Prepare Dataset
|
| 189 |
-
|
| 190 |
-
Run this scipt to download glue dataset.
|
| 191 |
-
|
| 192 |
-
```bash
|
| 193 |
-
cache_dir=/tmp/DeBERTa/
|
| 194 |
-
cd experiments/glue
|
| 195 |
-
./download_data.sh $cache_dir/glue_tasks
|
| 196 |
-
```
|
| 197 |
-
|
| 198 |
-
#### Finetune
|
| 199 |
-
|
| 200 |
-
Run tasks.
|
| 201 |
-
|
| 202 |
-
```bash
|
| 203 |
-
./mnli.sh
|
| 204 |
-
./cola.sh
|
| 205 |
-
./mrpc.sh
|
| 206 |
-
./qnli.sh
|
| 207 |
-
./qqp.sh
|
| 208 |
-
./rte.sh
|
| 209 |
-
./sst2.sh
|
| 210 |
-
./stsb.sh
|
| 211 |
-
```
|
| 212 |
-
|
| 213 |
-
### Mathematical reasoning
|
| 214 |
-
We have not yet completed the integration of HRA code into PEFT. Before that, if you want to try using the HRA method to fine-tune large models, you can follow the steps below.
|
| 215 |
-
|
| 216 |
-
Go to the llama folder
|
| 217 |
-
```bash
|
| 218 |
-
cd llama
|
| 219 |
-
```
|
| 220 |
-
|
| 221 |
-
#### Environment Setup
|
| 222 |
-
We recommend using Python 3.10 for your environment and use the conda to install it.
|
| 223 |
-
```bash
|
| 224 |
-
conda create -n pytorch python=3.10
|
| 225 |
-
```
|
| 226 |
-
Then install the required packages with the following command:
|
| 227 |
-
```bash
|
| 228 |
-
pip install -r requirements.txt
|
| 229 |
-
```
|
| 230 |
-
Please note that the peft package and transformer package must be downloaded with the versions consistent with those listed in the requirements file.
|
| 231 |
-
|
| 232 |
-
After completing the download, please replace the **oft** folder inside the **peft/tuners** within your running environment's **python/site-packages** with the **oft** folder from the current directory.
|
| 233 |
-
|
| 234 |
-
The path for the oft folder in the environment should be:
|
| 235 |
-
|
| 236 |
-
```bash
|
| 237 |
-
/your_path/anaconda3/envs/pytorch/lib/python3.10/site-packages/peft/tuners/
|
| 238 |
-
```
|
| 239 |
-
The **layer.py** in the current oft directory is implemented for when Ξ» is not infinity.
|
| 240 |
-
|
| 241 |
-
If you want to simulate when Ξ» is infinity, please replace **layer.py** with **layer_GS_HRA.py**, and set the hyperparameter Ξ» to 0 during training.
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
#### Prepare Dataset
|
| 245 |
-
The dataset we use for fine-tuning is MetaMathQA-40K, which can be downloaded through this [link](https://huggingface.co/datasets/meta-math/MetaMathQA-40K).
|
| 246 |
-
#### Prepare model
|
| 247 |
-
The model we use for fine-tuning is llama2. You can choose the model you want to fine-tune.
|
| 248 |
-
#### Finetune
|
| 249 |
-
Run the following code to complete the fine-tuning:
|
| 250 |
-
```bash
|
| 251 |
-
bash tune.sh
|
| 252 |
-
```
|
| 253 |
-
Please note that you need to change the dataset path, the path of the pre-trained model, and you can change the parameters according to your needs in tune.sh. That is:
|
| 254 |
-
```bash
|
| 255 |
-
BASE_MODEL="YOUR_MODEL_PATH"
|
| 256 |
-
DATA_PATH="YOUR_DATA_PATH"
|
| 257 |
-
OUTPUT="YOUR_MODEL_SAVED_PATH"
|
| 258 |
-
```
|
| 259 |
-
#### Evaluation
|
| 260 |
-
After the training is complete, you can run the following command to test:
|
| 261 |
-
```bash
|
| 262 |
-
bash test.sh
|
| 263 |
-
```
|
| 264 |
-
Please note to change the model path in it:
|
| 265 |
-
```bash
|
| 266 |
-
BASE_MODEL="YOUR_MODEL_PATH"
|
| 267 |
-
OUTPUT="YOUR_MODEL_SAVED_PATH"
|
| 268 |
-
```
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
## π Citing our work
|
| 273 |
-
If you find our work useful, please cite it:
|
| 274 |
-
```bibtex
|
| 275 |
-
@inproceedings{yuanbridging,
|
| 276 |
-
title={Bridging The Gap between Low-rank and Orthogonal Adaptation via Householder Reflection Adaptation},
|
| 277 |
-
author={Yuan, Shen and Liu, Haotian and Xu, Hongteng},
|
| 278 |
-
booktitle={The Thirty-eighth Annual Conference on Neural Information Processing Systems},
|
| 279 |
-
year={2024}
|
| 280 |
-
}
|
| 281 |
-
```
|
| 282 |
-
|
| 283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