diffusion-model-framework 1.1.0rc2__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.
Files changed (77) hide show
  1. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/.github/workflows/run-tests.yaml +1 -1
  2. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/.gitignore +1 -0
  3. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/PKG-INFO +2 -2
  4. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/README.md +1 -1
  5. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/assets/sampling-small.gif +1 -1
  6. diffusion_model_framework-1.1.0rc3/diffusion_models/__init__.py +1 -0
  7. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/diffusion_models/_store/dataset.py +25 -1
  8. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/diffusion_models/_store/diffuser.py +6 -1
  9. diffusion_model_framework-1.1.0rc3/diffusion_models/_store/model.py +26 -0
  10. diffusion_model_framework-1.1.0rc3/diffusion_models/cli.py +135 -0
  11. {diffusion_model_framework-1.1.0rc2/examples/config → diffusion_model_framework-1.1.0rc3/diffusion_models/conf}/baseline.yaml +21 -1
  12. diffusion_model_framework-1.1.0rc3/diffusion_models/conf/config/small_attention_ema.yaml +30 -0
  13. diffusion_model_framework-1.1.0rc3/diffusion_models/conf/config/small_attention_v.yaml +18 -0
  14. diffusion_model_framework-1.1.0rc3/diffusion_models/conf/infer.yaml +21 -0
  15. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/diffusion_models/diffusion_inference.py +12 -3
  16. diffusion_model_framework-1.1.0rc3/diffusion_models/diffusion_trainer.py +465 -0
  17. diffusion_model_framework-1.1.0rc3/diffusion_models/evaluation/__init__.py +4 -0
  18. diffusion_model_framework-1.1.0rc3/diffusion_models/evaluation/checkpoint.py +87 -0
  19. diffusion_model_framework-1.1.0rc3/diffusion_models/evaluation/fid.py +146 -0
  20. diffusion_model_framework-1.1.0rc3/diffusion_models/gaussian_diffusion/base_diffuser.py +151 -0
  21. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/diffusion_models/gaussian_diffusion/ddimm_diffuser.py +50 -5
  22. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/diffusion_models/gaussian_diffusion/gaussian_diffuser.py +17 -7
  23. diffusion_model_framework-1.1.0rc3/diffusion_models/inference.py +87 -0
  24. diffusion_model_framework-1.1.0rc3/diffusion_models/models/SmallAttentionUnet.py +212 -0
  25. diffusion_model_framework-1.1.0rc2/examples/train_model.py → diffusion_model_framework-1.1.0rc3/diffusion_models/training.py +27 -17
  26. diffusion_model_framework-1.1.0rc3/diffusion_models/utils/ema.py +97 -0
  27. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/diffusion_models/utils/schemas.py +102 -3
  28. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/docs/source/conf.py +1 -1
  29. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/docs/source/custom/pre-modules/introduction.md +1 -1
  30. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/pyproject.toml +11 -1
  31. diffusion_model_framework-1.1.0rc3/tests/evaluation/__init__.py +0 -0
  32. diffusion_model_framework-1.1.0rc3/tests/evaluation/test_fid.py +222 -0
  33. diffusion_model_framework-1.1.0rc3/tests/gaussian_diffusion/__init__.py +0 -0
  34. diffusion_model_framework-1.1.0rc3/tests/gaussian_diffusion/test_v_prediction.py +115 -0
  35. diffusion_model_framework-1.1.0rc3/tests/models/__init__.py +0 -0
  36. diffusion_model_framework-1.1.0rc3/tests/models/test_small_attention_unet.py +75 -0
  37. diffusion_model_framework-1.1.0rc3/tests/test_diffusion_trainer.py +273 -0
  38. diffusion_model_framework-1.1.0rc3/tests/utils/__init__.py +0 -0
  39. diffusion_model_framework-1.1.0rc3/tests/utils/test_ema.py +71 -0
  40. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/uv.lock +173 -1
  41. diffusion_model_framework-1.1.0rc2/diffusion_models/__init__.py +0 -1
  42. diffusion_model_framework-1.1.0rc2/diffusion_models/_store/model.py +0 -14
  43. diffusion_model_framework-1.1.0rc2/diffusion_models/diffusion_trainer.py +0 -247
  44. diffusion_model_framework-1.1.0rc2/diffusion_models/gaussian_diffusion/base_diffuser.py +0 -91
  45. diffusion_model_framework-1.1.0rc2/examples/infer.py +0 -53
  46. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/.github/workflows/build-and-release-docs.yaml +0 -0
  47. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/.github/workflows/release.yaml +0 -0
  48. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/CHANGELOG.md +0 -0
  49. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/diffusion_models/_store/__init__.py +0 -0
  50. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/diffusion_models/_store/beta_scheduler.py +0 -0
  51. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/diffusion_models/_store/optimizer.py +0 -0
  52. {diffusion_model_framework-1.1.0rc2/diffusion_models/gaussian_diffusion → diffusion_model_framework-1.1.0rc3/diffusion_models/conf}/__init__.py +0 -0
  53. {diffusion_model_framework-1.1.0rc2/diffusion_models/models → diffusion_model_framework-1.1.0rc3/diffusion_models/conf/config}/__init__.py +0 -0
  54. {diffusion_model_framework-1.1.0rc2/diffusion_models/utils → diffusion_model_framework-1.1.0rc3/diffusion_models/gaussian_diffusion}/__init__.py +0 -0
  55. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/diffusion_models/gaussian_diffusion/beta_schedulers.py +0 -0
  56. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/diffusion_models/models/SimpleUnet.py +0 -0
  57. {diffusion_model_framework-1.1.0rc2/examples → diffusion_model_framework-1.1.0rc3/diffusion_models/models}/__init__.py +0 -0
  58. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/diffusion_models/models/base_diffusion_model.py +0 -0
  59. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/diffusion_models/models/conditioned_simple_unet.py +0 -0
  60. {diffusion_model_framework-1.1.0rc2/tests → diffusion_model_framework-1.1.0rc3/diffusion_models/utils}/__init__.py +0 -0
  61. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/diffusion_models/utils/adopt.py +0 -0
  62. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/diffusion_models/utils/batch_processor.py +0 -0
  63. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/diffusion_models/utils/tensorboard.py +0 -0
  64. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/docs/Makefile +0 -0
  65. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/docs/make.bat +0 -0
  66. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/docs/source/_templates/versions.html +0 -0
  67. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/docs/source/custom/post-modules/.gitkeep +0 -0
  68. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/docs/source/examples.rst +0 -0
  69. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/docs/source/index.rst +0 -0
  70. {diffusion_model_framework-1.1.0rc2/tests/gaussian_diffusion → diffusion_model_framework-1.1.0rc3/examples}/__init__.py +0 -0
  71. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/examples/coco_playground.py +0 -0
  72. {diffusion_model_framework-1.1.0rc2/tests/utils → diffusion_model_framework-1.1.0rc3/tests}/__init__.py +0 -0
  73. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/tests/conftest.py +0 -0
  74. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/tests/gaussian_diffusion/test_beta_schedulers.py +0 -0
  75. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/tests/test_base.py +0 -0
  76. {diffusion_model_framework-1.1.0rc2 → diffusion_model_framework-1.1.0rc3}/tests/utils/test_schemas.py +0 -0
  77. {diffusion_model_framework-1.1.0rc2 → 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
  #----------------------------------------------
@@ -172,3 +172,4 @@ save/
172
172
  *.bin
173
173
  examples/generated*.gif
174
174
  test.ipynb
175
+ outputs/
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: diffusion-model-framework
3
- Version: 1.1.0-rc.2
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 python3 examples/train_model.py
59
+ uv run diffusion-models train
60
60
  ```
61
61
 
62
62
  Building a wheel is also possible via:
@@ -40,7 +40,7 @@ uv sync
40
40
  ```
41
41
  You can now start using the package to train your diffusion model. For example, try running:
42
42
  ```shell
43
- uv run python3 examples/train_model.py
43
+ uv run diffusion-models train
44
44
  ```
45
45
 
46
46
  Building a wheel is also possible via:
@@ -1,4 +1,4 @@
1
1
  [diffend] Oversized file quarantined before diffing.
2
- name: diffusion_model_framework-1.1.0rc2/assets/sampling-small.gif
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"
@@ -4,6 +4,7 @@ from torchvision import datasets
4
4
  from torchvision.transforms import v2
5
5
 
6
6
  dataset_store = store(group="dataset")
7
+ eval_dataset_store = store(group="eval_dataset")
7
8
 
8
9
  # Normalize(0.5, 0.5) maps [0, 1] -> [-1, 1]; its inverse is mean=-1, std=2.
9
10
  ImageTransforms = builds(
@@ -19,7 +20,7 @@ ImageTransforms = builds(
19
20
  dataset_store(
20
21
  builds(
21
22
  datasets.CelebA,
22
- root="../data",
23
+ root="data",
23
24
  download=False,
24
25
  split="train",
25
26
  transform=ImageTransforms,
@@ -38,3 +39,26 @@ dataset_store(
38
39
  ),
39
40
  name="fake",
40
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
+ )
@@ -16,5 +16,10 @@ diffuser_store(
16
16
  name="ddim",
17
17
  )
18
18
  diffuser_store(
19
- builds(GaussianDiffuser, beta_scheduler="${beta_scheduler}"), name="ddpm"
19
+ builds(
20
+ GaussianDiffuser,
21
+ beta_scheduler="${beta_scheduler}",
22
+ populate_full_signature=True,
23
+ ),
24
+ name="ddpm",
20
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,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()
@@ -1,6 +1,7 @@
1
1
  # The standard training configuration. Config groups (model, diffuser, ...)
2
2
  # are registered by diffusion_models/_store; override them from the command line,
3
- # e.g. `optimizer=adopt` or `training.batch_size=64`.
3
+ # e.g. `diffusion-models train optimizer=adopt training.batch_size=64`.
4
+ # Presets from conf/config/ are applied with `+config=<name>`.
4
5
  defaults:
5
6
  - _self_
6
7
  - model: simple_unet
@@ -8,9 +9,12 @@ defaults:
8
9
  - beta_scheduler: linear
9
10
  - optimizer: adamw
10
11
  - dataset: celeba
12
+ - eval_dataset: null # e.g. eval_dataset=celeba_valid, for FID
11
13
 
12
14
  seed: 0
13
15
  device: cuda
16
+ # A checkpoint (e.g. runs/<name>/<timestamp>/checkpoints/latest.pt) to resume from.
17
+ resume_from: null
14
18
  image_size: 64
15
19
  image_channels: 3
16
20
  reverse_image_size: 128
@@ -23,7 +27,13 @@ training:
23
27
  number_of_epochs: 500
24
28
  checkpoint_rate: 100
25
29
  mixed_precision_training: false
30
+ mixed_precision_dtype: bfloat16
31
+ channels_last: false
26
32
  gradient_clip: null
33
+ num_workers: 4
34
+ pin_memory: true
35
+ persistent_workers: true
36
+ compile: false
27
37
 
28
38
  logging:
29
39
  _target_: diffusion_models.utils.schemas.LogConfiguration
@@ -32,6 +42,16 @@ logging:
32
42
  number_of_images: 5
33
43
  weight_rate: 635
34
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
+
35
55
  hydra:
36
56
  run:
37
57
  dir: runs/${training.training_name}/${now:%Y-%m-%d_%H-%M-%S}
@@ -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
- image_grid = make_grid(reverse_transformed_images, nrow=5)
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(self, number_of_images: int, save_gif: bool = False) -> Image:
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
- "generated.gif",
94
+ gif_path,
86
95
  format="GIF",
87
96
  append_images=images,
88
97
  save_all=True,