Skip to content

Commit

Permalink
Add inversion equivalence test
Browse files Browse the repository at this point in the history
  • Loading branch information
clarencechen committed Mar 13, 2023
1 parent 1d7f076 commit 256042e
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,29 @@ def test_stable_diffusion_pix2pix_inversion(self):

assert np.abs(expected_slice - image_slice.cpu().numpy()).max() < 5e-2

def test_stable_diffusion_2_pix2pix_inversion(self):
pipe = StableDiffusionPix2PixZeroPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-1", safety_checker=None, torch_dtype=torch.float16
)
pipe.inverse_scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
pipe.inverse_scheduler = DDIMInverseScheduler.from_config(pipe.scheduler.config)

caption = "a photography of a cat with flowers"
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
pipe.enable_model_cpu_offload()
pipe.set_progress_bar_config(disable=None)

generator = torch.manual_seed(0)
output = pipe.invert(caption, image=self.raw_image, generator=generator, num_inference_steps=10)
inv_latents = output[0]

image_slice = inv_latents[0, -3:, -3:, -1].flatten()

assert inv_latents.shape == (1, 4, 64, 64)
expected_slice = np.array([0.7515, -0.2397, 0.4922, -0.9736, -0.7031, 0.4846, -1.0781, 1.1309, -0.6973])

assert np.abs(expected_slice - image_slice.cpu().numpy()).max() < 5e-2

def test_stable_diffusion_pix2pix_full(self):
# numpy array of https://huggingface.co/datasets/hf-internal-testing/diffusers-images/blob/main/pix2pix/dog.png
expected_image = load_numpy(
Expand Down

0 comments on commit 256042e

Please sign in to comment.