| import os |
| import os.path as osp |
| import shutil |
| import sys |
| from pathlib import Path |
|
|
| import av |
| import numpy as np |
| import torch |
| import torchvision |
| from einops import rearrange |
| from PIL import Image |
|
|
|
|
| def seed_everything(seed): |
| import random |
| import numpy as np |
|
|
| torch.manual_seed(seed) |
| torch.cuda.manual_seed_all(seed) |
| np.random.seed(seed % (2**32)) |
| random.seed(seed) |
|
|
| def delete_additional_ckpt(base_path, num_keep): |
| dirs = [] |
| for d in os.listdir(base_path): |
| if d.startswith("checkpoint-"): |
| dirs.append(d) |
| num_tot = len(dirs) |
| if num_tot <= num_keep: |
| return |
| |
| del_dirs = sorted(dirs, key=lambda x: int(x.split("-")[-1]))[: num_tot - num_keep] |
| for d in del_dirs: |
| path_to_dir = osp.join(base_path, d) |
| if osp.exists(path_to_dir): |
| shutil.rmtree(path_to_dir) |
| |
| def compute_snr(noise_scheduler, timesteps): |
| """ |
| Computes SNR as per |
| https://github.com/TiankaiHang/Min-SNR-Diffusion-Training/blob/521b624bd70c67cee4bdf49225915f5945a872e3/guided_diffusion/gaussian_diffusion.py#L847-L849 |
| """ |
| alphas_cumprod = noise_scheduler.alphas_cumprod |
| sqrt_alphas_cumprod = alphas_cumprod**0.5 |
| sqrt_one_minus_alphas_cumprod = (1.0 - alphas_cumprod) ** 0.5 |
|
|
| |
| |
| sqrt_alphas_cumprod = sqrt_alphas_cumprod.to(device=timesteps.device)[ |
| timesteps |
| ].float() |
| while len(sqrt_alphas_cumprod.shape) < len(timesteps.shape): |
| sqrt_alphas_cumprod = sqrt_alphas_cumprod[..., None] |
| alpha = sqrt_alphas_cumprod.expand(timesteps.shape) |
|
|
| sqrt_one_minus_alphas_cumprod = sqrt_one_minus_alphas_cumprod.to( |
| device=timesteps.device |
| )[timesteps].float() |
| while len(sqrt_one_minus_alphas_cumprod.shape) < len(timesteps.shape): |
| sqrt_one_minus_alphas_cumprod = sqrt_one_minus_alphas_cumprod[..., None] |
| sigma = sqrt_one_minus_alphas_cumprod.expand(timesteps.shape) |
|
|
| |
| snr = (alpha / sigma) ** 2 |
| return snr |
| |