diffusion-model-framework 0.1.2rc3__tar.gz → 1.0.0rc1__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.0rc1}/PKG-INFO +11 -8
  2. diffusion_model_framework-1.0.0rc1/diffusion_models/__init__.py +1 -0
  3. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0rc1}/diffusion_models/gaussian_diffusion/base_diffuser.py +4 -3
  4. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0rc1}/diffusion_models/gaussian_diffusion/beta_schedulers.py +4 -3
  5. diffusion_model_framework-1.0.0rc1/diffusion_models/gaussian_diffusion/ddimm_diffuser.py +187 -0
  6. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0rc1}/diffusion_models/gaussian_diffusion/gaussian_diffuser.py +21 -11
  7. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0rc1}/diffusion_models/models/SimpleUnet.py +2 -1
  8. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0rc1}/diffusion_models/models/base_diffusion_model.py +10 -6
  9. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0rc1}/diffusion_models/utils/schemas.py +11 -2
  10. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0rc1}/pyproject.toml +18 -7
  11. diffusion_model_framework-0.1.2rc3/diffusion_models/__init__.py +0 -1
  12. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0rc1}/README.md +0 -0
  13. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0rc1}/diffusion_models/diffusion_inference.py +0 -0
  14. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0rc1}/diffusion_models/diffusion_trainer.py +0 -0
  15. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0rc1}/diffusion_models/gaussian_diffusion/__init__.py +0 -0
  16. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0rc1}/diffusion_models/models/__init__.py +0 -0
  17. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0rc1}/diffusion_models/utils/__init__.py +0 -0
  18. {diffusion_model_framework-0.1.2rc3 → diffusion_model_framework-1.0.0rc1}/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.0rc1
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-rc.1"
@@ -7,12 +7,12 @@ import torch
7
7
  from diffusion_models.gaussian_diffusion.beta_schedulers import (
8
8
  BaseBetaScheduler,
9
9
  )
10
+
10
11
  if TYPE_CHECKING:
11
12
  from diffusion_models.models.base_diffusion_model import BaseDiffusionModel
12
13
 
13
14
 
14
15
  class BaseDiffuser(abc.ABC):
15
-
16
16
  def __init__(self, beta_scheduler: BaseBetaScheduler):
17
17
  """Initializes the object with the specified beta scheduler.
18
18
 
@@ -51,7 +51,9 @@ class BaseDiffuser(abc.ABC):
51
51
  raise NotImplementedError()
52
52
 
53
53
  @abc.abstractmethod
54
- def denoise_batch(self, images: torch.Tensor, model: "BaseDiffusionModel") -> List[torch.Tensor]:
54
+ def denoise_batch(
55
+ self, images: torch.Tensor, model: "BaseDiffusionModel"
56
+ ) -> List[torch.Tensor]:
55
57
  """Denoise a batch of images.
56
58
 
57
59
  Args:
@@ -75,4 +77,3 @@ class BaseDiffuser(abc.ABC):
75
77
  Default is "cpu".
76
78
  """
77
79
  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,187 @@
1
+ from enum import Enum
2
+ from typing import List, Tuple
3
+
4
+ import numpy as np
5
+ import torch
6
+ from tqdm import tqdm
7
+
8
+ from diffusion_models.gaussian_diffusion.base_diffuser import BaseDiffuser
9
+ from diffusion_models.gaussian_diffusion.beta_schedulers import \
10
+ BaseBetaScheduler
11
+ from diffusion_models.models.base_diffusion_model import BaseDiffusionModel
12
+ from diffusion_models.utils.schemas import Checkpoint
13
+
14
+ class DenoisingMode(str, Enum):
15
+ Linear = "linear"
16
+ Quadratic = "quadratic"
17
+
18
+ class DdimDiffuser(BaseDiffuser):
19
+ def __init__(self, beta_scheduler: BaseBetaScheduler):
20
+ """Initializes the class instance.
21
+
22
+ Args:
23
+ beta_scheduler (BaseBetaScheduler): The beta scheduler instance to be used.
24
+
25
+ """
26
+ super().__init__(beta_scheduler)
27
+ self.device: str = "cpu"
28
+ """The device to use. Defaults to cpu."""
29
+
30
+ @classmethod
31
+ def from_checkpoint(cls, checkpoint: Checkpoint) -> "DdimDiffuser":
32
+ """Instantiate a DDIM Diffuser from a training checkpoint.
33
+
34
+ Args:
35
+ checkpoint: The training checkpoint object containing
36
+ the trained model's parameters and configuration.
37
+
38
+ Returns:
39
+ An instance of the DdimDiffuser class initialized with the parameters
40
+ loaded from the given checkpoint.
41
+ """
42
+ return cls(
43
+ beta_scheduler=BaseBetaScheduler.from_tensors(
44
+ steps=checkpoint.beta_scheduler_config.steps,
45
+ betas=checkpoint.beta_scheduler_config.betas,
46
+ alpha_bars=checkpoint.beta_scheduler_config.alpha_bars,
47
+ )
48
+ )
49
+
50
+ def to(self, device: str = "cpu"):
51
+ """Moves the data to the specified device.
52
+
53
+ This performs a similar behaviour to the `to` method of PyTorch. moving the
54
+ GaussianDiffuser and the BetaScheduler to the specified device.
55
+
56
+ Args:
57
+ device: The device to which the method should move the object.
58
+ Default is "cpu".
59
+
60
+ Example:
61
+ >>> ddim_diffuser = DdimDiffuser()
62
+ >>> ddim_diffuser = ddim_diffuser.to(device="cuda")
63
+ """
64
+ self.device = device
65
+ self.beta_scheduler = self.beta_scheduler.to(self.device)
66
+ return self
67
+
68
+ def _diffuse_batch(
69
+ self, images: torch.Tensor, timesteps: torch.Tensor
70
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
71
+ noise = torch.randn_like(images, device=self.device)
72
+
73
+ alpha_bar_t = self.beta_scheduler.alpha_bars.gather(dim=0, index=timesteps)
74
+
75
+ alpha_bar_t = alpha_bar_t.reshape((-1, *((1,) * (len(images.shape) - 1))))
76
+
77
+ mu = torch.sqrt(alpha_bar_t)
78
+ sigma = torch.sqrt(1 - alpha_bar_t)
79
+ images = mu * images + sigma * noise
80
+ return images, noise, timesteps
81
+
82
+ def diffuse_batch(
83
+ self, images: torch.Tensor
84
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
85
+ """Diffuse a batch of images.
86
+
87
+ Diffuse the given batch of images by adding noise based on the beta scheduler.
88
+
89
+ Args:
90
+ images: Batch of images to diffuse.\n
91
+ Shape should be ``(B, C, H, W)``.
92
+
93
+ Returns:
94
+ A tuple containing three tensors
95
+
96
+ - images: Diffused batch of images.
97
+ - noise: Noise added to the images.
98
+ - timesteps: Timesteps used for diffusion.
99
+ """
100
+ timesteps = torch.randint(
101
+ 0, self.beta_scheduler.steps, (images.shape[0],), device=self.device
102
+ )
103
+ return self._diffuse_batch(images, timesteps)
104
+
105
+ @torch.no_grad()
106
+ def _denoise_step(
107
+ self,
108
+ images: torch.Tensor,
109
+ model: torch.nn.Module,
110
+ timestep: torch.Tensor,
111
+ timestep_prev: torch.Tensor,
112
+ eta: float = 0.0,
113
+ ) -> torch.Tensor:
114
+ epsilon_theta = model(images, timestep)
115
+
116
+ alpha_bar_t = self.beta_scheduler.alpha_bars.gather(
117
+ dim=0, index=timestep
118
+ ).reshape(-1, 1, 1, 1)
119
+
120
+ alpha_bar_t_prev = self.beta_scheduler.alpha_bars.gather(
121
+ dim=0, index=timestep_prev
122
+ ).reshape(-1, 1, 1, 1)
123
+
124
+ sigma = eta * torch.sqrt(
125
+ (1 - alpha_bar_t_prev)
126
+ / (1 - alpha_bar_t)
127
+ * (1 - alpha_bar_t / alpha_bar_t_prev)
128
+ )
129
+
130
+ epsilon_t = torch.randn_like(images)
131
+
132
+ mu = (
133
+ torch.sqrt(alpha_bar_t_prev / alpha_bar_t) * images
134
+ + (
135
+ torch.sqrt(1 - alpha_bar_t_prev - sigma**2)
136
+ - torch.sqrt((alpha_bar_t_prev * (1 - alpha_bar_t)) / alpha_bar_t)
137
+ ) * epsilon_theta
138
+ )
139
+ return mu + sigma * epsilon_t
140
+
141
+ def denoise_batch(
142
+ self,
143
+ images: torch.Tensor,
144
+ model: "BaseDiffusionModel",
145
+ number_of_steps: int = 50,
146
+ mode: DenoisingMode = DenoisingMode.Linear
147
+ ) -> List[torch.Tensor]:
148
+ """Denoise a batch of images.
149
+
150
+ This denoises a batch images. This is the image generation process.
151
+
152
+ Args:
153
+ images: A batch of noisy images.
154
+ 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
+
158
+ Returns:
159
+ A list of tensors containing a batch of denoised images.
160
+ """
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
+ 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
+ )
181
+
182
+ images = self._denoise_step(
183
+ images, model=model, timestep=timestep, timestep_prev=timestep_prev,
184
+ )
185
+ images = torch.clamp(images, -1.0, 1.0)
186
+ denoised_images.append(images)
187
+ return denoised_images
@@ -1,5 +1,8 @@
1
- from typing import List, Tuple, TYPE_CHECKING
1
+ from typing import TYPE_CHECKING
2
+ from typing import List
3
+ from typing import Tuple
2
4
 
5
+ import numpy as np
3
6
  import torch
4
7
  from tqdm import tqdm
5
8
 
@@ -9,9 +12,11 @@ from diffusion_models.gaussian_diffusion.beta_schedulers import (
9
12
  )
10
13
  from diffusion_models.utils.schemas import Checkpoint
11
14
 
15
+
12
16
  if TYPE_CHECKING:
13
17
  from diffusion_models.models.base_diffusion_model import BaseDiffusionModel
14
18
 
19
+
15
20
  class GaussianDiffuser(BaseDiffuser):
16
21
  def __init__(self, beta_scheduler: BaseBetaScheduler):
17
22
  """Initializes the class instance.
@@ -62,6 +67,20 @@ class GaussianDiffuser(BaseDiffuser):
62
67
  self.beta_scheduler = self.beta_scheduler.to(self.device)
63
68
  return self
64
69
 
70
+ def _diffuse_batch(
71
+ self, images: torch.Tensor, timesteps: torch.Tensor
72
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
73
+ noise = torch.randn_like(images, device=self.device)
74
+
75
+ alpha_bar_t = self.beta_scheduler.alpha_bars.gather(dim=0, index=timesteps)
76
+
77
+ alpha_bar_t = alpha_bar_t.reshape((-1, *((1,) * (len(images.shape) - 1))))
78
+
79
+ mu = torch.sqrt(alpha_bar_t)
80
+ sigma = torch.sqrt(1 - alpha_bar_t)
81
+ images = mu * images + sigma * noise
82
+ return images, noise, timesteps
83
+
65
84
  def diffuse_batch(
66
85
  self, images: torch.Tensor
67
86
  ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
@@ -83,16 +102,7 @@ class GaussianDiffuser(BaseDiffuser):
83
102
  timesteps = torch.randint(
84
103
  0, self.beta_scheduler.steps, (images.shape[0],), device=self.device
85
104
  )
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
105
+ return self._diffuse_batch(images, timesteps)
96
106
 
97
107
  @torch.no_grad()
98
108
  def _denoise_step(
@@ -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,9 +58,11 @@ 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]
@@ -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
@@ -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-rc.1"
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"