|  | 
|  | 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 | 
0 commit comments