|
1 |
| -# self-sup-corr-dev |
| 1 | +# SD4Match: Learning to Prompt Stable Diffusion Model for Semantic Matching |
| 2 | + |
| 3 | +**[Project Page](http://sd4match.active.vision/) | [Arxiv](https://arxiv.org/abs/2310.17569) | [Pretrained Prompt](https://www.robots.ox.ac.uk/~xinghui/sd4match/pretrained_prompts.zip)** |
| 4 | + |
| 5 | +[Xinghui Li<sup>1</sup>](https://scholar.google.com/citations?user=XLlgbBoAAAAJ&hl=en), |
| 6 | +Jingyi Lu<sup>2</sup>, |
| 7 | +[Kai Han<sup>2</sup>](https://www.kaihan.org/), |
| 8 | +[Victor Prisacariu<sup>1</sup>](https://www.robots.ox.ac.uk/~victor//) |
| 9 | + |
| 10 | +[<sup>1</sup>Active Vision Lab, University of Oxford](https://www.robots.ox.ac.uk/~lav/) |
| 11 | +[<sup>2</sup>Visual AI Lab, University of Hong Kong](https://visailab.github.io/) |
| 12 | + |
| 13 | +## Environment |
| 14 | +The environment can be easily installed through [conda](https://docs.conda.io/projects/miniconda/en/latest/) and pip. After downloading the code, run the following command: |
| 15 | +```shell |
| 16 | +$conda create -n sd4match python=3.10 |
| 17 | +$conda activate sd4match |
| 18 | + |
| 19 | +$conda install pytorch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 pytorch-cuda=11.6 -c pytorch -c nvidia |
| 20 | +$conda install xformers -c xformers |
| 21 | +$pip install yacs pandas scipy einops matplotlib triton timm diffusers accelerate transformers datasets tensorboard pykeops scikit-learn |
| 22 | +``` |
| 23 | + |
| 24 | +## Data |
| 25 | +#### PF-Pascal |
| 26 | +1. Download PF-Pascal dataset from [link](https://www.di.ens.fr/willow/research/proposalflow/). |
| 27 | +2. Rename the outermost directory from `PF-dataset-PASCAL` to `pf-pascal`. |
| 28 | +3. Download lists for image pairs from [link](www.robots.ox.ac.uk/~xinghui/sd4match/pf-pascal_image_pairs.zip). |
| 29 | +4. Place the lists for image pairs under `pf-pascal` directory. The structure should be: |
| 30 | +``` |
| 31 | +pf-pascal |
| 32 | +├── __MACOSX |
| 33 | +├── PF-dataset-PASCAL |
| 34 | +├── trn_pairs.csv |
| 35 | +├── val_pairs.csv |
| 36 | +└── test_pairs.csv |
| 37 | +``` |
| 38 | +#### PF-Willow |
| 39 | +1. Download PF-Willow dataset from the [link](https://www.di.ens.fr/willow/research/proposalflow/). |
| 40 | +2. Rename the outermost directory from `PF-dataset` to `pf-willow`. |
| 41 | +3. Download lists for image pairs from [link](www.robots.ox.ac.uk/~xinghui/sd4match/test_pairs.csv). |
| 42 | +4. Place the lists for image pairs under `pf-willow` directory. The structure should be: |
| 43 | +``` |
| 44 | +pf-willow |
| 45 | +├── __MACOSX |
| 46 | +├── PF-dataset |
| 47 | +└── test_pairs.csv |
| 48 | +``` |
| 49 | +#### SPair-71K |
| 50 | +1. Download SPair-71K dataset from [link](https://cvlab.postech.ac.kr/research/SPair-71k/). After extraction, No more action required. |
| 51 | + |
| 52 | +## Setup |
| 53 | +1. Create symbol links to PF-Pascal, PF-Willow and SPair-71k dataset in `asset` directory. This can be done by: |
| 54 | +``` |
| 55 | +ln -s /your/path/to/pf-pascal asset/pf-pascal |
| 56 | +ln -s /your/path/to/pf-willow asset/pf-willow |
| 57 | +ln -s /your/path/to/SPair-71k asset/SPair-71k |
| 58 | +``` |
| 59 | +2. Create a directory named `sd4match` under `asset`. This is to save pre-computed features, checkpoints and learned prompts. |
| 60 | +``` |
| 61 | +# create sd4match directly |
| 62 | +mkdir asset/sd4match |
| 63 | +
|
| 64 | +# or create sd4match at anywhere you want and use symbol link |
| 65 | +ln -s /your/path/to/sd4match asset/sd4match |
| 66 | +``` |
| 67 | + |
| 68 | +3. Run `pre_compute_dino_feature.py`. This would pre-compute DINOv2 feature for all images in PF-Pascal, PF-Willow and SPair-71k and save them in `asset/sd4match`. The structure should be: |
| 69 | +``` |
| 70 | +sd4match |
| 71 | +└── asset |
| 72 | + └── DINOv2 |
| 73 | + ├── pfpascal |
| 74 | + | └── cached_output.pt |
| 75 | + ├── pfwillow |
| 76 | + | └── cached_output.pt |
| 77 | + └── spair |
| 78 | + └── cached_output.pt |
| 79 | +``` |
| 80 | + |
| 81 | +## Training |
| 82 | +The bash scripts for training are provided in `script` directory, and organized based on training data and prompt type. |
| 83 | + |
| 84 | +For example, to train `SD4Match-CPM` on SPair-71k dataset, run: |
| 85 | +``` |
| 86 | +cd script/spair |
| 87 | +sh sd4match_cpm.sh |
| 88 | +``` |
| 89 | +The batch size per GPU is currently set to `3`, which would take about `22G` GPU memory to train. Reduce the batch size if necessary. The training script will generate two directories in `asset/sd4match`: `log` and `prompt`. Tensorboard logs and training states are saved in `log`, and learned prompts are saved in `prompt`. For example, training `SD4Match-CPM` on SPair-71k dataset will generate: |
| 90 | +``` |
| 91 | +sd4match |
| 92 | +├── asset |
| 93 | +| ├── ... |
| 94 | +├── log |
| 95 | +| └── spair |
| 96 | +| └── CPM_spair_sd2-1_Pair-DINO-Feat-G25-C50_constant_lr0.01 |
| 97 | +| └── ...(Tensorboard log and training states) |
| 98 | +└── prompt |
| 99 | + └── CPM_spair_sd2-1_Pair-DINO-Feat-G25-C50 |
| 100 | + └── ckpt.pt |
| 101 | +``` |
| 102 | + |
| 103 | +## Testing |
| 104 | +To replicate our results reported in the paper on SPair-71k, either learning the prompt by yourself or downloading our pre-trained prompt and place them under `asset/sd4match/prompt` directory. Run: |
| 105 | +``` |
| 106 | +python test.py \ |
| 107 | +--dataset spair \ |
| 108 | +--prompt_type $PROMPT_NAME \ |
| 109 | +--timestep 50 \ |
| 110 | +--layer 1 |
| 111 | +``` |
| 112 | +Replace `$PROMPT_NAME` with prompt your want. It needs to have a corresponding directory under `asset/sd4match/prompt`. For example, to evaluate `SD4Match-CPM`, run: |
| 113 | +``` |
| 114 | +python test.py \ |
| 115 | +--dataset spair \ |
| 116 | +--prompt_type CPM_spair_sd2-1_Pair-DINO-Feat-G25-C50 \ |
| 117 | +--timestep 50 \ |
| 118 | +--layer 1 |
| 119 | +``` |
| 120 | + |
| 121 | +## Acknowledgement |
| 122 | +[Kai Han](https://www.kaihan.org/) is supported by Hong Kong Research |
| 123 | +Grant Council - Early Career Scheme (Grant No. 27208022), National Natural Science Foundation of |
| 124 | +China (Grant No. 62306251), and HKU Seed Fund for Basic Research. |
| 125 | + |
| 126 | +We also sincerely thank [Zirui Wang](https://scholar.google.com/citations?user=zCBKqa8AAAAJ&hl=en) for his inspiring discussion. |
| 127 | + |
| 128 | +## Citation |
| 129 | +``` |
| 130 | +@misc{li2023sd4match, |
| 131 | + title={SD4Match: Learning to Prompt Stable Diffusion Model for Semantic Matching}, |
| 132 | + author={Xinghui Li and Jingyi Lu and Kai Han and Victor Prisacariu}, |
| 133 | + year={2023}, |
| 134 | + eprint={2310.17569}, |
| 135 | + archivePrefix={arXiv}, |
| 136 | + primaryClass={cs.CV} |
| 137 | + } |
| 138 | +``` |
0 commit comments