Stable Diffusion模型魔搭最佳实践:训一只你的萌柯基

本文涉及的产品
模型在线服务 PAI-EAS,A10/V100等 500元 1个月
模型训练 PAI-DLC,5000CU*H 3个月
交互式建模 PAI-DSW,5000CU*H 3个月
简介: Stable Diffusion模型魔搭最佳实践:训一只你的萌柯基

环境配置和安装

本文在ModelScope的Notebook的环境(PAI-DSW)配置下运行 (可以单卡运行, 显存要求12G)

服务器连接与环境准备

1、进入ModelScope首页:modelscope.cn,进入我的Notebook

image.png

2、选择GPU环境

image.png

3、进入Terminal,先查看一下GPU的使用情况

image.png

git clone ModelScope,运行示例代码

#获取示例代码
git clone https://github.com/modelscope/modelscope.git
cd modelscope/
sh examples/pytorch/stable_diffusion/lora/run_train_lora.sh

以下介绍代码中的具体细节

模型链接和下载

使用社区开发者分享的stable diffusion系列模型,本文推荐的是stable-diffusion-v1.5:

模型链接:https://www.modelscope.cn/models/AI-ModelScope/stable-diffusion-v1-5/summary

社区支持直接下载模型的repo

# ### Loading Model and Tokenizer
WORK_DIR = 'runs/stable-diffusion-v1.5'
#使用社区lib下载模型
from modelscope.hub.snapshot_download import snapshot_download
model_dir = snapshot_download('AI-ModelScope/stable-diffusion-v1-5', 'v1.0.9')

模型推理

stable-diffusion-v1.5推理代码

from modelscope.utils.constant import Tasks
from modelscope.pipelines import pipeline
import cv2
pipe = pipeline(task=Tasks.text_to_image_synthesis, 
                model='AI-ModelScope/stable-diffusion-v1-5',
                model_revision='v1.0.9')
prompt = '飞流直下三千尺,油画'
output = pipe({'text': prompt})
cv2.imwrite('result.png', output['output_imgs'][0])

数据集链接和下载

本文使用小柯基的数据集作为微调数据集:https://modelscope.cn/datasets/buptwq/lora-stable-diffusion-finetune/summary. 

from modelscope.msdatasets import MsDataset
data = MsDataset.load(
    'buptwq/lora-stable-diffusion-finetune',
    split='train',     # Options: train, test, validation
    use_streaming=True
)
print(next(iter(data)))

模型训练最佳实践

微调过程分为如下几步:

  1. 使用ModelScope提供的微调方法构建最终模型
  2. 使用ModelScope提供的Trainer对模型进行微调

准备配置文件和数据集:

@dataclass(init=False)
class StableDiffusionLoraArguments(TrainingArgs):
    prompt: str = field(
        default='dog', metadata={
            'help': 'The pipeline prompt.',
        })
training_args = StableDiffusionLoraArguments(
    task='text-to-image-synthesis').parse_cli()
config, args = training_args.to_config()
if os.path.exists(args.train_dataset_name):
    # Load local dataset
    train_dataset = MsDataset.load(args.train_dataset_name)
    validation_dataset = MsDataset.load(args.train_dataset_name)
else:
    # Load online dataset
    train_dataset = MsDataset.load(
        args.train_dataset_name,
        split='train',
        download_mode=DownloadMode.FORCE_REDOWNLOAD)
    validation_dataset = MsDataset.load(
        args.train_dataset_name,
        split='validation',
        download_mode=DownloadMode.FORCE_REDOWNLOAD)
def cfg_modify_fn(cfg):
    if args.use_model_config:
        cfg.merge_from_dict(config)
    else:
        cfg = config
    cfg.train.lr_scheduler = {
        'type': 'LambdaLR',
        'lr_lambda': lambda _: 1,
        'last_epoch': -1
    }
    return cfg

开启微调:

kwargs = dict(
    model=training_args.model,
    model_revision=args.model_revision,
    work_dir=training_args.work_dir,
    train_dataset=train_dataset,
    eval_dataset=validation_dataset,
    cfg_modify_fn=cfg_modify_fn)
# build trainer and training
trainer = build_trainer(name=Trainers.lora_diffusion, default_args=kwargs)
trainer.train()

可视化:

Tensorboard 命令: (e.g.)

tensorboard --logdir /home/lora_diffusion/runs/events.out.tfevents.1689651932.dsw-4419-56cf86fcf8-ctp6l.236607.0 --port 6006

image.png

资源消耗

stable-diffusion-v1.5用lora的方式训练的显存占用如下,大约在12G.

image.png

推理训练后的模型,并验证结果

# pipeline after training and save result
pipe = pipeline(
    task=Tasks.text_to_image_synthesis,
    model=training_args.model,
    lora_dir=training_args.work_dir + '/output',
    model_revision=args.model_revision)
output = pipe({'text': args.prompt})
# visualize the result on ipynb and save it
output
cv2.imwrite('./lora_result.png', output['output_imgs'][0])

训练集:

image.png

生成结果:

开源代码链接:

https://github.com/modelscope/modelscope/tree/master/examples/pytorch/stable_diffusion/lora

相关文章
|
4月前
|
自然语言处理 数据可视化 物联网
Qwen1.5-MoE开源,魔搭社区推理训练最佳实践教程来啦
通义千问团队推出Qwen系列的首个MoE模型,Qwen1.5-MoE-A2.7B。
|
4月前
|
人工智能 搜索推荐
AIGC工具——Stable Diffusion
【1月更文挑战第11天】AIGC工具——Stable Diffusion
371 2
AIGC工具——Stable Diffusion
|
4月前
|
前端开发 Linux PyTorch
Stable Diffusion 本地安装 | AIGC
今天要介绍Stable Diffusion webUI则第三方通过Gradio搭建的Stable Diffusion的web前端,功能丰富,而且所有功能都是开源的。 【1月更文挑战第7天】
313 0
|
11月前
|
存储 物联网 Serverless
玩转AIGC,基于函数计算一键部署 Stable Diffusion
玩转AIGC,基于函数计算一键部署 Stable Diffusion
851 0
|
11月前
|
机器学习/深度学习 人工智能 自然语言处理
AI 绘画Stable Diffusion 研究(七) 一文读懂 Stable Diffusion 工作原理(2)
AI 绘画Stable Diffusion 研究(七) 一文读懂 Stable Diffusion 工作原理
411 0
|
2月前
|
机器学习/深度学习 数据采集 人工智能
「AIGC」Stable Diffusion教程详解
**Stable Diffusion教程摘要:** Stable Diffusion是AI绘画工具,利用GAN学习艺术家风格。基础教程涵盖软件介绍、配置需求(NVIDIA GPU、Windows 10/11)、安装及基础操作,如模型切换、VAE使用、采样步数调整等。AI作画原理涉及U-net、Diffusion模型、文本映射(如CLIP)和条件生成。Stable Diffusion运用Latent Diffusion Model从潜在空间生成高清图像,开源且在艺术创作中广泛应用。
73 0
|
3月前
|
关系型数据库 数据库 开发工具
Stable Diffusion 本地部署教程
Stable Diffusion 本地部署教程
129 0
|
4月前
|
Python
stable diffusion本地部署教程
stable diffusion本地部署教程
67 1
|
4月前
|
机器学习/深度学习 算法 PyTorch
Stable Diffusion 介绍与入门
Stable Diffusion 介绍与入门,简单的介绍
1496 2
Stable Diffusion 介绍与入门
|
11月前
|
人工智能 小程序 前端开发
AI | Stable Diffusion系列教程(二)
上一期更新了如何安装以及简单使用,这一期简单的讲讲常用的几个功能,以及下载模型,通过模型来生图。
305 0
AI | Stable Diffusion系列教程(二)