diffusion-model-framework 0.1.2rc3__tar.gz → 1.0.0__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-0.1.2rc3 → diffusion_model_framework-1.0.0}/PKG-INFO +11 -8
  2. diffusion_model_framework-1.0.0/diffusion_models/__init__.py +1 -0
  3. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0}/diffusion_models/diffusion_inference.py +3 -5
  4. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0}/diffusion_models/diffusion_trainer.py +6 -2
  5. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0}/diffusion_models/gaussian_diffusion/base_diffuser.py +16 -3
  6. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0}/diffusion_models/gaussian_diffusion/beta_schedulers.py +4 -3
  7. diffusion_model_framework-1.0.0/diffusion_models/gaussian_diffusion/ddimm_diffuser.py +215 -0
  8. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0}/diffusion_models/gaussian_diffusion/gaussian_diffuser.py +38 -19
  9. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0}/diffusion_models/models/SimpleUnet.py +2 -1
  10. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0}/diffusion_models/models/base_diffusion_model.py +10 -6
  11. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0}/diffusion_models/utils/schemas.py +19 -4
  12. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0}/pyproject.toml +18 -7
  13. diffusion_model_framework-0.1.2rc3/diffusion_models/__init__.py +0 -1
  14. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0}/README.md +0 -0
  15. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0}/diffusion_models/gaussian_diffusion/__init__.py +0 -0
  16. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0}/diffusion_models/models/__init__.py +0 -0
  17. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0}/diffusion_models/utils/__init__.py +0 -0
  18. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0}/diffusion_models/utils/tensorboard.py +0 -0
@@ -1,21 +1,24 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: diffusion-model-framework
3
- Version: 0.1.2rc3
3
+ Version: 1.0.0
4
4
  Summary:
5
5
  Home-page: https://github.com/sachahu1/Diffusion-Model-Framework
6
6
  Author: Sacha Hu
7
7
  Author-email: sachahu@mindsparkai.com
8
- Requires-Python: >=3.9,<3.12
8
+ Requires-Python: >=3.10,<3.13
9
9
  Classifier: Programming Language :: Python :: 3
10
- Classifier: Programming Language :: Python :: 3.9
11
10
  Classifier: Programming Language :: Python :: 3.10
12
11
  Classifier: Programming Language :: Python :: 3.11
13
- Requires-Dist: numpy (>=1.26.4,<2.0.0)
14
- Requires-Dist: pillow-simd (>=9.0.0.post1,<10.0.0)
15
- Requires-Dist: tensorboard (>=2.17.0,<3.0.0)
16
- Requires-Dist: torch (>=2.4.0,<3.0.0)
17
- Requires-Dist: torchvision (>=0.19.0,<0.20.0)
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Requires-Dist: jupyter (>=1.0.0,<2.0.0)
14
+ Requires-Dist: matplotlib (>=3.9.2,<4.0.0)
15
+ Requires-Dist: numpy (>=2.1.3,<3.0.0)
16
+ Requires-Dist: pillow (>=11.0.0,<12.0.0)
17
+ Requires-Dist: tensorboard (>=2.18.0,<3.0.0)
18
+ Requires-Dist: torch (>=2.5.1,<3.0.0)
19
+ Requires-Dist: torchvision (>=0.20.1,<0.21.0)
18
20
  Requires-Dist: tqdm (>=4.66.2,<5.0.0)
21
+ Requires-Dist: transformers (>=4.46.1,<5.0.0)
19
22
  Project-URL: Documentation, https://www.sachahu.com/docs/diffusion-model-framework/latest/index.html
20
23
  Project-URL: Repository, https://github.com/sachahu1/Diffusion-Model-Framework
21
24
  Description-Content-Type: text/markdown
@@ -0,0 +1 @@
1
+ __version__ = "1.0.0"
@@ -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,12 +7,13 @@ 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
11
+
10
12
  if TYPE_CHECKING:
11
13
  from diffusion_models.models.base_diffusion_model import BaseDiffusionModel
12
14
 
13
15
 
14
16
  class BaseDiffuser(abc.ABC):
15
-
16
17
  def __init__(self, beta_scheduler: BaseBetaScheduler):
17
18
  """Initializes the object with the specified beta scheduler.
18
19
 
@@ -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
@@ -51,7 +63,9 @@ class BaseDiffuser(abc.ABC):
51
63
  raise NotImplementedError()
52
64
 
53
65
  @abc.abstractmethod
54
- def denoise_batch(self, images: torch.Tensor, model: "BaseDiffusionModel") -> List[torch.Tensor]:
66
+ def denoise_batch(
67
+ self, images: torch.Tensor, model: "BaseDiffusionModel"
68
+ ) -> List[torch.Tensor]:
55
69
  """Denoise a batch of images.
56
70
 
57
71
  Args:
@@ -75,4 +89,3 @@ class BaseDiffuser(abc.ABC):
75
89
  Default is "cpu".
76
90
  """
77
91
  raise NotImplementedError()
78
-
@@ -6,7 +6,8 @@ import torch
6
6
 
7
7
 
8
8
  class BaseBetaScheduler:
9
- def __init__(self,
9
+ def __init__(
10
+ self,
10
11
  steps: int,
11
12
  enforce_zero_terminal_snr: bool = False,
12
13
  initialize: bool = True,
@@ -46,8 +47,8 @@ class BaseBetaScheduler:
46
47
  self.enforce_zero_terminal_snr()
47
48
 
48
49
  def _initialize(self):
49
- self.betas = self.sample_betas()
50
- self.alpha_bars = self.compute_alpha_bar()
50
+ self.betas = self.sample_betas()
51
+ self.alpha_bars = self.compute_alpha_bar()
51
52
 
52
53
  def enforce_zero_terminal_snr(self):
53
54
  """Enforce terminal SNR by adjusting :math:`\\beta` and :math:`\\bar{\\alpha}`.
@@ -0,0 +1,215 @@
1
+ from enum import Enum
2
+ from typing import List
3
+ from typing import Tuple
4
+
5
+ import numpy as np
6
+ import torch
7
+ from tqdm import tqdm
8
+
9
+ from diffusion_models.gaussian_diffusion.base_diffuser import BaseDiffuser
10
+ from diffusion_models.gaussian_diffusion.beta_schedulers import (
11
+ BaseBetaScheduler,
12
+ )
13
+ from diffusion_models.models.base_diffusion_model import BaseDiffusionModel
14
+ from diffusion_models.utils.schemas import Checkpoint, Timestep
15
+
16
+
17
+ class DenoisingMode(str, Enum):
18
+ Linear = "linear"
19
+ Quadratic = "quadratic"
20
+
21
+
22
+ class DdimDiffuser(BaseDiffuser):
23
+ def __init__(
24
+ self,
25
+ beta_scheduler: BaseBetaScheduler,
26
+ mode: DenoisingMode = DenoisingMode.Quadratic,
27
+ number_of_steps: int = 20,
28
+ ):
29
+ """Initializes the class instance.
30
+
31
+ Args:
32
+ beta_scheduler (BaseBetaScheduler): The beta scheduler instance to be used.
33
+
34
+ """
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
+
43
+ self.device: str = "cpu"
44
+ """The device to use. Defaults to cpu."""
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
+
74
+ @classmethod
75
+ def from_checkpoint(cls, checkpoint: Checkpoint) -> "DdimDiffuser":
76
+ """Instantiate a DDIM Diffuser from a training checkpoint.
77
+
78
+ Args:
79
+ checkpoint: The training checkpoint object containing
80
+ the trained model's parameters and configuration.
81
+
82
+ Returns:
83
+ An instance of the DdimDiffuser class initialized with the parameters
84
+ loaded from the given checkpoint.
85
+ """
86
+ return cls(
87
+ beta_scheduler=BaseBetaScheduler.from_tensors(
88
+ steps=checkpoint.beta_scheduler_config.steps,
89
+ betas=checkpoint.beta_scheduler_config.betas,
90
+ alpha_bars=checkpoint.beta_scheduler_config.alpha_bars,
91
+ )
92
+ )
93
+
94
+ def to(self, device: str = "cpu"):
95
+ """Moves the data to the specified device.
96
+
97
+ This performs a similar behaviour to the `to` method of PyTorch. moving the
98
+ GaussianDiffuser and the BetaScheduler to the specified device.
99
+
100
+ Args:
101
+ device: The device to which the method should move the object.
102
+ Default is "cpu".
103
+
104
+ Example:
105
+ >>> ddim_diffuser = DdimDiffuser()
106
+ >>> ddim_diffuser = ddim_diffuser.to(device="cuda")
107
+ """
108
+ self.device = device
109
+ self.beta_scheduler = self.beta_scheduler.to(self.device)
110
+ return self
111
+
112
+ def _diffuse_batch(
113
+ self, images: torch.Tensor, timesteps: torch.Tensor
114
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
115
+ noise = torch.randn_like(images, device=self.device)
116
+
117
+ alpha_bar_t = self.beta_scheduler.alpha_bars.gather(dim=0, index=timesteps)
118
+
119
+ alpha_bar_t = alpha_bar_t.reshape((-1, *((1,) * (len(images.shape) - 1))))
120
+
121
+ mu = torch.sqrt(alpha_bar_t)
122
+ sigma = torch.sqrt(1 - alpha_bar_t)
123
+ images = mu * images + sigma * noise
124
+ return images, noise, timesteps
125
+
126
+ def diffuse_batch(
127
+ self, images: torch.Tensor
128
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
129
+ """Diffuse a batch of images.
130
+
131
+ Diffuse the given batch of images by adding noise based on the beta scheduler.
132
+
133
+ Args:
134
+ images: Batch of images to diffuse.\n
135
+ Shape should be ``(B, C, H, W)``.
136
+
137
+ Returns:
138
+ A tuple containing three tensors
139
+
140
+ - images: Diffused batch of images.
141
+ - noise: Noise added to the images.
142
+ - timesteps: Timesteps used for diffusion.
143
+ """
144
+ timesteps = torch.randint(
145
+ 0, self.beta_scheduler.steps, (images.shape[0],), device=self.device
146
+ )
147
+ return self._diffuse_batch(images, timesteps)
148
+
149
+ @torch.no_grad()
150
+ def _denoise_step(
151
+ self,
152
+ images: torch.Tensor,
153
+ model: torch.nn.Module,
154
+ timestep: Timestep,
155
+ eta: float = 0.0,
156
+ ) -> torch.Tensor:
157
+ current_timestep = timestep.current
158
+ previous_timestep = timestep.previous
159
+
160
+ epsilon_theta = model(images, current_timestep)
161
+
162
+ alpha_bar_t = self.beta_scheduler.alpha_bars.gather(
163
+ dim=0, index=current_timestep
164
+ ).reshape(-1, 1, 1, 1)
165
+
166
+ alpha_bar_t_prev = self.beta_scheduler.alpha_bars.gather(
167
+ dim=0, index=previous_timestep
168
+ ).reshape(-1, 1, 1, 1)
169
+
170
+ sigma = eta * torch.sqrt(
171
+ (1 - alpha_bar_t_prev)
172
+ / (1 - alpha_bar_t)
173
+ * (1 - alpha_bar_t / alpha_bar_t_prev)
174
+ )
175
+
176
+ epsilon_t = torch.randn_like(images)
177
+
178
+ mu = (
179
+ torch.sqrt(alpha_bar_t_prev / alpha_bar_t) * images
180
+ + (
181
+ torch.sqrt(1 - alpha_bar_t_prev - sigma**2)
182
+ - torch.sqrt((alpha_bar_t_prev * (1 - alpha_bar_t)) / alpha_bar_t)
183
+ )
184
+ * epsilon_theta
185
+ )
186
+ return mu + sigma * epsilon_t
187
+
188
+ def denoise_batch(
189
+ self,
190
+ images: torch.Tensor,
191
+ model: "BaseDiffusionModel",
192
+ ) -> List[torch.Tensor]:
193
+ """Denoise a batch of images.
194
+
195
+ This denoises a batch images. This is the image generation process.
196
+
197
+ Args:
198
+ images: A batch of noisy images.
199
+ model: The model to be used for denoising.
200
+
201
+ Returns:
202
+ A list of tensors containing a batch of denoised images.
203
+ """
204
+ denoised_images = []
205
+ for i in tqdm(self.steps, desc="Denoising"):
206
+ timestep = self.get_timestep(images.shape[0], idx=i)
207
+
208
+ images = self._denoise_step(
209
+ images,
210
+ model=model,
211
+ timestep=timestep,
212
+ )
213
+ images = torch.clamp(images, -1.0, 1.0)
214
+ denoised_images.append(images)
215
+ return denoised_images
@@ -1,4 +1,4 @@
1
- from typing import List, Tuple, TYPE_CHECKING
1
+ from typing import TYPE_CHECKING, List, Tuple
2
2
 
3
3
  import torch
4
4
  from tqdm import tqdm
@@ -7,11 +7,12 @@ from diffusion_models.gaussian_diffusion.base_diffuser import BaseDiffuser
7
7
  from diffusion_models.gaussian_diffusion.beta_schedulers import (
8
8
  BaseBetaScheduler,
9
9
  )
10
- from diffusion_models.utils.schemas import Checkpoint
10
+ from diffusion_models.utils.schemas import Checkpoint, Timestep
11
11
 
12
12
  if TYPE_CHECKING:
13
13
  from diffusion_models.models.base_diffusion_model import BaseDiffusionModel
14
14
 
15
+
15
16
  class GaussianDiffuser(BaseDiffuser):
16
17
  def __init__(self, beta_scheduler: BaseBetaScheduler):
17
18
  """Initializes the class instance.
@@ -24,6 +25,16 @@ class GaussianDiffuser(BaseDiffuser):
24
25
  self.device: str = "cpu"
25
26
  """The device to use. Defaults to cpu."""
26
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
+
27
38
  @classmethod
28
39
  def from_checkpoint(cls, checkpoint: Checkpoint) -> "GaussianDiffuser":
29
40
  """Instantiate a Gaussian Diffuser from a training checkpoint.
@@ -62,6 +73,20 @@ class GaussianDiffuser(BaseDiffuser):
62
73
  self.beta_scheduler = self.beta_scheduler.to(self.device)
63
74
  return self
64
75
 
76
+ def _diffuse_batch(
77
+ self, images: torch.Tensor, timesteps: torch.Tensor
78
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
79
+ noise = torch.randn_like(images, device=self.device)
80
+
81
+ alpha_bar_t = self.beta_scheduler.alpha_bars.gather(dim=0, index=timesteps)
82
+
83
+ alpha_bar_t = alpha_bar_t.reshape((-1, *((1,) * (len(images.shape) - 1))))
84
+
85
+ mu = torch.sqrt(alpha_bar_t)
86
+ sigma = torch.sqrt(1 - alpha_bar_t)
87
+ images = mu * images + sigma * noise
88
+ return images, noise, timesteps
89
+
65
90
  def diffuse_batch(
66
91
  self, images: torch.Tensor
67
92
  ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
@@ -83,31 +108,25 @@ class GaussianDiffuser(BaseDiffuser):
83
108
  timesteps = torch.randint(
84
109
  0, self.beta_scheduler.steps, (images.shape[0],), device=self.device
85
110
  )
86
- noise = torch.randn_like(images, device=self.device)
87
-
88
- alpha_bar_t = self.beta_scheduler.alpha_bars.gather(dim=0, index=timesteps)
89
-
90
- alpha_bar_t = alpha_bar_t.reshape((-1, *((1,) * (len(images.shape) - 1))))
91
-
92
- mu = torch.sqrt(alpha_bar_t)
93
- sigma = torch.sqrt(1 - alpha_bar_t)
94
- images = mu * images + sigma * noise
95
- return images, noise, timesteps
111
+ return self._diffuse_batch(images, timesteps)
96
112
 
97
113
  @torch.no_grad()
98
114
  def _denoise_step(
99
- self, images: torch.Tensor, model: torch.nn.Module, timestep: torch.Tensor
115
+ self, images: torch.Tensor, model: torch.nn.Module, timestep: Timestep
100
116
  ) -> torch.Tensor:
101
- 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)
102
119
  alpha_t = 1 - beta_t
103
120
  alpha_bar_t = self.beta_scheduler.alpha_bars.gather(
104
- dim=0, index=timestep
121
+ dim=0, index=current_timestep
105
122
  ).reshape(-1, 1, 1, 1)
106
123
  mu = (1 / torch.sqrt(alpha_t)) * (
107
- 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))
108
127
  )
109
128
 
110
- if timestep[0] == 0:
129
+ if current_timestep[0] == 0:
111
130
  return mu
112
131
  else:
113
132
  sigma = torch.sqrt(beta_t) * torch.randn_like(images)
@@ -130,8 +149,8 @@ class GaussianDiffuser(BaseDiffuser):
130
149
  A list of tensors containing a batch of denoised images.
131
150
  """
132
151
  denoised_images = []
133
- for i in tqdm(range(self.beta_scheduler.steps)[::-1], desc="Denoising"):
134
- 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)
135
154
  images = self._denoise_step(images, model=model, timestep=timestep)
136
155
  images = torch.clamp(images, -1.0, 1.0)
137
156
  denoised_images.append(images)
@@ -4,6 +4,7 @@ import math
4
4
  import torch
5
5
  from torch import nn
6
6
 
7
+ from diffusion_models.gaussian_diffusion.base_diffuser import BaseDiffuser
7
8
  from diffusion_models.gaussian_diffusion.gaussian_diffuser import (
8
9
  GaussianDiffuser,
9
10
  )
@@ -63,7 +64,7 @@ class SinusoidalPositionEmbeddings(nn.Module):
63
64
  class SimpleUnet(BaseDiffusionModel):
64
65
  def __init__(
65
66
  self,
66
- diffuser: GaussianDiffuser,
67
+ diffuser: BaseDiffuser,
67
68
  image_channels: int,
68
69
  ):
69
70
  """A Simplified variant of the Unet architecture used in DDPM.
@@ -48,16 +48,20 @@ class BaseDiffusionModel(nn.Module, abc.ABC):
48
48
  def denoise(self, images: torch.Tensor) -> List[torch.Tensor]:
49
49
  """Denoise a batch of images.
50
50
 
51
- Args:
52
- images: A tensor containing a batch of images to denoise.
51
+ Args:
52
+ images: A tensor containing a batch of images to denoise.
53
53
 
54
- Returns:
55
- A list of tensors containing a batch of denoised images.
56
- """
54
+ Returns:
55
+ A list of tensors containing a batch of denoised images.
56
+ """
57
57
  return self.diffuser.denoise_batch(images=images, model=self)
58
58
 
59
59
  @abc.abstractmethod
60
- def forward(self, x: torch.Tensor, timestep: torch.Tensor) -> torch.Tensor:
60
+ def forward(
61
+ self,
62
+ x: torch.Tensor,
63
+ timestep: torch.Tensor,
64
+ ) -> torch.Tensor:
61
65
  """Forward pass of the diffusion model.
62
66
 
63
67
  The forward pass of the diffusion model, predicting the noise at a single
@@ -17,6 +17,7 @@ from diffusion_models.gaussian_diffusion.beta_schedulers import (
17
17
  @dataclass
18
18
  class TrainingConfiguration:
19
19
  """A training configuration for simple experiment management."""
20
+
20
21
  training_name: str
21
22
  """The name of the training."""
22
23
  batch_size: int
@@ -36,6 +37,7 @@ class TrainingConfiguration:
36
37
  @dataclass
37
38
  class LogConfiguration:
38
39
  """An object to manage logging configuration."""
40
+
39
41
  log_rate: int = 10
40
42
  """The rate at which training metrics are logged."""
41
43
  image_rate: int = 50
@@ -48,6 +50,7 @@ class LogConfiguration:
48
50
  @dataclass
49
51
  class BetaSchedulerConfiguration:
50
52
  """A simplified beta scheduler configuration."""
53
+
51
54
  steps: int
52
55
  """The number of steps in the beta scheduler."""
53
56
  betas: torch.Tensor
@@ -55,19 +58,21 @@ class BetaSchedulerConfiguration:
55
58
  alpha_bars: torch.Tensor
56
59
  """The alpha bar values."""
57
60
 
61
+
58
62
  @dataclass
59
63
  class Checkpoint:
60
64
  """A simplified checkpoint framework for easy saving and loading."""
65
+
61
66
  epoch: int
62
67
  """The current epoch."""
63
68
  model_state_dict: Dict[str, Any]
64
69
  """The model state dict."""
65
70
  optimizer_state_dict: Dict[str, Any]
66
71
  """The optimizer state dict."""
67
- scaler: Optional[GradScaler]
68
- """The GradScaler instance."""
69
72
  beta_scheduler_config: BetaSchedulerConfiguration
70
73
  """The beta scheduler configuration."""
74
+ scaler: Optional[GradScaler] = None
75
+ """The GradScaler instance."""
71
76
  tensorboard_run_name: Optional[str] = None
72
77
  """The name of the tensorboard run."""
73
78
  image_channels: int = 3
@@ -83,7 +88,9 @@ class Checkpoint:
83
88
  """
84
89
 
85
90
  @classmethod
86
- def from_file(cls, file_path: str, map_location: Optional[str] = None) -> "Checkpoint":
91
+ def from_file(
92
+ cls, file_path: str, map_location: Optional[str] = None
93
+ ) -> "Checkpoint":
87
94
  """Load and instantiate a checkpoint from a file.
88
95
 
89
96
  Args:
@@ -93,7 +100,9 @@ class Checkpoint:
93
100
  Returns:
94
101
  A checkpoint instance.
95
102
  """
96
- checkpoint = torch.load(f=file_path, weights_only=True, map_location=map_location)
103
+ checkpoint = torch.load(
104
+ f=file_path, weights_only=True, map_location=map_location
105
+ )
97
106
  checkpoint = cls(**checkpoint)
98
107
  beta_scheduler_config = BetaSchedulerConfiguration(
99
108
  **checkpoint.beta_scheduler_config
@@ -135,3 +144,9 @@ class OldCheckpoint:
135
144
  return Checkpoint(
136
145
  **dataclasses.asdict(self), beta_scheduler_config=beta_scheduler_config
137
146
  )
147
+
148
+
149
+ @dataclass
150
+ class Timestep:
151
+ current: torch.Tensor
152
+ previous: Optional[torch.Tensor] = None
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "diffusion-model-framework"
3
- version = "0.1.2-rc.3"
3
+ version = "1.0.0"
4
4
  description = ""
5
5
  authors = ["Sacha Hu <sachahu@mindsparkai.com>"]
6
6
  readme = "README.md"
@@ -11,13 +11,17 @@ packages = [
11
11
  ]
12
12
 
13
13
  [tool.poetry.dependencies]
14
- python = ">=3.9,<3.12"
15
- pillow-simd = "^9.0.0.post1"
14
+ python = ">=3.10,<3.13"
15
+ #pillow-simd = "^9.0.0"
16
16
  tqdm = "^4.66.2"
17
- numpy = "^1.26.4"
18
- torchvision = "^0.19.0"
19
- torch = "^2.4.0"
20
- tensorboard = "^2.17.0"
17
+ numpy = "^2.1.3"
18
+ torchvision = "^0.20.1"
19
+ torch = "^2.5.1"
20
+ tensorboard = "^2.18.0"
21
+ matplotlib = "^3.9.2"
22
+ jupyter = "^1.0.0"
23
+ pillow = "^11.0.0"
24
+ transformers = "^4.46.1"
21
25
 
22
26
  [tool.poetry.group.gdown]
23
27
  optional = true
@@ -51,6 +55,12 @@ sphinx-copybutton = "^0.5.2"
51
55
  sphinx-autoapi = "^3.2.1"
52
56
  gitpython = "^3.1.43"
53
57
 
58
+ [tool.poetry.group.coco]
59
+ optional = true
60
+
61
+ [tool.poetry.group.coco.dependencies]
62
+ pycocotools = "^2.0.8"
63
+
54
64
  [build-system]
55
65
  requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.0,<2.0.0"]
56
66
  build-backend = "poetry_dynamic_versioning.backend"
@@ -64,6 +74,7 @@ exclude = [".venv", ".env", ".git", "tests"]
64
74
  # Add the `line-too-long` rule to the enforced rule set. By default, Ruff omits rules that
65
75
  # overlap with the use of a formatter, like Black, but we can override this behavior by
66
76
  # explicitly adding the rule.
77
+ select = ["NPY201"]
67
78
  extend-select = [
68
79
  "B", # flake8-bugbear
69
80
  "C4", # flake8-comprehensions
@@ -1 +0,0 @@
1
- __version__ = "0.1.2-rc.3"