Krea2のLoRAを作成する

ダモタム
質問: Krea2 のLoRA学習
最近Krea2の派生モデルも増えてきたので、Krea2用のLoRAを学習してみたいです。どのようにすればKrea2のLoRAを作成できますか?

Krea2のLoRA学習手順を紹介します。

事前準備

Krea2のLoRA学習をするためのプログラム、musubi-tunerをインストールします。 インストール手順はこちらの記事を参照してください。

メモ
SDXLやAnimaではSDスクリプトを利用しますが、Krea2ではmusubi-tunerを利用します。

学習データの準備

LoRAで学習させる画像データを準備します。今回は1枚だけの画像で学習してみます。

学習データの配置

学習させたい画像データと画像を説明するテキストファイルを配置します。今回は画像1枚を学習します。

img.png 今回学習する画像
img.png 今回学習する画像 - Krea2のLoRAを作成する:画像1

キャプションのテキストは以下です。トリガのワード"krea_small_eye" と画像を表現する自然言語のキャプションを記述しています。

img.txt
krea_small_eye, close-up portrait of a woman in three-quarter view, long hair.

dataset.tomlの準備

学習データを配置したディレクトリの一つ上のディレクトリに、dataset.tomlファイルを作成します。
ファイルの内容は以下となります。

dataset.toml (書式)
[general]
resolution = [(画像の解像度基準幅), (画像の解像度基準高さ)]
caption_extension = ".txt"
batch_size = (バッチサイズ数)
enable_bucket = (バケットの有効設定)
bucket_no_upscale = (バケットのアップスケール設定)

[[datasets]]
image_directory = '(学習画像のディレクトリ)'
cache_directory = '(キャッシュファイルの保存ディレクトリ)'
num_repeats = (リピート数)

dataset.toml (今回のデモの例)
[general]
resolution = [1024, 1024]
caption_extension = ".txt"
batch_size = 1
enable_bucket = true
bucket_no_upscale = false


[[datasets]]
image_directory = 'D:\data\lora-krea2-small-eye-single\image'
cache_directory = 'D:\data\lora-krea2-small-eye-single\cache_krea2'
num_repeats = 64

基準解像度のtips
全部を無理やり指定された解像度(1024×1024)にリサイズする動作ではありません。 enable_bucket = true の場合、アスペクト比を保ちながら近いbucket解像度へリサイズされます。
resolution = [1024, 1024]
        ↓
基準面積 ≒ 1,048,576 pixels
        ↓
正方形     1024×1024
縦長       768×1344 付近
横長       1344×768 付近

config.tomlの準備

学習データを配置したディレクトリの一つ上のディレクトリに、config.tomlファイルを作成します。
ファイルの内容は以下となります。

config.toml (書式)
dit = "(学習元モデルファイルのパス)"
vae = "(VAEのフルパス)"

dataset_config = "(dataset.tomlのフルパス)"

output_dir = "(出力ディレクトリのフルパス)"
output_name = "(LoRAの出力名)"

network_module = "networks.lora_krea2"
network_dim = (Dimの値)
network_alpha = (Alphaの値)

optimizer_type = "(オプティマイザの値)"
learning_rate = (学習率)

max_train_epochs = (学習エポック数)
save_every_n_epochs = (何エポックごとに保存するか)

mixed_precision = "(精度)"
gradient_checkpointing = (Gradient Checkpoint の有無)

convrot_int8 = (Convrotの指定)
convrot_int8_bwd = "(Convrotの精度)"

timestep_sampling = "shift"
weighting_scheme = "none"
discrete_flow_shift = 2.5

max_data_loader_n_workers = (ワーカー数)
persistent_data_loader_workers = (ワーカーを使いまわすかの設定)

cache_latents = (Latentのキャッシュをするかの設定)

sdpa = true

seed = (シード数)

[Sample]
sample_every_n_epochs = (何エポックごとにサンプル画像を生成するか)
sample_prompts = "(プロンプトのテキストファイル)"
text_encoder = "(テキストエンコーダーのフルパス)"

config.toml (今回のデモの例)
dit = "D:/data/model/krea2_raw_int8_convrot.safetensors"
vae = "D:/data/model/qwen_image_vae.safetensors"

dataset_config = "D:/data/lora-krea2-small-eye-single/dataset.toml"

output_dir = "D:/data/lora-krea2-small-eye-single/output"
output_name = "krea2-small-eye-single"

network_module = "networks.lora_krea2"
network_dim = 32
network_alpha = 16

optimizer_type = "adamw8bit"
learning_rate = 1e-4

max_train_epochs = 10
save_every_n_epochs = 1

mixed_precision = "bf16"
gradient_checkpointing = true

convrot_int8 = true
convrot_int8_bwd = "bf16"

timestep_sampling = "shift"
weighting_scheme = "none"
discrete_flow_shift = 2.5

max_data_loader_n_workers = 2
persistent_data_loader_workers = true

cache_latents = true

sdpa = true

seed = 42

[Sample]
sample_every_n_epochs = 1
sample_prompts = "D:/data/lora-krea2-small-eye-single/sample-prompts.txt"
text_encoder = "D:/data/model/qwen3vl_4b_bf16.safetensors"


補足
D:\data\model\に Krea2のRAWモデル(krea2_raw_int8_convrot.safetensors)、VAE(qwen_image_vae.safetensors) の各モデルを配置します。
テキストエンコーダー (qwen3vl_4b_bf16.safetensors)はサンプル画像を作成する場合のみに必要ですが、後述のキャッシュ作成でも必要となるため、 D:\data\model\に配置します。学習のため、qwen3vl_4b_fp8_scaled.safetensors より qwen3vl_4b_bf16.safetensorsを利用する方が推奨です。


今回spdaを利用していますが、ほかの選択肢として以下があります。通常はSPDAで問題ないです。
SPDA以外の場合はsplit_attn = trueも推奨されています。

設定 特徴 追加依存 Krea2での扱い
sdpa = true PyTorch標準、安定 なし
flash_attn = true 高速になりやすい FlashAttention必要 GQAネイティブ対応
flash3 = true FlashAttention 3系 対応環境必要 選択可能
sage_attn = true 高速・省VRAMを狙える SageAttention必要 GQAネイティブ対応
xformers = true 定番実装 xformers必要 KV headを内部展開 (split_attn = true が必要)


また、動作確認後、さらに高速化する方法として compile = true オプションもあります。
主要な28個の SingleStreamBlock を torch.compile でコンパイルする機能を有効にしますがWindowsでは不安定になるという情報もあります。

サンプル画像作成プロンプト

サンプル画像を作成する際に利用するプロンプトを記述したテキストファイルを配置します。

sample-prompts.txt
プロンプトのテキスト --w (サンプル生成画像の幅) --h (サンプル生成画像の高さ) --d (シード値)
...
...

sample-prompts.txt (今回のデモの例)
anime girl portrait, upper body, looking at viewer, long hair, small natural eyes, gentle smile, delicate face, refined painterly anime illustration, clean linework, well-defined cel-style shadows, subtle painterly shading, soft lighting, simple blurred background --w 1024 --h 1024 --d 10000
anime girl portrait, upper body, looking at viewer, long hair, small natural eyes, gentle smile, delicate face, refined painterly anime illustration, clean linework, well-defined cel-style shadows, subtle painterly shading, soft lighting, simple blurred background --w 1024 --h 1024 --d 20000

起動コマンドの準備

musubi-tunerを実行するバッチファイルを作成します。

exec.bat
python src/musubi_tuner/krea2_cache_latents.py ^
  --dataset_config (Dataset.toml ファイルのフルパス) ^
  --vae (VAEファイルのフルパス)

python src/musubi_tuner/krea2_cache_text_encoder_outputs.py ^
  --dataset_config (Dataset.toml ファイルのフルパス) ^
  --text_encoder (テキストエンコーダーファイルのフルパス) ^
  --batch_size 1

accelerate launch ^
  --num_cpu_threads_per_process 1 ^
  --mixed_precision bf16 ^
  src/musubi_tuner/krea2_train_network.py ^
  --config_file=(config.toml ファイルのフルパス)

exec.bat (今回のデモの例)
python src/musubi_tuner/krea2_cache_latents.py ^
  --dataset_config D:\data\lora-krea2-small-eye-single\dataset.toml ^
  --vae D:\data\model\qwen_image_vae.safetensors

python src/musubi_tuner/krea2_cache_text_encoder_outputs.py ^
  --dataset_config D:\data\lora-krea2-small-eye-single\dataset.toml ^
  --text_encoder D:\data\model\qwen3vl_4b_bf16.safetensors ^
  --batch_size 1

accelerate launch ^
  --num_cpu_threads_per_process 1 ^
  --mixed_precision bf16 ^
  src/musubi_tuner/krea2_train_network.py ^
  --config_file=D:\data\lora-krea2-small-eye-single\config.toml

学習の実行

コマンドプロンプトを表示して、または直接作成したexec.batを実行します。

キャッシュディレクトリにはlatentとtext encoderのキャッシュファイルの 2つのファイルが保存されます。
Krea2のLoRAを作成する:画像2

学習処理が始まり、完了しました。
Krea2のLoRAを作成する:画像3

出力ディレクトリのsampleディレクトリに出力された画像を確認します。
Krea2のLoRAを作成する:画像4

出力ディレクトリにLoRAのファイルが保存されています。
Krea2のLoRAを作成する:画像5

LoRAモデルの利用

サンプル出力の結果を確認したところ。Epoch 7あたりが良さそうなので、エポック7のLoRAを導入します。
Krea2のLoRAを作成する:画像6

今回はComfyUIで利用します。以下のディレクトリに作成したLoRAを配置します。

(CmofyUIの配置ディレクトリ)\ComfyUI\models\loras


ワークフローは下図です。
Krea2のLoRAを作成する:画像7

プロンプトは以下を利用しています。

Prompt
A beautiful young woman standing in a classroom, wearing a white short-sleeved Japanese sailor-style school uniform. She wears a white sailor blouse with a wide sailor collar and clean navy-blue stripe trim along the collar and short sleeves, a vivid red neckerchief tied at the chest, and a matching dark navy pleated mini skirt. Refined painterly anime illustration with soft controlled brush strokes, clean confident linework, well-defined cel-style shadow shapes, delicate translucent color layering, glossy detailed eyes, luminous rim light and highlights, smooth gradients, crisp facial details with carefully rendered clothing folds and a softly painted atmospheric classroom background with windows and desks. Fresh, airy, radiant, highly saturated yet harmonious colors, polished anime key visual, high detail, clean finish, landscape composition.

画像生成結果は下図です。

LoRAありでモデルの強度が1.0の場合の結果です。

Krea2のLoRAを作成する:画像8
Krea2のLoRAを作成する:画像9
Krea2のLoRAを作成する:画像10
Krea2のLoRAを作成する:画像11


LoRAありでモデルの強度が0.5の場合の結果です。

Krea2のLoRAを作成する:画像12
Krea2のLoRAを作成する:画像13
Krea2のLoRAを作成する:画像14
Krea2のLoRAを作成する:画像15

LoRAなしの場合の結果です。

Krea2のLoRAを作成する:画像16
Krea2のLoRAを作成する:画像17
Krea2のLoRAを作成する:画像18
Krea2のLoRAを作成する:画像19


学習したキャラクターの絵柄が反映されることが確認できました。

AuthorPortraitAlt
著者
iPentecのメインデザイナー
イタリア好き。Webページ、Webクリエイティブのデザインを担当。PhotoshopやIllustratorの作業もする。 最近は生成AIの画像生成の沼に沈んでいる。
作成日: 2026-07-27

関連するページ