|
1 | 1 | # 🤗 Noise Conditional Score Networks
|
2 | 2 |
|
3 |
| -[](https://github.com/py-img-gen/diffusers-ncsn/actions/workflows/ci.yaml) [](https://github.com/ermongroup/ncsn) |
| 3 | +[](https://github.com/py-img-gen/diffusers-ncsn/actions/workflows/ci.yaml) |
| 4 | +[](https://github.com/ermongroup/ncsn) |
| 5 | +[](https://huggingface.co/py-img-gen/ncsn-mnist) |
4 | 6 |
|
5 | 7 | [`🤗 diffusers`](https://github.com/huggingface/diffusers) implementation of the paper ["Generative Modeling by Estimating Gradients of the Data Distribution" [Yang+ NeurIPS'19]](https://arxiv.org/abs/1907.05600).
|
6 | 8 |
|
7 |
| -## Installation |
| 9 | +## How to use |
| 10 | + |
| 11 | +### Use without installation |
| 12 | + |
| 13 | +You can load the pretrained pipeline directly from the HF Hub as follows: |
| 14 | + |
| 15 | +```python |
| 16 | +import torch |
| 17 | +from diffusers import DiffusionPipeline |
| 18 | +from diffusers.utils import make_image_grid |
| 19 | + |
| 20 | +# Specify the device to use |
| 21 | +device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| 22 | + |
| 23 | +# |
| 24 | +# Load the pipeline from the Hugging Face Hub |
| 25 | +# |
| 26 | +pipe = DiffusionPipeline.from_pretrained( |
| 27 | + "py-img-gen/ncsn-mnist", trust_remote_code=True |
| 28 | +) |
| 29 | +pipe = pipe.to(device) |
| 30 | + |
| 31 | +# Generate samples; here, we specify the seed and generate 16 images |
| 32 | +output = pipe( |
| 33 | + batch_size=16, |
| 34 | + generator=torch.manual_seed(42), |
| 35 | +) |
| 36 | + |
| 37 | +# Create a grid image from the generated samples |
| 38 | +image = make_image_grid(images=output.images, rows=4, cols=4) |
| 39 | +image.save("output.png") |
| 40 | +``` |
| 41 | + |
| 42 | +### Use with installation |
| 43 | + |
| 44 | +First, install the package from this repository: |
8 | 45 |
|
9 | 46 | ```shell
|
10 | 47 | pip install git+https://github.com/py-img-gen/diffusers-ncsn
|
11 | 48 | ```
|
12 | 49 |
|
| 50 | +Then, you can use the package as follows: |
| 51 | + |
| 52 | +```python |
| 53 | +import torch |
| 54 | + |
| 55 | +from ncsn.pipeline_ncsn import NCSNPipeline |
| 56 | + |
| 57 | +# Specify the device to use |
| 58 | +device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| 59 | + |
| 60 | +# |
| 61 | +# Load the pipeline from the HF Hub through the NCSNPipeline of this library |
| 62 | +# |
| 63 | +pipe = NCSNPipeline.from_pretrained("py-img-gen/ncsn-mnist", trust_remote_code=True) |
| 64 | +pipe = pipe.to(device) |
| 65 | + |
| 66 | +# Generate samples; here, we specify the seed and generate 16 images |
| 67 | +output = pipe( |
| 68 | + batch_size=16, |
| 69 | + generator=torch.manual_seed(42), |
| 70 | +) |
| 71 | + |
| 72 | +# Create a grid image from the generated samples |
| 73 | +image = make_image_grid(images=output.images, rows=4, cols=4) |
| 74 | +image.save("output.png") |
| 75 | +``` |
| 76 | + |
| 77 | +## Pretrained models and pipeline |
| 78 | + |
| 79 | +[](https://huggingface.co/py-img-gen/ncsn-mnist) |
| 80 | + |
13 | 81 | ## Showcase
|
14 | 82 |
|
15 | 83 | ### MNIST
|
16 | 84 |
|
17 | 85 | Example of generating MNIST character images using the model trained with [`train_mnist.py`](https://github.com/py-img-gen/diffusers-ncsn/blob/main/train_mnist.py).
|
18 | 86 |
|
19 |
| -<div align="center"> |
| 87 | +<p align="center"> |
20 | 88 | <img alt="mnist" src="https://github.com/user-attachments/assets/483b6637-2684-4844-8aa1-12b866d46226" width="50%" />
|
21 |
| -</div> |
| 89 | +</p> |
| 90 | + |
| 91 | +# Notes on uploading pipelines to the HF Hub with custom components |
| 92 | + |
| 93 | +While referring to 📝 [Load community pipelines and components - huggingface diffusers](https://huggingface.co/docs/diffusers/using-diffusers/custom_pipeline_overview#community-components), pay attention to the following points. |
| 94 | +- Change [the `_class_name` attribute](https://huggingface.co/py-img-gen/ncsn-mnist/blob/main/model_index.json#L2) in [`model_index.json`](https://huggingface.co/py-img-gen/ncsn-mnist/blob/main/model_index.json) to `["pipeline_ncsn", "NCSNPipeline"]`. |
| 95 | +- Upload [`pipeline_ncsn.py`](https://github.com/py-img-gen/diffusers-ncsn/blob/main/src/ncsn/pipeline_ncsn.py) to [the root of the pipeline repository](https://huggingface.co/py-img-gen/ncsn-mnist/blob/main/pipeline_ncsn.py). |
| 96 | +- Upload custom components to each subfolder: |
| 97 | + - Upload [`scheduling_ncsn.py`](https://github.com/py-img-gen/diffusers-ncsn/blob/main/src/ncsn/scheduler/scheduling_ncsn.py) to [the `scheduler` subfolder](https://huggingface.co/py-img-gen/ncsn-mnist/tree/main/scheduler). |
| 98 | + - Upload [`unet_2d_ncsn.py`](https://github.com/py-img-gen/diffusers-ncsn/blob/main/src/ncsn/unet/unet_2d_ncsn.py) to [the `unet` subfolder](https://huggingface.co/py-img-gen/ncsn-mnist/tree/main/unet). |
| 99 | +- Ensure that the custom components are placed in each subfolder because they are referenced by relative paths from `pipeline_ncsn.py`. |
| 100 | + - Based on this, the code in this library is also placed in the same directory structure as the HF Hub. |
| 101 | + - For example, `pipeline_ncsn.py` imports `unet_2d_ncsn.py` as `from .unet.unet_2d_ncsn import UNet2DModelForNCSN` because it is placed in the `unet` subfolder. |
22 | 102 |
|
23 | 103 | ## Acknowledgements
|
24 | 104 |
|
|
0 commit comments