Skip to content

Commit 9ec0652

Browse files
authored
Merge pull request #829 from mi804/qwen-image-edit-autoresize
support edit_image_auto_resize
2 parents b3f57ed + 7e34808 commit 9ec0652

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

diffsynth/pipelines/qwen_image.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,9 @@ def __call__(
280280
eligen_entity_prompts: list[str] = None,
281281
eligen_entity_masks: list[Image.Image] = None,
282282
eligen_enable_on_negative: bool = False,
283-
# Edit Image
283+
# Qwen-Image-Edit
284284
edit_image: Image.Image = None,
285+
edit_image_auto_resize: bool = True,
285286
edit_rope_interpolation: bool = False,
286287
# FP8
287288
enable_fp8_attention: bool = False,
@@ -313,7 +314,7 @@ def __call__(
313314
"blockwise_controlnet_inputs": blockwise_controlnet_inputs,
314315
"tiled": tiled, "tile_size": tile_size, "tile_stride": tile_stride,
315316
"eligen_entity_prompts": eligen_entity_prompts, "eligen_entity_masks": eligen_entity_masks, "eligen_enable_on_negative": eligen_enable_on_negative,
316-
"edit_image": edit_image, "edit_rope_interpolation": edit_rope_interpolation,
317+
"edit_image": edit_image, "edit_image_auto_resize": edit_image_auto_resize, "edit_rope_interpolation": edit_rope_interpolation,
317318
}
318319
for unit in self.units:
319320
inputs_shared, inputs_posi, inputs_nega = self.unit_runner(unit, self, inputs_shared, inputs_posi, inputs_nega)
@@ -586,17 +587,33 @@ def process(self, pipe: QwenImagePipeline, blockwise_controlnet_inputs: list[Con
586587
class QwenImageUnit_EditImageEmbedder(PipelineUnit):
587588
def __init__(self):
588589
super().__init__(
589-
input_params=("edit_image", "tiled", "tile_size", "tile_stride"),
590+
input_params=("edit_image", "tiled", "tile_size", "tile_stride", "edit_image_auto_resize"),
590591
onload_model_names=("vae",)
591592
)
592593

593-
def process(self, pipe: QwenImagePipeline, edit_image, tiled, tile_size, tile_stride):
594+
595+
def calculate_dimensions(self, target_area, ratio):
596+
import math
597+
width = math.sqrt(target_area * ratio)
598+
height = width / ratio
599+
width = round(width / 32) * 32
600+
height = round(height / 32) * 32
601+
return width, height
602+
603+
604+
def edit_image_auto_resize(self, edit_image):
605+
calculated_width, calculated_height = self.calculate_dimensions(1024 * 1024, edit_image.size[0] / edit_image.size[1])
606+
return edit_image.resize((calculated_width, calculated_height))
607+
608+
609+
def process(self, pipe: QwenImagePipeline, edit_image, tiled, tile_size, tile_stride, edit_image_auto_resize=False):
594610
if edit_image is None:
595611
return {}
612+
resized_edit_image = self.edit_image_auto_resize(edit_image) if edit_image_auto_resize else edit_image
596613
pipe.load_models_to_device(['vae'])
597-
edit_image = pipe.preprocess_image(edit_image).to(device=pipe.device, dtype=pipe.torch_dtype)
614+
edit_image = pipe.preprocess_image(resized_edit_image).to(device=pipe.device, dtype=pipe.torch_dtype)
598615
edit_latents = pipe.vae.encode(edit_image, tiled=tiled, tile_size=tile_size, tile_stride=tile_stride)
599-
return {"edit_latents": edit_latents}
616+
return {"edit_latents": edit_latents, "edit_image": resized_edit_image}
600617

601618

602619
def model_fn_qwen_image(

examples/qwen_image/model_inference/Qwen-Image-Edit-Lowres-Fix.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222

2323
prompt = "将裙子变成粉色"
2424
image = image.resize((512, 384))
25-
image = pipe(prompt, edit_image=image, seed=1, num_inference_steps=40, height=1024, width=768, edit_rope_interpolation=True)
25+
image = pipe(prompt, edit_image=image, seed=1, num_inference_steps=40, height=1024, width=768, edit_rope_interpolation=True, edit_image_auto_resize=False)
2626
image.save(f"image2.jpg")

examples/qwen_image/model_inference/Qwen-Image-Edit.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313
processor_config=ModelConfig(model_id="Qwen/Qwen-Image-Edit", origin_file_pattern="processor/"),
1414
)
1515
prompt = "精致肖像,水下少女,蓝裙飘逸,发丝轻扬,光影透澈,气泡环绕,面容恬静,细节精致,梦幻唯美。"
16-
image = pipe(prompt=prompt, seed=0, num_inference_steps=40, height=1024, width=1024)
17-
image.save("image1.jpg")
16+
input_image = pipe(prompt=prompt, seed=0, num_inference_steps=40, height=1328, width=1024)
17+
input_image.save("image1.jpg")
1818

1919
prompt = "将裙子改为粉色"
20-
image = pipe(prompt, edit_image=image, seed=1, num_inference_steps=40, height=1024, width=1024)
20+
# edit_image_auto_resize=True: auto resize input image to match the area of 1024*1024 with the original aspect ratio
21+
image = pipe(prompt, edit_image=input_image, seed=1, num_inference_steps=40, height=1328, width=1024, edit_image_auto_resize=True)
2122
image.save(f"image2.jpg")
23+
24+
# edit_image_auto_resize=False: do not resize input image
25+
image = pipe(prompt, edit_image=input_image, seed=1, num_inference_steps=40, height=1328, width=1024, edit_image_auto_resize=False)
26+
image.save(f"image3.jpg")

examples/qwen_image/model_inference_low_vram/Qwen-Image-Edit-Lowres-Fix.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424

2525
prompt = "将裙子变成粉色"
2626
image = image.resize((512, 384))
27-
image = pipe(prompt, edit_image=image, seed=1, num_inference_steps=40, height=1024, width=768, edit_rope_interpolation=True)
27+
image = pipe(prompt, edit_image=image, seed=1, num_inference_steps=40, height=1024, width=768, edit_rope_interpolation=True, edit_image_auto_resize=False)
2828
image.save(f"image2.jpg")

examples/qwen_image/model_training/train.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def forward_preprocess(self, data):
7878
"rand_device": self.pipe.device,
7979
"use_gradient_checkpointing": self.use_gradient_checkpointing,
8080
"use_gradient_checkpointing_offload": self.use_gradient_checkpointing_offload,
81+
"edit_image_auto_resize": True,
8182
}
8283

8384
# Extra inputs

0 commit comments

Comments
 (0)