Skip to content

[Feature] Support Kandinsky v2.2 #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM nvcr.io/nvidia/pytorch:23.11-py3
FROM nvcr.io/nvidia/pytorch:23.12-py3

RUN apt update -y && apt install -y \
git tmux
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ For detailed user guides and advanced guides, please refer to our [Documentation
- [Run PixArt-α](https://diffengine.readthedocs.io/en/latest/run_guides/run_pixart_alpha.html)
- [Run PixArt-α LoRA](https://diffengine.readthedocs.io/en/latest/run_guides/run_pixart_alpha_lora.html)
- [Run PixArt-α DreamBooth](https://diffengine.readthedocs.io/en/latest/run_guides/run_pixart_alpha_dreambooth.html)
- - [Run Kandinsky 2.2](https://diffengine.readthedocs.io/en/latest/run_guides/run_kandinsky_v22.html)
- [Inference](https://diffengine.readthedocs.io/en/latest/run_guides/inference.html)

</details>
Expand Down Expand Up @@ -248,6 +249,9 @@ For detailed user guides and advanced guides, please refer to our [Documentation
<td>
<b>PixArt-α</b>
</td>
<td>
<b>Kandinsky</b>
</td>
</tr>
<tr valign="top">
<td>
Expand All @@ -269,6 +273,11 @@ For detailed user guides and advanced guides, please refer to our [Documentation
<li><a href="configs/pixart_alpha_dreambooth/README.md">DreamBooth (CVPR'2023)</a></li>
</ul>
</td>
<td>
<ul>
<li><a href="configs/kandinsky_v22/README.md">Kandinsky 2.2 (2023)</a></li>
</ul>
</td>
</tr>
</td>
</tr>
Expand Down
30 changes: 30 additions & 0 deletions configs/_base_/datasets/pokemon_blip_kandinsky_decoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
train_pipeline = [
dict(type="CLIPImageProcessor",
pretrained="kandinsky-community/kandinsky-2-2-prior"),
dict(type="torchvision/Resize", size=768, interpolation="bicubic"),
dict(type="RandomCrop", size=768),
dict(type="RandomHorizontalFlip", p=0.5),
dict(type="torchvision/ToTensor"),
dict(type="torchvision/Normalize", mean=[0.5], std=[0.5]),
dict(type="PackInputs", input_keys=["img", "text", "clip_img"]),
]
train_dataloader = dict(
batch_size=4,
num_workers=4,
dataset=dict(
type="HFDataset",
dataset="lambdalabs/pokemon-blip-captions",
pipeline=train_pipeline),
sampler=dict(type="DefaultSampler", shuffle=True),
)

val_dataloader = None
val_evaluator = None
test_dataloader = val_dataloader
test_evaluator = val_evaluator

custom_hooks = [
dict(type="VisualizationHook", prompt=["yoda pokemon"] * 4,
height=768, width=768),
dict(type="SDCheckpointHook"),
]
25 changes: 25 additions & 0 deletions configs/_base_/datasets/pokemon_blip_kandinsky_prior.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
train_pipeline = [
dict(type="CLIPImageProcessor", output_key="img",
pretrained="kandinsky-community/kandinsky-2-2-prior"),
dict(type="PackInputs"),
]
train_dataloader = dict(
batch_size=4,
num_workers=4,
dataset=dict(
type="HFDataset",
dataset="lambdalabs/pokemon-blip-captions",
pipeline=train_pipeline),
sampler=dict(type="DefaultSampler", shuffle=True),
)

val_dataloader = None
val_evaluator = None
test_dataloader = val_dataloader
test_evaluator = val_evaluator

custom_hooks = [
dict(type="VisualizationHook", prompt=["yoda pokemon"] * 4,
height=512, width=512),
dict(type="PriorSaveHook"),
]
2 changes: 1 addition & 1 deletion configs/_base_/datasets/pokemon_blip_wuerstchen.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
custom_hooks = [
dict(type="VisualizationHook", prompt=["A robot pokemon, 4k photo"] * 4,
height=768, width=768),
dict(type="WuerstchenSaveHook"),
dict(type="PriorSaveHook"),
]
4 changes: 4 additions & 0 deletions configs/_base_/models/kandinsky_v22_decoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
model = dict(
type="KandinskyV22Decoder",
decoder_model="kandinsky-community/kandinsky-2-2-decoder",
prior_model="kandinsky-community/kandinsky-2-2-prior")
4 changes: 4 additions & 0 deletions configs/_base_/models/kandinsky_v22_prior.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
model = dict(
type="KandinskyV22Prior",
decoder_model="kandinsky-community/kandinsky-2-2-decoder",
prior_model="kandinsky-community/kandinsky-2-2-prior")
72 changes: 72 additions & 0 deletions configs/kandinsky_v22/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Kandinsky 2.2

[Kandinsky 2.2](https://habr.com/ru/companies/sberbank/articles/747446/)

## Abstract

Kandinsky 2.2 brings substantial improvements upon its predecessor, Kandinsky 2.1, by introducing a new, more powerful image encoder - CLIP-ViT-G and the ControlNet support. The switch to CLIP-ViT-G as the image encoder significantly increases the model’s capability to generate more aesthetic pictures and better understand text, thus enhancing the model’s overall performance. The addition of the ControlNet mechanism allows the model to effectively control the process of generating images. This leads to more accurate and visually appealing outputs and opens new possibilities for text-guided image manipulation.

<div align=center>
<img src="https://github.com/okotaku/diffengine/assets/24734142/b07d82fb-4c2c-4216-a4b1-a64b278cee2a"/>
</div>

## Citation

```
```

## Run Training

Run Training

```
# single gpu
$ mim train diffengine ${CONFIG_FILE}
# multi gpus
$ mim train diffengine ${CONFIG_FILE} --gpus 2 --launcher pytorch

# Example.
$ mim train diffengine configs/kandinsky_v22/kandinsky_v22_prior_pokemon_blip.py
```

## Inference prior with diffusers

Once you have trained a model, specify the path to the saved model and utilize it for inference using the `diffusers.pipeline` module.

```py
import torch
from diffusers import AutoPipelineForText2Image, PriorTransformer

prompt = 'yoda pokemon'
checkpoint = 'work_dirs/kandinsky_v22_prior_pokemon_blip/step10450'

prior = PriorTransformer.from_pretrained(
checkpoint, subfolder="prior",
)
pipe = AutoPipelineForText2Image.from_pretrained(
"kandinsky-community/kandinsky-2-2-decoder",
prior_prior=prior,
torch_dtype=torch.float32,
)
pipe.to('cuda')

image = pipe(
prompt,
num_inference_steps=50,
width=512,
height=512,
).images[0]
image.save('demo.png')
```

You can see more details on [`docs/source/run_guides/run_kandinsky_v22.md`](../../docs/source/run_guides/run_kandinsky_v22.md#inference-with-diffusers).

## Results Example

#### kandinsky_v22_prior_pokemon_blip

![example1](https://github.com/okotaku/diffengine/assets/24734142/b709f558-5c03-4235-98d7-fe1c663182b8)

#### kandinsky_v22_decoder_pokemon_blip

![example1](https://github.com/okotaku/diffengine/assets/24734142/6c9cce50-9f31-4637-9933-27697d65c830)
6 changes: 6 additions & 0 deletions configs/kandinsky_v22/kandinsky_v22_decoder_pokemon_blip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
_base_ = [
"../_base_/models/kandinsky_v22_decoder.py",
"../_base_/datasets/pokemon_blip_kandinsky_decoder.py",
"../_base_/schedules/stable_diffusion_50e.py",
"../_base_/default_runtime.py",
]
6 changes: 6 additions & 0 deletions configs/kandinsky_v22/kandinsky_v22_prior_pokemon_blip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
_base_ = [
"../_base_/models/kandinsky_v22_prior.py",
"../_base_/datasets/pokemon_blip_kandinsky_prior.py",
"../_base_/schedules/stable_diffusion_50e.py",
"../_base_/default_runtime.py",
]
9 changes: 7 additions & 2 deletions diffengine/datasets/transforms/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,15 @@ class CLIPImageProcessor(BaseTransform):
results. Defaults to 'clip_img'.
"""

def __init__(self, key: str = "img", output_key: str = "clip_img") -> None:
def __init__(self, key: str = "img", output_key: str = "clip_img",
pretrained: str | None = None) -> None:
self.key = key
self.output_key = output_key
self.pipeline = HFCLIPImageProcessor()
if pretrained is None:
self.pipeline = HFCLIPImageProcessor()
else:
self.pipeline = HFCLIPImageProcessor.from_pretrained(
pretrained, subfolder="image_processor")

def transform(self, results: dict) -> dict | tuple[list, list] | None:
"""Transform.
Expand Down
4 changes: 2 additions & 2 deletions diffengine/engine/hooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from .lcm_ema_update_hook import LCMEMAUpdateHook
from .peft_save_hook import PeftSaveHook
from .pixart_checkpoint_hook import PixArtCheckpointHook
from .prior_save_hook import PriorSaveHook
from .sd_checkpoint_hook import SDCheckpointHook
from .t2i_adapter_save_hook import T2IAdapterSaveHook
from .unet_ema_hook import UnetEMAHook
from .visualization_hook import VisualizationHook
from .wuerstchen_save_hook import WuerstchenSaveHook

__all__ = [
"VisualizationHook",
Expand All @@ -21,7 +21,7 @@
"T2IAdapterSaveHook",
"CompileHook",
"FastNormHook",
"WuerstchenSaveHook",
"PriorSaveHook",
"LCMEMAUpdateHook",
"PixArtCheckpointHook",
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@


@HOOKS.register_module()
class WuerstchenSaveHook(Hook):
"""Wuerstchen Save Hook.
class PriorSaveHook(Hook):
"""Prior Save Hook.

Save Wuerstchen weights with diffusers format and pick up Wuerstchen
weights from checkpoint.
Save Prior weights with diffusers format and pick up Prior weights from
checkpoint.
"""

priority = "VERY_LOW"
Expand All @@ -30,7 +30,8 @@ def before_save_checkpoint(self, runner, checkpoint: dict) -> None:
model = model.module
ckpt_path = osp.join(runner.work_dir, f"step{runner.iter}")
model.prior.save_pretrained(osp.join(ckpt_path, "prior"))
if model.finetune_text_encoder:
if hasattr(
model, "finetune_text_encoder") and model.finetune_text_encoder:
model.text_encoder.save_pretrained(
osp.join(ckpt_path, "text_encoder"))

Expand Down
1 change: 1 addition & 0 deletions diffengine/models/editors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .esd import * # noqa: F403
from .instruct_pix2pix import * # noqa: F403
from .ip_adapter import * # noqa: F403
from .kandinsky import * # noqa: F403
from .lcm import * # noqa: F403
from .pixart_alpha import * # noqa: F403
from .ssd_1b import * # noqa: F403
Expand Down
1 change: 1 addition & 0 deletions diffengine/models/editors/deepfloyd_if/deepfloyd_if.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class DeepFloydIF(BaseModel):
training. Choose between 'epsilon' or 'v_prediction' or leave
`None`. If left to `None` the default prediction type of the
scheduler: `noise_scheduler.config.prediciton_type` is chosen.
Defaults to None.
data_preprocessor (dict, optional): The pre-process config of
:class:`SDDataPreprocessor`.
noise_generator (dict, optional): The noise generator config.
Expand Down
6 changes: 6 additions & 0 deletions diffengine/models/editors/kandinsky/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .kandinskyv22_decoder import KandinskyV22Decoder
from .kandinskyv22_decoder_preprocessor import KandinskyV22DecoderDataPreprocessor
from .kandinskyv22_prior import KandinskyV22Prior

__all__ = ["KandinskyV22Prior", "KandinskyV22Decoder",
"KandinskyV22DecoderDataPreprocessor"]
Loading