junnyu commited on
Commit
5802ba8
·
1 Parent(s): 062d854

Upload pipeline.py

Browse files
Files changed (1) hide show
  1. pipeline.py +99 -0
pipeline.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 typing import Optional, Tuple, Union
18
+
19
+ import paddle
20
+
21
+ from ppdiffusers import DiffusionPipeline, ImagePipelineOutput
22
+
23
+
24
+ class CustomPipeline(DiffusionPipeline):
25
+ r"""
26
+ This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods the
27
+ library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)
28
+
29
+ Parameters:
30
+ unet ([`UNet2DModel`]): U-Net architecture to denoise the encoded image.
31
+ scheduler ([`SchedulerMixin`]):
32
+ A scheduler to be used in combination with `unet` to denoise the encoded image. Can be one of
33
+ [`DDPMScheduler`], or [`DDIMScheduler`].
34
+ """
35
+
36
+ def __init__(self, unet, scheduler):
37
+ super().__init__()
38
+ self.register_modules(unet=unet, scheduler=scheduler)
39
+
40
+ @paddle.no_grad()
41
+ def __call__(
42
+ self,
43
+ batch_size: int = 1,
44
+ generator: Optional[paddle.Generator] = None,
45
+ num_inference_steps: int = 50,
46
+ output_type: Optional[str] = "pil",
47
+ return_dict: bool = True,
48
+ **kwargs,
49
+ ) -> Union[ImagePipelineOutput, Tuple]:
50
+ r"""
51
+ Args:
52
+ batch_size (`int`, *optional*, defaults to 1):
53
+ The number of images to generate.
54
+ generator (`paddle.Generator`, *optional*):
55
+ A paddle generator to make generation deterministic.
56
+ eta (`float`, *optional*, defaults to 0.0):
57
+ The eta parameter which controls the scale of the variance (0 is DDIM and 1 is one type of DDPM).
58
+ num_inference_steps (`int`, *optional*, defaults to 50):
59
+ The number of denoising steps. More denoising steps usually lead to a higher quality image at the
60
+ expense of slower inference.
61
+ output_type (`str`, *optional*, defaults to `"pil"`):
62
+ The output format of the generate image. Choose between
63
+ [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`.
64
+ return_dict (`bool`, *optional*, defaults to `True`):
65
+ Whether or not to return a [`~pipeline_utils.ImagePipelineOutput`] instead of a plain tuple.
66
+
67
+ Returns:
68
+ [`~pipeline_utils.ImagePipelineOutput`] or `tuple`: [`~pipelines.utils.ImagePipelineOutput`] if
69
+ `return_dict` is True, otherwise a `tuple. When returning a tuple, the first element is a list with the
70
+ generated images.
71
+ """
72
+
73
+ # Sample gaussian noise to begin loop
74
+ image = paddle.randn(
75
+ (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size),
76
+ generator=generator,
77
+ )
78
+
79
+ # set step values
80
+ self.scheduler.set_timesteps(num_inference_steps)
81
+
82
+ for t in self.progress_bar(self.scheduler.timesteps):
83
+ # 1. predict noise model_output
84
+ model_output = self.unet(image, t).sample
85
+
86
+ # 2. predict previous mean of image x_t-1 and add variance depending on eta
87
+ # eta corresponds to η in paper and should be between [0, 1]
88
+ # do x_t -> x_t-1
89
+ image = self.scheduler.step(model_output, t, image).prev_sample
90
+
91
+ image = (image / 2 + 0.5).clip(0, 1)
92
+ image = image.cast("float32").transpose([0, 2, 3, 1]).numpy()
93
+ if output_type == "pil":
94
+ image = self.numpy_to_pil(image)
95
+
96
+ if not return_dict:
97
+ return (image,)
98
+
99
+ return ImagePipelineOutput(images=image), "This is a test"