Skip to content

Commit cf09c6a

Browse files
authored
Merge pull request #1177 from KohakuBlueleaf/random-strength-noise
Random strength for Noise Offset and input perturbation noise
2 parents 80dbbf5 + 53954a1 commit cf09c6a

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

library/train_util.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3087,6 +3087,11 @@ def add_training_arguments(parser: argparse.ArgumentParser, support_dreambooth:
30873087
default=None,
30883088
help="enable noise offset with this value (if enabled, around 0.1 is recommended) / Noise offsetを有効にしてこの値を設定する(有効にする場合は0.1程度を推奨)",
30893089
)
3090+
parser.add_argument(
3091+
"--noise_offset_random_strength",
3092+
action="store_true",
3093+
help="use random strength between 0~noise_offset for noise offset. / noise offsetにおいて、0からnoise_offsetの間でランダムな強度を使用します。",
3094+
)
30903095
parser.add_argument(
30913096
"--multires_noise_iterations",
30923097
type=int,
@@ -3100,6 +3105,12 @@ def add_training_arguments(parser: argparse.ArgumentParser, support_dreambooth:
31003105
help="enable input perturbation noise. used for regularization. recommended value: around 0.1 (from arxiv.org/abs/2301.11706) "
31013106
+ "/ input perturbation noiseを有効にする。正則化に使用される。推奨値: 0.1程度 (arxiv.org/abs/2301.11706 より)",
31023107
)
3108+
parser.add_argument(
3109+
"--ip_noise_gamma_random_strength",
3110+
action="store_true",
3111+
help="Use random strength between 0~ip_noise_gamma for input perturbation noise."
3112+
+ "/ input perturbation noiseにおいて、0からip_noise_gammaの間でランダムな強度を使用します。",
3113+
)
31033114
# parser.add_argument(
31043115
# "--perlin_noise",
31053116
# type=int,
@@ -4656,7 +4667,11 @@ def get_noise_noisy_latents_and_timesteps(args, noise_scheduler, latents):
46564667
# Sample noise that we'll add to the latents
46574668
noise = torch.randn_like(latents, device=latents.device)
46584669
if args.noise_offset:
4659-
noise = custom_train_functions.apply_noise_offset(latents, noise, args.noise_offset, args.adaptive_noise_scale)
4670+
if args.noise_offset_random_strength:
4671+
noise_offset = torch.rand(1, device=latents.device) * args.noise_offset
4672+
else:
4673+
noise_offset = args.noise_offset
4674+
noise = custom_train_functions.apply_noise_offset(latents, noise, noise_offset, args.adaptive_noise_scale)
46604675
if args.multires_noise_iterations:
46614676
noise = custom_train_functions.pyramid_noise_like(
46624677
noise, latents.device, args.multires_noise_iterations, args.multires_noise_discount
@@ -4673,7 +4688,11 @@ def get_noise_noisy_latents_and_timesteps(args, noise_scheduler, latents):
46734688
# Add noise to the latents according to the noise magnitude at each timestep
46744689
# (this is the forward diffusion process)
46754690
if args.ip_noise_gamma:
4676-
noisy_latents = noise_scheduler.add_noise(latents, noise + args.ip_noise_gamma * torch.randn_like(latents), timesteps)
4691+
if args.ip_noise_gamma_random_strength:
4692+
strength = torch.rand(1, device=latents.device) * args.ip_noise_gamma
4693+
else:
4694+
strength = args.ip_noise_gamma
4695+
noisy_latents = noise_scheduler.add_noise(latents, noise + strength * torch.randn_like(latents), timesteps)
46774696
else:
46784697
noisy_latents = noise_scheduler.add_noise(latents, noise, timesteps)
46794698

0 commit comments

Comments
 (0)