Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
4746498
VisualCloze
lzyhha Apr 14, 2025
6507b60
Merge branch 'main' of https://github.com/lzyhha/diffusers into main
lzyhha Apr 20, 2025
fbf534e
style quality
lzyhha Apr 21, 2025
c2f5771
add docs
lzyhha Apr 21, 2025
4a5dbe2
add docs
lzyhha Apr 21, 2025
7c6b26c
Merge branch 'main' into main
lzyhha Apr 21, 2025
97943bb
typo
lzyhha Apr 21, 2025
d1459d5
Merge branch 'main' of https://github.com/lzyhha/diffusers
lzyhha Apr 21, 2025
b93152f
Update docs/source/en/api/pipelines/visualcloze.md
lzyhha Apr 22, 2025
c70520c
delete einops
lzyhha Apr 22, 2025
7fe072b
style quality
lzyhha Apr 22, 2025
c98b148
Merge branch 'main' into main
sayakpaul Apr 22, 2025
8a6f7b6
Update src/diffusers/pipelines/visualcloze/pipeline_visualcloze.py
lzyhha Apr 22, 2025
0ef118c
reorg
lzyhha Apr 23, 2025
206d531
Merge branch 'main' of https://github.com/lzyhha/diffusers into main
lzyhha Apr 23, 2025
cef2e24
refine doc
lzyhha Apr 23, 2025
060abe8
style quality
lzyhha Apr 23, 2025
0ea3343
typo
lzyhha Apr 23, 2025
94e7d87
Merge branch 'main' into main
lzyhha Apr 23, 2025
c420015
typo
lzyhha Apr 23, 2025
72e021f
Update src/diffusers/image_processor.py
lzyhha Apr 24, 2025
0973942
add comment
lzyhha Apr 24, 2025
ba3a528
test
lzyhha Apr 24, 2025
73f95d0
style
lzyhha Apr 24, 2025
415aadb
Modified based on review
lzyhha Apr 25, 2025
6666dd7
style
lzyhha Apr 25, 2025
adea49d
restore image_processor
lzyhha Apr 25, 2025
3a016ab
Merge branch 'main' into main
lzyhha Apr 25, 2025
7378e28
update example url
lzyhha Apr 28, 2025
01330a0
style
lzyhha Apr 28, 2025
e8f90b8
Merge branch 'main' of https://github.com/lzyhha/diffusers into main
lzyhha Apr 28, 2025
9dac423
Merge branch 'main' into main
lzyhha Apr 28, 2025
096a2da
fix-copies
lzyhha Apr 28, 2025
74f2752
VisualClozeGenerationPipeline
lzyhha Apr 30, 2025
63ca13b
combine
lzyhha Apr 30, 2025
2f570a0
tests docs
lzyhha Apr 30, 2025
276b452
remove VisualClozeUpsamplingPipeline
lzyhha Apr 30, 2025
0e12e05
style
lzyhha Apr 30, 2025
b7e43a6
quality
lzyhha Apr 30, 2025
8039e1b
test examples
lzyhha Apr 30, 2025
50cfa58
quality style
lzyhha May 1, 2025
4f6aa77
Merge branch 'main' into main
lzyhha May 1, 2025
5f00185
typo
lzyhha May 2, 2025
9de1e78
make fix-copies
lzyhha May 2, 2025
bb56663
Merge branch 'main' into main
sayakpaul May 3, 2025
0cb652a
fix test_callback_cfg and test_save_load_dduf in VisualClozePipelineF…
lzyhha May 5, 2025
3849692
Merge branch 'main' of https://github.com/lzyhha/diffusers
lzyhha May 5, 2025
26750b1
add EXAMPLE_DOC_STRING to VisualClozeGenerationPipeline
lzyhha May 6, 2025
0ca04f6
delete maybe_free_model_hooks from pipeline_visualcloze_combined
lzyhha May 8, 2025
92132b0
Apply suggestions from code review
a-r-r-o-w May 12, 2025
07f5040
Merge branch 'main' into main
a-r-r-o-w May 12, 2025
9abe8b4
fix test_save_load_local test; add reason for skipping cfg test
a-r-r-o-w May 12, 2025
10de9b4
more save_load test fixes
a-r-r-o-w May 12, 2025
559d79a
fix tests in generation pipeline tests
a-r-r-o-w May 12, 2025
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: 2 additions & 0 deletions docs/source/en/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,8 @@
title: UniDiffuser
- local: api/pipelines/value_guided_sampling
title: Value-guided sampling
- local: api/pipelines/visualcloze
title: VisualCloze
- local: api/pipelines/wan
title: Wan
- local: api/pipelines/wuerstchen
Expand Down
1 change: 1 addition & 0 deletions docs/source/en/api/pipelines/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ The table below lists all the pipelines currently available in 🤗 Diffusers an
| [UniDiffuser](unidiffuser) | text2image, image2text, image variation, text variation, unconditional image generation, unconditional audio generation |
| [Value-guided planning](value_guided_sampling) | value guided sampling |
| [Wuerstchen](wuerstchen) | text2image |
| [VisualCloze](visualcloze) | text2image, image2image, subject driven generation, inpainting, style transfer, image restoration, image editing, [depth,normal,edge,pose]2image, [depth,normal,edge,pose]-estimation, virtual try-on, image relighting |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc: @stevhliu do you think it's alright?


## DiffusionPipeline

Expand Down
200 changes: 200 additions & 0 deletions docs/source/en/api/pipelines/visualcloze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<!--Copyright 2025 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-->

# VisualCloze

[VisualCloze: A Universal Image Generation Framework via Visual In-Context Learning](https://arxiv.org/abs/2504.07960) is an in-context learning based universal image generation framework that can 1) support various in-domain tasks, 2) generalize to unseen tasks through in-context learning, 3) unify multiple tasks into one step and generate both target image and intermediate results, and 4) support reverse-engineering a set of conditions from a target image.

The abstract from the paper is:

*Recent progress in diffusion models significantly advances various image generation tasks. However, the current mainstream approach remains focused on building task-specific models, which have limited efficiency when supporting a wide range of different needs. While universal models attempt to address this limitation, they face critical challenges, including generalizable task instruction, appropriate task distributions, and unified architectural design. To tackle these challenges, we propose VisualCloze, a universal image generation framework, which supports a wide range of in-domain tasks, generalization to unseen ones, unseen unification of multiple tasks, and reverse generation. Unlike existing methods that rely on language-based task instruction, leading to task ambiguity and weak generalization, we integrate visual in-context learning, allowing models to identify tasks from visual demonstrations. Meanwhile, the inherent sparsity of visual task distributions hampers the learning of transferable knowledge across tasks. To this end, we introduce Graph200K, a graph-structured dataset that establishes various interrelated tasks, enhancing task density and transferable knowledge. Furthermore, we uncover that our unified image generation formulation shared a consistent objective with image infilling, enabling us to leverage the strong generative priors of pre-trained infilling models without modifying the architectures. The codes, dataset, and models are available at https://visualcloze.github.io.*

## Inference

### Model loading

VisualCloze releases two models suitable for diffusers, i.e., VisualClozePipeline-384 and VisualClozePipeline-512, which are trained with resolutions of 384 and 512, respectively.
The resolution means that each image is resized to the area of the square of it before concatenating images into a grid layout.
In this case, VisualCloze uses [SDEdit](https://arxiv.org/abs/2108.01073) to upsample the generated images.
```python
import torch
from diffusers import VisualClozePipeline

pipe = VisualClozePipeline.from_pretrained("VisualCloze/VisualClozePipeline-384", resolution=384, torch_dtype=torch.bfloat16)
pipe.to("cuda")
```

### Input prompts
VisualCloze supports a wide variety of tasks. You need to pass a task prompt to describe the intention of the generation task, and optionally, a content prompt to describe the caption of the image to be generated. When the content prompt is not needed, None should also be passed.

### Input images

The input image should be a List[List[Image|None]]. Excluding the last row, each row represents an in-context example. The last row represents the current query, where the image to be generated is set to None.
When using batch inference, the input images should be a List[List[List[Image|None]]], and the input prompts should be a List[str|None].

### Resolution

By default, the model first generates an image with a resolution of ${model.resolution}^2$, and then upsamples it by a factor of three. You can try setting the `upsampling_height` and `upsampling_width` parameters to generate images with different size.


### Examples


More examples covering a wide range of tasks can be found in the [Online Demo](https://huggingface.co/spaces/VisualCloze/VisualCloze) and [Github Repo](https://github.com/lzyhha/VisualCloze).
Here, the document shows simple examples for mask2image, edge-detection, and subject-driven generation.

#### mask2image

```python

# Load in-context images (make sure the paths are correct and accessible)
image_paths = [
# in-context examples
[
load_image('https://huggingface.co/VisualCloze/VisualCloze/resolve/main/examples/2c4e256fa512cb7e7f433f4c7f9101de_sam2_mask.jpg'),
load_image('https://huggingface.co/VisualCloze/VisualCloze/resolve/main/examples/2c4e256fa512cb7e7f433f4c7f9101de.jpg'),
],
# query with the target image
[
load_image('https://huggingface.co/VisualCloze/VisualCloze/resolve/main/examples/9c565b1aad76b22f5bb836744a93561a_sam2_mask.jpg'),
None, # No image needed for the target image
],
]

# Task and content prompt
task_prompt = "In each row, a logical task is demonstrated to achieve [IMAGE2] an aesthetically pleasing photograph based on [IMAGE1] sam 2-generated masks with rich color coding."
content_prompt = """Majestic photo of a golden eagle perched on a rocky outcrop in a mountainous landscape.
The eagle is positioned in the right foreground, facing left, with its sharp beak and keen eyes prominently visible.
Its plumage is a mix of dark brown and golden hues, with intricate feather details.
The background features a soft-focus view of snow-capped mountains under a cloudy sky, creating a serene and grandiose atmosphere.
The foreground includes rugged rocks and patches of green moss. Photorealistic, medium depth of field,
soft natural lighting, cool color palette, high contrast, sharp focus on the eagle, blurred background,
tranquil, majestic, wildlife photography."""

# Run the pipeline
image_result = pipe(
task_prompt=task_prompt,
content_prompt=content_prompt,
image=image_paths,
upsampling_width=1344,
upsampling_height=768,
upsampling_strength=0.4,
guidance_scale=30,
num_inference_steps=30,
max_sequence_length=512,
generator=torch.Generator("cpu").manual_seed(0)
).images[0][0]

# Save the resulting image
image_result.save("visualcloze.png")
```

#### Example for edge-detection

```python
# Load in-context images (make sure the paths are correct and accessible)
image_paths = [
# in-context examples
[
load_image('https://huggingface.co/VisualCloze/VisualCloze/resolve/main/examples/de5a8b250bf407aa7e04913562dcba90.jpg'),
load_image('https://huggingface.co/VisualCloze/VisualCloze/resolve/main/examples/de5a8b250bf407aa7e04913562dcba90_hed_512.jpg'),
],
[
load_image('https://huggingface.co/VisualCloze/VisualCloze/resolve/main/examples/5bf755ed9dbb9b3e223e7ba35232b06e.jpg'),
load_image('https://huggingface.co/VisualCloze/VisualCloze/resolve/main/examples/5bf755ed9dbb9b3e223e7ba35232b06e_hed_512.jpg'),
],
# query with the target image
[
load_image('https://huggingface.co/VisualCloze/VisualCloze/resolve/main/examples/53b3f413257bee9e499b823b44623b1a.jpg'),
None, # No image needed for the target image
],
]

# Task and content prompt
task_prompt = "Each row illustrates a pathway from [IMAGE1] a sharp and beautifully composed photograph to [IMAGE2] edge map with natural well-connected outlines using a clear logical task."
content_prompt = ""

# Run the pipeline
image_result = pipe(
task_prompt=task_prompt,
content_prompt=content_prompt,
image=image_paths,
upsampling_width=864,
upsampling_height=1152,
upsampling_strength=0.4,
guidance_scale=30,
num_inference_steps=30,
max_sequence_length=512,
generator=torch.Generator("cpu").manual_seed(0)
).images[0][0]

# Save the resulting image
image_result.save("visualcloze.png")
```

#### Example for subject-driven generation

```python
# Load in-context images (make sure the paths are correct and accessible)
image_paths = [
# in-context examples
[
load_image('https://huggingface.co/VisualCloze/VisualCloze/resolve/main/examples/data-00004-of-00022-7170_reference.jpg'),
load_image('https://huggingface.co/VisualCloze/VisualCloze/resolve/main/examples/data-00004-of-00022-7170_depth-anything-v2_Large.jpg'),
load_image('https://huggingface.co/VisualCloze/VisualCloze/resolve/main/examples/data-00004-of-00022-7170_target.jpg'),
],
[
load_image('https://huggingface.co/VisualCloze/VisualCloze/resolve/main/examples/data-00013-of-00022-4696_reference.jpg'),
load_image('https://huggingface.co/VisualCloze/VisualCloze/resolve/main/examples/data-00013-of-00022-4696_depth-anything-v2_Large.jpg'),
load_image('https://huggingface.co/VisualCloze/VisualCloze/resolve/main/examples/data-00013-of-00022-4696_target.jpg'),
],
# query with the target image
[
load_image('https://huggingface.co/VisualCloze/VisualCloze/resolve/main/examples/data-00005-of-00022-4396_reference.jpg'),
load_image('https://huggingface.co/VisualCloze/VisualCloze/resolve/main/examples/data-00005-of-00022-4396_depth-anything-v2_Large.jpg'),
None, # No image needed for the target image
],
]

# Task and content prompt
task_prompt = """Each row describes a process that begins with [IMAGE1] an image containing the key object,
[IMAGE2] depth map revealing gray-toned spatial layers and results in
[IMAGE3] an image with artistic qualitya high-quality image with exceptional detail."""
content_prompt = """A vintage porcelain collector's item. Beneath a blossoming cherry tree in early spring,
this treasure is photographed up close, with soft pink petals drifting through the air and vibrant blossoms framing the scene."""

# Run the pipeline
image_result = pipe(
task_prompt=task_prompt,
content_prompt=content_prompt,
image=image_paths,
upsampling_width=1024,
upsampling_height=1024,
upsampling_strength=0.2,
guidance_scale=30,
num_inference_steps=30,
max_sequence_length=512,
generator=torch.Generator("cpu").manual_seed(0)
).images[0][0]

# Save the resulting image
image_result.save("visualcloze.png")
```

## VisualClozePipeline

[[autodoc]] VisualClozePipeline
- all
- __call__
2 changes: 2 additions & 0 deletions src/diffusers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@
"VersatileDiffusionPipeline",
"VersatileDiffusionTextToImagePipeline",
"VideoToVideoSDPipeline",
"VisualClozePipeline",
"VQDiffusionPipeline",
"WanImageToVideoPipeline",
"WanPipeline",
Expand Down Expand Up @@ -1086,6 +1087,7 @@
VersatileDiffusionPipeline,
VersatileDiffusionTextToImagePipeline,
VideoToVideoSDPipeline,
VisualClozePipeline,
VQDiffusionPipeline,
WanImageToVideoPipeline,
WanPipeline,
Expand Down
Loading