π€ diffusers
implementation of the paper "Generative Modeling by Estimating Gradients of the Data Distribution" [Yang+ NeurIPS'19].
You can load the pretrained pipeline directly from the HF Hub as follows:
import torch
from diffusers import DiffusionPipeline
from diffusers.utils import make_image_grid
# Specify the device to use
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
#
# Load the pipeline from the Hugging Face Hub
#
pipe = DiffusionPipeline.from_pretrained(
"py-img-gen/ncsn-mnist", trust_remote_code=True
)
pipe = pipe.to(device)
# Generate samples; here, we specify the seed and generate 16 images
output = pipe(
batch_size=16,
generator=torch.manual_seed(42),
)
# Create a grid image from the generated samples
image = make_image_grid(images=output.images, rows=4, cols=4)
image.save("output.png")
First, install the package from this repository:
pip install diffusers-ncsn
Then, you can use the package as follows:
import torch
from ncsn.pipeline_ncsn import NCSNPipeline
# Specify the device to use
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
#
# Load the pipeline from the HF Hub through the NCSNPipeline of this library
#
pipe = NCSNPipeline.from_pretrained("py-img-gen/ncsn-mnist", trust_remote_code=True)
pipe = pipe.to(device)
# Generate samples; here, we specify the seed and generate 16 images
output = pipe(
batch_size=16,
generator=torch.manual_seed(42),
)
# Create a grid image from the generated samples
image = make_image_grid(images=output.images, rows=4, cols=4)
image.save("output.png")
Example of generating MNIST character images using the model trained with train_mnist.py
.
While referring to π Load community pipelines and components - huggingface diffusers, pay attention to the following points.
- Change the
_class_name
attribute inmodel_index.json
to["pipeline_ncsn", "NCSNPipeline"]
. - Upload
pipeline_ncsn.py
to the root of the pipeline repository. - Upload custom components to each subfolder:
- Upload
scheduling_ncsn.py
to thescheduler
subfolder. - Upload
unet_2d_ncsn.py
to theunet
subfolder.
- Upload
- Ensure that the custom components are placed in each subfolder because they are referenced by relative paths from
pipeline_ncsn.py
.- Based on this, the code in this library is also placed in the same directory structure as the HF Hub.
- For example,
pipeline_ncsn.py
importsunet_2d_ncsn.py
asfrom .unet.unet_2d_ncsn import UNet2DModelForNCSN
because it is placed in theunet
subfolder.
diffusers-ncsn is licensed under Apache 2.0. A full copy of the license can be found on GitHub.
- JeongJiHeon/ScoreDiffusionModel: The Pytorch Tutorial of Score-based and Diffusion Model https://github.com/JeongJiHeon/ScoreDiffusionModel/tree/main