Update README.md
Browse files
README.md
CHANGED
|
@@ -6,40 +6,42 @@ This is a custom block designed to extract depth maps from input images using th
|
|
| 6 |
|
| 7 |
```python
|
| 8 |
import torch
|
| 9 |
-
from diffusers
|
| 10 |
-
from diffusers.modular_pipelines.stable_diffusion_xl import TEXT2IMAGE_BLOCKS, CONTROLNET_BLOCKS
|
| 11 |
from diffusers.utils import load_image
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
my_blocks = TEXT2IMAGE_BLOCKS.copy()
|
| 17 |
-
my_blocks.insert("depth_processor", depth_processor_block, 1)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
blocks =
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
pipe
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
output =
|
| 39 |
prompt=prompt,
|
| 40 |
image=image,
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
output_type="pil",
|
| 44 |
-
)
|
| 45 |
```
|
|
|
|
| 6 |
|
| 7 |
```python
|
| 8 |
import torch
|
| 9 |
+
from diffusers import ModularPipeline, ComponentsManager, ModularPipelineBlocks
|
|
|
|
| 10 |
from diffusers.utils import load_image
|
| 11 |
|
| 12 |
+
# Use ComponentsManager to enable auto CPU offloading for memory efficiency
|
| 13 |
+
manager = ComponentsManager()
|
| 14 |
+
manager.enable_auto_cpu_offload(device="cuda:0")
|
| 15 |
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Initialize pipeline
|
| 18 |
+
pipe = ModularPipeline.from_pretrained("Qwen/Qwen-Image", components_manager=manager)
|
| 19 |
|
| 20 |
+
# Insert a depth processing block
|
| 21 |
+
blocks = pipe.blocks.get_workflow("controlnet_text2image")
|
| 22 |
+
depth_block = ModularPipelineBlocks.from_pretrained(
|
| 23 |
+
"diffusers/depth-processor-custom-block",
|
| 24 |
+
trust_remote_code=True,
|
| 25 |
+
)
|
| 26 |
+
blocks.sub_blocks.insert("depth", depth_block, 0)
|
| 27 |
|
| 28 |
+
# Reinitialize the pipeline for ControlNet
|
| 29 |
+
pipe = blocks.init_pipeline("Qwen/Qwen-Image", components_manager=manager)
|
| 30 |
+
pipe.load_components(torch_dtype=torch.bfloat16)
|
| 31 |
|
| 32 |
+
# Load the ControlNet model
|
| 33 |
+
controlnet_spec = pipeline.get_component_spec("controlnet")
|
| 34 |
+
controlnet_spec.pretrained_model_name_or_path = "InstantX/Qwen-Image-ControlNet-Union"
|
| 35 |
+
controlnet = controlnet_spec.load(torch_dtype=torch.bfloat16)
|
| 36 |
+
pipeline.update_components(controlnet=controlnet)
|
| 37 |
|
| 38 |
+
# Infer
|
| 39 |
+
prompt = "cat wizard with red hat, gandalf, lord of the rings, detailed, fantasy, cute, adorable, Pixar, Disney"
|
| 40 |
+
image = load_image("https://github.com/Trgtuan10/Image_storage/blob/main/cute_cat.png?raw=true")
|
| 41 |
|
| 42 |
+
output = pipeline(
|
| 43 |
prompt=prompt,
|
| 44 |
image=image,
|
| 45 |
+
).images[0]
|
| 46 |
+
output
|
|
|
|
|
|
|
| 47 |
```
|