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_inpaint.py
1451 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
20
from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline
21
from diffusers.utils.testing_utils import (
22
is_onnx_available,
23
load_image,
24
nightly,
25
require_onnxruntime,
26
require_torch_gpu,
27
)
28
29
from ...test_pipelines_onnx_common import OnnxPipelineTesterMixin
30
31
32
if is_onnx_available():
33
import onnxruntime as ort
34
35
36
class OnnxStableDiffusionPipelineFastTests(OnnxPipelineTesterMixin, unittest.TestCase):
37
# FIXME: add fast tests
38
pass
39
40
41
@nightly
42
@require_onnxruntime
43
@require_torch_gpu
44
class OnnxStableDiffusionInpaintPipelineIntegrationTests(unittest.TestCase):
45
@property
46
def gpu_provider(self):
47
return (
48
"CUDAExecutionProvider",
49
{
50
"gpu_mem_limit": "15000000000", # 15GB
51
"arena_extend_strategy": "kSameAsRequested",
52
},
53
)
54
55
@property
56
def gpu_options(self):
57
options = ort.SessionOptions()
58
options.enable_mem_pattern = False
59
return options
60
61
def test_inference_default_pndm(self):
62
init_image = load_image(
63
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
64
"/in_paint/overture-creations-5sI6fQgYIuo.png"
65
)
66
mask_image = load_image(
67
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
68
"/in_paint/overture-creations-5sI6fQgYIuo_mask.png"
69
)
70
pipe = OnnxStableDiffusionInpaintPipeline.from_pretrained(
71
"runwayml/stable-diffusion-inpainting",
72
revision="onnx",
73
safety_checker=None,
74
feature_extractor=None,
75
provider=self.gpu_provider,
76
sess_options=self.gpu_options,
77
)
78
pipe.set_progress_bar_config(disable=None)
79
80
prompt = "A red cat sitting on a park bench"
81
82
generator = np.random.RandomState(0)
83
output = pipe(
84
prompt=prompt,
85
image=init_image,
86
mask_image=mask_image,
87
guidance_scale=7.5,
88
num_inference_steps=10,
89
generator=generator,
90
output_type="np",
91
)
92
images = output.images
93
image_slice = images[0, 255:258, 255:258, -1]
94
95
assert images.shape == (1, 512, 512, 3)
96
expected_slice = np.array([0.2514, 0.3007, 0.3517, 0.1790, 0.2382, 0.3167, 0.1944, 0.2273, 0.2464])
97
98
assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-3
99
100
def test_inference_k_lms(self):
101
init_image = load_image(
102
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
103
"/in_paint/overture-creations-5sI6fQgYIuo.png"
104
)
105
mask_image = load_image(
106
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
107
"/in_paint/overture-creations-5sI6fQgYIuo_mask.png"
108
)
109
lms_scheduler = LMSDiscreteScheduler.from_pretrained(
110
"runwayml/stable-diffusion-inpainting", subfolder="scheduler", revision="onnx"
111
)
112
pipe = OnnxStableDiffusionInpaintPipeline.from_pretrained(
113
"runwayml/stable-diffusion-inpainting",
114
revision="onnx",
115
scheduler=lms_scheduler,
116
safety_checker=None,
117
feature_extractor=None,
118
provider=self.gpu_provider,
119
sess_options=self.gpu_options,
120
)
121
pipe.set_progress_bar_config(disable=None)
122
123
prompt = "A red cat sitting on a park bench"
124
125
generator = np.random.RandomState(0)
126
output = pipe(
127
prompt=prompt,
128
image=init_image,
129
mask_image=mask_image,
130
guidance_scale=7.5,
131
num_inference_steps=20,
132
generator=generator,
133
output_type="np",
134
)
135
images = output.images
136
image_slice = images[0, 255:258, 255:258, -1]
137
138
assert images.shape == (1, 512, 512, 3)
139
expected_slice = np.array([0.0086, 0.0077, 0.0083, 0.0093, 0.0107, 0.0139, 0.0094, 0.0097, 0.0125])
140
141
assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-3
142
143