diffusion-model-framework 1.1.0rc1__tar.gz → 1.1.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.
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/.gitignore +7 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/PKG-INFO +1 -1
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/assets/sampling-small.gif +1 -1
- diffusion_model_framework-1.1.0rc2/diffusion_models/__init__.py +1 -0
- diffusion_model_framework-1.1.0rc2/diffusion_models/_store/__init__.py +15 -0
- diffusion_model_framework-1.1.0rc2/diffusion_models/_store/beta_scheduler.py +15 -0
- diffusion_model_framework-1.1.0rc2/diffusion_models/_store/dataset.py +40 -0
- diffusion_model_framework-1.1.0rc2/diffusion_models/_store/diffuser.py +20 -0
- diffusion_model_framework-1.1.0rc2/diffusion_models/_store/model.py +14 -0
- diffusion_model_framework-1.1.0rc2/diffusion_models/_store/optimizer.py +30 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/diffusion_models/diffusion_trainer.py +48 -8
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/diffusion_models/gaussian_diffusion/ddimm_diffuser.py +1 -1
- diffusion_model_framework-1.1.0rc2/diffusion_models/utils/adopt.py +501 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/diffusion_models/utils/schemas.py +4 -0
- diffusion_model_framework-1.1.0rc2/diffusion_models/utils/tensorboard.py +78 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/docs/source/conf.py +1 -1
- diffusion_model_framework-1.1.0rc2/examples/config/baseline.yaml +40 -0
- diffusion_model_framework-1.1.0rc2/examples/train_model.py +118 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/pyproject.toml +5 -1
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/uv.lock +56 -1
- diffusion_model_framework-1.1.0rc1/diffusion_models/__init__.py +0 -1
- diffusion_model_framework-1.1.0rc1/diffusion_models/utils/adopt.py +0 -501
- diffusion_model_framework-1.1.0rc1/diffusion_models/utils/tensorboard.py +0 -45
- diffusion_model_framework-1.1.0rc1/examples/train_model.py +0 -118
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/.github/workflows/build-and-release-docs.yaml +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/.github/workflows/release.yaml +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/.github/workflows/run-tests.yaml +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/CHANGELOG.md +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/README.md +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/diffusion_models/diffusion_inference.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/diffusion_models/gaussian_diffusion/__init__.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/diffusion_models/gaussian_diffusion/base_diffuser.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/diffusion_models/gaussian_diffusion/beta_schedulers.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/diffusion_models/gaussian_diffusion/gaussian_diffuser.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/diffusion_models/models/SimpleUnet.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/diffusion_models/models/__init__.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/diffusion_models/models/base_diffusion_model.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/diffusion_models/models/conditioned_simple_unet.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/diffusion_models/utils/__init__.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/diffusion_models/utils/batch_processor.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/docs/Makefile +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/docs/make.bat +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/docs/source/_templates/versions.html +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/docs/source/custom/post-modules/.gitkeep +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/docs/source/custom/pre-modules/introduction.md +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/docs/source/examples.rst +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/docs/source/index.rst +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/examples/__init__.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/examples/coco_playground.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/examples/infer.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/tests/__init__.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/tests/conftest.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/tests/gaussian_diffusion/__init__.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/tests/gaussian_diffusion/test_beta_schedulers.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/tests/test_base.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/tests/utils/__init__.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/tests/utils/test_schemas.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/tests/utils/test_tensorboard.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: diffusion-model-framework
|
|
3
|
-
Version: 1.1.0-rc.
|
|
3
|
+
Version: 1.1.0-rc.2
|
|
4
4
|
Project-URL: Documentation, https://www.sachahu.com/docs/diffusion-model-framework/latest/index.html
|
|
5
5
|
Project-URL: Repository, https://github.com/sachahu1/Diffusion-Model-Framework
|
|
6
6
|
Author-email: Sacha Hu <sachahu@mindsparkai.com>
|
{diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc2}/assets/sampling-small.gif
RENAMED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
[diffend] Oversized file quarantined before diffing.
|
|
2
|
-
name: diffusion_model_framework-1.1.
|
|
2
|
+
name: diffusion_model_framework-1.1.0rc2/assets/sampling-small.gif
|
|
3
3
|
size: 48830777 bytes
|
|
4
4
|
sha256: 634d757881475b35905ffb7981d84d813cdc183c09f42c285b25556a4fc6a14f
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "1.1.0-rc.2"
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""Hydra-zen config stores for the training example.
|
|
2
|
+
|
|
3
|
+
Importing this package registers every config group (``beta_scheduler``,
|
|
4
|
+
``diffuser``, ``model``, ``optimizer``, ``dataset``) with hydra-zen's store.
|
|
5
|
+
The top-level configs live next to the scripts that use them, e.g.
|
|
6
|
+
``examples/config/baseline.yaml``.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from diffusion_models._store import ( # noqa: F401
|
|
10
|
+
beta_scheduler,
|
|
11
|
+
dataset,
|
|
12
|
+
diffuser,
|
|
13
|
+
model,
|
|
14
|
+
optimizer,
|
|
15
|
+
)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from hydra_zen import builds, store
|
|
2
|
+
|
|
3
|
+
from diffusion_models.gaussian_diffusion.beta_schedulers import (
|
|
4
|
+
CosineBetaScheduler,
|
|
5
|
+
LinearBetaScheduler,
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
beta_scheduler_store = store(group="beta_scheduler")
|
|
9
|
+
|
|
10
|
+
beta_scheduler_store(
|
|
11
|
+
builds(LinearBetaScheduler, populate_full_signature=True), name="linear"
|
|
12
|
+
)
|
|
13
|
+
beta_scheduler_store(
|
|
14
|
+
builds(CosineBetaScheduler, populate_full_signature=True), name="cosine"
|
|
15
|
+
)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from hydra_zen import builds, store
|
|
3
|
+
from torchvision import datasets
|
|
4
|
+
from torchvision.transforms import v2
|
|
5
|
+
|
|
6
|
+
dataset_store = store(group="dataset")
|
|
7
|
+
|
|
8
|
+
# Normalize(0.5, 0.5) maps [0, 1] -> [-1, 1]; its inverse is mean=-1, std=2.
|
|
9
|
+
ImageTransforms = builds(
|
|
10
|
+
v2.Compose,
|
|
11
|
+
transforms=[
|
|
12
|
+
builds(v2.ToImage),
|
|
13
|
+
builds(v2.Resize, size=["${image_size}", "${image_size}"]),
|
|
14
|
+
builds(v2.ToDtype, dtype=builds(torch.get_default_dtype), scale=True),
|
|
15
|
+
builds(v2.Normalize, mean=[0.5] * 3, std=[0.5] * 3),
|
|
16
|
+
],
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
dataset_store(
|
|
20
|
+
builds(
|
|
21
|
+
datasets.CelebA,
|
|
22
|
+
root="../data",
|
|
23
|
+
download=False,
|
|
24
|
+
split="train",
|
|
25
|
+
transform=ImageTransforms,
|
|
26
|
+
),
|
|
27
|
+
name="celeba",
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
# Random images, handy for smoke-testing the pipeline without any data.
|
|
31
|
+
dataset_store(
|
|
32
|
+
builds(
|
|
33
|
+
datasets.FakeData,
|
|
34
|
+
size=64,
|
|
35
|
+
image_size=["${image_channels}", "${image_size}", "${image_size}"],
|
|
36
|
+
transform=ImageTransforms,
|
|
37
|
+
populate_full_signature=True,
|
|
38
|
+
),
|
|
39
|
+
name="fake",
|
|
40
|
+
)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from hydra_zen import builds, store
|
|
2
|
+
|
|
3
|
+
from diffusion_models.gaussian_diffusion.ddimm_diffuser import DdimDiffuser
|
|
4
|
+
from diffusion_models.gaussian_diffusion.gaussian_diffuser import (
|
|
5
|
+
GaussianDiffuser,
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
diffuser_store = store(group="diffuser")
|
|
9
|
+
|
|
10
|
+
diffuser_store(
|
|
11
|
+
builds(
|
|
12
|
+
DdimDiffuser,
|
|
13
|
+
beta_scheduler="${beta_scheduler}",
|
|
14
|
+
populate_full_signature=True,
|
|
15
|
+
),
|
|
16
|
+
name="ddim",
|
|
17
|
+
)
|
|
18
|
+
diffuser_store(
|
|
19
|
+
builds(GaussianDiffuser, beta_scheduler="${beta_scheduler}"), name="ddpm"
|
|
20
|
+
)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from hydra_zen import builds, store
|
|
2
|
+
|
|
3
|
+
from diffusion_models.models.SimpleUnet import SimpleUnet
|
|
4
|
+
|
|
5
|
+
model_store = store(group="model")
|
|
6
|
+
|
|
7
|
+
model_store(
|
|
8
|
+
builds(
|
|
9
|
+
SimpleUnet,
|
|
10
|
+
diffuser="${diffuser}",
|
|
11
|
+
image_channels="${image_channels}",
|
|
12
|
+
),
|
|
13
|
+
name="simple_unet",
|
|
14
|
+
)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from hydra_zen import builds, store
|
|
2
|
+
from torch.optim import AdamW
|
|
3
|
+
|
|
4
|
+
from diffusion_models.utils.adopt import ADOPT
|
|
5
|
+
|
|
6
|
+
optimizer_store = store(group="optimizer")
|
|
7
|
+
|
|
8
|
+
# Partials: the model parameters are only known at runtime.
|
|
9
|
+
# ``hydra_convert="all"`` yields plain tuples/lists instead of OmegaConf
|
|
10
|
+
# containers, keeping optimizer state loadable with ``weights_only=True``.
|
|
11
|
+
optimizer_store(
|
|
12
|
+
builds(
|
|
13
|
+
AdamW,
|
|
14
|
+
lr=2e-4,
|
|
15
|
+
populate_full_signature=True,
|
|
16
|
+
zen_partial=True,
|
|
17
|
+
hydra_convert="all",
|
|
18
|
+
),
|
|
19
|
+
name="adamw",
|
|
20
|
+
)
|
|
21
|
+
optimizer_store(
|
|
22
|
+
builds(
|
|
23
|
+
ADOPT,
|
|
24
|
+
lr=2e-4,
|
|
25
|
+
populate_full_signature=True,
|
|
26
|
+
zen_partial=True,
|
|
27
|
+
hydra_convert="all",
|
|
28
|
+
),
|
|
29
|
+
name="adopt",
|
|
30
|
+
)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import pathlib
|
|
2
|
-
from typing import Callable, Dict
|
|
2
|
+
from typing import Callable, Dict, Optional, Union
|
|
3
3
|
|
|
4
4
|
import torch
|
|
5
5
|
from torch.nn import functional as F
|
|
@@ -28,6 +28,8 @@ class DiffusionTrainer:
|
|
|
28
28
|
log_configuration: LogConfiguration = LogConfiguration(),
|
|
29
29
|
reverse_transforms: Callable = lambda x: x,
|
|
30
30
|
device: str = "cuda",
|
|
31
|
+
output_directory: Optional[Union[str, pathlib.Path]] = None,
|
|
32
|
+
run_config: Optional[str] = None,
|
|
31
33
|
):
|
|
32
34
|
"""A diffusion trainer framework.
|
|
33
35
|
|
|
@@ -42,6 +44,12 @@ class DiffusionTrainer:
|
|
|
42
44
|
log_configuration: The logging configuration to use.
|
|
43
45
|
reverse_transforms: The reverse transforms to use.
|
|
44
46
|
device: The device to use.
|
|
47
|
+
output_directory: Where to write this run's artifacts. When provided,
|
|
48
|
+
checkpoints go to ``<output_directory>/checkpoints`` and tensorboard
|
|
49
|
+
logs to ``<output_directory>/tensorboard``. Otherwise the legacy
|
|
50
|
+
``../checkpoints/<name>`` and ``../runs/<name>`` locations are used.
|
|
51
|
+
run_config: The resolved configuration (YAML) of the run. It is logged
|
|
52
|
+
to tensorboard and stored in every checkpoint for reproducibility.
|
|
45
53
|
"""
|
|
46
54
|
self.model = model.to(device)
|
|
47
55
|
"""The diffusion model to use."""
|
|
@@ -69,26 +77,40 @@ class DiffusionTrainer:
|
|
|
69
77
|
self._image_shape = dataset[0][0].shape
|
|
70
78
|
|
|
71
79
|
self.scaler = torch.amp.GradScaler(
|
|
72
|
-
device=device
|
|
73
|
-
|
|
80
|
+
device=device,
|
|
81
|
+
enabled=training_configuration.mixed_precision_training,
|
|
74
82
|
)
|
|
75
83
|
"""A torch GradScaler object."""
|
|
76
84
|
|
|
77
85
|
self.log_configuration = log_configuration
|
|
78
86
|
"""A LogConfiguration object."""
|
|
79
87
|
|
|
80
|
-
|
|
81
|
-
pathlib.Path(
|
|
82
|
-
/
|
|
83
|
-
|
|
88
|
+
if output_directory is not None:
|
|
89
|
+
output_directory = pathlib.Path(output_directory)
|
|
90
|
+
self.checkpoint_path = output_directory / "checkpoints"
|
|
91
|
+
log_directory = output_directory / "tensorboard"
|
|
92
|
+
else:
|
|
93
|
+
self.checkpoint_path = (
|
|
94
|
+
pathlib.Path("../checkpoints")
|
|
95
|
+
/ self.training_configuration.training_name
|
|
96
|
+
)
|
|
97
|
+
log_directory = None
|
|
84
98
|
"""The path to save checkpoints."""
|
|
85
99
|
|
|
86
|
-
self.checkpoint_path.mkdir(exist_ok=True)
|
|
100
|
+
self.checkpoint_path.mkdir(parents=True, exist_ok=True)
|
|
87
101
|
self.tensorboard_manager = TensorboardManager(
|
|
88
102
|
log_name=self.training_configuration.training_name,
|
|
103
|
+
log_directory=log_directory,
|
|
89
104
|
)
|
|
90
105
|
"""A tensorboard manager instance."""
|
|
91
106
|
|
|
107
|
+
self.run_config = run_config
|
|
108
|
+
"""The resolved run configuration (YAML), if any."""
|
|
109
|
+
if run_config is not None:
|
|
110
|
+
self.tensorboard_manager.log_text(
|
|
111
|
+
"config", f"```yaml\n{run_config}\n```"
|
|
112
|
+
)
|
|
113
|
+
|
|
92
114
|
self.reverse_transforms = reverse_transforms
|
|
93
115
|
"""A set of reverse transforms."""
|
|
94
116
|
|
|
@@ -115,6 +137,7 @@ class DiffusionTrainer:
|
|
|
115
137
|
alpha_bars=self.model.diffuser.beta_scheduler.alpha_bars,
|
|
116
138
|
),
|
|
117
139
|
tensorboard_run_name=self.tensorboard_manager.summary_writer.log_dir,
|
|
140
|
+
config_yaml=self.run_config,
|
|
118
141
|
)
|
|
119
142
|
checkpoint.to_file(self.checkpoint_path / checkpoint_name)
|
|
120
143
|
|
|
@@ -179,12 +202,29 @@ class DiffusionTrainer:
|
|
|
179
202
|
metrics: A dictionary mapping metric names to values.
|
|
180
203
|
global_step: The current global step.
|
|
181
204
|
"""
|
|
205
|
+
was_training = self.model.training
|
|
182
206
|
self.model.eval()
|
|
207
|
+
try:
|
|
208
|
+
self._log_to_tensorboard(metrics=metrics, global_step=global_step)
|
|
209
|
+
finally:
|
|
210
|
+
# Logging switches to eval mode (BatchNorm, etc.); always restore.
|
|
211
|
+
self.model.train(was_training)
|
|
212
|
+
|
|
213
|
+
@torch.no_grad()
|
|
214
|
+
def _log_to_tensorboard(self, metrics: Dict[str, float], global_step: int):
|
|
183
215
|
if global_step % self.log_configuration.log_rate == 0:
|
|
184
216
|
self.tensorboard_manager.log_metrics(
|
|
185
217
|
metrics=metrics, global_step=global_step
|
|
186
218
|
)
|
|
187
219
|
|
|
220
|
+
if (
|
|
221
|
+
self.log_configuration.weight_rate is not None
|
|
222
|
+
and global_step % self.log_configuration.weight_rate == 0
|
|
223
|
+
):
|
|
224
|
+
self.tensorboard_manager.log_weights(
|
|
225
|
+
model=self.model, global_step=global_step
|
|
226
|
+
)
|
|
227
|
+
|
|
188
228
|
if (global_step % self.log_configuration.image_rate == 0) and (
|
|
189
229
|
self.log_configuration.number_of_images > 0
|
|
190
230
|
):
|
|
@@ -56,7 +56,7 @@ class DdimDiffuser(BaseDiffuser):
|
|
|
56
56
|
** 2
|
|
57
57
|
).astype(int)
|
|
58
58
|
self._time_steps = time_steps + 1
|
|
59
|
-
self._time_steps_prev = np.concatenate([[0],
|
|
59
|
+
self._time_steps_prev = np.concatenate([[0], self._time_steps[:-1]])
|
|
60
60
|
return list(range(self.number_of_steps))[::-1]
|
|
61
61
|
|
|
62
62
|
def get_timestep(self, number_of_images: int, idx: int) -> Timestep:
|