-
Notifications
You must be signed in to change notification settings - Fork 6k
Stable-Diffusion-Inpainting: Training Pipeline V1.5, V2 #6922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cryptexis
wants to merge
20
commits into
huggingface:main
Choose a base branch
from
cryptexis:sd_15_inpainting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,926
−0
Open
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
2116de2
wip: training script
882cb67
wip: update documentation
89854ee
fix: README
969605f
fix: README title
18191cc
Merge branch 'main' into sd_15_inpainting
sayakpaul 69d4494
wip: integrating LAMA masking
272dc87
wip: merged commits
07c8fd1
wip: final fixes
c1c3a0e
wip: updating README
cd619ff
Merge branch 'main' into sd_15_inpainting
sayakpaul 94d877c
wip: last inference step with log_validation
5532dea
Merge branch 'main' into sd_15_inpainting
sayakpaul 5dd28bd
wip: fixing log_validation, tests
235655f
Merge branch 'sd_15_inpainting' of github.com:cryptexis/diffusers int…
5179539
run quality
sayakpaul 2d07574
Merge branch 'main' into sd_15_inpainting
sayakpaul 8f33ed1
Update examples/inpainting/README.md
yiyixuxu f2b04e3
Update examples/inpainting/train_inpainting.py
yiyixuxu 7dc6bfb
Update examples/inpainting/train_inpainting.py
yiyixuxu d11619a
Update examples/inpainting/train_inpainting.py
yiyixuxu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
# Stable Diffusion Inpainting fine-tuning | ||
|
||
The `train_inpainting.py` script shows how to fine-tune stable diffusion model on your own dataset. | ||
|
||
___Note___: | ||
|
||
___This script is experimental. The script fine-tunes the whole model and often times the model overfits and runs into issues like catastrophic forgetting. It's recommended to try different hyperparamters to get the best result on your dataset.___ | ||
|
||
|
||
## Running locally with PyTorch | ||
### Installing the dependencies | ||
|
||
Before running the scripts, make sure to install the library's training dependencies: | ||
|
||
**Important** | ||
|
||
To make sure you can successfully run the latest versions of the example scripts, we highly recommend **installing from source** and keeping the install up to date as we update the example scripts frequently and install some example-specific requirements. To do this, execute the following steps in a new virtual environment: | ||
```bash | ||
git clone https://github.com/huggingface/diffusers | ||
cd diffusers | ||
pip install . | ||
``` | ||
|
||
Then cd in the example folder and run | ||
```bash | ||
pip install -r requirements.txt | ||
``` | ||
|
||
And initialize an [🤗Accelerate](https://github.com/huggingface/accelerate/) environment with: | ||
|
||
```bash | ||
accelerate config | ||
``` | ||
|
||
Note also that we use PEFT library as backend for LoRA training, make sure to have `peft>=0.6.0` installed in your environment. | ||
|
||
### Pokemon example | ||
|
||
You need to accept the model license before downloading or using the weights. In this example we'll use model version `sd-v1-5-inpainting` from runwayml, so you'll need to visit [its card](https://huggingface.co/runwayml/stable-diffusion-inpainting), read the license and tick the checkbox if you agree. | ||
|
||
You have to be a registered user in 🤗 Hugging Face Hub, and you'll also need to use an access token for the code to work. For more information on access tokens, please refer to [this section of the documentation](https://huggingface.co/docs/hub/security-tokens). | ||
|
||
Run the following command to authenticate your token | ||
|
||
```bash | ||
huggingface-cli login | ||
``` | ||
|
||
If you have already cloned the repo, then you won't need to go through these steps. | ||
|
||
<br> | ||
|
||
#### Hardware | ||
With `gradient_checkpointing` and `mixed_precision` it should be possible to fine tune the model on a single 24GB GPU. For higher `batch_size` and faster training it's better to use GPUs with >30GB memory. | ||
|
||
**___Note: Change the `resolution` to 768 if you are using the [stable-diffusion-2-inpainting](https://huggingface.co/stabilityai/stable-diffusion-2-inpainting) 768x768 model.___** | ||
<!-- accelerate_snippet_start --> | ||
```bash | ||
export MODEL_NAME="runwayml/stable-diffusion-inpainting" | ||
sayakpaul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
export DATASET_NAME="lambdalabs/pokemon-blip-captions" | ||
|
||
|
||
accelerate launch --mixed_precision="fp16" train_inpainting.py \ | ||
--pretrained_model_name_or_path=$MODEL_NAME \ | ||
--dataset_name=$DATASET_NAME \ | ||
--use_ema \ | ||
--resolution=512 --center_crop --random_flip \ | ||
--train_batch_size=4 \ | ||
--gradient_accumulation_steps=2 \ | ||
--gradient_checkpointing \ | ||
--max_train_steps=15000 \ | ||
--learning_rate=1e-05 \ | ||
--max_grad_norm=1 --seed=42 \ | ||
--lr_scheduler="constant" --lr_warmup_steps=0 \ | ||
--output_dir="sd-pokemon-model-inpaint" \ | ||
--validation_size=3 | ||
``` | ||
<!-- accelerate_snippet_end --> | ||
|
||
|
||
To run on your own training files prepare the dataset according to the format required by `datasets`, you can find the instructions for how to do that in this [document](https://huggingface.co/docs/datasets/v2.4.0/en/image_load#imagefolder-with-metadata). | ||
If you wish to use custom loading logic, you should modify the script, we have left pointers for that in the training script. | ||
|
||
```bash | ||
export MODEL_NAME="runwayml/stable-diffusion-inpainting" | ||
export DATASET_NAME="path_to_your_dataset" (NOT IMPLEMENTED) | ||
|
||
|
||
accelerate launch --mixed_precision="fp16" train_inpainting.py \ | ||
--pretrained_model_name_or_path=$MODEL_NAME \ | ||
--dataset_name=$DATASET_NAME \ | ||
--use_ema \ | ||
--resolution=512 --center_crop --random_flip \ | ||
--train_batch_size=4 \ | ||
--gradient_accumulation_steps=2 \ | ||
--gradient_checkpointing \ | ||
--max_train_steps=15000 \ | ||
--learning_rate=1e-05 \ | ||
--max_grad_norm=1 --seed=42 \ | ||
--lr_scheduler="constant" --lr_warmup_steps=0 \ | ||
--output_dir="sd-pokemon-model-inpaint" \ | ||
--validation_size=3 | ||
``` | ||
|
||
|
||
Once the training is finished the model will be saved in the `output_dir` specified in the command. In this example it's `sd-pokemon-model-inpaint`. To load the fine-tuned model for inference just pass that path to `StableDiffusionInpaintPipeline` | ||
|
||
```python | ||
import torch | ||
from PIL import Image | ||
from diffusers import StableDiffusionInpaintPipeline | ||
|
||
init_image = Image.open("path_to_image").resize((512, 512)) | ||
mask_image = Image.open("path_to_mask").resize((512, 512)) | ||
|
||
model_path = "path_to_saved_model" | ||
pipe = StableDiffusionInpaintPipeline.from_pretrained(model_path, torch_dtype=torch.float16) | ||
pipe.to("cuda") | ||
|
||
inpainted_image = pipe( | ||
prompt = "yoda", | ||
image = init_image, | ||
mask_image = mask_image, | ||
).images[0] | ||
|
||
inpainted_image.save("inpainted-yoda-pokemon.png") | ||
``` | ||
|
||
Checkpoints only save the unet, so to run inference from a checkpoint, just load the unet | ||
|
||
```python | ||
import torch | ||
from PIL import Image | ||
from diffusers import StableDiffusionInpaintPipeline, UNet2DConditionModel | ||
|
||
init_image = Image.open("path_to_image").resize((512, 512)) | ||
mask_image = Image.open("path_to_mask").resize((512, 512)) | ||
|
||
model_path = "path_to_saved_model" | ||
unet = UNet2DConditionModel.from_pretrained(model_path + "/checkpoint-<N>/unet", torch_dtype=torch.float16) | ||
|
||
pipe = StableDiffusionInpaintPipeline.from_pretrained("<initial model>", unet=unet, torch_dtype=torch.float16) | ||
pipe.to("cuda") | ||
|
||
inpainted_image = pipe( | ||
prompt = "yoda", | ||
image = init_image, | ||
mask_image = mask_image, | ||
).images[0] | ||
|
||
inpainted_image.save("inpainted-yoda-pokemon.png") | ||
``` | ||
|
||
#### Training with multiple GPUs | ||
|
||
`accelerate` allows for seamless multi-GPU training. Follow the instructions [here](https://huggingface.co/docs/accelerate/basic_tutorials/launch) | ||
for running distributed training with `accelerate`. Here is an example command: | ||
|
||
```bash | ||
export MODEL_NAME="runwayml/stable-diffusion-inpainting" | ||
export DATASET_NAME="lambdalabs/pokemon-blip-captions" | ||
|
||
|
||
accelerate launch --mixed_precision="fp16" --multi_gpu train_inpainting.py \ | ||
--pretrained_model_name_or_path=$MODEL_NAME \ | ||
--dataset_name=$DATASET_NAME \ | ||
--use_ema \ | ||
--resolution=512 --center_crop --random_flip \ | ||
--train_batch_size=4 \ | ||
--gradient_accumulation_steps=2 \ | ||
--gradient_checkpointing \ | ||
--max_train_steps=15000 \ | ||
--learning_rate=1e-05 \ | ||
--max_grad_norm=1 --seed=42 \ | ||
--lr_scheduler="constant" --lr_warmup_steps=0 \ | ||
--output_dir="sd-pokemon-model-inpaint" \ | ||
--validation_size=3 | ||
``` | ||
|
||
|
||
#### Training with Min-SNR weighting | ||
|
||
We support training with the Min-SNR weighting strategy proposed in [Efficient Diffusion Training via Min-SNR Weighting Strategy](https://arxiv.org/abs/2303.09556) which helps to achieve faster convergence | ||
by rebalancing the loss. In order to use it, one needs to set the `--snr_gamma` argument. The recommended | ||
value when using it is 5.0. | ||
|
||
You can find [this project on Weights and Biases](https://wandb.ai/sayakpaul/text2image-finetune-minsnr) that compares the loss surfaces of the following setups: | ||
|
||
* Training without the Min-SNR weighting strategy | ||
* Training with the Min-SNR weighting strategy (`snr_gamma` set to 5.0) | ||
* Training with the Min-SNR weighting strategy (`snr_gamma` set to 1.0) | ||
|
||
For our small Pokemons dataset, the effects of Min-SNR weighting strategy might not appear to be pronounced, but for larger datasets, we believe the effects will be more pronounced. | ||
|
||
Also, note that in this example, we either predict `epsilon` (i.e., the noise) or the `v_prediction`. For both of these cases, the formulation of the Min-SNR weighting strategy that we have used holds. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
accelerate>=0.16.0 | ||
torchvision | ||
transformers>=4.25.1 | ||
datasets | ||
ftfy | ||
tensorboard | ||
Jinja2 | ||
peft==0.7.0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.