|
| 1 | +# Copyright 2022 The HuggingFace Team. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | + |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | + |
| 17 | +from diffusers import DiffusionPipeline |
| 18 | +from diffusers import UNetGLIDEModel |
| 19 | + |
| 20 | +import tqdm |
| 21 | +import torch |
| 22 | + |
| 23 | + |
| 24 | +class GLIDE(DiffusionPipeline): |
| 25 | + def __init__(self, unet: UNetGLIDEModel, noise_scheduler): |
| 26 | + super().__init__() |
| 27 | + self.register_modules(unet=unet, noise_scheduler=noise_scheduler) |
| 28 | + |
| 29 | + def __call__(self, generator=None, torch_device=None): |
| 30 | + torch_device = "cuda" if torch.cuda.is_available() else "cpu" |
| 31 | + |
| 32 | + self.unet.to(torch_device) |
| 33 | + # 1. Sample gaussian noise |
| 34 | + image = self.noise_scheduler.sample_noise((1, self.unet.in_channels, self.unet.resolution, self.unet.resolution), device=torch_device, generator=generator) |
| 35 | + for t in tqdm.tqdm(reversed(range(len(self.noise_scheduler))), total=len(self.noise_scheduler)): |
| 36 | + # i) define coefficients for time step t |
| 37 | + clip_image_coeff = 1 / torch.sqrt(self.noise_scheduler.get_alpha_prod(t)) |
| 38 | + clip_noise_coeff = torch.sqrt(1 / self.noise_scheduler.get_alpha_prod(t) - 1) |
| 39 | + image_coeff = (1 - self.noise_scheduler.get_alpha_prod(t - 1)) * torch.sqrt(self.noise_scheduler.get_alpha(t)) / (1 - self.noise_scheduler.get_alpha_prod(t)) |
| 40 | + clip_coeff = torch.sqrt(self.noise_scheduler.get_alpha_prod(t - 1)) * self.noise_scheduler.get_beta(t) / (1 - self.noise_scheduler.get_alpha_prod(t)) |
| 41 | + |
| 42 | + # ii) predict noise residual |
| 43 | + with torch.no_grad(): |
| 44 | + noise_residual = self.unet(image, t) |
| 45 | + |
| 46 | + # iii) compute predicted image from residual |
| 47 | + # See 2nd formula at https://github.com/hojonathanho/diffusion/issues/5#issue-896554416 for comparison |
| 48 | + pred_mean = clip_image_coeff * image - clip_noise_coeff * noise_residual |
| 49 | + pred_mean = torch.clamp(pred_mean, -1, 1) |
| 50 | + prev_image = clip_coeff * pred_mean + image_coeff * image |
| 51 | + |
| 52 | + # iv) sample variance |
| 53 | + prev_variance = self.noise_scheduler.sample_variance(t, prev_image.shape, device=torch_device, generator=generator) |
| 54 | + |
| 55 | + # v) sample x_{t-1} ~ N(prev_image, prev_variance) |
| 56 | + sampled_prev_image = prev_image + prev_variance |
| 57 | + image = sampled_prev_image |
| 58 | + |
| 59 | + return image |
0 commit comments