mlpc-ucsd/TokenCompose

(CVPR 2024) 🧩 TokenCompose: Text-to-Image Diffusion with Token-level Supervision

Jupyter Notebook

137

30 commits

updated Dec 21, 2024

See the code

README

🧩 TokenCompose: Text-to-Image Diffusion with Token-level Supervision

Zirui Wang1, 3 · Zhizhou Sha2, 3 · Zheng Ding3 · Yilin Wang2, 3 · Zhuowen Tu3

1Princeton University · 2Tsinghua University · 3University of California, San Diego

CVPR 2024

Project done while Zirui Wang, Zhizhou Sha and Yilin Wang interned at UC San Diego.

Project Page | arXiv | X (Twitter)

Updates

If you use our method and/or model for your research project, we are happy to provide cross-reference here in the updates. :)

[04/04/2024] 🔥 Our training methodology is incorporated into CoMat which shows enhanced text-to-image attribute assignments.
[02/26/2024] 🔥 TokenCompose is accepted to CVPR 2024!
[02/20/2024] 🔥 TokenCompose is used as a base model from the RealCompo paper for enhanced compositionality.

https://github.com/mlpc-ucsd/TokenCompose/assets/59942464/93feea16-4eac-49c3-b286-ee390a325b17

A Stable Diffusion model finetuned with token-level consistency terms for enhanced multi-category instance composition and photorealism.


Logo
MethodMulti-category Instance CompositionPhotorealismEfficiency
Object AccuracyCOCOADE20KFID (COCO)FID (Flickr30K)Latency
MG2MG3MG4MG5MG2MG3MG4MG5
SD 1.429.8690.721.3350.740.8911.680.450.880.2189.810.4053.961.1416.521.131.890.3420.8871.467.540.17
Composable27.8363.330.5921.871.013.250.450.230.1869.610.9929.960.846.890.380.730.22-75.5713.810.15
Layout43.5993.220.6960.151.5819.490.882.270.4496.050.3467.830.9021.931.342.350.41-74.0018.890.20
Structured29.6490.401.0648.641.3210.710.920.680.2589.250.7253.051.2015.760.861.740.4921.1371.687.740.17
Attn-Exct45.1393.640.7665.101.2428.010.906.010.6191.740.4962.510.9426.120.785.890.40-71.6825.434.89
TokenCompose (Ours)52.1598.080.4076.161.0428.810.953.280.4897.750.3476.931.0933.921.476.210.6220.1971.137.560.14

🆕 Models

Stable Diffusion VersionCheckpoint 1Checkpoint 2
v1.4TokenCompose_SD14_ATokenCompose_SD14_B
v2.1TokenCompose_SD21_ATokenCompose_SD21_B

Our finetuned models do not contain any extra modules and can be directly used in a standard diffusion model library (e.g., HuggingFace's Diffusers) by replacing the pretrained U-Net with our finetuned U-Net in a plug-and-play manner. We provide a demo jupyter notebook which uses our model checkpoint to generate images.

You can also use the following code to download our checkpoints and generate images:

import torch
from diffusers import StableDiffusionPipeline

model_id = "mlpc-lab/TokenCompose_SD14_A"
device = "cuda"

pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
pipe = pipe.to(device)

prompt = "A cat and a wine glass"
image = pipe(prompt).images[0]  
    
image.save("cat_and_wine_glass.png")

📊 MultiGen

See MultiGen for details.

MethodCOCOADE20K
MG2MG3MG4MG5MG2MG3MG4MG5
SD 1.490.721.3350.740.8911.680.450.880.2189.810.4053.961.1416.521.131.890.34
Composable63.330.5921.871.013.250.450.230.1869.610.9929.960.846.890.380.730.22
Layout93.220.6960.151.5819.490.882.270.4496.050.3467.830.9021.931.342.350.41
Structured90.401.0648.641.3210.710.920.680.2589.250.7253.051.2015.760.861.740.49
Attn-Exct93.640.7665.101.2428.010.906.010.6191.740.4962.510.9426.120.785.890.40
Ours98.080.4076.161.0428.810.953.280.4897.750.3476.931.0933.921.476.210.62

💻 Environment Setup

For those who want to use our codebase to train your own diffusion models with token-level objectives, follow the below instructions:

conda create -n TokenCompose python=3.8.5
conda activate TokenCompose
conda install pytorch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 pytorch-cuda=11.7 -c pytorch -c nvidia
pip install -r requirements.txt

We have verified the environment setup using this specific package versions, but we expect that it will also work for newer versions too!

🛠️ Dataset Setup

If you want to use your own data, please refer to preprocess_data for details.

If you want to use our training data as examples or for research purposes, please follow the below instructions:

1. Setup the COCO Image Data

cd train/data
# download COCO train2017
wget http://images.cocodataset.org/zips/train2017.zip
unzip train2017.zip
rm train2017.zip
bash coco_data_setup.sh

After this step, you should have the following structure under the train/data directory:

train/data/
    coco_gsam_img/
        train/
            000000000142.jpg
            000000000370.jpg
            ...

2. Setup Token-wise Grounded Segmentation Maps

Download COCO segmentation data from Google Drive and put it under train/data directory.

After this step, you should have the following structure under the train/data directory:

train/data/
    coco_gsam_img/
        train/
            000000000142.jpg
            000000000370.jpg
            ...
    coco_gsam_seg.tar

Then, run the following command to unzip the segmentation data:

cd train/data
tar -xvf coco_gsam_seg.tar
rm coco_gsam_seg.tar

After the setup, you should have the following structure under the train/data directory:

train/data/
    coco_gsam_img/
        train/
            000000000142.jpg
            000000000370.jpg
            ...
    coco_gsam_seg/
        000000000142/
            mask_000000000142_bananas.png
            mask_000000000142_bread.png
            ...
        000000000370/
            mask_000000000370_bananas.png
            mask_000000000370_bread.png
            ...
        ...

📈 Training

We use wandb to log some curves and visualizations. Login to wandb before running the scripts.

wandb login

Then, to run TokenCompose, use the following command:

cd train
bash train.sh

The results will be saved under train/results directory.

🏷️ License

This repository is released under the Apache 2.0 license.

🙏 Acknowledgement

Our code is built upon diffusers, prompt-to-prompt, VISOR, Grounded-Segment-Anything, and CLIP. We thank all these authors for their nicely open sourced code and their great contributions to the community.

📝 Citation

If you find our work useful, please consider citing:

@InProceedings{Wang2024TokenCompose,
    author    = {Wang, Zirui and Sha, Zhizhou and Ding, Zheng and Wang, Yilin and Tu, Zhuowen},
    title     = {TokenCompose: Text-to-Image Diffusion with Token-level Supervision},
    booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
    month     = {June},
    year      = {2024},
    pages     = {8553-8564}
}
artificial-intelligence
computer-vision
diffusion-models
generative-ai
image-generation
latent-diffusion
machine-learning
multimodal
stable-diffusion
text-to-image

mlpc-ucsd/TokenCompose

(CVPR 2024) 🧩 TokenCompose: Text-to-Image Diffusion with Token-level Supervision

Jupyter Notebook

137

30 commits

updated Dec 21, 2024

See the code

README

🧩 TokenCompose: Text-to-Image Diffusion with Token-level Supervision

Zirui Wang1, 3 · Zhizhou Sha2, 3 · Zheng Ding3 · Yilin Wang2, 3 · Zhuowen Tu3

1Princeton University · 2Tsinghua University · 3University of California, San Diego

CVPR 2024

Project done while Zirui Wang, Zhizhou Sha and Yilin Wang interned at UC San Diego.

Project Page | arXiv | X (Twitter)

Updates

If you use our method and/or model for your research project, we are happy to provide cross-reference here in the updates. :)

[04/04/2024] 🔥 Our training methodology is incorporated into CoMat which shows enhanced text-to-image attribute assignments.
[02/26/2024] 🔥 TokenCompose is accepted to CVPR 2024!
[02/20/2024] 🔥 TokenCompose is used as a base model from the RealCompo paper for enhanced compositionality.

https://github.com/mlpc-ucsd/TokenCompose/assets/59942464/93feea16-4eac-49c3-b286-ee390a325b17

A Stable Diffusion model finetuned with token-level consistency terms for enhanced multi-category instance composition and photorealism.


Logo
MethodMulti-category Instance CompositionPhotorealismEfficiency
Object AccuracyCOCOADE20KFID (COCO)FID (Flickr30K)Latency
MG2MG3MG4MG5MG2MG3MG4MG5
SD 1.429.8690.721.3350.740.8911.680.450.880.2189.810.4053.961.1416.521.131.890.3420.8871.467.540.17
Composable27.8363.330.5921.871.013.250.450.230.1869.610.9929.960.846.890.380.730.22-75.5713.810.15
Layout43.5993.220.6960.151.5819.490.882.270.4496.050.3467.830.9021.931.342.350.41-74.0018.890.20
Structured29.6490.401.0648.641.3210.710.920.680.2589.250.7253.051.2015.760.861.740.4921.1371.687.740.17
Attn-Exct45.1393.640.7665.101.2428.010.906.010.6191.740.4962.510.9426.120.785.890.40-71.6825.434.89
TokenCompose (Ours)52.1598.080.4076.161.0428.810.953.280.4897.750.3476.931.0933.921.476.210.6220.1971.137.560.14

🆕 Models

Stable Diffusion VersionCheckpoint 1Checkpoint 2
v1.4TokenCompose_SD14_ATokenCompose_SD14_B
v2.1TokenCompose_SD21_ATokenCompose_SD21_B

Our finetuned models do not contain any extra modules and can be directly used in a standard diffusion model library (e.g., HuggingFace's Diffusers) by replacing the pretrained U-Net with our finetuned U-Net in a plug-and-play manner. We provide a demo jupyter notebook which uses our model checkpoint to generate images.

You can also use the following code to download our checkpoints and generate images:

import torch
from diffusers import StableDiffusionPipeline

model_id = "mlpc-lab/TokenCompose_SD14_A"
device = "cuda"

pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
pipe = pipe.to(device)

prompt = "A cat and a wine glass"
image = pipe(prompt).images[0]  
    
image.save("cat_and_wine_glass.png")

📊 MultiGen

See MultiGen for details.

MethodCOCOADE20K
MG2MG3MG4MG5MG2MG3MG4MG5
SD 1.490.721.3350.740.8911.680.450.880.2189.810.4053.961.1416.521.131.890.34
Composable63.330.5921.871.013.250.450.230.1869.610.9929.960.846.890.380.730.22
Layout93.220.6960.151.5819.490.882.270.4496.050.3467.830.9021.931.342.350.41
Structured90.401.0648.641.3210.710.920.680.2589.250.7253.051.2015.760.861.740.49
Attn-Exct93.640.7665.101.2428.010.906.010.6191.740.4962.510.9426.120.785.890.40
Ours98.080.4076.161.0428.810.953.280.4897.750.3476.931.0933.921.476.210.62

💻 Environment Setup

For those who want to use our codebase to train your own diffusion models with token-level objectives, follow the below instructions:

conda create -n TokenCompose python=3.8.5
conda activate TokenCompose
conda install pytorch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 pytorch-cuda=11.7 -c pytorch -c nvidia
pip install -r requirements.txt

We have verified the environment setup using this specific package versions, but we expect that it will also work for newer versions too!

🛠️ Dataset Setup

If you want to use your own data, please refer to preprocess_data for details.

If you want to use our training data as examples or for research purposes, please follow the below instructions:

1. Setup the COCO Image Data

cd train/data
# download COCO train2017
wget http://images.cocodataset.org/zips/train2017.zip
unzip train2017.zip
rm train2017.zip
bash coco_data_setup.sh

After this step, you should have the following structure under the train/data directory:

train/data/
    coco_gsam_img/
        train/
            000000000142.jpg
            000000000370.jpg
            ...

2. Setup Token-wise Grounded Segmentation Maps

Download COCO segmentation data from Google Drive and put it under train/data directory.

After this step, you should have the following structure under the train/data directory:

train/data/
    coco_gsam_img/
        train/
            000000000142.jpg
            000000000370.jpg
            ...
    coco_gsam_seg.tar

Then, run the following command to unzip the segmentation data:

cd train/data
tar -xvf coco_gsam_seg.tar
rm coco_gsam_seg.tar

After the setup, you should have the following structure under the train/data directory:

train/data/
    coco_gsam_img/
        train/
            000000000142.jpg
            000000000370.jpg
            ...
    coco_gsam_seg/
        000000000142/
            mask_000000000142_bananas.png
            mask_000000000142_bread.png
            ...
        000000000370/
            mask_000000000370_bananas.png
            mask_000000000370_bread.png
            ...
        ...

📈 Training

We use wandb to log some curves and visualizations. Login to wandb before running the scripts.

wandb login

Then, to run TokenCompose, use the following command:

cd train
bash train.sh

The results will be saved under train/results directory.

🏷️ License

This repository is released under the Apache 2.0 license.

🙏 Acknowledgement

Our code is built upon diffusers, prompt-to-prompt, VISOR, Grounded-Segment-Anything, and CLIP. We thank all these authors for their nicely open sourced code and their great contributions to the community.

📝 Citation

If you find our work useful, please consider citing:

@InProceedings{Wang2024TokenCompose,
    author    = {Wang, Zirui and Sha, Zhizhou and Ding, Zheng and Wang, Yilin and Tu, Zhuowen},
    title     = {TokenCompose: Text-to-Image Diffusion with Token-level Supervision},
    booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
    month     = {June},
    year      = {2024},
    pages     = {8553-8564}
}
artificial-intelligence
computer-vision
diffusion-models
generative-ai
image-generation
latent-diffusion
machine-learning
multimodal
stable-diffusion
text-to-image