基于LoRA微调部署Stable Diffusion【免费试用阿里云】

这篇具有很好参考价值的文章主要介绍了基于LoRA微调部署Stable Diffusion【免费试用阿里云】。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Stable Diffusion介绍

Stable Diffusion 是一种文本到图像的潜在扩散模型,由 Runway 和慕尼黑大学合作构建,第一个版本于 2021 年发布。目前主流版本包含 v1.5、v2和v2.1。它主要用于生成以文本描述为条件的详细图像,也应用于其他任务,如修复图像、生成受文本提示引导的图像到图像的转换等。
体验地址:https://stablediffusionweb.com/#demo
Stable Diffusion原理解读:
https://blog.csdn.net/v_JULY_v/article/details/131205615?spm=1001.2014.3001.5501
本文主要介绍如何免费在 阿里云交互式建模(PAI-DSW) 中对Stable Diffusion的Web-UI部署,以及基于LoRA对Stable Diffusion进行微调。

部署效果如下图所示:

基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
图中的VAE叫做变分自编码器,VAE模型有2种功能,一种是滤镜(就像是PS、抖音、美图秀秀等)用到的滤镜一样,让出图的画面看上去不会灰蒙蒙的,让整体的色彩饱和度更高。另一种是微调,部分VAE会对出图的细节进行细微的调整。

环境及资源准备过程

交互式建模(PAI-DSW)的试用

在使用前,需要先在阿里云上注册并登录自己的账号:https://free.aliyun.com/,
登录完成后,左侧产品类别中选中机器学习平台API
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
或者可以直接点击 链接,可以看到阿里云在机器学习平台PAI中提供的 3 款试用产品。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
选择交互式建模-PAI-DSW,点击立即试用。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
选择同意服务协议,并点击立即试用。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
点击开通PAI并创建默认工作空间。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
点击完成授权(我这里已授权),并点击确认开同并创建默认工作空间。注意:这里没有选择开通OSS,如果需要长期储存,那你需要开通OSS(可能会有费用)。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
会显示开通完成,点击进入PAI控制台。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
在PAI控制台中点击创建的工作空间。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
进入后点击左侧的交互式建模,点击创建实例开始进行实例的创建。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算

在创建的工作空间中创建实例

填写实例名称,并选择GPU规格,规格名称为ecs.gn7i-c8g1.2xlarge。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
选择官方镜像中的stable-diffusion-webui-env:pytorch1.13-gpu-py310-cu117-ubuntu22.04,点击下一步后,再点击创建实例即可。
注意:创建DSW实例需要一定时间,通常大约需要2到15分钟。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
创建成功后点击打开,进入PAI-DSW开发环境。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算

Stable Diffusion的Web-UI部署

在AI-DSW开发环境的launcher中,选择Tool下的DSW Gallery
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
查找或搜索Stable Diffusion WebUI使用示例 ,并点击在DSW中打开
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算

下载stable-diffusion-webui开源库及其它依赖

运行Notebook中的对应命令,下载stable-diffusion-webui开源库及其他依赖。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
运行命令后,如下图所示,表示已下载完成。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算

安装常用插件

运行Notebook中的对应命令,安装tagcomplete和汉化插件。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
运行命令后,如下图所示,表示已安装完成。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算

下载模型

使用开源SD模型 Counterfeit-v2.5作为 base 模型。
运行Notebook中的对应命令,下载模型。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算

在DSW中启动WebUI

运行Notebook中的对应命令,启动WebUI服务。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
在返回结果中,单击URL链接(http://127.0.0.1:7860),进入WebUI页面,可以在WebUI页面进行推理。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
在文生图页配置以下参数:

  • 正向prompt:
    ((masterpiece,best quality)),1girl, solo, animal ears, rabbit, barefoot, knees up, dress, sitting, rabbit ears, short sleeves, looking at viewer, grass, short hair, smile, white hair, puffy sleeves, outdoors, puffy short sleeves, bangs, on ground, full body, animal, white dress, sunlight, brown eyes, dappled sunlight, day, depth of field
  • 负向prompt:
    EasyNegative, extra fingers,fewer fingers
  • 采样方法 : DPM++2M Karras
  • 高清修复: 勾选
  • 重绘幅度: 0.6
  • 放大倍率: 1.8
  • 高度: 832
  • 提示词相关性(CFG Scale): 10
    也可以根据需要设置其他相关参数。

单击生成,输出如图推理结果,且支持图片的保存。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算

Stable Diffusion的微调及Web-UI部署

安装 Diffusers

在Notebook中新建单元格,下载所要使用的 Diffuers 仓库

! git clone https://github.com/huggingface/diffusers
! cd diffusers && git checkout e126a82cc5d9afbeb9b476455de24dd3e7dd358a
! cd diffusers && pip install .

基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
验证是否安装成功

import diffusers

基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算

配置accelerate。

! mkdir -p /root/.cache/huggingface/accelerate/
! wget -c http://pai-vision-data-sh.oss-cn-shanghai.aliyuncs.com/aigc-data/accelerate/default_config.yaml -O /root/.cache/huggingface/accelerate/default_config.yaml

accelerate配置成功。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
安装相关依赖库

! cd diffusers/examples/text_to_image && pip install -r requirements.txt 

微调Stable Diffusion模型

在Notebook中执行下面代码,下载服装数据集和基于LoRA微调的代码。

! wget http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/EasyCV/datasets/try_on/cloth_train_example.tar.gz && tar -xvf cloth_train_example.tar.gz
! wget http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/EasyCV/datasets/try_on/train_text_to_image_lora.py

运行命令后,如下图所示,表示已下载完成。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
在Notebook中执行下面代码,查看示例图片

from PIL import Image
display(Image.open("cloth_train_example/train/20230407174421.jpg"))

基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
在Notebook中执行下面代码,下载预训练模型并转换成diffusers格式。

safety_checker_url = f"{prefix}/aigc-data/hug_model/models--CompVis--stable-diffusion-safety-checker.tar.gz"
aria2(safety_checker_url, safety_checker_url.split("/")[-1], "./")
! tar -xf models--CompVis--stable-diffusion-safety-checker.tar.gz -C /root/.cache/huggingface/hub/

clip_url = f"{prefix}/aigc-data/hug_model/models--openai--clip-vit-large-patch14.tar.gz"
aria2(clip_url, clip_url.split("/")[-1], "./")
! tar -xf models--openai--clip-vit-large-patch14.tar.gz -C /root/.cache/huggingface/hub/

model_url = f"{prefix}/aigc-data/sd_models/chilloutmix_NiPrunedFp32Fix.safetensors"
aria2(model_url, model_url.split("/")[-1], "stable-diffusion-webui/models/Stable-diffusion/")

! python diffusers/scripts/convert_original_stable_diffusion_to_diffusers.py \
--checkpoint_path=stable-diffusion-webui/models/Stable-diffusion/chilloutmix_NiPrunedFp32Fix.safetensors \
--dump_path=chilloutmix-ni --from_safetensors

运行命令后,如下图所示,表示已下载并转换完成。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算

模型微调。设置num_train_epochs为200,运行下面代码进行基于LoRA的微调训练。微调完成后会在cloth-model-lora目录中生成模型文件

! export MODEL_NAME="chilloutmix-ni" && \
export DATASET_NAME="cloth_train_example" && \
accelerate launch --mixed_precision="fp16" train_text_to_image_lora.py \
  --pretrained_model_name_or_path=$MODEL_NAME \
  --dataset_name=$DATASET_NAME --caption_column="text" \
  --width=640 --height=768 --random_flip \
  --train_batch_size=1 \
  --num_train_epochs=200 --checkpointing_steps=5000 \
  --learning_rate=1e-04 --lr_scheduler="constant" --lr_warmup_steps=0 \
  --seed=42 \
  --output_dir="cloth-model-lora" \
  --validation_prompt="cloth1" --validation_epochs=100

可以直接看到微调相关信息,如epochs,batch_size等,由于样本量较少,只有11个,微调过程大约16分钟。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
训练过程中可以打开Terminal,并输入 nvidia-smi命令查看显存占用情况。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
可以看到显存占用约14G。

准备WebUI所需模型文件

将lora模型文件转化成WebUI支持格式并拷贝到WebUI所在目录。

! wget -c http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/EasyCV/datasets/convert-to-safetensors.py
! python convert-to-safetensors.py --file='cloth-model-lora/pytorch_lora_weights.bin'
! mkdir stable-diffusion-webui/models/Lora
! cp cloth-model-lora/pytorch_lora_weights_converted.safetensors stable-diffusion-webui/models/Lora/cloth_lora_weights.safetensors

基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算

准备额外模型文件,为了加速下载,可以运行如下命令直接下载额外模型文件。

detection_url = f"{prefix}/aigc-data/codeformer/detection_Resnet50_Final.pth"
aria2(detection_url, detection_url.split("/")[-1], "stable-diffusion-webui/repositories/CodeFormer/weights/facelib/")
parse_url = f"{prefix}/aigc-data/codeformer/parsing_parsenet.pth"
aria2(parse_url, parse_url.split("/")[-1], "stable-diffusion-webui/repositories/CodeFormer/weights/facelib/")
codeformer_url = f"{prefix}/aigc-data/codeformer/codeformer-v0.1.0.pth"
aria2(codeformer_url, codeformer_url.split("/")[-1], "stable-diffusion-webui/models/Codeformer/")

embedding_url = f"{prefix}/aigc-data/embedding/ng_deepnegative_v1_75t.pt"
aria2(embedding_url, embedding_url.split("/")[-1], "stable-diffusion-webui/embeddings/")

model_lora_url = f"{prefix}/aigc-data/lora/koreanDollLikeness_v10.safetensors"
aria2(model_lora_url, model_lora_url.split("/")[-1], "stable-diffusion-webui/models/Lora/")

基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算

在DSW中启动WebUI

在Notebook中执行下面命令,启动WebUI。

! cd stable-diffusion-webui && python -m venv --system-site-packages --symlinks venv
! cd stable-diffusion-webui && \
  sed -i 's/can_run_as_root=0/can_run_as_root=1/g' webui.sh && \
  ./webui.sh --no-download-sd-model --xformers

在返回结果中,单击URL链接(http://127.0.0.1:7860),进入WebUI页面。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
点击链接后,可以在WebUI页面进行推理。
基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
在文生图页配置以下参数:

  • Prompt:cloth1,lora:koreanDollLikeness_v10:0.4, (extremely detailed CG unity 8k wallpaper),(RAW photo, best quality), (realistic, photo-realistic:1.2), a close up portrait photo, 1girl, shopping mall rooftop cafe, outdoor, smile, (high detailed skin:1.4), puffy eyes, gorgeous hair, air bangs, brown black hair, soft lighting, high quality,lora:cloth_lora_weights:1

  • Negative prompt:ng_deepnegative_v1_75t,paintings, sketches, (worst quality:2), (low quality:2), (normal quality:2), lowres, ((monochrome)), (grayscale:1.2), skin spots, acnes, skin blemishes, age spot, glans,extra fingers,fewer fingers,(watermark:1.2),(letters:1.2),(nsfw:1.2),teeth

采样方法(Sampler): Euler a
采样迭代步数(Steps): 50
宽度和高度: 640,768
也可以根据需要设置其他相关参数。

单击生成,输出如图推理结果,且支持图片的保存。

基于LoRA微调部署Stable Diffusion【免费试用阿里云】,stable diffusion,阿里云,云计算
注意
领取免费资源包后,请在免费额度和有效试用期内使用。如果免费额度用尽或试用期结束后,继续使用计算资源,会产生后付费账单。
资源包使用情况可以前往资源实例管理页面查看。

如果无需继续使用DSW实例,可以按照以下操作步骤停止DSW实例。

  • 登录PAI控制台。

  • 在左侧导航栏单击工作空间列表,在工作空间列表页面中单击默认工作空间名称,进入对应工作空间内。

  • 在工作空间页面的左侧导航栏选择模型开发与训练>交互式建模(DSW),进入交互式建模(DSW)页面。

  • 单击目标实例操作列下的停止,成功停止后即停止资源消耗。文章来源地址https://www.toymoban.com/news/detail-647725.html

到了这里,关于基于LoRA微调部署Stable Diffusion【免费试用阿里云】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LoRA微调stable diffusion models:原理和实战

    Diffusion Models专栏文章汇总:入门与实战 前言: AIGC大模型(如stable diffusion models)的训练成本已经超过绝大多数人的承受范围,如何利用已有的开源大模型,花费较小的成本微调出属于自己的专有模型?现在微调方法diffusion大模型基本只剩下Dreambooth、textual inversion、LoRA和Hypern

    2024年02月09日
    浏览(29)
  • Stable Diffusion:使用自己的数据集微调训练LoRA模型

    由于本人水平有限,难免出现错漏,敬请批评改正。 更多精彩内容,可点击进入YOLO系列专栏、自然语言处理 专栏或我的个人主页查看 基于DETR的人脸伪装检测 YOLOv7训练自己的数据集(口罩检测) YOLOv8训练自己的数据集(足球检测) YOLOv5:TensorRT加速YOLOv5模型推理 YOLOv5:I

    2024年02月12日
    浏览(26)
  • 【stable-diffusion】4090显卡下dreambooth、lora、sd模型微调的GUI环境安装(cuda驱动、pytorch、xformer)

    相关博文 stable-diffusion-webui的基础功能手动安装,了解代码结构、依赖、模型出处 cuda11.7下载-https://developer.nvidia.com/cuda-toolkit-archive cudnn8.6 for 11.x : https://developer.nvidia.com/rdp/cudnn-archive dreamboothlora 训练环境: 最原始的命令端 https://github.com/kohya-ss/sd-scripts GUI端口 https://github.co

    2024年02月06日
    浏览(68)
  • 关于stable diffusion的lora训练在linux远程工作站的部署

    在学校Arc中部署lora training,一大问题就是依赖缺失和冲突。可以利用miniconda或者anaconda建立虚拟环境来解决。 安装anaconda 或者 miniconda(官网上也有教程): 现在进入到虚拟环境中 首先解决cuda的配置,其中要配置合适的cuda版本以及对应版本的cuDNN conda官方库中的cuda包都不完

    2024年02月09日
    浏览(33)
  • 视频生成: 基于Stable Diffusion的微调方法

        chatGPT带来了几个月的AIGC热度,文本图像生成模型大行其道,但AI在视频生成任务上尚没有较好的开源仓库,并受限于“缺那么几百块A100\\\"的资源问题,大多数人无法展开视频生成的研究。好在目前有不少针对视频生成的相关paper,也有不少开源实现,事实上缺的是一个完

    2024年02月10日
    浏览(23)
  • 在Linux云服务器上部署stable diffusion的lora训练的另一种方法

    这里参考了https://www.bilibili.com/video/BV1fs4y1x7p2/?vd_source=419c9ebc4060e9bec24d95773bba5275 LoRA 模型训练教程 一键包 以及: https://colab.research.google.com/drive/1_f0qJdM43BSssNJWtgjIlk9DkIzLPadx#scrollTo=8Qp6STJk2Wjh 首先是安装miniconda 或者 anaconda 来获得虚拟环境: 建立虚拟环境: 这里我们只需要有pytho

    2024年02月16日
    浏览(21)
  • Stable Diffusion免费(三个月)通过阿里云轻松部署服务

    今天偶然间看到了阿里云官网的这个,就来尝试了一下,果然香碰碰,接下来三个月可以好好愉快的来玩耍了。 1、申请阿里云免费试用三个月,入口如下: 函数计算FC_无服务器计算_Serverless_容器与中间件-阿里云 阿里云函数计算(Function Compute)是一个事件驱动的全托管计算服

    2023年04月25日
    浏览(23)
  • 用Colab免费部署AI绘画云平台Stable Diffusion webUI

    Google Colab 版的 Stable Diffusion WebUI 1.4 webui github 地址:https://github.com/sd-webui/stable-diffusion-webui 今天就来交大家如果来搭建和使用这个云平台。 第一步:  打开链接 https://colab.research.google.com/github/altryne/sd-webui-colab/blob/main/Stable_Diffusion_WebUi_Altryne.ipynb 第二步: 连接运行时 打开后,点

    2024年02月09日
    浏览(29)
  • 使用Google Colab免费部署属于自己的Stable Diffusion模型在线服务

    Stable Diffusion模型(稳定扩散模型) 是一种用于生成式建模的深度学习模型,它使用随机微分方程(SDE)来建模连续时间的动态过程。在图像、声音、文本等各种领域都有广泛的应用。与传统的生成式模型相比,Stable Diffusion模型能够生成更加高质量的样本。其原理是通过对随

    2023年04月24日
    浏览(30)
  • 免费在Kaggle上部署stable diffusion,白嫖万元 32G 双GPU

    最近爆火的AI绘画项目stable diffusion,免费开源,受到广大用户的喜爱,但是它对电脑显卡有很高的要求,自己想玩AI但是购买服务器又太贵,本文将告诉你一个免费部署的方法。nbsp; nbsp; Kaggle注册 1、首先打开Kaggle官网: www.kaggle.com 可以使用国内邮箱账号注册 发送邮箱验证,

    2024年04月15日
    浏览(25)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包