From 902015adba936be155cd0e5119ff74f50f14976a Mon Sep 17 00:00:00 2001 From: menglcai Date: Wed, 2 Dec 2026 01:09:58 +0800 Subject: [PATCH] Resize the init image to height/width in img2img and inpaint Signed-off-by: menglcai --- .../pipelines/z_image/pipeline_z_image_img2img.py | 7 +++++-- .../pipelines/z_image/pipeline_z_image_inpaint.py | 2 +- tests/pipelines/z_image/test_z_image_img2img.py | 11 +++++++++++ tests/pipelines/z_image/test_z_image_inpaint.py | 5 +++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/diffusers/pipelines/z_image/pipeline_z_image_img2img.py b/src/diffusers/pipelines/z_image/pipeline_z_image_img2img.py index 48e3fae94a0d..e12df7533aea 100644 --- a/src/diffusers/pipelines/z_image/pipeline_z_image_img2img.py +++ b/src/diffusers/pipelines/z_image/pipeline_z_image_img2img.py @@ -192,7 +192,10 @@ def __init__( self.vae_scale_factor = ( 2 ** (len(self.vae.config.block_out_channels) - 1) if hasattr(self, "vae") and self.vae is not None else 8 ) - self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor * 2) + latent_channels = self.vae.config.latent_channels if getattr(self, "vae", None) else 16 + self.image_processor = VaeImageProcessor( + vae_scale_factor=self.vae_scale_factor * 2, vae_latent_channels=latent_channels + ) # Copied from diffusers.pipelines.z_image.pipeline_z_image.ZImagePipeline.encode_prompt def encode_prompt( @@ -486,7 +489,7 @@ def __call__( raise ValueError(f"The value of strength should be in [0.0, 1.0] but is {strength}") # 2. Preprocess image - init_image = self.image_processor.preprocess(image) + init_image = self.image_processor.preprocess(image, height=height, width=width) init_image = init_image.to(dtype=torch.float32) # Get dimensions from the preprocessed image if not specified diff --git a/src/diffusers/pipelines/z_image/pipeline_z_image_inpaint.py b/src/diffusers/pipelines/z_image/pipeline_z_image_inpaint.py index 64e0cca28ce7..4e7ab5dab850 100644 --- a/src/diffusers/pipelines/z_image/pipeline_z_image_inpaint.py +++ b/src/diffusers/pipelines/z_image/pipeline_z_image_inpaint.py @@ -670,7 +670,7 @@ def __call__( ) # 2. Preprocess image and mask - init_image = self.image_processor.preprocess(image) + init_image = self.image_processor.preprocess(image, height=height, width=width) init_image = init_image.to(dtype=torch.float32) # Get dimensions from the preprocessed image if not specified diff --git a/tests/pipelines/z_image/test_z_image_img2img.py b/tests/pipelines/z_image/test_z_image_img2img.py index 6016c53df8c3..731237feb25b 100644 --- a/tests/pipelines/z_image/test_z_image_img2img.py +++ b/tests/pipelines/z_image/test_z_image_img2img.py @@ -169,6 +169,17 @@ def test_inference(self): generated_slice = torch.cat([generated_slice[:8], generated_slice[-8:]]) assert_tensors_close(generated_slice, expected_slice, atol=5e-2) + def test_height_width_differ_from_image(self): + pipe = self.get_pipeline().to(torch_device) + image = self.run_pipe(pipe, height=64, width=48) + assert image.shape == (1, 3, 64, 48) + + def test_latent_image_with_height_width(self): + pipe = self.get_pipeline().to(torch_device) + latents = floats_tensor((1, 16, 32, 24), rng=random.Random(0)).to(torch_device) + image = self.run_pipe(pipe, image=latents, height=64, width=48) + assert image.shape == (1, 3, 64, 48) + def test_inference_batch_single_identical(self, batch_size=3, expected_max_diff=1e-1): # Z-Image pads the batch to a common sequence length, so batched and single runs diverge slightly more. super().test_inference_batch_single_identical(batch_size=batch_size, expected_max_diff=expected_max_diff) diff --git a/tests/pipelines/z_image/test_z_image_inpaint.py b/tests/pipelines/z_image/test_z_image_inpaint.py index 72899386e990..3e95c2197984 100644 --- a/tests/pipelines/z_image/test_z_image_inpaint.py +++ b/tests/pipelines/z_image/test_z_image_inpaint.py @@ -178,6 +178,11 @@ def test_inference(self): generated_slice = torch.cat([generated_slice[:8], generated_slice[-8:]]) assert_tensors_close(generated_slice, expected_slice, atol=5e-2) + def test_height_width_differ_from_image(self): + pipe = self.get_pipeline().to(torch_device) + image = self.run_pipe(pipe, height=64, width=48) + assert image.shape == (1, 3, 64, 48) + def test_inference_batch_single_identical(self, batch_size=3, expected_max_diff=1e-1): # Z-Image pads the batch to a common sequence length, so batched and single runs diverge slightly more. super().test_inference_batch_single_identical(batch_size=batch_size, expected_max_diff=expected_max_diff)