multimodalart/EchoMimic-zero
8
1import torch2import numpy as np3 4def get_alpha(alphas_cumprod, timestep):5 timestep_lt_zero_mask = torch.lt(timestep, 0).to(alphas_cumprod.dtype)6 normal_alpha = alphas_cumprod[torch.clip(timestep, 0)]7 one_alpha = torch.ones_like(normal_alpha).to(normal_alpha.dtype).to(normal_alpha.dtype) 8 return normal_alpha * (1 - timestep_lt_zero_mask) + one_alpha * timestep_lt_zero_mask9 10def psuedo_velocity_wrt_noisy_and_timestep(noisy_images, noisy_images_pre, alphas_cumprod, timestep, timestep_prev):11 alpha_prod_t = get_alpha(alphas_cumprod, timestep).view(-1, 1, 1, 1, 1).detach()12 beta_prod_t = 1 - alpha_prod_t13 alpha_prod_t_prev = get_alpha(alphas_cumprod, timestep_prev).view(-1, 1, 1, 1, 1).detach()14 beta_prod_t_prev = 1 - alpha_prod_t_prev15 16 a_s = (alpha_prod_t_prev ** (0.5)).to(noisy_images.dtype)17 a_t = (alpha_prod_t ** (0.5)).to(noisy_images.dtype)18 b_s = (beta_prod_t_prev ** (0.5)).to(noisy_images.dtype)19 b_t = (beta_prod_t ** (0.5)).to(noisy_images.dtype)20 21 psuedo_velocity = (noisy_images_pre - (22 a_s * a_t + b_s * b_t23 ) * noisy_images) / (24 b_s * a_t - a_s * b_t25 )26 27 return psuedo_velocity28 29def origin_by_velocity_and_sample(velocity, noisy_images, alphas_cumprod, timestep):30 alpha_prod_t = get_alpha(alphas_cumprod, timestep).view(-1, 1, 1, 1, 1).detach()31 beta_prod_t = 1 - alpha_prod_t32 a_t = (alpha_prod_t ** (0.5)).to(noisy_images.dtype)33 b_t = (beta_prod_t ** (0.5)).to(noisy_images.dtype)34 35 pred_original_sample = a_t * noisy_images - b_t * velocity36 return pred_original_sample37 