{"id":20255965,"url":"https://github.com/michedev/ddpms-pytorch","last_synced_at":"2025-04-09T07:10:23.465Z","repository":{"id":60370764,"uuid":"512798371","full_name":"Michedev/DDPMs-Pytorch","owner":"Michedev","description":"Implementation of various DDPM papers to understand how they work ","archived":false,"fork":false,"pushed_at":"2025-03-20T10:08:16.000Z","size":245,"stargazers_count":84,"open_issues_count":0,"forks_count":10,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-02T05:07:50.473Z","etag":null,"topics":["ddpm","deep-learning","generative-model","pytorch"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Michedev.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-07-11T14:51:20.000Z","updated_at":"2025-03-20T10:08:20.000Z","dependencies_parsed_at":"2024-11-14T10:44:43.118Z","dependency_job_id":"2eb878ed-239f-4fbf-9311-8c93f47d16d5","html_url":"https://github.com/Michedev/DDPMs-Pytorch","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Michedev%2FDDPMs-Pytorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Michedev%2FDDPMs-Pytorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Michedev%2FDDPMs-Pytorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Michedev%2FDDPMs-Pytorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Michedev","download_url":"https://codeload.github.com/Michedev/DDPMs-Pytorch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247994122,"owners_count":21030050,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ddpm","deep-learning","generative-model","pytorch"],"created_at":"2024-11-14T10:44:36.516Z","updated_at":"2025-04-09T07:10:23.442Z","avatar_url":"https://github.com/Michedev.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![PyPI - Downloads](https://img.shields.io/pypi/dm/ddpm)](https://pypi.org/project/ddpm/)\n[![PyPI](https://img.shields.io/pypi/v/ddpm)](https://pypi.org/project/ddpm/)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/ddpm)](https://pypi.org/project/ddpm/)\n\n# DDPM Pytorch\n\nPytorch implementation of \"_Improved Denoising Diffusion Probabilistic Models_\", \n\"_Denoising Diffusion Probabilistic Models_\" and \"_Classifier-free Diffusion Guidance_\"\n\n![](https://hojonathanho.github.io/diffusion/assets/img/pgm_diagram_xarrow.png)\n\n\n# How to use\n\nThere are two ways to use this repository:\n\n1.  Install pip package containing the pytorch lightning model, which includes also the training step\n\n        pip install ddpm\n\n2. Clone the repository to have the full control of the training\n\n        git clone https://github.com/Michedev/DDPMs-Pytorch\n\n# How to train\n\n1. Install the project environment via hatch (`pip install hatch`). There are two environments: _default_ has torch with cuda support, _cpu_ without it.\n\n       hatch env create\nor\n       hatch env create cpu\n\n\n2. Train the model\n\n       hatch run train \n\n   or for the cpu environment\n       \n       hatch run cpu:train\n\n   Note that this is valid for any `hatch run [env:]{command}` command \n\n   By default, the version of trained DDPM is from \"Improved Denoising Diffusion Probabilistic Models\" paper on MNIST dataset.\n   You can switch to the original DDPM by disabling the variational lower bound with the following command:\n      \n       hatch run train model.vlb=False\n   \n   You can also train the DDPM with the Classifier-free Diffusion Guidance by changing the model:\n\n       hatch run train model=unet_class_conditioned\n    \n    or via the shortcut\n\n       hatch run train-class-conditioned\n\n    Finally, under saved_models/{train-datetime} you can find the trained model, the tensorboard logs, the training config\n\n# How to generate\n\n1. Train a model (See previous section)\n\n2. Generate a new batch of images\n\n       hatch run generate -r RUN\n\n   The other options are: `[--seed SEED] [--device DEVICE] [--batch-size BATCH_SIZE] [-w W] [--scheduler {linear,cosine,tan}] [-T T]`\n\n# Configure the training\n\nUnder _config_ there are several yaml files containing the training parameters \nsuch as model class and paramters, noise steps, scheduler and so on. \nNote that the hyperparameters in such files are taken from \nthe papers \"_Improved Denoising Diffusion Probabilistic Models_\" \nand \"_Denoising Diffusion Probabilistic Models_\". Down below the explaination of the config file for train the model:\n\n    defaults:\n      - model: unet_paper  # take the model config from model/unet_paper.yaml\n      - scheduler: cosine  # use the cosine scheduler from scheduler/cosine.yaml\n      - dataset: mnist\n      - optional model_dataset: ${model}-${dataset}  # set particular hyper parameters for specific couples (model, dataset)\n      - optional model_scheduler: ${model}-${scheduler} # set particular hyper parameters for specific couples (model, scheduler)\n\n    batch_size: 128 # train batch size\n    noise_steps: 4_000  # noising steps; the T in \"Improved Denoising Diffusion Probabilistic Models\" and \"Denoising Diffusion Probabilistic Models\"\n    accelerator: null  # training hardware; for more details see pytorch lightning\n    devices: null  # training devices to use; for more details see pytorch lightning\n    gradient_clip_val: 0.0  # 0.0 means gradient clip disabled\n    gradient_clip_algorithm: norm  # gradient clip has two values: 'norm' or 'value\n    ema: true  # use Exponential Moving Average implemented in ema.py\n    ema_decay: 0.99  # decay factor of EMA\n\n    hydra:\n      run:\n        dir: saved_models/${now:%Y_%m_%d_%H_%M_%S}\n\n# Project structure\n\n      .\n      ├── callbacks  # Pytorch Lightning callbacks for training\n      │   ├── ema.py  # exponential moving average callback\n      ├── config  # config files for training for hydra\n      │   ├── dataset  # dataset config files\n      │   ├── model  # model config files\n      │   ├── model_dataset  # specific (model, dataset) config\n      │   ├── model_scheduler  # specific (model, scheduler) config\n      │   ├── scheduler  # scheduler config files\n      │   └── train.yaml  # training config file\n      ├── generate.py  # script for generating images\n      ├── model  # model files\n      │   ├── classifier_free_ddpm.py  # Classifier-free Diffusion Guidance\n      │   ├── ddpm.py  # Denoising Diffusion Probabilistic Models\n      │   ├── distributions.py  # distributions functions for diffusion\n      │   ├── unet_class.py  # UNet model for Classifier-free Diffusion Guidance\n      │   └── unet.py  # UNet model for Denoising Diffusion Probabilistic Models\n      ├── pyproject.toml  # setuptool file to publish model/ to pypi and to manage the envs\n      ├── readme.md   # this file\n      ├── readme_pip.md  # readme for pypi\n      ├── train.py  # script for training\n      ├── utils  # utility functions\n      └── variance_scheduler  # variance scheduler files\n          ├── cosine.py  # cosine variance scheduler\n          └── linear.py  # linear variance scheduler\n\n### Add custom dataset\n\nTo add a custom dataset, you need to create a new class that inherits from torch.utils.data.Dataset\nand implement the __len__ and __getitem__ methods. \nThen, you need to add the config file to the _config/dataset_ folder with a similar\nstructure of mnist.yaml\n\n    width: 28  # meta info about the dataset\n    height: 28\n    channels: 1   # number of image channels\n    num_classes: 10  # number of classes\n    files_location: ~/.cache/torchvision_dataset  # location where to store the dataset, in case to be downloaded\n    train:  #dataset.train is instantiated with this config\n      _target_: torchvision.datasets.MNIST  # Dataset class. Following arguments are passed to the dataset class constructor\n      root: ${dataset.files_location}\n      train: true\n      download: true\n      transform:\n        _target_: torchvision.transforms.ToTensor\n    val:  #dataset.val is instantiated with this config\n      _target_: torchvision.datasets.MNIST # Same dataset of train, but the validation split\n      root: ${dataset.files_location}\n      train: false\n      download: true\n      transform:\n        _target_: torchvision.transforms.ToTensor\n\n# Examples of custom training\n\n### Disable the variational lower bound, use Linear scheduler, use 1000 noise steps, train in GPU\n\n    hatch run train scheduler=linear accelerator='gpu' model.vlb=False noise_steps=1000\n\n\n## Classifier-free Guidance\n\nUse the labels for __Diffusion Guidance__, as in \"_Classifier-free Diffusion Guidance_\" with the following command\n\n    hatch run train model=unet_class_conditioned noise_steps=1000\n\n## Add your scheduler\n\n1. Add a new class (preferabily under `variance_scheduler/`) which subclasses `Scheduler` class or just copy the same methods syntax of `Scheduler`\n2. Define a new config under `config/scheduler` with the name _my-scheduler.yaml_ containing the following fields\n\n```   \n _target_: {your scheduler import path} (e.g. variance_scheduler.Linear)\n... // your scheduler additional parameters\n```\n\nFinally train with the following command\n\n    hatch run train scheduler=my-scheduler\n\n## Add your dataset\n\n1. Add a new class which subclasses `torch.utils.data.Dataset`\n\n2. Define a new config under `config/dataset` with the name _my-dataset.yaml_ containing the following fields\n\n```   \nwidth: ???\nheight: ???\nchannels: ???\ntrain:\n  _target_: {your dataset import path} (e.g. torchvision.datasets.MNIST)\n  // your dataset additional parameters\nval:\n  _target_: {your dataset import path} (e.g. torchvision.datasets.MNIST)\n  // your dataset additional parameters\n```\n\nFinally train with the following command\n\n    hatch run train dataset=my-dataset\n    ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichedev%2Fddpms-pytorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmichedev%2Fddpms-pytorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichedev%2Fddpms-pytorch/lists"}