Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/diffusers/pipelines/z_image/pipeline_z_image_img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/pipelines/z_image/test_z_image_img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions tests/pipelines/z_image/test_z_image_inpaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down