Skip to content

Commit a353c46

Browse files
[Docs] Training docs (#415)
finish training docs
1 parent c29d81c commit a353c46

File tree

3 files changed

+179
-28
lines changed

3 files changed

+179
-28
lines changed

docs/source/training/overview.mdx

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,60 @@ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express o
1010
specific language governing permissions and limitations under the License.
1111
-->
1212

13-
# Training
13+
# 🧨 Diffusers Training Examples
1414

15-
You can train diffusion models in multiple ways:
15+
Diffusers examples are a collection of scripts to demonstrate how to effectively use the `diffusers` library
16+
for a variety of use cases.
17+
18+
**Note**: If you are looking for **official** examples on how to use `diffusers` for inference,
19+
please have a look at [src/diffusers/pipelines](https://github.com/huggingface/diffusers/tree/main/src/diffusers/pipelines)
20+
21+
Our examples aspire to be **self-contained**, **easy-to-tweak**, **beginner-friendly** and for **one-purpose-only**.
22+
More specifically, this means:
23+
24+
- **Self-contained**: An example script shall only depend on "pip-install-able" Python packages that can be found in a `requirements.txt` file. Example scripts shall **not** depend on any local files. This means that one can simply download an example script, *e.g.* [train_unconditional.py](https://github.com/huggingface/diffusers/blob/main/examples/unconditional_image_generation/train_unconditional.py), install the required dependencies, *e.g.* [requirements.txt](https://github.com/huggingface/diffusers/blob/main/examples/unconditional_image_generation/requirements.txt) and execute the example script.
25+
- **Easy-to-tweak**: While we strive to present as many use cases as possible, the example scripts are just that - examples. It is expected that they won't work out-of-the box on your specific problem and that you will be required to change a few lines of code to adapt them to your needs. To help you with that, most of the examples fully expose the preprocessing of the data and the training loop to allow you to tweak and edit them as required.
26+
- **Beginner-friendly**: We do not aim for providing state-of-the-art training scripts for the newest models, but rather examples that can be used as a way to better understand diffusion models and how to use them with the `diffusers` library. We often purposefully leave out certain state-of-the-art methods if we consider them too complex for beginners.
27+
- **One-purpose-only**: Examples should show one task and one task only. Even if a task is from a modeling
28+
point of view very similar, *e.g.* image super-resolution and image modification tend to use the same model and training method, we want examples to showcase only one task to keep them as readable and easy-to-understand as possible.
29+
30+
We provide **official** examples that cover the most popular tasks of diffusion models.
31+
*Official* examples are **actively** maintained by the `diffusers` maintainers and we try to rigorously follow our example philosophy as defined above.
32+
If you feel like another important example should exist, we are more than happy to welcome a [Feature Request](https://github.com/huggingface/diffusers/issues/new?assignees=&labels=&template=feature_request.md&title=) or directly a [Pull Request](https://github.com/huggingface/diffusers/compare) from you!
33+
34+
Training examples show how to pretrain or fine-tune diffusion models for a variety of tasks. Currently we support:
1635

1736
- [Unconditional Training](./unconditional_training)
1837
- [Text-to-Image Training](./text2image)
1938
- [Text Inversion](./text_inversion)
39+
40+
41+
| Task | 🤗 Accelerate | 🤗 Datasets | Colab
42+
|---|---|:---:|:---:|
43+
| [**Unconditional Image Generation**](./unconditional_training) | | | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/training_example.ipynb)
44+
| [**Text-to-Image**](./text2image) | - | - |
45+
| [**Text-Inversion**](./text_inversion) | | | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/sd_textual_inversion_training.ipynb)
46+
47+
## Community
48+
49+
In addition, we provide **community** examples, which are examples added and maintained by our community.
50+
Community examples can consist of both *training* examples or *inference* pipelines.
51+
For such examples, we are more lenient regarding the philosophy defined above and also cannot guarantee to provide maintenance for every issue.
52+
Examples that are useful for the community, but are either not yet deemed popular or not yet following our above philosophy should go into the [community examples](https://github.com/huggingface/diffusers/tree/main/examples/community) folder. The community folder therefore includes training examples and inference pipelines.
53+
**Note**: Community examples can be a [great first contribution](https://github.com/huggingface/diffusers/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22) to show to the community how you like to use `diffusers` 🪄.
54+
55+
## Important note
56+
57+
To make sure you can successfully run the latest versions of the example scripts, you have to **install the library from source** and install some example-specific requirements. To do this, execute the following steps in a new virtual environment:
58+
59+
```bash
60+
git clone https://github.com/huggingface/diffusers
61+
cd diffusers
62+
pip install .
63+
```
64+
65+
Then cd in the example folder of your choice and run
66+
67+
```bash
68+
pip install -r requirements.txt
69+
```

docs/source/training/text2image.mdx

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,6 @@ specific language governing permissions and limitations under the License.
1111
-->
1212

1313

14+
# Text-to-Image Training
1415

15-
# Quicktour
16-
17-
Start using Diffusers🧨 quickly!
18-
To start, use the [`DiffusionPipeline`] for quick inference and sample generations!
19-
20-
```
21-
pip install diffusers
22-
```
23-
24-
## Main classes
25-
26-
### Models
27-
28-
### Schedulers
29-
30-
### Pipeliens
31-
32-
16+
Under construction 🚧

docs/source/training/unconditional_training.mdx

Lines changed: 125 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,140 @@ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express o
1010
specific language governing permissions and limitations under the License.
1111
-->
1212

13+
# Unconditional Image-Generation
1314

15+
In this section, we explain how one can train an unconditional image generation diffusion
16+
model. "Unconditional" because the model is not conditioned on any context to generate an image - once trained the model will simply generate images that resemble its training data
17+
distribution.
1418

15-
# Quicktour
19+
## Installing the dependencies
1620

17-
Start using Diffusers🧨 quickly!
18-
To start, use the [`DiffusionPipeline`] for quick inference and sample generations!
21+
Before running the scipts, make sure to install the library's training dependencies:
1922

23+
```bash
24+
pip install diffusers[training] accelerate datasets
2025
```
21-
pip install diffusers
26+
27+
And initialize an [🤗Accelerate](https://github.com/huggingface/accelerate/) environment with:
28+
29+
```bash
30+
accelerate config
31+
```
32+
33+
## Unconditional Flowers
34+
35+
The command to train a DDPM UNet model on the Oxford Flowers dataset:
36+
37+
```bash
38+
accelerate launch train_unconditional.py \
39+
--dataset_name="huggan/flowers-102-categories" \
40+
--resolution=64 \
41+
--output_dir="ddpm-ema-flowers-64" \
42+
--train_batch_size=16 \
43+
--num_epochs=100 \
44+
--gradient_accumulation_steps=1 \
45+
--learning_rate=1e-4 \
46+
--lr_warmup_steps=500 \
47+
--mixed_precision=no \
48+
--push_to_hub
49+
```
50+
An example trained model: https://huggingface.co/anton-l/ddpm-ema-flowers-64
51+
52+
A full training run takes 2 hours on 4xV100 GPUs.
53+
54+
<img src="https://user-images.githubusercontent.com/26864830/180248660-a0b143d0-b89a-42c5-8656-2ebf6ece7e52.png" width="700" />
55+
56+
## Unconditional Pokemon
57+
58+
The command to train a DDPM UNet model on the Pokemon dataset:
59+
60+
```bash
61+
accelerate launch train_unconditional.py \
62+
--dataset_name="huggan/pokemon" \
63+
--resolution=64 \
64+
--output_dir="ddpm-ema-pokemon-64" \
65+
--train_batch_size=16 \
66+
--num_epochs=100 \
67+
--gradient_accumulation_steps=1 \
68+
--learning_rate=1e-4 \
69+
--lr_warmup_steps=500 \
70+
--mixed_precision=no \
71+
--push_to_hub
72+
```
73+
An example trained model: https://huggingface.co/anton-l/ddpm-ema-pokemon-64
74+
75+
A full training run takes 2 hours on 4xV100 GPUs.
76+
77+
<img src="https://user-images.githubusercontent.com/26864830/180248200-928953b4-db38-48db-b0c6-8b740fe6786f.png" width="700" />
78+
79+
80+
## Using your own data
81+
82+
To use your own dataset, there are 2 ways:
83+
- you can either provide your own folder as `--train_data_dir`
84+
- or you can upload your dataset to the hub (possibly as a private repo, if you prefer so), and simply pass the `--dataset_name` argument.
85+
86+
**Note**: If you want to create your own training dataset please have a look at [this document](https://huggingface.co/docs/datasets/image_process#image-datasets).
87+
88+
Below, we explain both in more detail.
89+
90+
### Provide the dataset as a folder
91+
92+
If you provide your own folders with images, the script expects the following directory structure:
93+
94+
```bash
95+
data_dir/xxx.png
96+
data_dir/xxy.png
97+
data_dir/[...]/xxz.png
98+
```
99+
100+
In other words, the script will take care of gathering all images inside the folder. You can then run the script like this:
101+
102+
```bash
103+
accelerate launch train_unconditional.py \
104+
--train_data_dir <path-to-train-directory> \
105+
<other-arguments>
106+
```
107+
108+
Internally, the script will use the [`ImageFolder`](https://huggingface.co/docs/datasets/v2.0.0/en/image_process#imagefolder) feature which will automatically turn the folders into 🤗 Dataset objects.
109+
110+
### Upload your data to the hub, as a (possibly private) repo
111+
112+
It's very easy (and convenient) to upload your image dataset to the hub using the [`ImageFolder`](https://huggingface.co/docs/datasets/v2.0.0/en/image_process#imagefolder) feature available in 🤗 Datasets. Simply do the following:
113+
114+
```python
115+
from datasets import load_dataset
116+
117+
# example 1: local folder
118+
dataset = load_dataset("imagefolder", data_dir="path_to_your_folder")
119+
120+
# example 2: local files (suppoted formats are tar, gzip, zip, xz, rar, zstd)
121+
dataset = load_dataset("imagefolder", data_files="path_to_zip_file")
122+
123+
# example 3: remote files (supported formats are tar, gzip, zip, xz, rar, zstd)
124+
dataset = load_dataset(
125+
"imagefolder",
126+
data_files="https://download.microsoft.com/download/3/E/1/3E1C3F21-ECDB-4869-8368-6DEBA77B919F/kagglecatsanddogs_3367a.zip",
127+
)
128+
129+
# example 4: providing several splits
130+
dataset = load_dataset(
131+
"imagefolder", data_files={"train": ["path/to/file1", "path/to/file2"], "test": ["path/to/file3", "path/to/file4"]}
132+
)
22133
```
23134

24-
## Main classes
135+
`ImageFolder` will create an `image` column containing the PIL-encoded images.
25136

26-
### Models
137+
Next, push it to the hub!
27138

28-
### Schedulers
139+
```python
140+
# assuming you have ran the huggingface-cli login command in a terminal
141+
dataset.push_to_hub("name_of_your_dataset")
29142

30-
### Pipeliens
143+
# if you want to push to a private repo, simply pass private=True:
144+
dataset.push_to_hub("name_of_your_dataset", private=True)
145+
```
31146

147+
and that's it! You can now train your model by simply setting the `--dataset_name` argument to the name of your dataset on the hub.
32148

149+
More on this can also be found in [this blog post](https://huggingface.co/blog/image-search-datasets).

0 commit comments

Comments
 (0)