Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
shivamshrirao
GitHub Repository: shivamshrirao/diffusers
Path: blob/main/tests/pipelines/karras_ve/test_karras_ve.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 KarrasVePipeline, KarrasVeScheduler, UNet2DModel
22
from diffusers.utils.testing_utils import require_torch, slow, torch_device
23
24
25
torch.backends.cuda.matmul.allow_tf32 = False
26
27
28
class KarrasVePipelineFastTests(unittest.TestCase):
29
@property
30
def dummy_uncond_unet(self):
31
torch.manual_seed(0)
32
model = UNet2DModel(
33
block_out_channels=(32, 64),
34
layers_per_block=2,
35
sample_size=32,
36
in_channels=3,
37
out_channels=3,
38
down_block_types=("DownBlock2D", "AttnDownBlock2D"),
39
up_block_types=("AttnUpBlock2D", "UpBlock2D"),
40
)
41
return model
42
43
def test_inference(self):
44
unet = self.dummy_uncond_unet
45
scheduler = KarrasVeScheduler()
46
47
pipe = KarrasVePipeline(unet=unet, scheduler=scheduler)
48
pipe.to(torch_device)
49
pipe.set_progress_bar_config(disable=None)
50
51
generator = torch.manual_seed(0)
52
image = pipe(num_inference_steps=2, generator=generator, output_type="numpy").images
53
54
generator = torch.manual_seed(0)
55
image_from_tuple = pipe(num_inference_steps=2, generator=generator, output_type="numpy", return_dict=False)[0]
56
57
image_slice = image[0, -3:, -3:, -1]
58
image_from_tuple_slice = image_from_tuple[0, -3:, -3:, -1]
59
60
assert image.shape == (1, 32, 32, 3)
61
expected_slice = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0])
62
63
assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2
64
assert np.abs(image_from_tuple_slice.flatten() - expected_slice).max() < 1e-2
65
66
67
@slow
68
@require_torch
69
class KarrasVePipelineIntegrationTests(unittest.TestCase):
70
def test_inference(self):
71
model_id = "google/ncsnpp-celebahq-256"
72
model = UNet2DModel.from_pretrained(model_id)
73
scheduler = KarrasVeScheduler()
74
75
pipe = KarrasVePipeline(unet=model, scheduler=scheduler)
76
pipe.to(torch_device)
77
pipe.set_progress_bar_config(disable=None)
78
79
generator = torch.manual_seed(0)
80
image = pipe(num_inference_steps=20, generator=generator, output_type="numpy").images
81
82
image_slice = image[0, -3:, -3:, -1]
83
assert image.shape == (1, 256, 256, 3)
84
expected_slice = np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586])
85
86
assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2
87
88