diffusion-model-framework 1.1.0rc1__tar.gz → 1.1.0rc3__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.0rc3}/.github/workflows/run-tests.yaml +1 -1
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/.gitignore +8 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/PKG-INFO +2 -2
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/README.md +1 -1
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/assets/sampling-small.gif +1 -1
- diffusion_model_framework-1.1.0rc3/diffusion_models/__init__.py +1 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/_store/__init__.py +15 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/_store/beta_scheduler.py +15 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/_store/dataset.py +64 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/_store/diffuser.py +25 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/_store/model.py +26 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/_store/optimizer.py +30 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/cli.py +135 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/conf/baseline.yaml +60 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/conf/config/small_attention_ema.yaml +30 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/conf/config/small_attention_v.yaml +18 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/conf/infer.yaml +21 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/diffusion_models/diffusion_inference.py +12 -3
- diffusion_model_framework-1.1.0rc3/diffusion_models/diffusion_trainer.py +465 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/evaluation/__init__.py +4 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/evaluation/checkpoint.py +87 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/evaluation/fid.py +146 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/gaussian_diffusion/base_diffuser.py +151 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/diffusion_models/gaussian_diffusion/ddimm_diffuser.py +51 -6
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/diffusion_models/gaussian_diffusion/gaussian_diffuser.py +17 -7
- diffusion_model_framework-1.1.0rc3/diffusion_models/inference.py +87 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/models/SmallAttentionUnet.py +212 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/training.py +128 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/utils/adopt.py +501 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/utils/ema.py +97 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/utils/schemas.py +255 -0
- diffusion_model_framework-1.1.0rc3/diffusion_models/utils/tensorboard.py +78 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/docs/source/conf.py +1 -1
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/docs/source/custom/pre-modules/introduction.md +1 -1
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/pyproject.toml +15 -1
- diffusion_model_framework-1.1.0rc3/tests/evaluation/__init__.py +0 -0
- diffusion_model_framework-1.1.0rc3/tests/evaluation/test_fid.py +222 -0
- diffusion_model_framework-1.1.0rc3/tests/gaussian_diffusion/__init__.py +0 -0
- diffusion_model_framework-1.1.0rc3/tests/gaussian_diffusion/test_v_prediction.py +115 -0
- diffusion_model_framework-1.1.0rc3/tests/models/__init__.py +0 -0
- diffusion_model_framework-1.1.0rc3/tests/models/test_small_attention_unet.py +75 -0
- diffusion_model_framework-1.1.0rc3/tests/test_diffusion_trainer.py +273 -0
- diffusion_model_framework-1.1.0rc3/tests/utils/__init__.py +0 -0
- diffusion_model_framework-1.1.0rc3/tests/utils/test_ema.py +71 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/uv.lock +228 -1
- diffusion_model_framework-1.1.0rc1/diffusion_models/__init__.py +0 -1
- diffusion_model_framework-1.1.0rc1/diffusion_models/diffusion_trainer.py +0 -207
- diffusion_model_framework-1.1.0rc1/diffusion_models/gaussian_diffusion/base_diffuser.py +0 -91
- diffusion_model_framework-1.1.0rc1/diffusion_models/utils/adopt.py +0 -501
- diffusion_model_framework-1.1.0rc1/diffusion_models/utils/schemas.py +0 -152
- diffusion_model_framework-1.1.0rc1/diffusion_models/utils/tensorboard.py +0 -45
- diffusion_model_framework-1.1.0rc1/examples/infer.py +0 -53
- diffusion_model_framework-1.1.0rc1/examples/train_model.py +0 -118
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/.github/workflows/build-and-release-docs.yaml +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/.github/workflows/release.yaml +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/CHANGELOG.md +0 -0
- {diffusion_model_framework-1.1.0rc1/diffusion_models/gaussian_diffusion → diffusion_model_framework-1.1.0rc3/diffusion_models/conf}/__init__.py +0 -0
- {diffusion_model_framework-1.1.0rc1/diffusion_models/models → diffusion_model_framework-1.1.0rc3/diffusion_models/conf/config}/__init__.py +0 -0
- {diffusion_model_framework-1.1.0rc1/diffusion_models/utils → diffusion_model_framework-1.1.0rc3/diffusion_models/gaussian_diffusion}/__init__.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/diffusion_models/gaussian_diffusion/beta_schedulers.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/diffusion_models/models/SimpleUnet.py +0 -0
- {diffusion_model_framework-1.1.0rc1/examples → diffusion_model_framework-1.1.0rc3/diffusion_models/models}/__init__.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/diffusion_models/models/base_diffusion_model.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/diffusion_models/models/conditioned_simple_unet.py +0 -0
- {diffusion_model_framework-1.1.0rc1/tests → diffusion_model_framework-1.1.0rc3/diffusion_models/utils}/__init__.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/diffusion_models/utils/batch_processor.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/docs/Makefile +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/docs/make.bat +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/docs/source/_templates/versions.html +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/docs/source/custom/post-modules/.gitkeep +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/docs/source/examples.rst +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/docs/source/index.rst +0 -0
- {diffusion_model_framework-1.1.0rc1/tests/gaussian_diffusion → diffusion_model_framework-1.1.0rc3/examples}/__init__.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/examples/coco_playground.py +0 -0
- {diffusion_model_framework-1.1.0rc1/tests/utils → diffusion_model_framework-1.1.0rc3/tests}/__init__.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/tests/conftest.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/tests/gaussian_diffusion/test_beta_schedulers.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/tests/test_base.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/tests/utils/test_schemas.py +0 -0
- {diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/tests/utils/test_tensorboard.py +0 -0
|
@@ -25,7 +25,7 @@ jobs:
|
|
|
25
25
|
with:
|
|
26
26
|
enable-cache: true
|
|
27
27
|
- name: Install dependencies
|
|
28
|
-
run: uv sync --locked --group test
|
|
28
|
+
run: uv sync --locked --group test --group evaluation
|
|
29
29
|
#----------------------------------------------
|
|
30
30
|
# add matrix specifics and run test suite
|
|
31
31
|
#----------------------------------------------
|
|
@@ -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.3
|
|
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>
|
|
@@ -56,7 +56,7 @@ uv sync
|
|
|
56
56
|
```
|
|
57
57
|
You can now start using the package to train your diffusion model. For example, try running:
|
|
58
58
|
```shell
|
|
59
|
-
uv run
|
|
59
|
+
uv run diffusion-models train
|
|
60
60
|
```
|
|
61
61
|
|
|
62
62
|
Building a wheel is also possible via:
|
{diffusion_model_framework-1.1.0rc1 → diffusion_model_framework-1.1.0rc3}/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.0rc3/assets/sampling-small.gif
|
|
3
3
|
size: 48830777 bytes
|
|
4
4
|
sha256: 634d757881475b35905ffb7981d84d813cdc183c09f42c285b25556a4fc6a14f
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "1.1.0-rc.3"
|
|
@@ -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,64 @@
|
|
|
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
|
+
eval_dataset_store = store(group="eval_dataset")
|
|
8
|
+
|
|
9
|
+
# Normalize(0.5, 0.5) maps [0, 1] -> [-1, 1]; its inverse is mean=-1, std=2.
|
|
10
|
+
ImageTransforms = builds(
|
|
11
|
+
v2.Compose,
|
|
12
|
+
transforms=[
|
|
13
|
+
builds(v2.ToImage),
|
|
14
|
+
builds(v2.Resize, size=["${image_size}", "${image_size}"]),
|
|
15
|
+
builds(v2.ToDtype, dtype=builds(torch.get_default_dtype), scale=True),
|
|
16
|
+
builds(v2.Normalize, mean=[0.5] * 3, std=[0.5] * 3),
|
|
17
|
+
],
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
dataset_store(
|
|
21
|
+
builds(
|
|
22
|
+
datasets.CelebA,
|
|
23
|
+
root="data",
|
|
24
|
+
download=False,
|
|
25
|
+
split="train",
|
|
26
|
+
transform=ImageTransforms,
|
|
27
|
+
),
|
|
28
|
+
name="celeba",
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
# Random images, handy for smoke-testing the pipeline without any data.
|
|
32
|
+
dataset_store(
|
|
33
|
+
builds(
|
|
34
|
+
datasets.FakeData,
|
|
35
|
+
size=64,
|
|
36
|
+
image_size=["${image_channels}", "${image_size}", "${image_size}"],
|
|
37
|
+
transform=ImageTransforms,
|
|
38
|
+
populate_full_signature=True,
|
|
39
|
+
),
|
|
40
|
+
name="fake",
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
# Held-out reference images for FID/KID. Select one with `eval_dataset=<name>`.
|
|
44
|
+
eval_dataset_store(
|
|
45
|
+
builds(
|
|
46
|
+
datasets.CelebA,
|
|
47
|
+
root="data",
|
|
48
|
+
download=False,
|
|
49
|
+
split="valid",
|
|
50
|
+
transform=ImageTransforms,
|
|
51
|
+
),
|
|
52
|
+
name="celeba_valid",
|
|
53
|
+
)
|
|
54
|
+
eval_dataset_store(
|
|
55
|
+
builds(
|
|
56
|
+
datasets.FakeData,
|
|
57
|
+
size=64,
|
|
58
|
+
image_size=["${image_channels}", "${image_size}", "${image_size}"],
|
|
59
|
+
transform=ImageTransforms,
|
|
60
|
+
random_offset=1,
|
|
61
|
+
populate_full_signature=True,
|
|
62
|
+
),
|
|
63
|
+
name="fake",
|
|
64
|
+
)
|
|
@@ -0,0 +1,25 @@
|
|
|
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(
|
|
20
|
+
GaussianDiffuser,
|
|
21
|
+
beta_scheduler="${beta_scheduler}",
|
|
22
|
+
populate_full_signature=True,
|
|
23
|
+
),
|
|
24
|
+
name="ddpm",
|
|
25
|
+
)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from hydra_zen import builds, store
|
|
2
|
+
|
|
3
|
+
from diffusion_models.models.SimpleUnet import SimpleUnet
|
|
4
|
+
from diffusion_models.models.SmallAttentionUnet import SmallAttentionUnet
|
|
5
|
+
|
|
6
|
+
model_store = store(group="model")
|
|
7
|
+
|
|
8
|
+
model_store(
|
|
9
|
+
builds(
|
|
10
|
+
SimpleUnet,
|
|
11
|
+
diffuser="${diffuser}",
|
|
12
|
+
image_channels="${image_channels}",
|
|
13
|
+
),
|
|
14
|
+
name="simple_unet",
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
model_store(
|
|
18
|
+
builds(
|
|
19
|
+
SmallAttentionUnet,
|
|
20
|
+
diffuser="${diffuser}",
|
|
21
|
+
image_channels="${image_channels}",
|
|
22
|
+
populate_full_signature=True,
|
|
23
|
+
hydra_convert="all",
|
|
24
|
+
),
|
|
25
|
+
name="small_attention_unet",
|
|
26
|
+
)
|
|
@@ -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
|
+
)
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"""The ``diffusion-models`` command line interface.
|
|
2
|
+
|
|
3
|
+
Hydra does the heavy lifting for training and inference, so everything after the
|
|
4
|
+
sub-command is forwarded to it untouched::
|
|
5
|
+
|
|
6
|
+
diffusion-models train +config=small_attention_ema training.batch_size=64
|
|
7
|
+
diffusion-models train --help # hydra's own help, listing the config groups
|
|
8
|
+
diffusion-models infer checkpoint=<path> +config=small_attention_ema
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import importlib
|
|
12
|
+
import pathlib
|
|
13
|
+
import sys
|
|
14
|
+
from typing import Tuple
|
|
15
|
+
|
|
16
|
+
import click
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@click.group(context_settings={"help_option_names": ["-h", "--help"]})
|
|
20
|
+
def main() -> None:
|
|
21
|
+
"""Tools for training and running diffusion models."""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _run_hydra(command, module, extra, overrides, config_directories):
|
|
25
|
+
"""Forwards ``overrides`` to the hydra entry point ``module.main``."""
|
|
26
|
+
try:
|
|
27
|
+
entry_point = importlib.import_module(module).main
|
|
28
|
+
except ModuleNotFoundError as error: # pragma: no cover
|
|
29
|
+
raise click.ClickException(
|
|
30
|
+
f"Missing {extra} dependency ({error.name}). "
|
|
31
|
+
"Install them with `uv sync --group training`."
|
|
32
|
+
) from error
|
|
33
|
+
|
|
34
|
+
arguments = list(overrides)
|
|
35
|
+
if config_directories:
|
|
36
|
+
paths = ",".join(f"file://{path.resolve()}" for path in config_directories)
|
|
37
|
+
arguments.append(f"hydra.searchpath=[{paths}]")
|
|
38
|
+
# Hydra reads its overrides straight from sys.argv.
|
|
39
|
+
sys.argv = [f"{sys.argv[0]} {command}", *arguments]
|
|
40
|
+
entry_point()
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
_HYDRA_COMMAND = dict(
|
|
44
|
+
context_settings={
|
|
45
|
+
"ignore_unknown_options": True,
|
|
46
|
+
"allow_extra_args": True,
|
|
47
|
+
# Let ``--help`` reach hydra, which knows about the config groups.
|
|
48
|
+
"help_option_names": [],
|
|
49
|
+
},
|
|
50
|
+
add_help_option=False,
|
|
51
|
+
)
|
|
52
|
+
_config_dir_option = click.option(
|
|
53
|
+
"--config-dir",
|
|
54
|
+
"config_directories",
|
|
55
|
+
multiple=True,
|
|
56
|
+
type=click.Path(exists=True, file_okay=False, path_type=pathlib.Path),
|
|
57
|
+
help="An extra directory to search for hydra configs and `+config=` presets.",
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@main.command(**_HYDRA_COMMAND)
|
|
62
|
+
@_config_dir_option
|
|
63
|
+
@click.argument("overrides", nargs=-1, type=click.UNPROCESSED)
|
|
64
|
+
def train(config_directories: Tuple[pathlib.Path, ...], overrides) -> None:
|
|
65
|
+
"""Train a diffusion model.
|
|
66
|
+
|
|
67
|
+
OVERRIDES are hydra overrides and flags, e.g. `+config=small_attention_ema`,
|
|
68
|
+
`training.batch_size=64`, `resume_from=<checkpoint>` or `--multirun`.
|
|
69
|
+
Pass `--help` to list the available config groups.
|
|
70
|
+
"""
|
|
71
|
+
_run_hydra(
|
|
72
|
+
"train", "diffusion_models.training", "training", overrides, config_directories
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@main.command(**_HYDRA_COMMAND)
|
|
77
|
+
@_config_dir_option
|
|
78
|
+
@click.argument("overrides", nargs=-1, type=click.UNPROCESSED)
|
|
79
|
+
def infer(config_directories: Tuple[pathlib.Path, ...], overrides) -> None:
|
|
80
|
+
"""Generate images with a trained checkpoint.
|
|
81
|
+
|
|
82
|
+
OVERRIDES are hydra overrides: `checkpoint=<path>` is required and the model
|
|
83
|
+
config should match the training run, e.g. `+config=small_attention_ema`.
|
|
84
|
+
Other options: `number_of_images=25`, `save_gif=true`, `steps=50`,
|
|
85
|
+
`use_ema=false`, `device=cpu`. Pass `--help` to list the config groups.
|
|
86
|
+
"""
|
|
87
|
+
_run_hydra(
|
|
88
|
+
"infer", "diffusion_models.inference", "training", overrides, config_directories
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@main.command()
|
|
93
|
+
@click.argument(
|
|
94
|
+
"checkpoint",
|
|
95
|
+
type=click.Path(exists=True, dir_okay=False, path_type=pathlib.Path),
|
|
96
|
+
)
|
|
97
|
+
@click.option("--samples", default=10000, show_default=True, help="Images.")
|
|
98
|
+
@click.option("--batch-size", default=100, show_default=True)
|
|
99
|
+
@click.option("--steps", type=int, help="Override the DDIM sampling steps.")
|
|
100
|
+
@click.option("--device", default="cuda", show_default=True)
|
|
101
|
+
@click.option("--ema/--no-ema", default=True, show_default=True)
|
|
102
|
+
@click.option("--kid/--no-kid", default=True, show_default=True)
|
|
103
|
+
@click.option("--seed", default=0, show_default=True)
|
|
104
|
+
def fid(
|
|
105
|
+
checkpoint, samples, batch_size, steps, device, ema, kid, seed
|
|
106
|
+
) -> None:
|
|
107
|
+
"""Compute FID (and KID) of a training CHECKPOINT.
|
|
108
|
+
|
|
109
|
+
Needs the `evaluation` and `training` dependency groups. Only compare scores
|
|
110
|
+
that use the same number of samples.
|
|
111
|
+
"""
|
|
112
|
+
try:
|
|
113
|
+
from diffusion_models.evaluation.checkpoint import evaluate_checkpoint
|
|
114
|
+
except ModuleNotFoundError as error: # pragma: no cover
|
|
115
|
+
raise click.ClickException(
|
|
116
|
+
f"Missing evaluation dependency ({error.name}). "
|
|
117
|
+
"Install them with `uv sync --group training --group evaluation`."
|
|
118
|
+
) from error
|
|
119
|
+
|
|
120
|
+
metrics = evaluate_checkpoint(
|
|
121
|
+
checkpoint,
|
|
122
|
+
number_of_samples=samples,
|
|
123
|
+
batch_size=batch_size,
|
|
124
|
+
device=device,
|
|
125
|
+
use_ema=ema,
|
|
126
|
+
compute_kid=kid,
|
|
127
|
+
steps=steps,
|
|
128
|
+
seed=seed,
|
|
129
|
+
)
|
|
130
|
+
for name, value in metrics.items():
|
|
131
|
+
click.echo(f"{name}: {value:.4f}")
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
if __name__ == "__main__":
|
|
135
|
+
main()
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# The standard training configuration. Config groups (model, diffuser, ...)
|
|
2
|
+
# are registered by diffusion_models/_store; override them from the command line,
|
|
3
|
+
# e.g. `diffusion-models train optimizer=adopt training.batch_size=64`.
|
|
4
|
+
# Presets from conf/config/ are applied with `+config=<name>`.
|
|
5
|
+
defaults:
|
|
6
|
+
- _self_
|
|
7
|
+
- model: simple_unet
|
|
8
|
+
- diffuser: ddim
|
|
9
|
+
- beta_scheduler: linear
|
|
10
|
+
- optimizer: adamw
|
|
11
|
+
- dataset: celeba
|
|
12
|
+
- eval_dataset: null # e.g. eval_dataset=celeba_valid, for FID
|
|
13
|
+
|
|
14
|
+
seed: 0
|
|
15
|
+
device: cuda
|
|
16
|
+
# A checkpoint (e.g. runs/<name>/<timestamp>/checkpoints/latest.pt) to resume from.
|
|
17
|
+
resume_from: null
|
|
18
|
+
image_size: 64
|
|
19
|
+
image_channels: 3
|
|
20
|
+
reverse_image_size: 128
|
|
21
|
+
|
|
22
|
+
training:
|
|
23
|
+
_target_: diffusion_models.utils.schemas.TrainingConfiguration
|
|
24
|
+
training_name: diffusion
|
|
25
|
+
batch_size: 256
|
|
26
|
+
learning_rate: ${optimizer.lr}
|
|
27
|
+
number_of_epochs: 500
|
|
28
|
+
checkpoint_rate: 100
|
|
29
|
+
mixed_precision_training: false
|
|
30
|
+
mixed_precision_dtype: bfloat16
|
|
31
|
+
channels_last: false
|
|
32
|
+
gradient_clip: null
|
|
33
|
+
num_workers: 4
|
|
34
|
+
pin_memory: true
|
|
35
|
+
persistent_workers: true
|
|
36
|
+
compile: false
|
|
37
|
+
|
|
38
|
+
logging:
|
|
39
|
+
_target_: diffusion_models.utils.schemas.LogConfiguration
|
|
40
|
+
log_rate: 10
|
|
41
|
+
image_rate: 635
|
|
42
|
+
number_of_images: 5
|
|
43
|
+
weight_rate: 635
|
|
44
|
+
|
|
45
|
+
evaluation:
|
|
46
|
+
_target_: diffusion_models.utils.schemas.EvaluationConfiguration
|
|
47
|
+
fid_rate: null # epochs between FID/KID evaluations; null disables
|
|
48
|
+
number_of_samples: 5000
|
|
49
|
+
batch_size: 100
|
|
50
|
+
compute_kid: true
|
|
51
|
+
kid_subset_size: 1000
|
|
52
|
+
seed: 0 # sampling noise seed, so scores are comparable across runs
|
|
53
|
+
num_workers: 4 # dataloader workers reading the real images
|
|
54
|
+
|
|
55
|
+
hydra:
|
|
56
|
+
run:
|
|
57
|
+
dir: runs/${training.training_name}/${now:%Y-%m-%d_%H-%M-%S}
|
|
58
|
+
sweep:
|
|
59
|
+
dir: runs/${training.training_name}/multirun/${now:%Y-%m-%d_%H-%M-%S}
|
|
60
|
+
subdir: ${hydra.job.num}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# @package _global_
|
|
2
|
+
# A GroupNorm/attention Unet with EMA weights, bf16 mixed precision and frequent
|
|
3
|
+
# checkpointing, applied on top of the baseline. Launch with:
|
|
4
|
+
# diffusion-models train +config=small_attention_ema
|
|
5
|
+
defaults:
|
|
6
|
+
- override /model: small_attention_unet
|
|
7
|
+
- override /eval_dataset: celeba_valid
|
|
8
|
+
|
|
9
|
+
training:
|
|
10
|
+
training_name: small_attention_ema
|
|
11
|
+
batch_size: 96 # ~5.3 GB peak with compile; 128 peaks at ~6.7 GB
|
|
12
|
+
checkpoint_rate: 10
|
|
13
|
+
latest_checkpoint_rate: 1 # rolling latest.pt, overwritten every epoch
|
|
14
|
+
mixed_precision_training: true
|
|
15
|
+
mixed_precision_dtype: bfloat16 # no GradScaler needed
|
|
16
|
+
channels_last: true # fast with mixed precision, very slow in fp32
|
|
17
|
+
compile: true # ~1.5x faster; the first step takes about a minute to compile
|
|
18
|
+
gradient_clip: 1.0
|
|
19
|
+
ema_decay: 0.999
|
|
20
|
+
ema_warmup_steps: 1000
|
|
21
|
+
|
|
22
|
+
optimizer:
|
|
23
|
+
fused: true
|
|
24
|
+
|
|
25
|
+
logging:
|
|
26
|
+
image_rate: 1695 # once per epoch: 162770 CelebA images // batch size 96
|
|
27
|
+
weight_rate: 1695
|
|
28
|
+
|
|
29
|
+
evaluation:
|
|
30
|
+
fid_rate: 5 # FID/KID of the EMA model every 5 epochs (and after the last)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# @package _global_
|
|
2
|
+
# small_attention_ema with v-prediction. The model predicts the velocity
|
|
3
|
+
# v = sqrt(alpha_bar) * noise - sqrt(1 - alpha_bar) * image, which stays
|
|
4
|
+
# well-conditioned at the zero-terminal-SNR end of the schedule, and sampling
|
|
5
|
+
# uses trailing timestep spacing so the last step is pure noise (t = 999).
|
|
6
|
+
# Launch with:
|
|
7
|
+
# diffusion-models train +config=small_attention_v
|
|
8
|
+
defaults:
|
|
9
|
+
- small_attention_ema
|
|
10
|
+
- override /diffuser: ddim
|
|
11
|
+
|
|
12
|
+
training:
|
|
13
|
+
training_name: small_attention_v
|
|
14
|
+
|
|
15
|
+
diffuser:
|
|
16
|
+
prediction_type: V # enum member name (Epsilon or V)
|
|
17
|
+
mode: Trailing
|
|
18
|
+
number_of_steps: 50 # v-prediction + trailing spacing: 50 DDIM steps for FID/samples
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Inference configuration: the training baseline (so the model, diffuser and
|
|
2
|
+
# beta scheduler groups can be rebuilt and overridden the same way) plus the
|
|
3
|
+
# options of `diffusion-models infer`. Use the same overrides as the training run, e.g.
|
|
4
|
+
# diffusion-models infer checkpoint=<path> +config=small_attention_ema
|
|
5
|
+
defaults:
|
|
6
|
+
- baseline
|
|
7
|
+
- _self_
|
|
8
|
+
|
|
9
|
+
# The checkpoint holding the trained weights (required).
|
|
10
|
+
checkpoint: ???
|
|
11
|
+
# Use the EMA weights when the checkpoint has them.
|
|
12
|
+
use_ema: true
|
|
13
|
+
number_of_images: 25
|
|
14
|
+
# Also save the denoising process as a GIF.
|
|
15
|
+
save_gif: false
|
|
16
|
+
# Overrides the number of DDIM sampling steps, when the diffuser has that option.
|
|
17
|
+
steps: null
|
|
18
|
+
|
|
19
|
+
hydra:
|
|
20
|
+
run:
|
|
21
|
+
dir: outputs/infer/${now:%Y-%m-%d_%H-%M-%S}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import logging
|
|
2
|
+
import math
|
|
2
3
|
from typing import Callable, Tuple
|
|
3
4
|
|
|
4
5
|
import torch
|
|
@@ -45,16 +46,24 @@ class DiffusionInference:
|
|
|
45
46
|
|
|
46
47
|
def _visualise_images(self, denoised_images: torch.Tensor):
|
|
47
48
|
reverse_transformed_images = self.reverse_transforms(denoised_images)
|
|
48
|
-
|
|
49
|
+
# As square as possible, e.g. 4x4 for 16 images and 5x5 for 25.
|
|
50
|
+
columns = math.ceil(math.sqrt(len(reverse_transformed_images)))
|
|
51
|
+
image_grid = make_grid(reverse_transformed_images, nrow=columns)
|
|
49
52
|
pil_images = transforms.ToPILImage()(image_grid)
|
|
50
53
|
return pil_images
|
|
51
54
|
|
|
52
|
-
def generate(
|
|
55
|
+
def generate(
|
|
56
|
+
self,
|
|
57
|
+
number_of_images: int,
|
|
58
|
+
save_gif: bool = False,
|
|
59
|
+
gif_path: str = "generated.gif",
|
|
60
|
+
) -> Image:
|
|
53
61
|
"""Generate a batch of images.
|
|
54
62
|
|
|
55
63
|
Args:
|
|
56
64
|
number_of_images: The number of images to generate.
|
|
57
65
|
save_gif: Whether to save the generation process as a GIF.
|
|
66
|
+
gif_path: Where the GIF is saved when ``save_gif`` is set.
|
|
58
67
|
|
|
59
68
|
Returns:
|
|
60
69
|
A PIL image of the generated images stacked together.
|
|
@@ -82,7 +91,7 @@ class DiffusionInference:
|
|
|
82
91
|
|
|
83
92
|
logging.info("Saving GIF")
|
|
84
93
|
frame_one.save(
|
|
85
|
-
|
|
94
|
+
gif_path,
|
|
86
95
|
format="GIF",
|
|
87
96
|
append_images=images,
|
|
88
97
|
save_all=True,
|