Skip to content

Commit 83709d5

Browse files
affromeroaffromerosayakpaulhlky
authored
Flux Control(Depth/Canny) + Inpaint (#10192)
* flux_control_inpaint - failing test_flux_different_prompts * removing test_flux_different_prompts? * fix style * fix from PR comments * fix style * reducing guidance_scale in demo * Update src/diffusers/pipelines/flux/pipeline_flux_control_inpaint.py Co-authored-by: hlky <[email protected]> * make * prepare_latents is not copied from * update docs * typos --------- Co-authored-by: affromero <[email protected]> Co-authored-by: Sayak Paul <[email protected]> Co-authored-by: hlky <[email protected]>
1 parent 8eb73c8 commit 83709d5

File tree

8 files changed

+1468
-0
lines changed

8 files changed

+1468
-0
lines changed

docs/source/en/_toctree.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@
400400
title: DiT
401401
- local: api/pipelines/flux
402402
title: Flux
403+
- local: api/pipelines/control_flux_inpaint
404+
title: FluxControlInpaint
403405
- local: api/pipelines/hunyuandit
404406
title: Hunyuan-DiT
405407
- local: api/pipelines/hunyuan_video
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<!--Copyright 2024 The HuggingFace Team, The Black Forest Team. All rights reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
4+
the License. You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
specific language governing permissions and limitations under the License.
11+
-->
12+
13+
# FluxControlInpaint
14+
15+
FluxControlInpaintPipeline is an implementation of Inpainting for Flux.1 Depth/Canny models. It is a pipeline that allows you to inpaint images using the Flux.1 Depth/Canny models. The pipeline takes an image and a mask as input and returns the inpainted image.
16+
17+
FLUX.1 Depth and Canny [dev] is a 12 billion parameter rectified flow transformer capable of generating an image based on a text description while following the structure of a given input image. **This is not a ControlNet model**.
18+
19+
| Control type | Developer | Link |
20+
| -------- | ---------- | ---- |
21+
| Depth | [Black Forest Labs](https://huggingface.co/black-forest-labs) | [Link](https://huggingface.co/black-forest-labs/FLUX.1-Depth-dev) |
22+
| Canny | [Black Forest Labs](https://huggingface.co/black-forest-labs) | [Link](https://huggingface.co/black-forest-labs/FLUX.1-Canny-dev) |
23+
24+
25+
<Tip>
26+
27+
Flux can be quite expensive to run on consumer hardware devices. However, you can perform a suite of optimizations to run it faster and in a more memory-friendly manner. Check out [this section](https://huggingface.co/blog/sd3#memory-optimizations-for-sd3) for more details. Additionally, Flux can benefit from quantization for memory efficiency with a trade-off in inference latency. Refer to [this blog post](https://huggingface.co/blog/quanto-diffusers) to learn more. For an exhaustive list of resources, check out [this gist](https://gist.github.com/sayakpaul/b664605caf0aa3bf8585ab109dd5ac9c).
28+
29+
</Tip>
30+
31+
```python
32+
import torch
33+
from diffusers import FluxControlInpaintPipeline
34+
from diffusers.models.transformers import FluxTransformer2DModel
35+
from transformers import T5EncoderModel
36+
from diffusers.utils import load_image, make_image_grid
37+
from image_gen_aux import DepthPreprocessor # https://github.com/huggingface/image_gen_aux
38+
from PIL import Image
39+
import numpy as np
40+
41+
pipe = FluxControlInpaintPipeline.from_pretrained(
42+
"black-forest-labs/FLUX.1-Depth-dev",
43+
torch_dtype=torch.bfloat16,
44+
)
45+
# use following lines if you have GPU constraints
46+
# ---------------------------------------------------------------
47+
transformer = FluxTransformer2DModel.from_pretrained(
48+
"sayakpaul/FLUX.1-Depth-dev-nf4", subfolder="transformer", torch_dtype=torch.bfloat16
49+
)
50+
text_encoder_2 = T5EncoderModel.from_pretrained(
51+
"sayakpaul/FLUX.1-Depth-dev-nf4", subfolder="text_encoder_2", torch_dtype=torch.bfloat16
52+
)
53+
pipe.transformer = transformer
54+
pipe.text_encoder_2 = text_encoder_2
55+
pipe.enable_model_cpu_offload()
56+
# ---------------------------------------------------------------
57+
pipe.to("cuda")
58+
59+
prompt = "a blue robot singing opera with human-like expressions"
60+
image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/robot.png")
61+
62+
head_mask = np.zeros_like(image)
63+
head_mask[65:580,300:642] = 255
64+
mask_image = Image.fromarray(head_mask)
65+
66+
processor = DepthPreprocessor.from_pretrained("LiheYoung/depth-anything-large-hf")
67+
control_image = processor(image)[0].convert("RGB")
68+
69+
output = pipe(
70+
prompt=prompt,
71+
image=image,
72+
control_image=control_image,
73+
mask_image=mask_image,
74+
num_inference_steps=30,
75+
strength=0.9,
76+
guidance_scale=10.0,
77+
generator=torch.Generator().manual_seed(42),
78+
).images[0]
79+
make_image_grid([image, control_image, mask_image, output.resize(image.size)], rows=1, cols=4).save("output.png")
80+
```
81+
82+
## FluxControlInpaintPipeline
83+
[[autodoc]] FluxControlInpaintPipeline
84+
- all
85+
- __call__
86+
87+
88+
## FluxPipelineOutput
89+
[[autodoc]] pipelines.flux.pipeline_output.FluxPipelineOutput

src/diffusers/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@
277277
"CogView3PlusPipeline",
278278
"CycleDiffusionPipeline",
279279
"FluxControlImg2ImgPipeline",
280+
"FluxControlInpaintPipeline",
280281
"FluxControlNetImg2ImgPipeline",
281282
"FluxControlNetInpaintPipeline",
282283
"FluxControlNetPipeline",
@@ -765,6 +766,7 @@
765766
CogView3PlusPipeline,
766767
CycleDiffusionPipeline,
767768
FluxControlImg2ImgPipeline,
769+
FluxControlInpaintPipeline,
768770
FluxControlNetImg2ImgPipeline,
769771
FluxControlNetInpaintPipeline,
770772
FluxControlNetPipeline,

src/diffusers/pipelines/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
]
129129
_import_structure["flux"] = [
130130
"FluxControlPipeline",
131+
"FluxControlInpaintPipeline",
131132
"FluxControlImg2ImgPipeline",
132133
"FluxControlNetPipeline",
133134
"FluxControlNetImg2ImgPipeline",
@@ -539,6 +540,7 @@
539540
)
540541
from .flux import (
541542
FluxControlImg2ImgPipeline,
543+
FluxControlInpaintPipeline,
542544
FluxControlNetImg2ImgPipeline,
543545
FluxControlNetInpaintPipeline,
544546
FluxControlNetPipeline,

src/diffusers/pipelines/flux/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
_import_structure["pipeline_flux"] = ["FluxPipeline"]
2727
_import_structure["pipeline_flux_control"] = ["FluxControlPipeline"]
2828
_import_structure["pipeline_flux_control_img2img"] = ["FluxControlImg2ImgPipeline"]
29+
_import_structure["pipeline_flux_control_inpaint"] = ["FluxControlInpaintPipeline"]
2930
_import_structure["pipeline_flux_controlnet"] = ["FluxControlNetPipeline"]
3031
_import_structure["pipeline_flux_controlnet_image_to_image"] = ["FluxControlNetImg2ImgPipeline"]
3132
_import_structure["pipeline_flux_controlnet_inpainting"] = ["FluxControlNetInpaintPipeline"]
@@ -44,6 +45,7 @@
4445
from .pipeline_flux import FluxPipeline
4546
from .pipeline_flux_control import FluxControlPipeline
4647
from .pipeline_flux_control_img2img import FluxControlImg2ImgPipeline
48+
from .pipeline_flux_control_inpaint import FluxControlInpaintPipeline
4749
from .pipeline_flux_controlnet import FluxControlNetPipeline
4850
from .pipeline_flux_controlnet_image_to_image import FluxControlNetImg2ImgPipeline
4951
from .pipeline_flux_controlnet_inpainting import FluxControlNetInpaintPipeline

0 commit comments

Comments
 (0)