Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
shivamshrirao
GitHub Repository: shivamshrirao/diffusers
Path: blob/main/tests/pipelines/latent_diffusion/test_latent_diffusion_superresolution.py
1448 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 random
17
import unittest
18
19
import numpy as np
20
import torch
21
22
from diffusers import DDIMScheduler, LDMSuperResolutionPipeline, UNet2DModel, VQModel
23
from diffusers.utils import PIL_INTERPOLATION, floats_tensor, load_image, slow, torch_device
24
from diffusers.utils.testing_utils import require_torch
25
26
27
torch.backends.cuda.matmul.allow_tf32 = False
28
29
30
class LDMSuperResolutionPipelineFastTests(unittest.TestCase):
31
@property
32
def dummy_image(self):
33
batch_size = 1
34
num_channels = 3
35
sizes = (32, 32)
36
37
image = floats_tensor((batch_size, num_channels) + sizes, rng=random.Random(0)).to(torch_device)
38
return image
39
40
@property
41
def dummy_uncond_unet(self):
42
torch.manual_seed(0)
43
model = UNet2DModel(
44
block_out_channels=(32, 64),
45
layers_per_block=2,
46
sample_size=32,
47
in_channels=6,
48
out_channels=3,
49
down_block_types=("DownBlock2D", "AttnDownBlock2D"),
50
up_block_types=("AttnUpBlock2D", "UpBlock2D"),
51
)
52
return model
53
54
@property
55
def dummy_vq_model(self):
56
torch.manual_seed(0)
57
model = VQModel(
58
block_out_channels=[32, 64],
59
in_channels=3,
60
out_channels=3,
61
down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"],
62
up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"],
63
latent_channels=3,
64
)
65
return model
66
67
def test_inference_superresolution(self):
68
device = "cpu"
69
unet = self.dummy_uncond_unet
70
scheduler = DDIMScheduler()
71
vqvae = self.dummy_vq_model
72
73
ldm = LDMSuperResolutionPipeline(unet=unet, vqvae=vqvae, scheduler=scheduler)
74
ldm.to(device)
75
ldm.set_progress_bar_config(disable=None)
76
77
init_image = self.dummy_image.to(device)
78
79
generator = torch.Generator(device=device).manual_seed(0)
80
image = ldm(image=init_image, generator=generator, num_inference_steps=2, output_type="numpy").images
81
82
image_slice = image[0, -3:, -3:, -1]
83
84
assert image.shape == (1, 64, 64, 3)
85
expected_slice = np.array([0.8678, 0.8245, 0.6381, 0.6830, 0.4385, 0.5599, 0.4641, 0.6201, 0.5150])
86
87
assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2
88
89
@unittest.skipIf(torch_device != "cuda", "This test requires a GPU")
90
def test_inference_superresolution_fp16(self):
91
unet = self.dummy_uncond_unet
92
scheduler = DDIMScheduler()
93
vqvae = self.dummy_vq_model
94
95
# put models in fp16
96
unet = unet.half()
97
vqvae = vqvae.half()
98
99
ldm = LDMSuperResolutionPipeline(unet=unet, vqvae=vqvae, scheduler=scheduler)
100
ldm.to(torch_device)
101
ldm.set_progress_bar_config(disable=None)
102
103
init_image = self.dummy_image.to(torch_device)
104
105
image = ldm(init_image, num_inference_steps=2, output_type="numpy").images
106
107
assert image.shape == (1, 64, 64, 3)
108
109
110
@slow
111
@require_torch
112
class LDMSuperResolutionPipelineIntegrationTests(unittest.TestCase):
113
def test_inference_superresolution(self):
114
init_image = load_image(
115
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
116
"/vq_diffusion/teddy_bear_pool.png"
117
)
118
init_image = init_image.resize((64, 64), resample=PIL_INTERPOLATION["lanczos"])
119
120
ldm = LDMSuperResolutionPipeline.from_pretrained("duongna/ldm-super-resolution", device_map="auto")
121
ldm.set_progress_bar_config(disable=None)
122
123
generator = torch.manual_seed(0)
124
image = ldm(image=init_image, generator=generator, num_inference_steps=20, output_type="numpy").images
125
126
image_slice = image[0, -3:, -3:, -1]
127
128
assert image.shape == (1, 256, 256, 3)
129
expected_slice = np.array([0.7644, 0.7679, 0.7642, 0.7633, 0.7666, 0.7560, 0.7425, 0.7257, 0.6907])
130
131
assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2
132
133