消费级显卡微调可图Kolors最佳实践!

简介: 近期,快手开源了一种名为Kolors(可图)的文本到图像生成模型,该模型具有对英语和汉语的深刻理解,并能够生成高质量、逼真的图像。

近期,快手开源了一种名为Kolors(可图)的文本到图像生成模型,该模型具有对英语和汉语的深刻理解,并能够生成高质量、逼真的图像。

魔搭社区在DiffSynth-Studio中提供了可图Kolors微调脚本。

代码开源链接:

https://github.com/Kwai-Kolors/Kolors

模型开源链接:

https://modelscope.cn/models/Kwai-Kolors/Kolors

技术报告链接:

https://github.com/Kwai-Kolors/Kolors/blob/master/imgs/Kolors_paper.pdf

微调脚本链接:

https://github.com/modelscope/DiffSynth-Studio/tree/main/examples/train/kolors

微调最佳实践

下载模型权重

下载可图Kolors模型

modelscope download --model=Kwai-Kolors/Kolors --local_dir models/kolors/Kolors

image.gif

下载额外的VAE模型(https://modelscope.cn/models/AI-ModelScope/sdxl-vae-fp16-fix

modelscope download --model=AI-ModelScope/sdxl-vae-fp16-fix --local_dir models/kolors/sdxl-vae-fp16-fix diffusion_pytorch_model.safetensors

image.gif

模型文件结构:

models
├── kolors
│   └── Kolors
│       ├── text_encoder
│       │   ├── config.json
│       │   ├── pytorch_model-00001-of-00007.bin
│       │   ├── pytorch_model-00002-of-00007.bin
│       │   ├── pytorch_model-00003-of-00007.bin
│       │   ├── pytorch_model-00004-of-00007.bin
│       │   ├── pytorch_model-00005-of-00007.bin
│       │   ├── pytorch_model-00006-of-00007.bin
│       │   ├── pytorch_model-00007-of-00007.bin
│       │   └── pytorch_model.bin.index.json
│       ├── unet
│       │   └── diffusion_pytorch_model.safetensors
│       └── vae
│           └── diffusion_pytorch_model.safetensors
└── sdxl-vae-fp16-fix
    └── diffusion_pytorch_model.safetensors

image.gif

微调:

安装依赖:

pip install peft lightning pandas torchvision

image.gif

数据准备:

我们准备了一些开源数据集:

柯基小狗数据集:

https://modelscope.cn/datasets/buptwq/lora-stable-diffusion-finetune

文生图风格定制数据集(metadata做了汉化):

https://modelscope.cn/datasets/iic/style_custom_dataset

数据集按照如下格式:

data/dog/
└── train
    ├── 00.jpg
    ├── 01.jpg
    ├── 02.jpg
    ├── 03.jpg
    ├── 04.jpg
    └── metadata.csv

image.gif

metadata.csv:

file_name,text
00.jpg,一只小狗
01.jpg,一只小狗
02.jpg,一只小狗
03.jpg,一只小狗
04.jpg,一只小狗

image.gif

训练lora模型:

我们提供了训练脚本 train_kolors_lora.py,在运行该训练脚本之前,需要先clone本项目

https://github.com/modelscope/DiffSynth-Studio.git
cd DiffSynth-Studio

image.gif

采用以下设置,需要22GB VRAM

CUDA_VISIBLE_DEVICES="0" python examples/train/kolors/train_kolors_lora.py \
  --pretrained_unet_path models/kolors/Kolors/unet/diffusion_pytorch_model.safetensors \
  --pretrained_text_encoder_path models/kolors/Kolors/text_encoder \
  --pretrained_fp16_vae_path models/sdxl-vae-fp16-fix/diffusion_pytorch_model.safetensors \
  --dataset_path data/dog \
  --output_path ./models \
  --max_epochs 10 \
  --center_crop \
  --use_gradient_checkpointing \
  --precision "16-mixed"

image.gif

可选参数:

-h, --help            show this help message and exit
  --pretrained_unet_path PRETRAINED_UNET_PATH
                        Path to pretrained model (UNet). For example, `models/kolors/Kolors/unet/diffusion_pytorch_model.safetensors`.
  --pretrained_text_encoder_path PRETRAINED_TEXT_ENCODER_PATH
                        Path to pretrained model (Text Encoder). For example, `models/kolors/Kolors/text_encoder`.
  --pretrained_fp16_vae_path PRETRAINED_FP16_VAE_PATH
                        Path to pretrained model (VAE). For example, `models/kolors/Kolors/sdxl-vae-fp16-fix/diffusion_pytorch_model.safetensors`.
  --dataset_path DATASET_PATH
                        The path of the Dataset.
  --output_path OUTPUT_PATH
                        Path to save the model.
  --steps_per_epoch STEPS_PER_EPOCH
                        Number of steps per epoch.
  --height HEIGHT       Image height.
  --width WIDTH         Image width.
  --center_crop         Whether to center crop the input images to the resolution. If not set, the images will be randomly cropped. The images will be resized to the resolution first before cropping.
  --random_flip         Whether to randomly flip images horizontally
  --batch_size BATCH_SIZE
                        Batch size (per device) for the training dataloader.
  --dataloader_num_workers DATALOADER_NUM_WORKERS
                        Number of subprocesses to use for data loading. 0 means that the data will be loaded in the main process.
  --precision {32,16,16-mixed}
                        Training precision
  --learning_rate LEARNING_RATE
                        Learning rate.
  --lora_rank LORA_RANK
                        The dimension of the LoRA update matrices.
  --lora_alpha LORA_ALPHA
                        The weight of the LoRA update matrices.
  --use_gradient_checkpointing
                        Whether to use gradient checkpointing.
  --accumulate_grad_batches ACCUMULATE_GRAD_BATCHES
                        The number of batches in gradient accumulation.
  --training_strategy {auto,deepspeed_stage_1,deepspeed_stage_2,deepspeed_stage_3}
                        Training strategy
  --max_epochs MAX_EPOCHS
                        Number of epochs.

image.gif

训练后推理

训练完成后,可以使用自己训练的LoRA来生成新图像。以下是一些示例:

from diffsynth import ModelManager, KolorsImagePipeline
from peft import LoraConfig, inject_adapter_in_model
import torch
def load_lora(model, lora_rank, lora_alpha, lora_path):
    lora_config = LoraConfig(
        r=lora_rank,
        lora_alpha=lora_alpha,
        init_lora_weights="gaussian",
        target_modules=["to_q", "to_k", "to_v", "to_out"],
    )
    model = inject_adapter_in_model(lora_config, model)
    state_dict = torch.load(lora_path, map_location="cpu")
    model.load_state_dict(state_dict, strict=False)
    return model
# Load models
model_manager = ModelManager(torch_dtype=torch.float16, device="cuda",
                             file_path_list=[
                                 "models/kolors/Kolors/text_encoder",
                                 "models/kolors/Kolors/unet/diffusion_pytorch_model.safetensors",
                                 "models/kolors/Kolors/vae/diffusion_pytorch_model.safetensors"
                             ])
pipe = KolorsImagePipeline.from_model_manager(model_manager)
# Generate an image with lora
pipe.unet = load_lora(
    pipe.unet,
    lora_rank=4, lora_alpha=4.0, # The two parameters should be consistent with those in your training script.
    lora_path="path/to/your/lora/model/lightning_logs/version_x/checkpoints/epoch=x-step=xxx.ckpt"
)
torch.manual_seed(0)
image = pipe(
    prompt="一只小狗蹦蹦跳跳,周围是姹紫嫣红的鲜花,远处是山脉",
    negative_prompt="",
    cfg_scale=4,
    num_inference_steps=50, height=1024, width=1024,
)
image.save("image_with_lora.jpg")

image.gif

柯基lora:

Prompt: 一只小狗蹦蹦跳跳,周围是姹紫嫣红的鲜花,远处是山脉

image.gif

3D风格lora:

Prompt:一只小狗和一只小猫3D

image.gif

点击链接👇直达链接

https://modelscope.cn/models/Kwai-Kolors/Kolors?from=alizishequ__text

相关文章
|
数据采集 并行计算 JavaScript
实战指南:在 Node.js 中利用多线程提升性能
在 Node.js 的世界中,多线程技术一直是一个受到广泛关注的领域。最初,Node.js 设计为单线程模式。随着技术发展,Node.js 引入了多线程支持,进而利用多核处理器的强大性能,提升了应用性能。接下来的内容将深入探讨 Node.js 如何实现多线程,以及在何种场合应该采用这种技术。
|
9月前
|
机器学习/深度学习 数据采集 人工智能
基于可图Kolors的皮影戏风格LoRA训练&创作
可图Kolors-LoRA风格故事挑战赛比赛过程心得分享
304 8
基于可图Kolors的皮影戏风格LoRA训练&创作
可图IP-adapter-plus开源,魔搭送你一本中文咒语书
快手可图团队基于Kolors-Basemodel 提供 IP-Adapter-Plus 权重和推理代码,魔搭社区新鲜出炉最佳实践,结合中文咒语书,给你喜欢的IP定制风格吧!
可图IP-adapter-plus开源,魔搭送你一本中文咒语书
|
Linux 数据安全/隐私保护 Windows
更换(Pypi)pip源到国内镜像
pip国内的一些镜像 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.
247088 2
|
10月前
|
JavaScript 计算机视觉
FLUX.1 Tools 全家桶开源!文末附一键ComfyUI启动链接
Black Forest Labs 发布了 FLUX.1 Tools,一套增强 FLUX.1 文本转图像模型的工具集,包括 FLUX.1 Fill、FLUX.1 Depth、FLUX.1 Canny 和 FLUX.1 Redux,分别用于图像修复、深度引导、边缘检测和图像重组。提供详细的安装指南和模型下载链接,支持用户快速上手并优化图像处理流程。
3761 4
FLUX.1 Tools 全家桶开源!文末附一键ComfyUI启动链接
|
6月前
|
数据采集 编解码 缓存
通义万相,开源!
通义万相,开源!
|
9月前
|
数据采集 人工智能 自然语言处理
Qwen模型角色扮演最佳实践
角色扮演大模型通过模拟特定角色的行为、语言风格和情感表达,实现高度拟人化和定制化的互动体验。与传统通用模型相比,角色扮演模型在语言风格、性格特征和情绪反应上更加细腻,提供更真实的交互体验。本文介绍了如何通过system prompt、few-shot学习和微调等技术实现大模型的拟人化,包括使用阿里云百炼平台进行角色扮演测试,以及如何通过合成数据和Lora微调提高模型的表演效果。最终,展示了如何通过优化数据质量和训练策略,显著提升角色扮演模型的表现。
|
10月前
GLM-4模型微调报内核版本不匹配的错误
GLM-4模型微调报内核版本不匹配的错误
|
10月前
|
人工智能 自然语言处理 PyTorch
基于openi平台免费华为昇腾910B芯片部署qwen2.5 Instruct 14B大模型
基于OpenI平台和华为昇腾910B芯片,本方案详细介绍了如何免费部署Qwen-2.5 Instruct 14B大模型。涵盖准备工作、模型适配、部署步骤及性能优化等内容,适用于NLP任务部署、本地化适配及实时服务化等多种应用场景。
2984 1
|
12月前
|
文字识别 自然语言处理 数据可视化
Qwen2.5 全链路模型体验、下载、推理、微调、部署实战!
在 Qwen2 发布后的过去三个月里,许多开发者基于 Qwen2 语言模型构建了新的模型,并提供了宝贵的反馈。在这段时间里,通义千问团队专注于创建更智能、更博学的语言模型。今天,Qwen 家族的最新成员:Qwen2.5系列正式开源
Qwen2.5 全链路模型体验、下载、推理、微调、部署实战!