CoolFace
Apppublic

memef4rmer/edit_anything

sourceHugging Faceccupdated 3y agoView on Hugging Face
0likes
sampling_util.py22 linesDownload Raw Back to diffusion
1import torch2import numpy as np3 4 5def append_dims(x, target_dims):6    """Appends dimensions to the end of a tensor until it has target_dims dimensions.7    From https://github.com/crowsonkb/k-diffusion/blob/master/k_diffusion/utils.py"""8    dims_to_append = target_dims - x.ndim9    if dims_to_append < 0:10        raise ValueError(f'input has {x.ndim} dims but target_dims is {target_dims}, which is less')11    return x[(...,) + (None,) * dims_to_append]12 13 14def norm_thresholding(x0, value):15    s = append_dims(x0.pow(2).flatten(1).mean(1).sqrt().clamp(min=value), x0.ndim)16    return x0 * (value / s)17 18 19def spatial_norm_thresholding(x0, value):20    # b c h w21    s = x0.pow(2).mean(1, keepdim=True).sqrt().clamp(min=value)22    return x0 * (value / s)