sayakpaul HF Staff commited on
Commit
f0cb84f
·
verified ·
1 Parent(s): d6f85ef

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +27 -25
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.modular_pipelines import ModularPipelineBlocks, SequentialPipelineBlocks
10
- from diffusers.modular_pipelines.stable_diffusion_xl import TEXT2IMAGE_BLOCKS, CONTROLNET_BLOCKS
11
  from diffusers.utils import load_image
12
 
13
- # fetch the depth processor block that will create our depth map
14
- depth_processor_block = ModularPipelineBlocks.from_pretrained("diffusers/depth-processor-custom-block", trust_remote_code=true)
 
15
 
16
- my_blocks = TEXT2IMAGE_BLOCKS.copy()
17
- my_blocks.insert("depth_processor", depth_processor_block, 1)
18
 
19
- # replace text to image denoise block with controlnet denoise block
20
- my_blocks.sub_blocks["denoise"] = CONTROLNET_BLOCKS["denoise"]
21
 
22
- # create our initial set of controlnet blocks
23
- blocks = SequentialPipelineBlocks.from_blocks_dict(my_blocks)
24
-
25
- repo_id = "diffusers/modular-stable-diffusion-xl-base-1.0"
26
-
27
- # Initialize the pipeline object we can use to run our blocks
28
- pipe = blocks.init_pipeline(repo_id)
29
 
30
- # Load model component weights
31
- pipe.load_components(torch_dtype=torch.float16, device_map="cuda")
 
32
 
33
- image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true")
34
- image = image.resize((1024, 1024))
 
 
 
35
 
36
- prompt = ["A red car"]
 
 
37
 
38
- output = pipe(
39
  prompt=prompt,
40
  image=image,
41
- num_inference_steps=35,
42
- guidance_scale=7.5,
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
  ```