该项目显示了如何在您自己的数据集中微调稳定扩散模型。
注意:此脚本是实验性的。该脚本微调整个模型,并且经常会使模型过度贴合,并遇到灾难性遗忘之类的问题。建议尝试不同的超级参数以在数据集中获得最佳结果。
在运行脚本之前,请确保安装库的培训依赖项(例如Pytorch和?Transformers):
git clone https://github.com/huggingface/diffusers
cd diffusers
pip install .然后运行
cd ..
pip install -r requirements.txt并用以下方式初始化加速环境
accelerate config然后,您应该使用model_download.py下载验证的稳定扩散模型:
python model_download.py --repo_id runwayml/stable-diffusion-v1-5
# If you cannot connect to huggingface, you should use the following command:
python model_download.py --repo_id runwayml/stable-diffusion-v1-5 --mirror要在自己的数据集上微调稳定的扩散模型,您需要以以下格式准备数据集:
首先,在Root Directory中创建dataset集目录,您应该创建三个子目录jpg , hint和train 。在jpg目录中,您应该将所有目标图像放在数据集中。在hint目录中,您应该将所有源图像(条件图像)放在数据集中。在train目录中,您应该放一个metadata.jsonl 。 metadata.jsonl应采用以下格式:
{ "jpg" : " ./dataset/jpg/<name_of_target_image_1> " , "txt" : " <prompt_1> " , "hint" : " ./dataset/hint/<name_of_source_image_1> " }
{ "jpg" : " ./dataset/jpg/<name_of_target_image_2> " , "txt" : " <prompt_2> " , "hint" : " ./dataset/hint/<name_of_source_image_1> " }
…这是dataset集目录的结构:
dataset
├── hint
│ ├── a.png
│ ├── b.png
│ └── c.png
├── jpg
│ ├── a.png
│ ├── b.png
│ └── c.png
└── train
└── metadata.jsonl在metadata.jsonl文件中:
{ "jpg" : " ./dataset/jpg/a.png " , "txt" : " a " , "hint" : " ./dataset/hint/a.png " }
{ "jpg" : " ./dataset/jpg/b.png " , "txt" : " b " , "hint" : " ./dataset/hint/b.png " }
{ "jpg" : " ./dataset/jpg/c.png " , "txt" : " c " , "hint" : " ./dataset/hint/c.png " }训练洛拉模型,运行:
./train_lora.sh您可以在run_lora.sh文件中更改一些超参数。例如,您可以更改--num_train_epochs更改训练时期的数量。
要训练控制网络模型,请运行:
./train_controlnet.sh您可以在run_controlnet.sh文件中更改一些超参数。例如,您可以更改--num_train_epochs更改训练时期的数量。
要同时训练ControlNet和Lora,请运行:
./train_controlnet_and_lora.sh请注意,您应该更改ControlNet和Lora模型的输出目录,以开始自己的培训。
只是运行:
./train_lora.sh && ./train_controlnet.sh && ./train_controlnet_and_lora.sh您将在controlnet-lora-output目录中获取所有模型。
您可以更改inference.py文件中模型的路径和条件图像。然后运行:
python inference.py您将在根目录中获得output.png 。
该项目基于扩散器及其示例。