Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
shivamshrirao
GitHub Repository: shivamshrirao/diffusers
Path: blob/main/tests/pipelines/ddim/test_ddim.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 DDIMPipeline, DDIMScheduler, UNet2DModel
22
from diffusers.utils.testing_utils import require_torch_gpu, slow, torch_device
23
24
from ...pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS
25
from ...test_pipelines_common import PipelineTesterMixin
26
27
28
torch.backends.cuda.matmul.allow_tf32 = False
29
30
31
class DDIMPipelineFastTests(PipelineTesterMixin, unittest.TestCase):
32
pipeline_class = DDIMPipeline
33
params = UNCONDITIONAL_IMAGE_GENERATION_PARAMS
34
required_optional_params = PipelineTesterMixin.required_optional_params - {
35
"num_images_per_prompt",
36
"latents",
37
"callback",
38
"callback_steps",
39
}
40
batch_params = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS
41
test_cpu_offload = False
42
43
def get_dummy_components(self):
44
torch.manual_seed(0)
45
unet = UNet2DModel(
46
block_out_channels=(32, 64),
47
layers_per_block=2,
48
sample_size=32,
49
in_channels=3,
50
out_channels=3,
51
down_block_types=("DownBlock2D", "AttnDownBlock2D"),
52
up_block_types=("AttnUpBlock2D", "UpBlock2D"),
53
)
54
scheduler = DDIMScheduler()
55
components = {"unet": unet, "scheduler": scheduler}
56
return components
57
58
def get_dummy_inputs(self, device, seed=0):
59
if str(device).startswith("mps"):
60
generator = torch.manual_seed(seed)
61
else:
62
generator = torch.Generator(device=device).manual_seed(seed)
63
inputs = {
64
"batch_size": 1,
65
"generator": generator,
66
"num_inference_steps": 2,
67
"output_type": "numpy",
68
}
69
return inputs
70
71
def test_inference(self):
72
device = "cpu"
73
74
components = self.get_dummy_components()
75
pipe = self.pipeline_class(**components)
76
pipe.to(device)
77
pipe.set_progress_bar_config(disable=None)
78
79
inputs = self.get_dummy_inputs(device)
80
image = pipe(**inputs).images
81
image_slice = image[0, -3:, -3:, -1]
82
83
self.assertEqual(image.shape, (1, 32, 32, 3))
84
expected_slice = np.array(
85
[1.000e00, 5.717e-01, 4.717e-01, 1.000e00, 0.000e00, 1.000e00, 3.000e-04, 0.000e00, 9.000e-04]
86
)
87
max_diff = np.abs(image_slice.flatten() - expected_slice).max()
88
self.assertLessEqual(max_diff, 1e-3)
89
90
91
@slow
92
@require_torch_gpu
93
class DDIMPipelineIntegrationTests(unittest.TestCase):
94
def test_inference_cifar10(self):
95
model_id = "google/ddpm-cifar10-32"
96
97
unet = UNet2DModel.from_pretrained(model_id)
98
scheduler = DDIMScheduler()
99
100
ddim = DDIMPipeline(unet=unet, scheduler=scheduler)
101
ddim.to(torch_device)
102
ddim.set_progress_bar_config(disable=None)
103
104
generator = torch.manual_seed(0)
105
image = ddim(generator=generator, eta=0.0, output_type="numpy").images
106
107
image_slice = image[0, -3:, -3:, -1]
108
109
assert image.shape == (1, 32, 32, 3)
110
expected_slice = np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453])
111
112
assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2
113
114
def test_inference_ema_bedroom(self):
115
model_id = "google/ddpm-ema-bedroom-256"
116
117
unet = UNet2DModel.from_pretrained(model_id)
118
scheduler = DDIMScheduler.from_pretrained(model_id)
119
120
ddpm = DDIMPipeline(unet=unet, scheduler=scheduler)
121
ddpm.to(torch_device)
122
ddpm.set_progress_bar_config(disable=None)
123
124
generator = torch.manual_seed(0)
125
image = ddpm(generator=generator, output_type="numpy").images
126
127
image_slice = image[0, -3:, -3:, -1]
128
129
assert image.shape == (1, 256, 256, 3)
130
expected_slice = np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069])
131
132
assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2
133
134