From 1d46f17ea55195f6598c20f2a317c6421073e38a Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Thu, 12 Jun 2025 21:28:58 +0800 Subject: [PATCH 01/41] Update shared.ts --- mikazuki/schema/shared.ts | 72 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/mikazuki/schema/shared.ts b/mikazuki/schema/shared.ts index 1ab52a6d..d693c96f 100644 --- a/mikazuki/schema/shared.ts +++ b/mikazuki/schema/shared.ts @@ -196,6 +196,76 @@ Schema.object({}), ]), ]), + CONTRASTIVE: Schema.intersect([ + + // 对比学习开关 + Schema.object({ + enable_contrastive: Schema.boolean() + .default(false) + .description('启用对比学习模块'), + }).description('对比学习设置'), + + // 只有 enable_contrastive = true 时,下面的配置才生效 + Schema.union([ + Schema.object({ + enable_contrastive: Schema.const(true).required(), + + // 负样本策略 + negative_sampling_method: Schema.union([ + 'random_permutation', + 'random_sampling', + 'distance_based', + 'memory_queue', + 'momentum_encoder', + 'cluster', + 'adversarial', + ]) + .default('random_permutation') + .description('选择负样本生成策略'), + + // 距离/相似度筛选参数(method=distance_based) + distance_metric: Schema.union(['euclidean', 'cosine']) + .default('cosine') + .description('距离/相似度计算方式'), + + // 记忆银行参数(method=memory_queue) + queue_capacity: Schema.number() + .default(4096) + .description('负样本队列大小'), + + // 动量编码器参数(method=momentum_encoder) + momentum_coefficient: Schema.number() + .min(0) + .max(1) + .default(0.999) + .description('动量更新系数'), + + // 聚类负样本参数(method=cluster) + num_clusters: Schema.number() + .default(10) + .description('聚类簇数量'), + + // 对抗式负样本参数(method=adversarial) + adversarial_epsilon: Schema.number() + .default(0.01) + .description('对抗扰动幅度'), + + // 对比损失权重 λ + contrastive_weight: Schema.number() + .min(0) + .max(1) + .default(0.5) + .description('对比损失权重'), + + // 对比学习暖启动 + contrastive_warmup_steps: Schema.number() + .default(100) + .description('使用随机负样本的步数'), + }), + Schema.object({}), + ]), + +]).description('对比学习配置'); LOG_SETTINGS: Schema.intersect([ Schema.object({ @@ -240,4 +310,4 @@ } return data -})() \ No newline at end of file +})() From ddf149581ce9e63ee545dea8c9c3ef0deeabec00 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Thu, 12 Jun 2025 21:29:59 +0800 Subject: [PATCH 02/41] Update flux-lora.ts --- mikazuki/schema/flux-lora.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mikazuki/schema/flux-lora.ts b/mikazuki/schema/flux-lora.ts index 94277405..0d864bad 100644 --- a/mikazuki/schema/flux-lora.ts +++ b/mikazuki/schema/flux-lora.ts @@ -63,6 +63,8 @@ Schema.intersect([ SHARED_SCHEMAS.NETWORK_OPTION_BASEWEIGHT, ]), + //对比学习设置 + SHARED_SCHEMAS.CONTRASTIVE // 预览图设置 SHARED_SCHEMAS.PREVIEW_IMAGE, From 1d3106773023a1a50b677d57a3b55464917a83d0 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Thu, 12 Jun 2025 21:31:28 +0800 Subject: [PATCH 03/41] Update dreambooth.ts --- mikazuki/schema/dreambooth.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mikazuki/schema/dreambooth.ts b/mikazuki/schema/dreambooth.ts index c6a71a31..243987c1 100644 --- a/mikazuki/schema/dreambooth.ts +++ b/mikazuki/schema/dreambooth.ts @@ -132,7 +132,8 @@ Schema.intersect([ optimizer_args_custom: Schema.array(String).role("table").description("自定义 optimizer_args,一行一个"), }) ]), - + //对比学习设置 + SHARED_SCHEMAS.CONTRASTIVE Schema.intersect([ Schema.object({ enable_preview: Schema.boolean().default(false).description("启用训练预览图"), From b77ccfb297ac2afd4de3e5a94c4eb262fb70c72d Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Thu, 12 Jun 2025 21:31:59 +0800 Subject: [PATCH 04/41] Update lora-master.ts --- mikazuki/schema/lora-master.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mikazuki/schema/lora-master.ts b/mikazuki/schema/lora-master.ts index 50e731a8..295b3e90 100644 --- a/mikazuki/schema/lora-master.ts +++ b/mikazuki/schema/lora-master.ts @@ -69,7 +69,8 @@ Schema.intersect([ SHARED_SCHEMAS.NETWORK_OPTION_BASEWEIGHT, ]), - + //对比学习设置 + SHARED_SCHEMAS.CONTRASTIVE // 预览图设置 SHARED_SCHEMAS.PREVIEW_IMAGE, From 6ea70dc762bc8483ed758649a1896b9c4d691028 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Thu, 12 Jun 2025 21:32:28 +0800 Subject: [PATCH 05/41] Update sd3-lora.ts --- mikazuki/schema/sd3-lora.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mikazuki/schema/sd3-lora.ts b/mikazuki/schema/sd3-lora.ts index aa033b9c..7a540825 100644 --- a/mikazuki/schema/sd3-lora.ts +++ b/mikazuki/schema/sd3-lora.ts @@ -25,7 +25,7 @@ Schema.intersect([ // 保存设置 SHARED_SCHEMAS.SAVE_SETTINGS, - + Schema.object({ max_train_epochs: Schema.number().min(1).default(20).description("最大训练 epoch(轮数)"), train_batch_size: Schema.number().min(1).default(1).description("批量大小, 越高显存占用越高"), @@ -54,7 +54,8 @@ Schema.intersect([ SHARED_SCHEMAS.NETWORK_OPTION_BASEWEIGHT, ]), - + //对比学习设置 + SHARED_SCHEMAS.CONTRASTIVE // 预览图设置 SHARED_SCHEMAS.PREVIEW_IMAGE, From 5daa822953508065fd40caa90230670d0ab2f581 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 00:31:08 +0800 Subject: [PATCH 06/41] Update shared.ts --- mikazuki/schema/shared.ts | 38 ++++++++------------------------------ 1 file changed, 8 insertions(+), 30 deletions(-) diff --git a/mikazuki/schema/shared.ts b/mikazuki/schema/shared.ts index d693c96f..a6b44b8c 100644 --- a/mikazuki/schema/shared.ts +++ b/mikazuki/schema/shared.ts @@ -212,44 +212,22 @@ // 负样本策略 negative_sampling_method: Schema.union([ - 'random_permutation', - 'random_sampling', - 'distance_based', - 'memory_queue', - 'momentum_encoder', - 'cluster', - 'adversarial', + 'Random-Noise', + 'Permutation', + 'Random-index', + 'Circular', + 'Hard-Negative', ]) - .default('random_permutation') + .default('Random-Noise') .description('选择负样本生成策略'), - // 距离/相似度筛选参数(method=distance_based) - distance_metric: Schema.union(['euclidean', 'cosine']) - .default('cosine') - .description('距离/相似度计算方式'), - - // 记忆银行参数(method=memory_queue) - queue_capacity: Schema.number() - .default(4096) - .description('负样本队列大小'), - - // 动量编码器参数(method=momentum_encoder) - momentum_coefficient: Schema.number() + // 噪音强度参数 + noise_strength: Schema.number() .min(0) .max(1) .default(0.999) .description('动量更新系数'), - // 聚类负样本参数(method=cluster) - num_clusters: Schema.number() - .default(10) - .description('聚类簇数量'), - - // 对抗式负样本参数(method=adversarial) - adversarial_epsilon: Schema.number() - .default(0.01) - .description('对抗扰动幅度'), - // 对比损失权重 λ contrastive_weight: Schema.number() .min(0) From b3ec592a169a72f2c0998306e3bb4d974fb7bac3 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 00:31:54 +0800 Subject: [PATCH 07/41] Update shared.ts --- mikazuki/schema/shared.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mikazuki/schema/shared.ts b/mikazuki/schema/shared.ts index a6b44b8c..8209a96e 100644 --- a/mikazuki/schema/shared.ts +++ b/mikazuki/schema/shared.ts @@ -224,15 +224,15 @@ // 噪音强度参数 noise_strength: Schema.number() .min(0) - .max(1) - .default(0.999) - .description('动量更新系数'), + .max(10) + .default(1.0) + .description('噪音强度参数'), // 对比损失权重 λ contrastive_weight: Schema.number() .min(0) .max(1) - .default(0.5) + .default(0.05) .description('对比损失权重'), // 对比学习暖启动 From b12489c942d2f71baa3995deeaca3b7ebd7786c5 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 00:34:30 +0800 Subject: [PATCH 08/41] Create contrastive.py --- scripts/dev/contrastive.py | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 scripts/dev/contrastive.py diff --git a/scripts/dev/contrastive.py b/scripts/dev/contrastive.py new file mode 100644 index 00000000..826f16f2 --- /dev/null +++ b/scripts/dev/contrastive.py @@ -0,0 +1,40 @@ +import torch + +def contrastive_target(latents, noise, method='Random-Noise', + noise_strength=0.1): + ''' + Generate Negative samples for contrastive learning. + Args: + latents: The input latent representations. + noise: The input noise. + method: The method used for generating negative samples. + noise_strength: The strength of the noise applied. + Returns: + latents_neg: The negative latent representations. + noise_neg: The negative noise. + ''' + if method == 'Random-Noise' or latents.shape[0] == 1: + latents_neg = latents + torch.randn_like(latents) * noise_strength + noise_neg = noise + torch.randn_like(noise) * noise_strength + elif method == 'Permutation': + perm = torch.randperm(latents.shape[0]) + latents_neg = latents[perm] + noise_neg = noise[perm] + elif method == 'Random-index': + idx = torch.randint(0, latents.shape[0], (latents.shape[0],)) + latents_neg = latents[idx] + noise_neg = noise[idx] + elif method == 'Circular': + perm = torch.arange(latents.shape[0]) + perm = torch.roll(perm, shifts=1) + latents_neg = latents[perm] + noise_neg = noise[perm] + elif method == 'Hard-Negative': + sim = torch.cdist(latents, latents, p=2) # 欧氏距离矩阵 + sim.fill_diagonal_(float('inf')) # 忽略自身 + idx_hard = sim.argmin(dim=1) + latents_neg = latents[idx_hard] + noise_neg = noise[idx_hard] + else: + raise ValueError(f'Unknown method: {method}') + return latents_neg, noise_neg From 1277313327f44f9298dc54ce406f80c43bd7db46 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 00:37:48 +0800 Subject: [PATCH 09/41] Update train_network.py --- scripts/dev/train_network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/dev/train_network.py b/scripts/dev/train_network.py index 2d279b3b..4b70b9b8 100644 --- a/scripts/dev/train_network.py +++ b/scripts/dev/train_network.py @@ -11,7 +11,7 @@ from multiprocessing import Value import numpy as np import toml - +from contrastive import contrastive_target from tqdm import tqdm import torch From 9b8b2f68eb42678d3d248375d8c63e9af16ea31c Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 00:40:30 +0800 Subject: [PATCH 10/41] Create contrastive.py --- scripts/stable/contrastive.py | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 scripts/stable/contrastive.py diff --git a/scripts/stable/contrastive.py b/scripts/stable/contrastive.py new file mode 100644 index 00000000..826f16f2 --- /dev/null +++ b/scripts/stable/contrastive.py @@ -0,0 +1,40 @@ +import torch + +def contrastive_target(latents, noise, method='Random-Noise', + noise_strength=0.1): + ''' + Generate Negative samples for contrastive learning. + Args: + latents: The input latent representations. + noise: The input noise. + method: The method used for generating negative samples. + noise_strength: The strength of the noise applied. + Returns: + latents_neg: The negative latent representations. + noise_neg: The negative noise. + ''' + if method == 'Random-Noise' or latents.shape[0] == 1: + latents_neg = latents + torch.randn_like(latents) * noise_strength + noise_neg = noise + torch.randn_like(noise) * noise_strength + elif method == 'Permutation': + perm = torch.randperm(latents.shape[0]) + latents_neg = latents[perm] + noise_neg = noise[perm] + elif method == 'Random-index': + idx = torch.randint(0, latents.shape[0], (latents.shape[0],)) + latents_neg = latents[idx] + noise_neg = noise[idx] + elif method == 'Circular': + perm = torch.arange(latents.shape[0]) + perm = torch.roll(perm, shifts=1) + latents_neg = latents[perm] + noise_neg = noise[perm] + elif method == 'Hard-Negative': + sim = torch.cdist(latents, latents, p=2) # 欧氏距离矩阵 + sim.fill_diagonal_(float('inf')) # 忽略自身 + idx_hard = sim.argmin(dim=1) + latents_neg = latents[idx_hard] + noise_neg = noise[idx_hard] + else: + raise ValueError(f'Unknown method: {method}') + return latents_neg, noise_neg From 48a452552bddba4bff8ab6104408ea38798c7316 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 00:52:31 +0800 Subject: [PATCH 11/41] Update train_network.py --- scripts/stable/train_network.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/scripts/stable/train_network.py b/scripts/stable/train_network.py index 7bf125dc..0c615389 100644 --- a/scripts/stable/train_network.py +++ b/scripts/stable/train_network.py @@ -8,7 +8,7 @@ import json from multiprocessing import Value import toml - +from contrastive import contrastive_target from tqdm import tqdm import torch @@ -984,6 +984,19 @@ def remove_model(old_ckpt_name): loss = train_util.conditional_loss( noise_pred.float(), target.float(), reduction="none", loss_type=args.loss_type, huber_c=huber_c ) + if args.enable_contrastive and epoch>= args.contrastive_warmup_steps: + latents_neg, noise_neg = contrastive_target(latents, noise, method=args.negative_sampling_method,noise_strength=args.noise_strength) + if args.v_parameterization: + # v-parameterization training + target_neg = noise_scheduler.get_velocity(latents_neg, noise_neg, timesteps) + else: + target_neg = noise_neg + loss_neg = train_util.conditional_loss( + noise_pred_neg.float(), target_neg.float(), reduction="none", loss_type=args.loss_type, huber_c=huber_c + ) + loss = loss - args.contrastive_weight*loss_neg + + if args.masked_loss or ("alpha_masks" in batch and batch["alpha_masks"] is not None): loss = apply_masked_loss(loss, batch) loss = loss.mean([1, 2, 3]) From e2a5f92210aa7553f03d52ff2fcba35c2c2a75f6 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 00:57:17 +0800 Subject: [PATCH 12/41] Update flux_train.py --- scripts/dev/flux_train.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/scripts/dev/flux_train.py b/scripts/dev/flux_train.py index 6f98adea..313a1298 100644 --- a/scripts/dev/flux_train.py +++ b/scripts/dev/flux_train.py @@ -19,7 +19,7 @@ import time from typing import List, Optional, Tuple, Union import toml - +from contrastive import contrastive_target from tqdm import tqdm import torch @@ -669,6 +669,12 @@ def grad_hook(parameter: torch.Tensor): # calculate loss huber_c = train_util.get_huber_threshold_if_needed(args, timesteps, noise_scheduler) loss = train_util.conditional_loss(model_pred.float(), target.float(), args.loss_type, "none", huber_c) + if args.enable_contrastive and epoch>= args.contrastive_warmup_steps: + latents_neg, noise_neg = contrastive_target(latents, noise, method=args.negative_sampling_method,noise_strength=args.noise_strength) + target_neg = noise_neg - latents_neg + loss_neg = train_util.conditional_loss( + noise_pred_neg.float(), target_neg.float(), args.loss_type, "none", huber_c) + loss = loss - args.contrastive_weight*loss_neg if weighting is not None: loss = loss * weighting if args.masked_loss or ("alpha_masks" in batch and batch["alpha_masks"] is not None): From dad2a56a62f5fa6bdd7b5e2e2ca719ec331326be Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 01:12:06 +0800 Subject: [PATCH 13/41] Update shared.ts --- mikazuki/schema/shared.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mikazuki/schema/shared.ts b/mikazuki/schema/shared.ts index 8209a96e..732102bb 100644 --- a/mikazuki/schema/shared.ts +++ b/mikazuki/schema/shared.ts @@ -243,7 +243,7 @@ Schema.object({}), ]), -]).description('对比学习配置'); +]).description('对比学习配置'), LOG_SETTINGS: Schema.intersect([ Schema.object({ From 8ec3c6e3860dc5c0c1e0274f108bf0f1da18564c Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 01:20:43 +0800 Subject: [PATCH 14/41] Update shared.ts --- mikazuki/schema/shared.ts | 95 +++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 48 deletions(-) diff --git a/mikazuki/schema/shared.ts b/mikazuki/schema/shared.ts index 732102bb..4fd7451e 100644 --- a/mikazuki/schema/shared.ts +++ b/mikazuki/schema/shared.ts @@ -196,54 +196,53 @@ Schema.object({}), ]), ]), - CONTRASTIVE: Schema.intersect([ - - // 对比学习开关 - Schema.object({ - enable_contrastive: Schema.boolean() - .default(false) - .description('启用对比学习模块'), - }).description('对比学习设置'), - - // 只有 enable_contrastive = true 时,下面的配置才生效 - Schema.union([ - Schema.object({ - enable_contrastive: Schema.const(true).required(), - - // 负样本策略 - negative_sampling_method: Schema.union([ - 'Random-Noise', - 'Permutation', - 'Random-index', - 'Circular', - 'Hard-Negative', - ]) - .default('Random-Noise') - .description('选择负样本生成策略'), - - // 噪音强度参数 - noise_strength: Schema.number() - .min(0) - .max(10) - .default(1.0) - .description('噪音强度参数'), - - // 对比损失权重 λ - contrastive_weight: Schema.number() - .min(0) - .max(1) - .default(0.05) - .description('对比损失权重'), - - // 对比学习暖启动 - contrastive_warmup_steps: Schema.number() - .default(100) - .description('使用随机负样本的步数'), - }), - Schema.object({}), - ]), - -]).description('对比学习配置'), + CONTRASTIVE: Schema.intersect([ + // 对比学习开关 + Schema.object({ + enable_contrastive: Schema.boolean() + .default(false) + .description('启用对比学习模块'), + }).description('对比学习设置'), + + // 只有 enable_contrastive = true 时,下面的配置才生效 + Schema.union([ + Schema.object({ + enable_contrastive: Schema.const(true).required(), + + // 负样本策略 + negative_sampling_method: Schema.union([ + 'Random-Noise', + 'Permutation', + 'Random-index', + 'Circular', + 'Hard-Negative', + ]) + .default('Random-Noise') + .description('选择负样本生成策略'), + + // 噪音强度参数 + noise_strength: Schema.number() + .min(0) + .max(10) + .default(1.0) + .description('噪音强度参数'), + + // 对比损失权重 λ + contrastive_weight: Schema.number() + .min(0) + .max(1) + .default(0.05) + .description('对比损失权重'), + + // 对比学习暖启动 + contrastive_warmup_steps: Schema.number() + .default(100) + .description('使用随机负样本的步数'), + }), + Schema.object({}), + ]), + + ]).description('对比学习配置'), LOG_SETTINGS: Schema.intersect([ Schema.object({ From 11419e428a7c6509111e6aa7946f8f3b59fc963a Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 01:23:53 +0800 Subject: [PATCH 15/41] Update shared.ts --- mikazuki/schema/shared.ts | 78 ++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 47 deletions(-) diff --git a/mikazuki/schema/shared.ts b/mikazuki/schema/shared.ts index 4fd7451e..ec8b7060 100644 --- a/mikazuki/schema/shared.ts +++ b/mikazuki/schema/shared.ts @@ -196,53 +196,37 @@ Schema.object({}), ]), ]), - CONTRASTIVE: Schema.intersect([ - // 对比学习开关 - Schema.object({ - enable_contrastive: Schema.boolean() - .default(false) - .description('启用对比学习模块'), - }).description('对比学习设置'), - - // 只有 enable_contrastive = true 时,下面的配置才生效 - Schema.union([ - Schema.object({ - enable_contrastive: Schema.const(true).required(), - - // 负样本策略 - negative_sampling_method: Schema.union([ - 'Random-Noise', - 'Permutation', - 'Random-index', - 'Circular', - 'Hard-Negative', - ]) - .default('Random-Noise') - .description('选择负样本生成策略'), - - // 噪音强度参数 - noise_strength: Schema.number() - .min(0) - .max(10) - .default(1.0) - .description('噪音强度参数'), - - // 对比损失权重 λ - contrastive_weight: Schema.number() - .min(0) - .max(1) - .default(0.05) - .description('对比损失权重'), - - // 对比学习暖启动 - contrastive_warmup_steps: Schema.number() - .default(100) - .description('使用随机负样本的步数'), - }), - Schema.object({}), - ]), - - ]).description('对比学习配置'), + CONTRASTIVE: Schema.object({ + enable_contrastive: Schema.boolean() + .default(false) + .description('启用对比学习模块'), + + negative_sampling_method: Schema.union([ + 'Random-Noise','Permutation','Random-index','Circular','Hard-Negative', + ]) + .default('Random-Noise') + .description('选择负样本生成策略') + .onlyWhen('enable_contrastive', Schema.const(true)), + + noise_strength: Schema.number() + .min(0) + .max(10) + .default(1.0) + .description('噪音强度参数') + .onlyWhen('enable_contrastive', Schema.const(true)), + + contrastive_weight: Schema.number() + .min(0) + .max(1) + .default(0.05) + .description('对比损失权重') + .onlyWhen('enable_contrastive', Schema.const(true)), + + contrastive_warmup_steps: Schema.number() + .default(100) + .description('使用随机负样本的步数') + .onlyWhen('enable_contrastive', Schema.const(true)), + }).description('对比学习配置'), LOG_SETTINGS: Schema.intersect([ Schema.object({ From dfd5b69fdf0f2fbcdf41dd017f61123bfe62d833 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 10:25:53 +0800 Subject: [PATCH 16/41] Update .gitmodules --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index a8e75f14..b14d3344 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ [submodule "frontend"] path = frontend - url = https://github.com/hanamizuki-ai/lora-gui-dist + url = https://github.com/hdfhssg/lora-gui-dist [submodule "mikazuki/dataset-tag-editor"] path = mikazuki/dataset-tag-editor url = https://github.com/Akegarasu/dataset-tag-editor From 5d086074959c1d4b2c73eab933d83a2405339814 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 11:02:41 +0800 Subject: [PATCH 17/41] Update shared.ts --- mikazuki/schema/shared.ts | 54 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/mikazuki/schema/shared.ts b/mikazuki/schema/shared.ts index ec8b7060..58ff4329 100644 --- a/mikazuki/schema/shared.ts +++ b/mikazuki/schema/shared.ts @@ -171,33 +171,7 @@ Schema.object({ optimizer_args_custom: Schema.array(String).role('table').description('自定义 optimizer_args,一行一个'), }) - ]), - - PREVIEW_IMAGE: Schema.intersect([ - Schema.object({ - enable_preview: Schema.boolean().default(false).description('启用训练预览图'), - }).description('训练预览图设置'), - - Schema.union([ - Schema.object({ - enable_preview: Schema.const(true).required(), - randomly_choice_prompt: Schema.boolean().default(false).description('随机选择预览图 Prompt'), - prompt_file: Schema.string().role('textarea').description('预览图 Prompt 文件路径。填写后将采用文件内的 prompt,而下方的选项将失效。'), - positive_prompts: Schema.string().role('textarea').default('masterpiece, best quality, 1girl, solo').description("Prompt"), - negative_prompts: Schema.string().role('textarea').default('lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts,signature, watermark, username, blurry').description("Negative Prompt"), - sample_width: Schema.number().default(512).description('预览图宽'), - sample_height: Schema.number().default(512).description('预览图高'), - sample_cfg: Schema.number().min(1).max(30).default(7).description('CFG Scale'), - sample_seed: Schema.number().default(2333).description('种子'), - sample_steps: Schema.number().min(1).max(300).default(24).description('迭代步数'), - sample_sampler: Schema.union(["ddim", "pndm", "lms", "euler", "euler_a", "heun", "dpm_2", "dpm_2_a", "dpmsolver", "dpmsolver++", "dpmsingle", "k_lms", "k_euler", "k_euler_a", "k_dpm_2", "k_dpm_2_a"]).default("euler_a").description("生成预览图所用采样器"), - sample_every_n_epochs: Schema.number().default(2).description("每 N 个 epoch 生成一次预览图"), - }), - Schema.object({}), - ]), - ]), - CONTRASTIVE: Schema.object({ - enable_contrastive: Schema.boolean() + enable_contrastive: Schema.boolean() .default(false) .description('启用对比学习模块'), @@ -226,7 +200,31 @@ .default(100) .description('使用随机负样本的步数') .onlyWhen('enable_contrastive', Schema.const(true)), - }).description('对比学习配置'), + ]), + + PREVIEW_IMAGE: Schema.intersect([ + Schema.object({ + enable_preview: Schema.boolean().default(false).description('启用训练预览图'), + }).description('训练预览图设置'), + + Schema.union([ + Schema.object({ + enable_preview: Schema.const(true).required(), + randomly_choice_prompt: Schema.boolean().default(false).description('随机选择预览图 Prompt'), + prompt_file: Schema.string().role('textarea').description('预览图 Prompt 文件路径。填写后将采用文件内的 prompt,而下方的选项将失效。'), + positive_prompts: Schema.string().role('textarea').default('masterpiece, best quality, 1girl, solo').description("Prompt"), + negative_prompts: Schema.string().role('textarea').default('lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts,signature, watermark, username, blurry').description("Negative Prompt"), + sample_width: Schema.number().default(512).description('预览图宽'), + sample_height: Schema.number().default(512).description('预览图高'), + sample_cfg: Schema.number().min(1).max(30).default(7).description('CFG Scale'), + sample_seed: Schema.number().default(2333).description('种子'), + sample_steps: Schema.number().min(1).max(300).default(24).description('迭代步数'), + sample_sampler: Schema.union(["ddim", "pndm", "lms", "euler", "euler_a", "heun", "dpm_2", "dpm_2_a", "dpmsolver", "dpmsolver++", "dpmsingle", "k_lms", "k_euler", "k_euler_a", "k_dpm_2", "k_dpm_2_a"]).default("euler_a").description("生成预览图所用采样器"), + sample_every_n_epochs: Schema.number().default(2).description("每 N 个 epoch 生成一次预览图"), + }), + Schema.object({}), + ]), + ]), LOG_SETTINGS: Schema.intersect([ Schema.object({ From 5d5390c09bedc039f0e89f4faf95a710bb1b3070 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 11:03:41 +0800 Subject: [PATCH 18/41] Update dreambooth.ts --- mikazuki/schema/dreambooth.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/mikazuki/schema/dreambooth.ts b/mikazuki/schema/dreambooth.ts index 243987c1..09710dc5 100644 --- a/mikazuki/schema/dreambooth.ts +++ b/mikazuki/schema/dreambooth.ts @@ -132,8 +132,6 @@ Schema.intersect([ optimizer_args_custom: Schema.array(String).role("table").description("自定义 optimizer_args,一行一个"), }) ]), - //对比学习设置 - SHARED_SCHEMAS.CONTRASTIVE Schema.intersect([ Schema.object({ enable_preview: Schema.boolean().default(false).description("启用训练预览图"), From c92478e86f042dacd177fc73d3dc0cf22e9d6035 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 11:04:05 +0800 Subject: [PATCH 19/41] Update flux-lora.ts --- mikazuki/schema/flux-lora.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/mikazuki/schema/flux-lora.ts b/mikazuki/schema/flux-lora.ts index 0d864bad..94277405 100644 --- a/mikazuki/schema/flux-lora.ts +++ b/mikazuki/schema/flux-lora.ts @@ -63,8 +63,6 @@ Schema.intersect([ SHARED_SCHEMAS.NETWORK_OPTION_BASEWEIGHT, ]), - //对比学习设置 - SHARED_SCHEMAS.CONTRASTIVE // 预览图设置 SHARED_SCHEMAS.PREVIEW_IMAGE, From 9d1a2c0b74d36699a18585db0f603ac464437b3d Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 11:04:19 +0800 Subject: [PATCH 20/41] Update lora-master.ts --- mikazuki/schema/lora-master.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/mikazuki/schema/lora-master.ts b/mikazuki/schema/lora-master.ts index 295b3e90..d0327399 100644 --- a/mikazuki/schema/lora-master.ts +++ b/mikazuki/schema/lora-master.ts @@ -69,8 +69,6 @@ Schema.intersect([ SHARED_SCHEMAS.NETWORK_OPTION_BASEWEIGHT, ]), - //对比学习设置 - SHARED_SCHEMAS.CONTRASTIVE // 预览图设置 SHARED_SCHEMAS.PREVIEW_IMAGE, From 56c1ee62063d6b0bedcb88c8e8e9a343c36715ef Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 11:04:38 +0800 Subject: [PATCH 21/41] Update sd3-lora.ts --- mikazuki/schema/sd3-lora.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/mikazuki/schema/sd3-lora.ts b/mikazuki/schema/sd3-lora.ts index 7a540825..33b2a325 100644 --- a/mikazuki/schema/sd3-lora.ts +++ b/mikazuki/schema/sd3-lora.ts @@ -54,8 +54,6 @@ Schema.intersect([ SHARED_SCHEMAS.NETWORK_OPTION_BASEWEIGHT, ]), - //对比学习设置 - SHARED_SCHEMAS.CONTRASTIVE // 预览图设置 SHARED_SCHEMAS.PREVIEW_IMAGE, From 626933e9ad8faacdb3a59be224de5e2b6fcdf88e Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 11:05:50 +0800 Subject: [PATCH 22/41] Update shared.ts --- mikazuki/schema/shared.ts | 30 +----------------------------- 1 file changed, 1 insertion(+), 29 deletions(-) diff --git a/mikazuki/schema/shared.ts b/mikazuki/schema/shared.ts index 58ff4329..d2134a34 100644 --- a/mikazuki/schema/shared.ts +++ b/mikazuki/schema/shared.ts @@ -171,35 +171,7 @@ Schema.object({ optimizer_args_custom: Schema.array(String).role('table').description('自定义 optimizer_args,一行一个'), }) - enable_contrastive: Schema.boolean() - .default(false) - .description('启用对比学习模块'), - - negative_sampling_method: Schema.union([ - 'Random-Noise','Permutation','Random-index','Circular','Hard-Negative', - ]) - .default('Random-Noise') - .description('选择负样本生成策略') - .onlyWhen('enable_contrastive', Schema.const(true)), - - noise_strength: Schema.number() - .min(0) - .max(10) - .default(1.0) - .description('噪音强度参数') - .onlyWhen('enable_contrastive', Schema.const(true)), - - contrastive_weight: Schema.number() - .min(0) - .max(1) - .default(0.05) - .description('对比损失权重') - .onlyWhen('enable_contrastive', Schema.const(true)), - - contrastive_warmup_steps: Schema.number() - .default(100) - .description('使用随机负样本的步数') - .onlyWhen('enable_contrastive', Schema.const(true)), + ]), PREVIEW_IMAGE: Schema.intersect([ From 617d896e8cc70f874a1945c038187e0a8794cc44 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 21:35:27 +0800 Subject: [PATCH 23/41] Update default.toml --- config/default.toml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config/default.toml b/config/default.toml index 3e2cbd31..b7bb8f4d 100644 --- a/config/default.toml +++ b/config/default.toml @@ -38,6 +38,11 @@ lowram = false clip_skip = 2 mixed_precision = "fp16" save_precision = "fp16" +enable_contrastive = false +negative_sampling_method = "Random-Noise" +noise_strength = 1.0 +contrastive_weight = 0.05 +contrastive_warmup_steps = 100 [sample_prompt] sample_sampler = "euler_a" From 0ed282ac35f24644dc46e539a34b11a0c3bdcd5f Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 21:35:50 +0800 Subject: [PATCH 24/41] Update lora.toml --- config/lora.toml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/config/lora.toml b/config/lora.toml index 66212978..3fb7642c 100644 --- a/config/lora.toml +++ b/config/lora.toml @@ -41,6 +41,11 @@ max_train_epochs = 10 resolution = "512,512" clip_skip = 2 mixed_precision = "fp16" +enable_contrastive = false +negative_sampling_method = "Random-Noise" +noise_strength = 1.0 +contrastive_weight = 0.05 +contrastive_warmup_steps = 100 [sample_prompt_arguments] sample_sampler = "euler_a" @@ -59,4 +64,4 @@ save_precision = "fp16" [others] cache_latents = true shuffle_caption = true -enable_bucket = true \ No newline at end of file +enable_bucket = true From 3d53910d055dc2800c6bcd0a0bc82bd2d519f120 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 21:57:40 +0800 Subject: [PATCH 25/41] Update dreambooth.ts --- mikazuki/schema/dreambooth.ts | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/mikazuki/schema/dreambooth.ts b/mikazuki/schema/dreambooth.ts index 09710dc5..200e5424 100644 --- a/mikazuki/schema/dreambooth.ts +++ b/mikazuki/schema/dreambooth.ts @@ -132,6 +132,44 @@ Schema.intersect([ optimizer_args_custom: Schema.array(String).role("table").description("自定义 optimizer_args,一行一个"), }) ]), + Schema.intersect([ + // 对比学习主开关 + Schema.object({ + enable_contrastive: Schema.boolean() + .default(false) + .description('启用对比学习模块'), + }).description('对比学习配置'), + + // 当 enable_contrastive = true 时,才展示下面这些配置;否则只用空对象 + Schema.union([ + Schema.object({ + enable_contrastive: Schema.const(true).required(), + + negative_sampling_method: Schema.union([ + 'Random-Noise', 'Permutation', 'Random-index', 'Circular', 'Hard-Negative', + ]) + .default('Random-Noise') + .description('选择负样本生成策略'), + + noise_strength: Schema.number() + .min(0) + .max(10) + .default(1.0) + .description('噪音强度参数'), + + contrastive_weight: Schema.number() + .min(0) + .max(1) + .default(0.05) + .description('对比损失权重'), + + contrastive_warmup_steps: Schema.number() + .default(100) + .description('使用随机负样本的步数'), + }), + Schema.object({}), + ]), + ]), Schema.intersect([ Schema.object({ enable_preview: Schema.boolean().default(false).description("启用训练预览图"), From c9d2d80ab1369f2c2b5fb71d158fd297c3082ff3 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 21:58:08 +0800 Subject: [PATCH 26/41] Update flux-lora.ts --- mikazuki/schema/flux-lora.ts | 39 +++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/mikazuki/schema/flux-lora.ts b/mikazuki/schema/flux-lora.ts index 94277405..9b37083e 100644 --- a/mikazuki/schema/flux-lora.ts +++ b/mikazuki/schema/flux-lora.ts @@ -62,7 +62,44 @@ Schema.intersect([ SHARED_SCHEMAS.NETWORK_OPTION_BASEWEIGHT, ]), - + Schema.intersect([ + // 对比学习主开关 + Schema.object({ + enable_contrastive: Schema.boolean() + .default(false) + .description('启用对比学习模块'), + }).description('对比学习配置'), + + // 当 enable_contrastive = true 时,才展示下面这些配置;否则只用空对象 + Schema.union([ + Schema.object({ + enable_contrastive: Schema.const(true).required(), + + negative_sampling_method: Schema.union([ + 'Random-Noise', 'Permutation', 'Random-index', 'Circular', 'Hard-Negative', + ]) + .default('Random-Noise') + .description('选择负样本生成策略'), + + noise_strength: Schema.number() + .min(0) + .max(10) + .default(1.0) + .description('噪音强度参数'), + + contrastive_weight: Schema.number() + .min(0) + .max(1) + .default(0.05) + .description('对比损失权重'), + + contrastive_warmup_steps: Schema.number() + .default(100) + .description('使用随机负样本的步数'), + }), + Schema.object({}), + ]), + ]), // 预览图设置 SHARED_SCHEMAS.PREVIEW_IMAGE, From fcfd3597b235652c6bf812cd905c27a2248ffeba Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 21:58:26 +0800 Subject: [PATCH 27/41] Update lora-basic.ts --- mikazuki/schema/lora-basic.ts | 39 ++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/mikazuki/schema/lora-basic.ts b/mikazuki/schema/lora-basic.ts index 8ee03e62..b12209a8 100644 --- a/mikazuki/schema/lora-basic.ts +++ b/mikazuki/schema/lora-basic.ts @@ -46,7 +46,44 @@ Schema.intersect([ ]).default("AdamW8bit").description("优化器设置"), }) ]), - + Schema.intersect([ + // 对比学习主开关 + Schema.object({ + enable_contrastive: Schema.boolean() + .default(false) + .description('启用对比学习模块'), + }).description('对比学习配置'), + + // 当 enable_contrastive = true 时,才展示下面这些配置;否则只用空对象 + Schema.union([ + Schema.object({ + enable_contrastive: Schema.const(true).required(), + + negative_sampling_method: Schema.union([ + 'Random-Noise', 'Permutation', 'Random-index', 'Circular', 'Hard-Negative', + ]) + .default('Random-Noise') + .description('选择负样本生成策略'), + + noise_strength: Schema.number() + .min(0) + .max(10) + .default(1.0) + .description('噪音强度参数'), + + contrastive_weight: Schema.number() + .min(0) + .max(1) + .default(0.05) + .description('对比损失权重'), + + contrastive_warmup_steps: Schema.number() + .default(100) + .description('使用随机负样本的步数'), + }), + Schema.object({}), + ]), + ]), Schema.intersect([ Schema.object({ enable_preview: Schema.boolean().default(false).description('启用训练预览图'), From 80f673d88e704df35226a1550cf846ec85b705c1 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 21:58:47 +0800 Subject: [PATCH 28/41] Update lora-master.ts --- mikazuki/schema/lora-master.ts | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/mikazuki/schema/lora-master.ts b/mikazuki/schema/lora-master.ts index d0327399..22aa3bec 100644 --- a/mikazuki/schema/lora-master.ts +++ b/mikazuki/schema/lora-master.ts @@ -69,6 +69,44 @@ Schema.intersect([ SHARED_SCHEMAS.NETWORK_OPTION_BASEWEIGHT, ]), + Schema.intersect([ + // 对比学习主开关 + Schema.object({ + enable_contrastive: Schema.boolean() + .default(false) + .description('启用对比学习模块'), + }).description('对比学习配置'), + + // 当 enable_contrastive = true 时,才展示下面这些配置;否则只用空对象 + Schema.union([ + Schema.object({ + enable_contrastive: Schema.const(true).required(), + + negative_sampling_method: Schema.union([ + 'Random-Noise', 'Permutation', 'Random-index', 'Circular', 'Hard-Negative', + ]) + .default('Random-Noise') + .description('选择负样本生成策略'), + + noise_strength: Schema.number() + .min(0) + .max(10) + .default(1.0) + .description('噪音强度参数'), + + contrastive_weight: Schema.number() + .min(0) + .max(1) + .default(0.05) + .description('对比损失权重'), + + contrastive_warmup_steps: Schema.number() + .default(100) + .description('使用随机负样本的步数'), + }), + Schema.object({}), + ]), + ]), // 预览图设置 SHARED_SCHEMAS.PREVIEW_IMAGE, From 82fe2c407502159d5109501637df0106f2984d55 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 21:59:01 +0800 Subject: [PATCH 29/41] Update lumina2-lora.ts --- mikazuki/schema/lumina2-lora.ts | 41 +++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/mikazuki/schema/lumina2-lora.ts b/mikazuki/schema/lumina2-lora.ts index 1233c987..8075e480 100644 --- a/mikazuki/schema/lumina2-lora.ts +++ b/mikazuki/schema/lumina2-lora.ts @@ -64,7 +64,44 @@ Schema.intersect([ SHARED_SCHEMAS.NETWORK_OPTION_BASEWEIGHT, ]), - + Schema.intersect([ + // 对比学习主开关 + Schema.object({ + enable_contrastive: Schema.boolean() + .default(false) + .description('启用对比学习模块'), + }).description('对比学习配置'), + + // 当 enable_contrastive = true 时,才展示下面这些配置;否则只用空对象 + Schema.union([ + Schema.object({ + enable_contrastive: Schema.const(true).required(), + + negative_sampling_method: Schema.union([ + 'Random-Noise', 'Permutation', 'Random-index', 'Circular', 'Hard-Negative', + ]) + .default('Random-Noise') + .description('选择负样本生成策略'), + + noise_strength: Schema.number() + .min(0) + .max(10) + .default(1.0) + .description('噪音强度参数'), + + contrastive_weight: Schema.number() + .min(0) + .max(1) + .default(0.05) + .description('对比损失权重'), + + contrastive_warmup_steps: Schema.number() + .default(100) + .description('使用随机负样本的步数'), + }), + Schema.object({}), + ]), + ]), // 预览图设置 SHARED_SCHEMAS.PREVIEW_IMAGE, @@ -96,4 +133,4 @@ Schema.intersect([ // 分布式训练 SHARED_SCHEMAS.DISTRIBUTED_TRAINING -]); \ No newline at end of file +]); From 9784fce0b8342bae87de2848d8eaf163813f856b Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 21:59:23 +0800 Subject: [PATCH 30/41] Update sd3-lora.ts --- mikazuki/schema/sd3-lora.ts | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/mikazuki/schema/sd3-lora.ts b/mikazuki/schema/sd3-lora.ts index 33b2a325..d9089b54 100644 --- a/mikazuki/schema/sd3-lora.ts +++ b/mikazuki/schema/sd3-lora.ts @@ -54,6 +54,44 @@ Schema.intersect([ SHARED_SCHEMAS.NETWORK_OPTION_BASEWEIGHT, ]), + Schema.intersect([ + // 对比学习主开关 + Schema.object({ + enable_contrastive: Schema.boolean() + .default(false) + .description('启用对比学习模块'), + }).description('对比学习配置'), + + // 当 enable_contrastive = true 时,才展示下面这些配置;否则只用空对象 + Schema.union([ + Schema.object({ + enable_contrastive: Schema.const(true).required(), + + negative_sampling_method: Schema.union([ + 'Random-Noise', 'Permutation', 'Random-index', 'Circular', 'Hard-Negative', + ]) + .default('Random-Noise') + .description('选择负样本生成策略'), + + noise_strength: Schema.number() + .min(0) + .max(10) + .default(1.0) + .description('噪音强度参数'), + + contrastive_weight: Schema.number() + .min(0) + .max(1) + .default(0.05) + .description('对比损失权重'), + + contrastive_warmup_steps: Schema.number() + .default(100) + .description('使用随机负样本的步数'), + }), + Schema.object({}), + ]), + ]), // 预览图设置 SHARED_SCHEMAS.PREVIEW_IMAGE, From 10f23608e923a1f27f9c697173ca3e5da3eed3e3 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 22:28:10 +0800 Subject: [PATCH 31/41] Update sd3-lora.ts --- mikazuki/schema/sd3-lora.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mikazuki/schema/sd3-lora.ts b/mikazuki/schema/sd3-lora.ts index d9089b54..8d99e24a 100644 --- a/mikazuki/schema/sd3-lora.ts +++ b/mikazuki/schema/sd3-lora.ts @@ -46,6 +46,7 @@ Schema.intersect([ network_alpha: Schema.number().min(1).default(1).description("常用值:等于 network_dim 或 network_dim*1/2 或 1。使用较小的 alpha 需要提升学习率"), network_args_custom: Schema.array(String).role('table').description('自定义 network_args,一行一个'), enable_base_weight: Schema.boolean().default(false).description('启用基础权重(差异炼丹)'), + network_scale: Schema.number().step(0.05).default(1.0).description('网络缩放系数,应该小于等于1.0'), }).description("网络设置"), // lycoris 参数 @@ -74,18 +75,21 @@ Schema.intersect([ .description('选择负样本生成策略'), noise_strength: Schema.number() + .step(0.5) .min(0) .max(10) .default(1.0) .description('噪音强度参数'), contrastive_weight: Schema.number() + .step(0.05) .min(0) .max(1) .default(0.05) .description('对比损失权重'), contrastive_warmup_steps: Schema.number() + .step(10) .default(100) .description('使用随机负样本的步数'), }), From 63dafa120d4387ff1a7d863420adc415805ab259 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 22:28:34 +0800 Subject: [PATCH 32/41] Update lumina2-lora.ts --- mikazuki/schema/lumina2-lora.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mikazuki/schema/lumina2-lora.ts b/mikazuki/schema/lumina2-lora.ts index 8075e480..86925dbc 100644 --- a/mikazuki/schema/lumina2-lora.ts +++ b/mikazuki/schema/lumina2-lora.ts @@ -56,6 +56,7 @@ Schema.intersect([ scale_weight_norms: Schema.number().step(0.01).min(0).default(1.0).description("最大范数正则化。如果使用,推荐为 1"), network_args_custom: Schema.array(String).role('table').description('自定义 network_args,一行一个'), enable_base_weight: Schema.boolean().default(false).description('启用基础权重(差异炼丹)'), + network_scale: Schema.number().step(0.05).default(1.0).description('网络缩放系数,应该小于等于1.0'), }).description("网络设置"), // lycoris 参数 @@ -84,18 +85,21 @@ Schema.intersect([ .description('选择负样本生成策略'), noise_strength: Schema.number() + .step(0.5) .min(0) .max(10) .default(1.0) .description('噪音强度参数'), contrastive_weight: Schema.number() + .step(0.05) .min(0) .max(1) .default(0.05) .description('对比损失权重'), contrastive_warmup_steps: Schema.number() + .step(10) .default(100) .description('使用随机负样本的步数'), }), From 7bf0d048f7261f613451701b67f6647d0521996b Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 22:29:40 +0800 Subject: [PATCH 33/41] Update lora-master.ts --- mikazuki/schema/lora-master.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mikazuki/schema/lora-master.ts b/mikazuki/schema/lora-master.ts index 22aa3bec..10547622 100644 --- a/mikazuki/schema/lora-master.ts +++ b/mikazuki/schema/lora-master.ts @@ -55,6 +55,7 @@ Schema.intersect([ network_args_custom: Schema.array(String).role('table').description('自定义 network_args,一行一个'), enable_block_weights: Schema.boolean().default(false).description('启用分层学习率训练(只支持网络模块 networks.lora)'), enable_base_weight: Schema.boolean().default(false).description('启用基础权重(差异炼丹)'), + network_scale: Schema.number().step(0.05).default(1.0).description('网络缩放系数,应该小于等于1.0'), }).description("网络设置"), // lycoris 参数 @@ -89,18 +90,21 @@ Schema.intersect([ .description('选择负样本生成策略'), noise_strength: Schema.number() + .step(0.5) .min(0) .max(10) .default(1.0) .description('噪音强度参数'), contrastive_weight: Schema.number() + .step(0.05) .min(0) .max(1) .default(0.05) .description('对比损失权重'), contrastive_warmup_steps: Schema.number() + .step(10) .default(100) .description('使用随机负样本的步数'), }), From 41cd3a3f1eba51b43d435f01cddbf3d4291d7b99 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 22:31:00 +0800 Subject: [PATCH 34/41] Update lora-basic.ts --- mikazuki/schema/lora-basic.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mikazuki/schema/lora-basic.ts b/mikazuki/schema/lora-basic.ts index b12209a8..28c02a89 100644 --- a/mikazuki/schema/lora-basic.ts +++ b/mikazuki/schema/lora-basic.ts @@ -66,18 +66,21 @@ Schema.intersect([ .description('选择负样本生成策略'), noise_strength: Schema.number() + .step(0.5) .min(0) .max(10) .default(1.0) .description('噪音强度参数'), contrastive_weight: Schema.number() + .step(0.05) .min(0) .max(1) .default(0.05) .description('对比损失权重'), contrastive_warmup_steps: Schema.number() + .step(10) .default(100) .description('使用随机负样本的步数'), }), From 2cb9f87e87e5696e5ad3f89468ba9434acb0ae45 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 22:31:38 +0800 Subject: [PATCH 35/41] Update flux-lora.ts --- mikazuki/schema/flux-lora.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mikazuki/schema/flux-lora.ts b/mikazuki/schema/flux-lora.ts index 9b37083e..94d4127d 100644 --- a/mikazuki/schema/flux-lora.ts +++ b/mikazuki/schema/flux-lora.ts @@ -54,6 +54,7 @@ Schema.intersect([ scale_weight_norms: Schema.number().step(0.01).min(0).description("最大范数正则化。如果使用,推荐为 1"), network_args_custom: Schema.array(String).role('table').description('自定义 network_args,一行一个'), enable_base_weight: Schema.boolean().default(false).description('启用基础权重(差异炼丹)'), + network_scale: Schema.number().step(0.05).default(1.0).description('网络缩放系数,应该小于等于1.0'), }).description("网络设置"), // lycoris 参数 @@ -82,18 +83,21 @@ Schema.intersect([ .description('选择负样本生成策略'), noise_strength: Schema.number() + .step(0.5) .min(0) .max(10) .default(1.0) .description('噪音强度参数'), contrastive_weight: Schema.number() + .step(0.05) .min(0) .max(1) .default(0.05) .description('对比损失权重'), contrastive_warmup_steps: Schema.number() + .step(10) .default(100) .description('使用随机负样本的步数'), }), From 0329fc0d63ad5a315be00c6836ea4a99e4ca0f01 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 22:33:00 +0800 Subject: [PATCH 36/41] Update dreambooth.ts --- mikazuki/schema/dreambooth.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mikazuki/schema/dreambooth.ts b/mikazuki/schema/dreambooth.ts index 200e5424..4b8d1632 100644 --- a/mikazuki/schema/dreambooth.ts +++ b/mikazuki/schema/dreambooth.ts @@ -152,18 +152,21 @@ Schema.intersect([ .description('选择负样本生成策略'), noise_strength: Schema.number() + .step(0.5) .min(0) .max(10) .default(1.0) .description('噪音强度参数'), contrastive_weight: Schema.number() + .step(0.05) .min(0) .max(1) .default(0.05) .description('对比损失权重'), contrastive_warmup_steps: Schema.number() + .step(10) .default(100) .description('使用随机负样本的步数'), }), From 7deedcd61f1b91402ab253a9feba8b79e0bc2ab7 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 22:41:35 +0800 Subject: [PATCH 37/41] Update train_network.py --- scripts/stable/train_network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/stable/train_network.py b/scripts/stable/train_network.py index 0c615389..cbaf787a 100644 --- a/scripts/stable/train_network.py +++ b/scripts/stable/train_network.py @@ -973,7 +973,7 @@ def remove_model(old_ckpt_name): text_encoder_conds, batch, weight_dtype, - ) + )*args.network_scale if args.v_parameterization: # v-parameterization training From 8eac233f1bdaa94a868805e9c57e304e7cf1d2c7 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 22:49:05 +0800 Subject: [PATCH 38/41] Update train_network.py --- scripts/dev/train_network.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/scripts/dev/train_network.py b/scripts/dev/train_network.py index 4b70b9b8..642a7dba 100644 --- a/scripts/dev/train_network.py +++ b/scripts/dev/train_network.py @@ -441,9 +441,22 @@ def process_batch( train_unet, is_train=is_train, ) - + noise_pred = noise_pred*args.network_scale huber_c = train_util.get_huber_threshold_if_needed(args, timesteps, noise_scheduler) loss = train_util.conditional_loss(noise_pred.float(), target.float(), args.loss_type, "none", huber_c) + if args.enable_contrastive and epoch>= args.contrastive_warmup_steps: + latents_neg, noise_neg = contrastive_target(latents, noise, method=args.negative_sampling_method,noise_strength=args.noise_strength) + if args.v_parameterization: + # v-parameterization training + target_neg = noise_scheduler.get_velocity(latents_neg, noise_neg, timesteps) + else: + target_neg = noise_neg + loss_neg = train_util.conditional_loss( + noise_pred_neg.float(), target_neg.float(), reduction="none", loss_type=args.loss_type, huber_c=huber_c + ) + loss = loss - args.contrastive_weight*loss_neg + + if weighting is not None: loss = loss * weighting if args.masked_loss or ("alpha_masks" in batch and batch["alpha_masks"] is not None): From ea7923600a86a9259054f22197d97e09db034ce7 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 22:51:01 +0800 Subject: [PATCH 39/41] Update flux_train.py --- scripts/dev/flux_train.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/dev/flux_train.py b/scripts/dev/flux_train.py index 313a1298..0d42dfc6 100644 --- a/scripts/dev/flux_train.py +++ b/scripts/dev/flux_train.py @@ -665,7 +665,8 @@ def grad_hook(parameter: torch.Tensor): # flow matching loss: this is different from SD3 target = noise - latents - + + model_pred = model_pred*args.network_scale # calculate loss huber_c = train_util.get_huber_threshold_if_needed(args, timesteps, noise_scheduler) loss = train_util.conditional_loss(model_pred.float(), target.float(), args.loss_type, "none", huber_c) From 51d927d6c7ac3adc991c22d3437996b060a54fdd Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 22:59:23 +0800 Subject: [PATCH 40/41] Update train_network.py --- scripts/dev/train_network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/dev/train_network.py b/scripts/dev/train_network.py index 642a7dba..6298611c 100644 --- a/scripts/dev/train_network.py +++ b/scripts/dev/train_network.py @@ -444,7 +444,7 @@ def process_batch( noise_pred = noise_pred*args.network_scale huber_c = train_util.get_huber_threshold_if_needed(args, timesteps, noise_scheduler) loss = train_util.conditional_loss(noise_pred.float(), target.float(), args.loss_type, "none", huber_c) - if args.enable_contrastive and epoch>= args.contrastive_warmup_steps: + if args.enable_contrastive: latents_neg, noise_neg = contrastive_target(latents, noise, method=args.negative_sampling_method,noise_strength=args.noise_strength) if args.v_parameterization: # v-parameterization training From d618925db4defcc11f93fa7836d86183b3fba712 Mon Sep 17 00:00:00 2001 From: PengXin <78085965+hdfhssg@users.noreply.github.com> Date: Fri, 13 Jun 2025 23:07:27 +0800 Subject: [PATCH 41/41] Update train_network.py --- scripts/dev/train_network.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/scripts/dev/train_network.py b/scripts/dev/train_network.py index 6298611c..c5bfd432 100644 --- a/scripts/dev/train_network.py +++ b/scripts/dev/train_network.py @@ -444,18 +444,6 @@ def process_batch( noise_pred = noise_pred*args.network_scale huber_c = train_util.get_huber_threshold_if_needed(args, timesteps, noise_scheduler) loss = train_util.conditional_loss(noise_pred.float(), target.float(), args.loss_type, "none", huber_c) - if args.enable_contrastive: - latents_neg, noise_neg = contrastive_target(latents, noise, method=args.negative_sampling_method,noise_strength=args.noise_strength) - if args.v_parameterization: - # v-parameterization training - target_neg = noise_scheduler.get_velocity(latents_neg, noise_neg, timesteps) - else: - target_neg = noise_neg - loss_neg = train_util.conditional_loss( - noise_pred_neg.float(), target_neg.float(), reduction="none", loss_type=args.loss_type, huber_c=huber_c - ) - loss = loss - args.contrastive_weight*loss_neg - if weighting is not None: loss = loss * weighting