湖南火电建设有限公司招标网站,wordpress 怎么汉化主题,珠海模板开发建站,中国十大流量网站文章目录 基于LoRA进行Stable Diffusion的微调数据集模型下载环境配置微调过程 推理WebUI部署 基于LoRA进行Stable Diffusion的微调
数据集
本次微调使用的数据集为#xff1a; LambdaLabs的Pokemon数据集
使用git clone命令下载数据集
git clone https://huggingface.co/… 文章目录 基于LoRA进行Stable Diffusion的微调数据集模型下载环境配置微调过程 推理WebUI部署 基于LoRA进行Stable Diffusion的微调
数据集
本次微调使用的数据集为 LambdaLabs的Pokemon数据集
使用git clone命令下载数据集
git clone https://huggingface.co/datasets/lambdalabs/pokemon-blip-captions数据集一共883条样本包含两个部分image图和 text文如下图所示。
模型下载
git clone https://huggingface.co/runwayml/stable-diffusion-v1-5环境配置
# 创建一个新的conda环境
conda create -n diffusers python3.10
# 激活conda环境
conda activate diffusers
# 下载模型仓库
git clone https://github.com/huggingface/diffusers
# 进入diffusers目录
cd diffusers
# 进行安装
pip install .
cd examples/text_to_image
# 安装环境所需的包
pip install -r requirements.txt微调过程
微调时只需要使用以下命令运行 train_text_to_image_lora.py 文件即可。需要根据下载的路径文件地址对相应的参数进行修改如 MODEL_NAME、DATASET_NAME 等也可以根据GPU资源调整相应的参数如 train_batch_size、gradient_accumulation_steps 等。
export MODEL_NAME/data/sim_chatgpt/stable-diffusion-v1-5
export OUTPUT_DIR./finetune/lora/pokemon
export DATASET_NAME./pokemon-blip-captionsnohup accelerate launch --mixed_precisionfp16 train_text_to_image_lora.py \--pretrained_model_name_or_path$MODEL_NAME \--dataset_name$DATASET_NAME \--dataloader_num_workers8 \--resolution512 --center_crop --random_flip \--train_batch_size2 \--gradient_accumulation_steps4 \--max_train_steps7500 \--learning_rate1e-04 \--max_grad_norm1 \--lr_schedulercosine --lr_warmup_steps0 \--output_dir${OUTPUT_DIR} \--checkpointing_steps500 \--validation_promptTotoro \--seed1337 \ finetune_log0725.out 21 备注参数设置参考这里去掉了 export HUB_MODEL_ID“pokemon-lora” –push_to_hub –hub_model_id${HUB_MODEL_ID} –report_towandb 样本数据量为883这里设置了train_batch_size为2max_train_steps为7500 显存占用约11个G训练时长约8个小时左右。 显存占用情况如下
推理
微调完成后可以使用下面代码进行推理。
from diffusers import StableDiffusionPipeline
import torch
model_path ./finetune/lora/pokemon
pipe StableDiffusionPipeline.from_pretrained(/data/sim_chatgpt/stable-diffusion-v1-5, torch_dtypetorch.float16)
pipe.unet.load_attn_procs(model_path)
pipe.to(cuda)prompt A pokemon with green eyes and red legs.
image pipe(prompt, num_inference_steps30, guidance_scale7.5).images[0]
image.save(pokemon.png) 代码运行后会生成一个 pokemon.png 的图片如下图所示。
WebUI部署
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui需要将原模型文件以及微调后的lora模型文件放到 ~/stable-diffusion-webui//models/Stable-diffusion 下
cp -r /data/sim_chatgpt/stable-diffusion-v1-5/* ~/stable-diffusion-webui//models/Stable-diffusion/
mkdir ~/stable-diffusion-webui//models/Lora
cp -r ~/diffusers/examples/text_to_image/finetune/lora/pokemon/* ~/stable-diffusion-webui//models/Lora/./webui.sh --no-download-sd-model --xformers --no-gradio-queue
报错 RuntimeError: Couldn’t install gfpgan. 解决办法 安装 https://github.com/TencentARC/GFPGAN
git clone https://github.com/TencentARC/GFPGAN
pip install basicsr -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com# Install facexlib - https://github.com/xinntao/facexlib
# We use face detection and face restoration helper in the facexlib package
pip install facexlib pip install -r requirements.txt
# 报错无法安装待解决
python setup.py develop# If you want to enhance the background (non-face) regions with Real-ESRGAN,
# you also need to install the realesrgan package
pip install realesrgan参考 https://huggingface.co/blog/lora https://huggingface.co/blog/zh/lora https://github.com/AUTOMATIC1111/stable-diffusion-webui