Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
shivamshrirao
GitHub Repository: shivamshrirao/diffusers
Path: blob/main/tests/pipelines/stable_diffusion/test_onnx_stable_diffusion_upscale.py
1448 views
1
# coding=utf-8
2
# Copyright 2022 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 random
17
import unittest
18
19
import numpy as np
20
import torch
21
22
from diffusers import (
23
DPMSolverMultistepScheduler,
24
EulerAncestralDiscreteScheduler,
25
EulerDiscreteScheduler,
26
LMSDiscreteScheduler,
27
OnnxStableDiffusionUpscalePipeline,
28
PNDMScheduler,
29
)
30
from diffusers.utils import floats_tensor
31
from diffusers.utils.testing_utils import (
32
is_onnx_available,
33
load_image,
34
nightly,
35
require_onnxruntime,
36
require_torch_gpu,
37
)
38
39
from ...test_pipelines_onnx_common import OnnxPipelineTesterMixin
40
41
42
if is_onnx_available():
43
import onnxruntime as ort
44
45
46
class OnnxStableDiffusionUpscalePipelineFastTests(OnnxPipelineTesterMixin, unittest.TestCase):
47
# TODO: is there an appropriate internal test set?
48
hub_checkpoint = "ssube/stable-diffusion-x4-upscaler-onnx"
49
50
def get_dummy_inputs(self, seed=0):
51
image = floats_tensor((1, 3, 128, 128), rng=random.Random(seed))
52
generator = torch.manual_seed(seed)
53
inputs = {
54
"prompt": "A painting of a squirrel eating a burger",
55
"image": image,
56
"generator": generator,
57
"num_inference_steps": 3,
58
"guidance_scale": 7.5,
59
"output_type": "numpy",
60
}
61
return inputs
62
63
def test_pipeline_default_ddpm(self):
64
pipe = OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint, provider="CPUExecutionProvider")
65
pipe.set_progress_bar_config(disable=None)
66
67
inputs = self.get_dummy_inputs()
68
image = pipe(**inputs).images
69
image_slice = image[0, -3:, -3:, -1].flatten()
70
71
# started as 128, should now be 512
72
assert image.shape == (1, 512, 512, 3)
73
expected_slice = np.array(
74
[0.6974782, 0.68902093, 0.70135885, 0.7583618, 0.7804545, 0.7854912, 0.78667426, 0.78743863, 0.78070223]
75
)
76
assert np.abs(image_slice - expected_slice).max() < 1e-1
77
78
def test_pipeline_pndm(self):
79
pipe = OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint, provider="CPUExecutionProvider")
80
pipe.scheduler = PNDMScheduler.from_config(pipe.scheduler.config, skip_prk_steps=True)
81
pipe.set_progress_bar_config(disable=None)
82
83
inputs = self.get_dummy_inputs()
84
image = pipe(**inputs).images
85
image_slice = image[0, -3:, -3:, -1]
86
87
assert image.shape == (1, 512, 512, 3)
88
expected_slice = np.array(
89
[0.6898892, 0.59240556, 0.52499527, 0.58866215, 0.52258235, 0.52572715, 0.62414473, 0.6174387, 0.6214964]
90
)
91
assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-1
92
93
def test_pipeline_dpm_multistep(self):
94
pipe = OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint, provider="CPUExecutionProvider")
95
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
96
pipe.set_progress_bar_config(disable=None)
97
98
inputs = self.get_dummy_inputs()
99
image = pipe(**inputs).images
100
image_slice = image[0, -3:, -3:, -1]
101
102
assert image.shape == (1, 512, 512, 3)
103
expected_slice = np.array(
104
[0.7659278, 0.76437664, 0.75579107, 0.7691116, 0.77666986, 0.7727672, 0.7758664, 0.7812226, 0.76942515]
105
)
106
107
assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-1
108
109
def test_pipeline_euler(self):
110
pipe = OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint, provider="CPUExecutionProvider")
111
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
112
pipe.set_progress_bar_config(disable=None)
113
114
inputs = self.get_dummy_inputs()
115
image = pipe(**inputs).images
116
image_slice = image[0, -3:, -3:, -1]
117
118
assert image.shape == (1, 512, 512, 3)
119
expected_slice = np.array(
120
[0.6974782, 0.68902093, 0.70135885, 0.7583618, 0.7804545, 0.7854912, 0.78667426, 0.78743863, 0.78070223]
121
)
122
assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-1
123
124
def test_pipeline_euler_ancestral(self):
125
pipe = OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint, provider="CPUExecutionProvider")
126
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
127
pipe.set_progress_bar_config(disable=None)
128
129
inputs = self.get_dummy_inputs()
130
image = pipe(**inputs).images
131
image_slice = image[0, -3:, -3:, -1]
132
133
assert image.shape == (1, 512, 512, 3)
134
expected_slice = np.array(
135
[0.77424496, 0.773601, 0.7645288, 0.7769598, 0.7772739, 0.7738688, 0.78187233, 0.77879584, 0.767043]
136
)
137
138
assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-1
139
140
141
@nightly
142
@require_onnxruntime
143
@require_torch_gpu
144
class OnnxStableDiffusionUpscalePipelineIntegrationTests(unittest.TestCase):
145
@property
146
def gpu_provider(self):
147
return (
148
"CUDAExecutionProvider",
149
{
150
"gpu_mem_limit": "15000000000", # 15GB
151
"arena_extend_strategy": "kSameAsRequested",
152
},
153
)
154
155
@property
156
def gpu_options(self):
157
options = ort.SessionOptions()
158
options.enable_mem_pattern = False
159
return options
160
161
def test_inference_default_ddpm(self):
162
init_image = load_image(
163
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
164
"/img2img/sketch-mountains-input.jpg"
165
)
166
init_image = init_image.resize((128, 128))
167
# using the PNDM scheduler by default
168
pipe = OnnxStableDiffusionUpscalePipeline.from_pretrained(
169
"ssube/stable-diffusion-x4-upscaler-onnx",
170
provider=self.gpu_provider,
171
sess_options=self.gpu_options,
172
)
173
pipe.set_progress_bar_config(disable=None)
174
175
prompt = "A fantasy landscape, trending on artstation"
176
177
generator = torch.manual_seed(0)
178
output = pipe(
179
prompt=prompt,
180
image=init_image,
181
guidance_scale=7.5,
182
num_inference_steps=10,
183
generator=generator,
184
output_type="np",
185
)
186
images = output.images
187
image_slice = images[0, 255:258, 383:386, -1]
188
189
assert images.shape == (1, 512, 512, 3)
190
expected_slice = np.array([0.4883, 0.4947, 0.4980, 0.4975, 0.4982, 0.4980, 0.5000, 0.5006, 0.4972])
191
# TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues
192
193
assert np.abs(image_slice.flatten() - expected_slice).max() < 2e-2
194
195
def test_inference_k_lms(self):
196
init_image = load_image(
197
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
198
"/img2img/sketch-mountains-input.jpg"
199
)
200
init_image = init_image.resize((128, 128))
201
lms_scheduler = LMSDiscreteScheduler.from_pretrained(
202
"ssube/stable-diffusion-x4-upscaler-onnx", subfolder="scheduler"
203
)
204
pipe = OnnxStableDiffusionUpscalePipeline.from_pretrained(
205
"ssube/stable-diffusion-x4-upscaler-onnx",
206
scheduler=lms_scheduler,
207
provider=self.gpu_provider,
208
sess_options=self.gpu_options,
209
)
210
pipe.set_progress_bar_config(disable=None)
211
212
prompt = "A fantasy landscape, trending on artstation"
213
214
generator = torch.manual_seed(0)
215
output = pipe(
216
prompt=prompt,
217
image=init_image,
218
guidance_scale=7.5,
219
num_inference_steps=20,
220
generator=generator,
221
output_type="np",
222
)
223
images = output.images
224
image_slice = images[0, 255:258, 383:386, -1]
225
226
assert images.shape == (1, 512, 512, 3)
227
expected_slice = np.array(
228
[0.50173753, 0.50223356, 0.502039, 0.50233036, 0.5023725, 0.5022601, 0.5018758, 0.50234085, 0.50241566]
229
)
230
# TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues
231
232
assert np.abs(image_slice.flatten() - expected_slice).max() < 2e-2
233
234