🚀 simpletuner-lora
simpletuner-lora 是一个基于 stabilityai/stable-diffusion-3.5-large 的 LyCORIS 适配器,可用于文生图等图像生成任务。
🚀 快速开始
本项目是从 stabilityai/stable-diffusion-3.5-large 派生而来的 LyCORIS 适配器。
训练期间使用的主要验证提示为:
A photo-realistic image of a cat
✨ 主要特性
- 基于稳定扩散模型
stabilityai/stable-diffusion-3.5-large
派生,可用于文生图、图生图等任务。
- 提供了训练和验证的详细设置,方便复现和调整。
- 给出了推理代码示例,便于快速上手使用。
📚 详细文档
验证设置
- CFG:
3.0
- CFG Rescale:
0.0
- 步数:
20
- 采样器:
FlowMatchEulerDiscreteScheduler
- 种子:
42
- 分辨率:
1024x1024
- 跳过层引导:
注意:验证设置不一定与训练设置相同。
你可以在以下图库中找到一些示例图像:
文本编码器未进行训练,推理时可复用基础模型的文本编码器。
训练设置
- 训练轮数: 1
- 训练步数: 10000
- 学习率: 0.0001
- 最大梯度值: 2.0
- 有效批量大小: 2
- 微批量大小: 1
- 梯度累积步数: 1
- GPU 数量: 2
- 梯度检查点: 启用
- 预测类型: 流匹配 (额外参数=['shift=3'])
- 优化器: adamw_bf16
- 可训练参数精度: 纯 BF16
- 基础模型精度:
no_change
- 字幕丢弃概率: 10.0%
LyCORIS 配置
{
"algo": "lokr",
"multiplier": 1.0,
"full_matrix": true,
"linear_alpha": 1,
"factor": 16,
"apply_preset": {
"target_module": [
"Attention",
"FeedForward"
],
"module_algo_map": {
"Attention": {
"factor": 16
},
"FeedForward": {
"factor": 8
}
}
}
}
数据集
pseudo-camera-10k-sd3
- 重复次数: 0
- 图像总数: ~14102
- 纵横比桶总数: 1
- 分辨率: 1.048576 兆像素
- 裁剪: 是
- 裁剪样式: 居中
- 裁剪纵横比: 方形
- 用于正则化数据: 否
💻 使用示例
基础用法
import torch
from diffusers import DiffusionPipeline
from lycoris import create_lycoris_from_weights
def download_adapter(repo_id: str):
import os
from huggingface_hub import hf_hub_download
adapter_filename = "pytorch_lora_weights.safetensors"
cache_dir = os.environ.get('HF_PATH', os.path.expanduser('~/.cache/huggingface/hub/models'))
cleaned_adapter_path = repo_id.replace("/", "_").replace("\\", "_").replace(":", "_")
path_to_adapter = os.path.join(cache_dir, cleaned_adapter_path)
path_to_adapter_file = os.path.join(path_to_adapter, adapter_filename)
os.makedirs(path_to_adapter, exist_ok=True)
hf_hub_download(
repo_id=repo_id, filename=adapter_filename, local_dir=path_to_adapter
)
return path_to_adapter_file
model_id = 'stabilityai/stable-diffusion-3.5-large'
adapter_repo_id = 'ShanZard/simpletuner-lora'
adapter_filename = 'pytorch_lora_weights.safetensors'
adapter_file_path = download_adapter(repo_id=adapter_repo_id)
pipeline = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
lora_scale = 1.0
wrapper, _ = create_lycoris_from_weights(lora_scale, adapter_file_path, pipeline.transformer)
wrapper.merge_to()
prompt = "A photo-realistic image of a cat"
negative_prompt = 'blurry, cropped, ugly'
pipeline.to('cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu')
model_output = pipeline(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=20,
generator=torch.Generator(device='cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu').manual_seed(42),
width=1024,
height=1024,
guidance_scale=3.0,
).images[0]
model_output.save("output.png", format="PNG")
高级用法
在基础用法的基础上,你可以根据需要调整以下参数:
prompt
和 negative_prompt
:修改生成图像的提示词和负面提示词。
num_inference_steps
:调整推理步数,影响图像生成的质量和速度。
guidance_scale
:调整引导比例,控制生成图像与提示词的匹配程度。
prompt = "A beautiful landscape with a lake and mountains"
negative_prompt = 'low quality, blurry'
num_inference_steps = 30
model_output = pipeline(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=num_inference_steps,
generator=torch.Generator(device='cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu').manual_seed(42),
width=1024,
height=1024,
guidance_scale=3.0,
).images[0]
model_output.save("output_advanced.png", format="PNG")
📄 许可证
本项目使用其他许可证。具体请参考相关文件。