diffusion-model-framework 1.0.0rc1__tar.gz → 1.0.0rc2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (18) hide show
  1. {diffusion_model_framework-1.0.0rc1 → diffusion_model_framework-1.0.0rc2}/PKG-INFO +1 -1
  2. diffusion_model_framework-1.0.0rc2/diffusion_models/__init__.py +1 -0
  3. {diffusion_model_framework-1.0.0rc1 → diffusion_model_framework-1.0.0rc2}/diffusion_models/diffusion_inference.py +3 -5
  4. {diffusion_model_framework-1.0.0rc1 → diffusion_model_framework-1.0.0rc2}/diffusion_models/diffusion_trainer.py +6 -2
  5. {diffusion_model_framework-1.0.0rc1 → diffusion_model_framework-1.0.0rc2}/diffusion_models/gaussian_diffusion/base_diffuser.py +12 -0
  6. {diffusion_model_framework-1.0.0rc1 → diffusion_model_framework-1.0.0rc2}/diffusion_models/gaussian_diffusion/ddimm_diffuser.py +63 -35
  7. {diffusion_model_framework-1.0.0rc1 → diffusion_model_framework-1.0.0rc2}/diffusion_models/gaussian_diffusion/gaussian_diffuser.py +22 -13
  8. {diffusion_model_framework-1.0.0rc1 → diffusion_model_framework-1.0.0rc2}/diffusion_models/utils/schemas.py +5 -0
  9. {diffusion_model_framework-1.0.0rc1 → diffusion_model_framework-1.0.0rc2}/pyproject.toml +1 -1
  10. diffusion_model_framework-1.0.0rc1/diffusion_models/__init__.py +0 -1
  11. {diffusion_model_framework-1.0.0rc1 → diffusion_model_framework-1.0.0rc2}/README.md +0 -0
  12. {diffusion_model_framework-1.0.0rc1 → diffusion_model_framework-1.0.0rc2}/diffusion_models/gaussian_diffusion/__init__.py +0 -0
  13. {diffusion_model_framework-1.0.0rc1 → diffusion_model_framework-1.0.0rc2}/diffusion_models/gaussian_diffusion/beta_schedulers.py +0 -0
  14. {diffusion_model_framework-1.0.0rc1 → diffusion_model_framework-1.0.0rc2}/diffusion_models/models/SimpleUnet.py +0 -0
  15. {diffusion_model_framework-1.0.0rc1 → diffusion_model_framework-1.0.0rc2}/diffusion_models/models/__init__.py +0 -0
  16. {diffusion_model_framework-1.0.0rc1 → diffusion_model_framework-1.0.0rc2}/diffusion_models/models/base_diffusion_model.py +0 -0
  17. {diffusion_model_framework-1.0.0rc1 → diffusion_model_framework-1.0.0rc2}/diffusion_models/utils/__init__.py +0 -0
  18. {diffusion_model_framework-1.0.0rc1 → diffusion_model_framework-1.0.0rc2}/diffusion_models/utils/tensorboard.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: diffusion-model-framework
3
- Version: 1.0.0rc1
3
+ Version: 1.0.0rc2
4
4
  Summary:
5
5
  Home-page: https://github.com/sachahu1/Diffusion-Model-Framework
6
6
  Author: Sacha Hu
@@ -0,0 +1 @@
1
+ __version__ = "1.0.0-rc.2"
@@ -49,9 +49,7 @@ class DiffusionInference:
49
49
  pil_images = transforms.ToPILImage()(image_grid)
50
50
  return pil_images
51
51
 
52
- def generate(
53
- self, number_of_images: int, save_gif: bool = False
54
- ) -> Image:
52
+ def generate(self, number_of_images: int, save_gif: bool = False) -> Image:
55
53
  """Generate a batch of images.
56
54
 
57
55
  Args:
@@ -117,8 +115,8 @@ class DiffusionInference:
117
115
  device=self.device,
118
116
  )
119
117
 
120
- for i in range(self.model.diffuser.beta_scheduler.steps)[::-1]:
121
- timestep = torch.full((images.shape[0],), i, device=self.device)
118
+ for i in self.model.diffuser.steps:
119
+ timestep = self.model.diffuser.get_timestep(images.shape[0], i)
122
120
  images = self.model.diffuser._denoise_step(
123
121
  images, model=self.model, timestep=timestep
124
122
  )
@@ -7,8 +7,12 @@ from torch.utils.data import DataLoader, Dataset
7
7
  from tqdm import tqdm
8
8
 
9
9
  from diffusion_models.models.base_diffusion_model import BaseDiffusionModel
10
- from diffusion_models.utils.schemas import BetaSchedulerConfiguration, \
11
- Checkpoint, LogConfiguration, TrainingConfiguration
10
+ from diffusion_models.utils.schemas import (
11
+ BetaSchedulerConfiguration,
12
+ Checkpoint,
13
+ LogConfiguration,
14
+ TrainingConfiguration,
15
+ )
12
16
  from diffusion_models.utils.tensorboard import TensorboardManager
13
17
 
14
18
 
@@ -7,6 +7,7 @@ import torch
7
7
  from diffusion_models.gaussian_diffusion.beta_schedulers import (
8
8
  BaseBetaScheduler,
9
9
  )
10
+ from diffusion_models.utils.schemas import Timestep
10
11
 
11
12
  if TYPE_CHECKING:
12
13
  from diffusion_models.models.base_diffusion_model import BaseDiffusionModel
@@ -32,6 +33,17 @@ class BaseDiffuser(abc.ABC):
32
33
  self.beta_scheduler: BaseBetaScheduler = beta_scheduler
33
34
  """The beta scheduler used by the diffuser."""
34
35
 
36
+ @property
37
+ @abc.abstractmethod
38
+ def steps(self) -> List[int]:
39
+ """Returns the list of steps used in the denoising process."""
40
+ raise NotImplementedError
41
+
42
+ @abc.abstractmethod
43
+ def get_timestep(self, number_of_images: int, idx: int) -> Timestep:
44
+ """Get timestep information used for denoising."""
45
+ raise NotImplementedError
46
+
35
47
  @abc.abstractmethod
36
48
  def diffuse_batch(
37
49
  self, images: torch.Tensor
@@ -1,22 +1,31 @@
1
1
  from enum import Enum
2
- from typing import List, Tuple
2
+ from typing import List
3
+ from typing import Tuple
3
4
 
4
5
  import numpy as np
5
6
  import torch
6
7
  from tqdm import tqdm
7
8
 
8
9
  from diffusion_models.gaussian_diffusion.base_diffuser import BaseDiffuser
9
- from diffusion_models.gaussian_diffusion.beta_schedulers import \
10
- BaseBetaScheduler
10
+ from diffusion_models.gaussian_diffusion.beta_schedulers import (
11
+ BaseBetaScheduler,
12
+ )
11
13
  from diffusion_models.models.base_diffusion_model import BaseDiffusionModel
12
- from diffusion_models.utils.schemas import Checkpoint
14
+ from diffusion_models.utils.schemas import Checkpoint, Timestep
15
+
13
16
 
14
17
  class DenoisingMode(str, Enum):
15
18
  Linear = "linear"
16
19
  Quadratic = "quadratic"
17
20
 
21
+
18
22
  class DdimDiffuser(BaseDiffuser):
19
- def __init__(self, beta_scheduler: BaseBetaScheduler):
23
+ def __init__(
24
+ self,
25
+ beta_scheduler: BaseBetaScheduler,
26
+ mode: DenoisingMode = DenoisingMode.Quadratic,
27
+ number_of_steps: int = 20,
28
+ ):
20
29
  """Initializes the class instance.
21
30
 
22
31
  Args:
@@ -24,9 +33,44 @@ class DdimDiffuser(BaseDiffuser):
24
33
 
25
34
  """
26
35
  super().__init__(beta_scheduler)
36
+
37
+ self.number_of_steps = number_of_steps
38
+ """Number of steps to use in the denoising process."""
39
+
40
+ self.mode = mode
41
+ """Linear or Quadratic sampling."""
42
+
27
43
  self.device: str = "cpu"
28
44
  """The device to use. Defaults to cpu."""
29
45
 
46
+ @property
47
+ def steps(self) -> List[int]:
48
+ if self.mode == DenoisingMode.Linear:
49
+ a = self.beta_scheduler.steps // self.number_of_steps
50
+ time_steps = np.asarray(list(range(0, self.beta_scheduler.steps, a)))
51
+ else:
52
+ time_steps = (
53
+ np.linspace(
54
+ 0, np.sqrt(self.beta_scheduler.steps * 0.8), self.number_of_steps
55
+ )
56
+ ** 2
57
+ ).astype(int)
58
+ self._time_steps = time_steps + 1
59
+ self._time_steps_prev = np.concatenate([[0], time_steps[:-1]])
60
+ return list(range(self.number_of_steps))[::-1]
61
+
62
+ def get_timestep(self, number_of_images: int, idx: int) -> Timestep:
63
+ timestep = torch.full(
64
+ (number_of_images,), self._time_steps[idx], device=self.device
65
+ )
66
+ timestep_prev = torch.full(
67
+ (number_of_images,), self._time_steps_prev[idx], device=self.device
68
+ )
69
+ return Timestep(
70
+ current=timestep,
71
+ previous=timestep_prev,
72
+ )
73
+
30
74
  @classmethod
31
75
  def from_checkpoint(cls, checkpoint: Checkpoint) -> "DdimDiffuser":
32
76
  """Instantiate a DDIM Diffuser from a training checkpoint.
@@ -107,18 +151,20 @@ class DdimDiffuser(BaseDiffuser):
107
151
  self,
108
152
  images: torch.Tensor,
109
153
  model: torch.nn.Module,
110
- timestep: torch.Tensor,
111
- timestep_prev: torch.Tensor,
154
+ timestep: Timestep,
112
155
  eta: float = 0.0,
113
156
  ) -> torch.Tensor:
114
- epsilon_theta = model(images, timestep)
157
+ current_timestep = timestep.current
158
+ previous_timestep = timestep.previous
159
+
160
+ epsilon_theta = model(images, current_timestep)
115
161
 
116
162
  alpha_bar_t = self.beta_scheduler.alpha_bars.gather(
117
- dim=0, index=timestep
163
+ dim=0, index=current_timestep
118
164
  ).reshape(-1, 1, 1, 1)
119
165
 
120
166
  alpha_bar_t_prev = self.beta_scheduler.alpha_bars.gather(
121
- dim=0, index=timestep_prev
167
+ dim=0, index=previous_timestep
122
168
  ).reshape(-1, 1, 1, 1)
123
169
 
124
170
  sigma = eta * torch.sqrt(
@@ -134,7 +180,8 @@ class DdimDiffuser(BaseDiffuser):
134
180
  + (
135
181
  torch.sqrt(1 - alpha_bar_t_prev - sigma**2)
136
182
  - torch.sqrt((alpha_bar_t_prev * (1 - alpha_bar_t)) / alpha_bar_t)
137
- ) * epsilon_theta
183
+ )
184
+ * epsilon_theta
138
185
  )
139
186
  return mu + sigma * epsilon_t
140
187
 
@@ -142,8 +189,6 @@ class DdimDiffuser(BaseDiffuser):
142
189
  self,
143
190
  images: torch.Tensor,
144
191
  model: "BaseDiffusionModel",
145
- number_of_steps: int = 50,
146
- mode: DenoisingMode = DenoisingMode.Linear
147
192
  ) -> List[torch.Tensor]:
148
193
  """Denoise a batch of images.
149
194
 
@@ -152,35 +197,18 @@ class DdimDiffuser(BaseDiffuser):
152
197
  Args:
153
198
  images: A batch of noisy images.
154
199
  model: The model to be used for denoising.
155
- number_of_steps: Number of steps used in the denoising process.
156
- mode: Linear or Quadratic sampling.
157
200
 
158
201
  Returns:
159
202
  A list of tensors containing a batch of denoised images.
160
203
  """
161
- if mode == DenoisingMode.Linear:
162
- a = self.beta_scheduler.steps // number_of_steps
163
- time_steps = np.asarray(list(range(0, self.beta_scheduler.steps, a)))
164
- else:
165
- time_steps = (
166
- np.linspace(0, np.sqrt(self.beta_scheduler.steps * 0.8), number_of_steps)
167
- ** 2
168
- ).astype(int)
169
-
170
- time_steps = time_steps + 1
171
- time_steps_prev = np.concatenate([[0], time_steps[:-1]])
172
-
173
204
  denoised_images = []
174
- for i in tqdm(range(number_of_steps)[::-1], desc="Denoising"):
175
- timestep = torch.full(
176
- (images.shape[0],), time_steps[i], device=self.device
177
- )
178
- timestep_prev = torch.full(
179
- (images.shape[0],), time_steps_prev[i], device=self.device
180
- )
205
+ for i in tqdm(self.steps, desc="Denoising"):
206
+ timestep = self.get_timestep(images.shape[0], idx=i)
181
207
 
182
208
  images = self._denoise_step(
183
- images, model=model, timestep=timestep, timestep_prev=timestep_prev,
209
+ images,
210
+ model=model,
211
+ timestep=timestep,
184
212
  )
185
213
  images = torch.clamp(images, -1.0, 1.0)
186
214
  denoised_images.append(images)
@@ -1,8 +1,5 @@
1
- from typing import TYPE_CHECKING
2
- from typing import List
3
- from typing import Tuple
1
+ from typing import TYPE_CHECKING, List, Tuple
4
2
 
5
- import numpy as np
6
3
  import torch
7
4
  from tqdm import tqdm
8
5
 
@@ -10,8 +7,7 @@ from diffusion_models.gaussian_diffusion.base_diffuser import BaseDiffuser
10
7
  from diffusion_models.gaussian_diffusion.beta_schedulers import (
11
8
  BaseBetaScheduler,
12
9
  )
13
- from diffusion_models.utils.schemas import Checkpoint
14
-
10
+ from diffusion_models.utils.schemas import Checkpoint, Timestep
15
11
 
16
12
  if TYPE_CHECKING:
17
13
  from diffusion_models.models.base_diffusion_model import BaseDiffusionModel
@@ -29,6 +25,16 @@ class GaussianDiffuser(BaseDiffuser):
29
25
  self.device: str = "cpu"
30
26
  """The device to use. Defaults to cpu."""
31
27
 
28
+ @property
29
+ def steps(self) -> List[int]:
30
+ return list(range(self.beta_scheduler.steps))[::-1]
31
+
32
+ def get_timestep(self, number_of_images: int, idx: int):
33
+ timestep = torch.full((number_of_images,), idx, device=self.device)
34
+ return Timestep(
35
+ current=timestep,
36
+ )
37
+
32
38
  @classmethod
33
39
  def from_checkpoint(cls, checkpoint: Checkpoint) -> "GaussianDiffuser":
34
40
  """Instantiate a Gaussian Diffuser from a training checkpoint.
@@ -106,18 +112,21 @@ class GaussianDiffuser(BaseDiffuser):
106
112
 
107
113
  @torch.no_grad()
108
114
  def _denoise_step(
109
- self, images: torch.Tensor, model: torch.nn.Module, timestep: torch.Tensor
115
+ self, images: torch.Tensor, model: torch.nn.Module, timestep: Timestep
110
116
  ) -> torch.Tensor:
111
- beta_t = self.beta_scheduler.betas[timestep].reshape(-1, 1, 1, 1)
117
+ current_timestep = timestep.current
118
+ beta_t = self.beta_scheduler.betas[current_timestep].reshape(-1, 1, 1, 1)
112
119
  alpha_t = 1 - beta_t
113
120
  alpha_bar_t = self.beta_scheduler.alpha_bars.gather(
114
- dim=0, index=timestep
121
+ dim=0, index=current_timestep
115
122
  ).reshape(-1, 1, 1, 1)
116
123
  mu = (1 / torch.sqrt(alpha_t)) * (
117
- images - model(images, timestep) * (beta_t / torch.sqrt(1 - alpha_bar_t))
124
+ images
125
+ - model(images, current_timestep)
126
+ * (beta_t / torch.sqrt(1 - alpha_bar_t))
118
127
  )
119
128
 
120
- if timestep[0] == 0:
129
+ if current_timestep[0] == 0:
121
130
  return mu
122
131
  else:
123
132
  sigma = torch.sqrt(beta_t) * torch.randn_like(images)
@@ -140,8 +149,8 @@ class GaussianDiffuser(BaseDiffuser):
140
149
  A list of tensors containing a batch of denoised images.
141
150
  """
142
151
  denoised_images = []
143
- for i in tqdm(range(self.beta_scheduler.steps)[::-1], desc="Denoising"):
144
- timestep = torch.full((images.shape[0],), i, device=self.device)
152
+ for i in tqdm(self.steps, desc="Denoising"):
153
+ timestep = self.get_timestep(images.shape[0], idx=i)
145
154
  images = self._denoise_step(images, model=model, timestep=timestep)
146
155
  images = torch.clamp(images, -1.0, 1.0)
147
156
  denoised_images.append(images)
@@ -144,3 +144,8 @@ class OldCheckpoint:
144
144
  return Checkpoint(
145
145
  **dataclasses.asdict(self), beta_scheduler_config=beta_scheduler_config
146
146
  )
147
+
148
+ @dataclass
149
+ class Timestep:
150
+ current: torch.Tensor
151
+ previous: Optional[torch.Tensor] = None
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "diffusion-model-framework"
3
- version = "1.0.0-rc.1"
3
+ version = "1.0.0-rc.2"
4
4
  description = ""
5
5
  authors = ["Sacha Hu <sachahu@mindsparkai.com>"]
6
6
  readme = "README.md"
@@ -1 +0,0 @@
1
- __version__ = "1.0.0-rc.1"