Path: blob/main/tests/pipelines/versatile_diffusion/test_versatile_diffusion_image_variation.py
1450 views
# coding=utf-81# Copyright 2023 HuggingFace Inc.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.1415import unittest1617import numpy as np18import torch1920from diffusers import VersatileDiffusionImageVariationPipeline21from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device222324torch.backends.cuda.matmul.allow_tf32 = False252627class VersatileDiffusionImageVariationPipelineFastTests(unittest.TestCase):28pass293031@slow32@require_torch_gpu33class VersatileDiffusionImageVariationPipelineIntegrationTests(unittest.TestCase):34def test_inference_image_variations(self):35pipe = VersatileDiffusionImageVariationPipeline.from_pretrained("shi-labs/versatile-diffusion")36pipe.to(torch_device)37pipe.set_progress_bar_config(disable=None)3839image_prompt = load_image(40"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"41)42generator = torch.manual_seed(0)43image = pipe(44image=image_prompt,45generator=generator,46guidance_scale=7.5,47num_inference_steps=50,48output_type="numpy",49).images5051image_slice = image[0, 253:256, 253:256, -1]5253assert image.shape == (1, 512, 512, 3)54expected_slice = np.array([0.0441, 0.0469, 0.0507, 0.0575, 0.0632, 0.0650, 0.0865, 0.0909, 0.0945])5556assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2575859