Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
shivamshrirao
GitHub Repository: shivamshrirao/diffusers
Path: blob/main/tests/pipelines/versatile_diffusion/test_versatile_diffusion_image_variation.py
1450 views
1
# coding=utf-8
2
# Copyright 2023 HuggingFace Inc.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
# http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import unittest
17
18
import numpy as np
19
import torch
20
21
from diffusers import VersatileDiffusionImageVariationPipeline
22
from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device
23
24
25
torch.backends.cuda.matmul.allow_tf32 = False
26
27
28
class VersatileDiffusionImageVariationPipelineFastTests(unittest.TestCase):
29
pass
30
31
32
@slow
33
@require_torch_gpu
34
class VersatileDiffusionImageVariationPipelineIntegrationTests(unittest.TestCase):
35
def test_inference_image_variations(self):
36
pipe = VersatileDiffusionImageVariationPipeline.from_pretrained("shi-labs/versatile-diffusion")
37
pipe.to(torch_device)
38
pipe.set_progress_bar_config(disable=None)
39
40
image_prompt = load_image(
41
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"
42
)
43
generator = torch.manual_seed(0)
44
image = pipe(
45
image=image_prompt,
46
generator=generator,
47
guidance_scale=7.5,
48
num_inference_steps=50,
49
output_type="numpy",
50
).images
51
52
image_slice = image[0, 253:256, 253:256, -1]
53
54
assert image.shape == (1, 512, 512, 3)
55
expected_slice = np.array([0.0441, 0.0469, 0.0507, 0.0575, 0.0632, 0.0650, 0.0865, 0.0909, 0.0945])
56
57
assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2
58
59