mirror of
https://github.com/kohya-ss/sd-scripts.git
synced 2026-04-08 22:35:09 +00:00
update readme, add metadata for network module
This commit is contained in:
@@ -3241,20 +3241,21 @@ def add_training_arguments(parser: argparse.ArgumentParser, support_dreambooth:
|
||||
type=str,
|
||||
default="l2",
|
||||
choices=["l2", "huber", "smooth_l1"],
|
||||
help="The type of loss to use and whether it's scheduled based on the timestep"
|
||||
help="The type of loss function to use (L2, Huber, or smooth L1), default is L2 / 使用する損失関数の種類(L2、Huber、またはsmooth L1)、デフォルトはL2",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--huber_schedule",
|
||||
type=str,
|
||||
default="exponential",
|
||||
choices=["constant", "exponential", "snr"],
|
||||
help="The type of loss to use and whether it's scheduled based on the timestep"
|
||||
help="The scheduling method for Huber loss (constant, exponential, or SNR-based). Only used when loss_type is 'huber' or 'smooth_l1'. default is exponential"
|
||||
+ " / Huber損失のスケジューリング方法(constant、exponential、またはSNRベース)。loss_typeが'huber'または'smooth_l1'の場合に有効、デフォルトはexponential",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--huber_c",
|
||||
type=float,
|
||||
default=0.1,
|
||||
help="The huber loss parameter. Only used if one of the huber loss modes (huber or smooth l1) is selected with loss_type.",
|
||||
help="The huber loss parameter. Only used if one of the huber loss modes (huber or smooth l1) is selected with loss_type. default is 0.1 / Huber損失のパラメータ。loss_typeがhuberまたはsmooth l1の場合に有効。デフォルトは0.1",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
@@ -4862,39 +4863,39 @@ def save_sd_model_on_train_end_common(
|
||||
if args.huggingface_repo_id is not None:
|
||||
huggingface_util.upload(args, out_dir, "/" + model_name, force_sync_upload=True)
|
||||
|
||||
|
||||
def get_timesteps_and_huber_c(args, min_timestep, max_timestep, noise_scheduler, b_size, device):
|
||||
|
||||
#TODO: if a huber loss is selected, it will use constant timesteps for each batch
|
||||
# TODO: if a huber loss is selected, it will use constant timesteps for each batch
|
||||
# as. In the future there may be a smarter way
|
||||
|
||||
if args.loss_type == 'huber' or args.loss_type == 'smooth_l1':
|
||||
timesteps = torch.randint(
|
||||
min_timestep, max_timestep, (1,), device='cpu'
|
||||
)
|
||||
if args.loss_type == "huber" or args.loss_type == "smooth_l1":
|
||||
timesteps = torch.randint(min_timestep, max_timestep, (1,), device="cpu")
|
||||
timestep = timesteps.item()
|
||||
|
||||
if args.huber_schedule == "exponential":
|
||||
alpha = - math.log(args.huber_c) / noise_scheduler.config.num_train_timesteps
|
||||
alpha = -math.log(args.huber_c) / noise_scheduler.config.num_train_timesteps
|
||||
huber_c = math.exp(-alpha * timestep)
|
||||
elif args.huber_schedule == "snr":
|
||||
alphas_cumprod = noise_scheduler.alphas_cumprod[timestep]
|
||||
sigmas = ((1.0 - alphas_cumprod) / alphas_cumprod) ** 0.5
|
||||
huber_c = (1 - args.huber_c) / (1 + sigmas)**2 + args.huber_c
|
||||
huber_c = (1 - args.huber_c) / (1 + sigmas) ** 2 + args.huber_c
|
||||
elif args.huber_schedule == "constant":
|
||||
huber_c = args.huber_c
|
||||
else:
|
||||
raise NotImplementedError(f'Unknown Huber loss schedule {args.huber_schedule}!')
|
||||
raise NotImplementedError(f"Unknown Huber loss schedule {args.huber_schedule}!")
|
||||
|
||||
timesteps = timesteps.repeat(b_size).to(device)
|
||||
elif args.loss_type == 'l2':
|
||||
elif args.loss_type == "l2":
|
||||
timesteps = torch.randint(min_timestep, max_timestep, (b_size,), device=device)
|
||||
huber_c = 1 # may be anything, as it's not used
|
||||
huber_c = 1 # may be anything, as it's not used
|
||||
else:
|
||||
raise NotImplementedError(f'Unknown loss type {args.loss_type}')
|
||||
raise NotImplementedError(f"Unknown loss type {args.loss_type}")
|
||||
timesteps = timesteps.long()
|
||||
|
||||
return timesteps, huber_c
|
||||
|
||||
|
||||
def get_noise_noisy_latents_and_timesteps(args, noise_scheduler, latents):
|
||||
# Sample noise that we'll add to the latents
|
||||
noise = torch.randn_like(latents, device=latents.device)
|
||||
@@ -4929,27 +4930,31 @@ def get_noise_noisy_latents_and_timesteps(args, noise_scheduler, latents):
|
||||
|
||||
return noise, noisy_latents, timesteps, huber_c
|
||||
|
||||
|
||||
# NOTE: if you're using the scheduled version, huber_c has to depend on the timesteps already
|
||||
def conditional_loss(model_pred:torch.Tensor, target:torch.Tensor, reduction:str="mean", loss_type:str="l2", huber_c:float=0.1):
|
||||
|
||||
if loss_type == 'l2':
|
||||
def conditional_loss(
|
||||
model_pred: torch.Tensor, target: torch.Tensor, reduction: str = "mean", loss_type: str = "l2", huber_c: float = 0.1
|
||||
):
|
||||
|
||||
if loss_type == "l2":
|
||||
loss = torch.nn.functional.mse_loss(model_pred, target, reduction=reduction)
|
||||
elif loss_type == 'huber':
|
||||
elif loss_type == "huber":
|
||||
loss = 2 * huber_c * (torch.sqrt((model_pred - target) ** 2 + huber_c**2) - huber_c)
|
||||
if reduction == "mean":
|
||||
loss = torch.mean(loss)
|
||||
elif reduction == "sum":
|
||||
loss = torch.sum(loss)
|
||||
elif loss_type == 'smooth_l1':
|
||||
elif loss_type == "smooth_l1":
|
||||
loss = 2 * (torch.sqrt((model_pred - target) ** 2 + huber_c**2) - huber_c)
|
||||
if reduction == "mean":
|
||||
loss = torch.mean(loss)
|
||||
elif reduction == "sum":
|
||||
loss = torch.sum(loss)
|
||||
else:
|
||||
raise NotImplementedError(f'Unsupported Loss Type {loss_type}')
|
||||
raise NotImplementedError(f"Unsupported Loss Type {loss_type}")
|
||||
return loss
|
||||
|
||||
|
||||
def append_lr_to_logs(logs, lr_scheduler, optimizer_type, including_unet=True):
|
||||
names = []
|
||||
if including_unet:
|
||||
|
||||
Reference in New Issue
Block a user