Spaces:
Running on Zero
Running on Zero
Vansh Chugh commited on
Commit ·
191938e
1
Parent(s): a95f6c0
remove dead code
Browse files- .gitignore +0 -1
- networks/ncsnpp.py +0 -5
- networks/ncsnpp_utils/utils.py +0 -225
- testing/Sampler.py +0 -14
- utils/reverb_utils.py +0 -37
.gitignore
CHANGED
|
@@ -1,3 +1,2 @@
|
|
| 1 |
__pycache__/
|
| 2 |
*.pyc
|
| 3 |
-
DEPLOY.md
|
|
|
|
| 1 |
__pycache__/
|
| 2 |
*.pyc
|
|
|
networks/ncsnpp.py
CHANGED
|
@@ -273,11 +273,6 @@ class NCSNpp(nn.Module):
|
|
| 273 |
|
| 274 |
self.all_modules = nn.ModuleList(modules)
|
| 275 |
|
| 276 |
-
@staticmethod
|
| 277 |
-
def add_argparse_args(parser):
|
| 278 |
-
# parser.add_argument("--no-centered", dest="centered", action="store_false", help="The data is not centered")
|
| 279 |
-
return parser
|
| 280 |
-
|
| 281 |
def forward(self, x, time_cond=None):
|
| 282 |
"""
|
| 283 |
- x: b,2*D,F,T: contains x and y OR x: b,D,F,T contains only x
|
|
|
|
| 273 |
|
| 274 |
self.all_modules = nn.ModuleList(modules)
|
| 275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
def forward(self, x, time_cond=None):
|
| 277 |
"""
|
| 278 |
- x: b,2*D,F,T: contains x and y OR x: b,D,F,T contains only x
|
networks/ncsnpp_utils/utils.py
DELETED
|
@@ -1,225 +0,0 @@
|
|
| 1 |
-
# coding=utf-8
|
| 2 |
-
# Copyright 2020 The Google Research Authors.
|
| 3 |
-
#
|
| 4 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
-
# you may not use this file except in compliance with the License.
|
| 6 |
-
# You may obtain a copy of the License at
|
| 7 |
-
#
|
| 8 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
-
#
|
| 10 |
-
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
-
# See the License for the specific language governing permissions and
|
| 14 |
-
# limitations under the License.
|
| 15 |
-
|
| 16 |
-
"""All functions and modules related to model definition.
|
| 17 |
-
"""
|
| 18 |
-
|
| 19 |
-
import torch
|
| 20 |
-
#import sde_lib
|
| 21 |
-
import numpy as np
|
| 22 |
-
from ...sdes import *
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
_MODELS = {}
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
def variance_scaling(scale, mode, distribution,
|
| 29 |
-
in_axis=1, out_axis=0,
|
| 30 |
-
dtype=torch.float32,
|
| 31 |
-
device='cpu'):
|
| 32 |
-
"""Ported from JAX. """
|
| 33 |
-
|
| 34 |
-
def _compute_fans(shape, in_axis=1, out_axis=0):
|
| 35 |
-
receptive_field_size = np.prod(shape) / shape[in_axis] / shape[out_axis]
|
| 36 |
-
fan_in = shape[in_axis] * receptive_field_size
|
| 37 |
-
fan_out = shape[out_axis] * receptive_field_size
|
| 38 |
-
return fan_in, fan_out
|
| 39 |
-
|
| 40 |
-
def init(shape, dtype=dtype, device=device):
|
| 41 |
-
fan_in, fan_out = _compute_fans(shape, in_axis, out_axis)
|
| 42 |
-
if mode == "fan_in":
|
| 43 |
-
denominator = fan_in
|
| 44 |
-
elif mode == "fan_out":
|
| 45 |
-
denominator = fan_out
|
| 46 |
-
elif mode == "fan_avg":
|
| 47 |
-
denominator = (fan_in + fan_out) / 2
|
| 48 |
-
else:
|
| 49 |
-
raise ValueError(
|
| 50 |
-
"invalid mode for variance scaling initializer: {}".format(mode))
|
| 51 |
-
variance = scale / denominator
|
| 52 |
-
if distribution == "normal":
|
| 53 |
-
return torch.randn(*shape, dtype=dtype, device=device) * np.sqrt(variance)
|
| 54 |
-
elif distribution == "uniform":
|
| 55 |
-
return (torch.rand(*shape, dtype=dtype, device=device) * 2. - 1.) * np.sqrt(3 * variance)
|
| 56 |
-
else:
|
| 57 |
-
raise ValueError("invalid distribution for variance scaling initializer")
|
| 58 |
-
|
| 59 |
-
return init
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
def register_model(cls=None, *, name=None):
|
| 63 |
-
"""A decorator for registering model classes."""
|
| 64 |
-
|
| 65 |
-
def _register(cls):
|
| 66 |
-
if name is None:
|
| 67 |
-
local_name = cls.__name__
|
| 68 |
-
else:
|
| 69 |
-
local_name = name
|
| 70 |
-
if local_name in _MODELS:
|
| 71 |
-
raise ValueError(f'Already registered model with name: {local_name}')
|
| 72 |
-
_MODELS[local_name] = cls
|
| 73 |
-
return cls
|
| 74 |
-
|
| 75 |
-
if cls is None:
|
| 76 |
-
return _register
|
| 77 |
-
else:
|
| 78 |
-
return _register(cls)
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
def get_model(name):
|
| 82 |
-
return _MODELS[name]
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
def get_sigmas(sigma_min, sigma_max, num_scales):
|
| 86 |
-
"""Get sigmas --- the set of noise levels for SMLD from config files.
|
| 87 |
-
Args:
|
| 88 |
-
config: A ConfigDict object parsed from the config file
|
| 89 |
-
Returns:
|
| 90 |
-
sigmas: a jax numpy arrary of noise levels
|
| 91 |
-
"""
|
| 92 |
-
sigmas = np.exp(
|
| 93 |
-
np.linspace(np.log(sigma_max), np.log(sigma_min), num_scales))
|
| 94 |
-
|
| 95 |
-
return sigmas
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
def get_ddpm_params(config):
|
| 99 |
-
"""Get betas and alphas --- parameters used in the original DDPM paper."""
|
| 100 |
-
num_diffusion_timesteps = 1000
|
| 101 |
-
# parameters need to be adapted if number of time steps differs from 1000
|
| 102 |
-
beta_start = config.model.beta_min / config.model.num_scales
|
| 103 |
-
beta_end = config.model.beta_max / config.model.num_scales
|
| 104 |
-
betas = np.linspace(beta_start, beta_end, num_diffusion_timesteps, dtype=np.float64)
|
| 105 |
-
|
| 106 |
-
alphas = 1. - betas
|
| 107 |
-
alphas_cumprod = np.cumprod(alphas, axis=0)
|
| 108 |
-
sqrt_alphas_cumprod = np.sqrt(alphas_cumprod)
|
| 109 |
-
sqrt_1m_alphas_cumprod = np.sqrt(1. - alphas_cumprod)
|
| 110 |
-
|
| 111 |
-
return {
|
| 112 |
-
'betas': betas,
|
| 113 |
-
'alphas': alphas,
|
| 114 |
-
'alphas_cumprod': alphas_cumprod,
|
| 115 |
-
'sqrt_alphas_cumprod': sqrt_alphas_cumprod,
|
| 116 |
-
'sqrt_1m_alphas_cumprod': sqrt_1m_alphas_cumprod,
|
| 117 |
-
'beta_min': beta_start * (num_diffusion_timesteps - 1),
|
| 118 |
-
'beta_max': beta_end * (num_diffusion_timesteps - 1),
|
| 119 |
-
'num_diffusion_timesteps': num_diffusion_timesteps
|
| 120 |
-
}
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
def create_model(config):
|
| 124 |
-
"""Create the score model."""
|
| 125 |
-
model_name = config.model.name
|
| 126 |
-
score_model = get_model(model_name)(config)
|
| 127 |
-
score_model = score_model.to(config.device)
|
| 128 |
-
score_model = torch.nn.DataParallel(score_model)
|
| 129 |
-
return score_model
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
def get_model_fn(model, train=False):
|
| 133 |
-
"""Create a function to give the output of the score-based model.
|
| 134 |
-
|
| 135 |
-
Args:
|
| 136 |
-
model: The score model.
|
| 137 |
-
train: `True` for training and `False` for evaluation.
|
| 138 |
-
|
| 139 |
-
Returns:
|
| 140 |
-
A model function.
|
| 141 |
-
"""
|
| 142 |
-
|
| 143 |
-
def model_fn(x, labels):
|
| 144 |
-
"""Compute the output of the score-based model.
|
| 145 |
-
|
| 146 |
-
Args:
|
| 147 |
-
x: A mini-batch of input data.
|
| 148 |
-
labels: A mini-batch of conditioning variables for time steps. Should be interpreted differently
|
| 149 |
-
for different models.
|
| 150 |
-
|
| 151 |
-
Returns:
|
| 152 |
-
A tuple of (model output, new mutable states)
|
| 153 |
-
"""
|
| 154 |
-
if not train:
|
| 155 |
-
model.eval()
|
| 156 |
-
return model(x, labels)
|
| 157 |
-
else:
|
| 158 |
-
model.train()
|
| 159 |
-
return model(x, labels)
|
| 160 |
-
|
| 161 |
-
return model_fn
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
def get_score_fn(sde, model, train=False, continuous=False):
|
| 165 |
-
"""Wraps `score_fn` so that the model output corresponds to a real time-dependent score function.
|
| 166 |
-
|
| 167 |
-
Args:
|
| 168 |
-
sde: An `sde_lib.SDE` object that represents the forward SDE.
|
| 169 |
-
model: A score model.
|
| 170 |
-
train: `True` for training and `False` for evaluation.
|
| 171 |
-
continuous: If `True`, the score-based model is expected to directly take continuous time steps.
|
| 172 |
-
|
| 173 |
-
Returns:
|
| 174 |
-
A score function.
|
| 175 |
-
"""
|
| 176 |
-
model_fn = get_model_fn(model, train=train)
|
| 177 |
-
|
| 178 |
-
#if isinstance(sde, sde_lib.VPSDE) or isinstance(sde, sde_lib.subVPSDE):
|
| 179 |
-
if isinstance(sde, OUVPSDE):
|
| 180 |
-
def score_fn(x, t):
|
| 181 |
-
# Scale neural network output by standard deviation and flip sign
|
| 182 |
-
if continuous or isinstance(sde, sde_lib.subVPSDE):
|
| 183 |
-
# For VP-trained models, t=0 corresponds to the lowest noise level
|
| 184 |
-
# The maximum value of time embedding is assumed to 999 for
|
| 185 |
-
# continuously-trained models.
|
| 186 |
-
labels = t * 999
|
| 187 |
-
score = model_fn(x, labels)
|
| 188 |
-
std = sde.marginal_prob(torch.zeros_like(x), t)[1]
|
| 189 |
-
else:
|
| 190 |
-
# For VP-trained models, t=0 corresponds to the lowest noise level
|
| 191 |
-
labels = t * (sde.N - 1)
|
| 192 |
-
score = model_fn(x, labels)
|
| 193 |
-
std = sde.sqrt_1m_alphas_cumprod.to(labels.device)[labels.long()]
|
| 194 |
-
|
| 195 |
-
score = -score / std[:, None, None, None]
|
| 196 |
-
return score
|
| 197 |
-
|
| 198 |
-
#elif isinstance(sde, sde_lib.VESDE):
|
| 199 |
-
elif isinstance(sde, OUVESDE):
|
| 200 |
-
def score_fn(x, t):
|
| 201 |
-
if continuous:
|
| 202 |
-
labels = sde.marginal_prob(torch.zeros_like(x), t)[1]
|
| 203 |
-
else:
|
| 204 |
-
# For VE-trained models, t=0 corresponds to the highest noise level
|
| 205 |
-
labels = sde.T - t
|
| 206 |
-
labels *= sde.N - 1
|
| 207 |
-
labels = torch.round(labels).long()
|
| 208 |
-
|
| 209 |
-
score = model_fn(x, labels)
|
| 210 |
-
return score
|
| 211 |
-
|
| 212 |
-
else:
|
| 213 |
-
raise NotImplementedError(f"SDE class {sde.__class__.__name__} not yet supported.")
|
| 214 |
-
|
| 215 |
-
return score_fn
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
def to_flattened_numpy(x):
|
| 219 |
-
"""Flatten a torch tensor `x` and convert it to numpy."""
|
| 220 |
-
return x.detach().cpu().numpy().reshape((-1,))
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
def from_flattened_numpy(x, shape):
|
| 224 |
-
"""Form a torch tensor with the given `shape` from a flattened numpy array `x`."""
|
| 225 |
-
return torch.from_numpy(x.reshape(shape))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
@@ -71,16 +70,3 @@ class Sampler():
|
|
| 71 |
x_hat = self.diff_params.denoiser(x.unsqueeze(1), self.model, t_i).squeeze(1)
|
| 72 |
return x_hat
|
| 73 |
|
| 74 |
-
class NoSampler(Sampler):
|
| 75 |
-
|
| 76 |
-
def predict(self, *args, **kwargs):
|
| 77 |
-
return None
|
| 78 |
-
|
| 79 |
-
def predict_unconditional(self, *args, **kwargs):
|
| 80 |
-
return None
|
| 81 |
-
|
| 82 |
-
def predict_conditional(self, *args, **kwargs):
|
| 83 |
-
return None
|
| 84 |
-
|
| 85 |
-
def step(self, *args, **kwargs):
|
| 86 |
-
return None
|
|
|
|
| 1 |
|
|
|
|
| 2 |
import torch
|
| 3 |
import abc
|
| 4 |
|
|
|
|
| 70 |
x_hat = self.diff_params.denoiser(x.unsqueeze(1), self.model, t_i).squeeze(1)
|
| 71 |
return x_hat
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
utils/reverb_utils.py
CHANGED
|
@@ -22,40 +22,3 @@ def minimum_phase_version(h):
|
|
| 22 |
minimum_phase_h = minimum_phase_h[: -T_orig]
|
| 23 |
return minimum_phase_h
|
| 24 |
|
| 25 |
-
def fast_apply_RIR(y, filter, rm_delay=False, zero_pad=False):
|
| 26 |
-
|
| 27 |
-
if rm_delay:
|
| 28 |
-
filter = filter[ torch.argmax(filter): ]
|
| 29 |
-
|
| 30 |
-
filter = filter.unsqueeze(0).unsqueeze(0)
|
| 31 |
-
B = filter.to(y.device)
|
| 32 |
-
y = y.unsqueeze(1)
|
| 33 |
-
|
| 34 |
-
# Get the size of the input signal and filter
|
| 35 |
-
N = y.size(2)
|
| 36 |
-
M = filter.size(2)
|
| 37 |
-
|
| 38 |
-
# Compute the size of the FFT
|
| 39 |
-
if zero_pad:
|
| 40 |
-
fft_size=torch.tensor(2*N+2*M-1)
|
| 41 |
-
else:
|
| 42 |
-
fft_size=torch.tensor(N+M-1)
|
| 43 |
-
fft_size=int(2**torch.ceil(torch.log2(fft_size)))
|
| 44 |
-
|
| 45 |
-
# Perform FFT on the input signal and filter
|
| 46 |
-
Y = torch.fft.fft(y, fft_size, dim=2)
|
| 47 |
-
H = torch.fft.fft(B, fft_size, dim=2)
|
| 48 |
-
|
| 49 |
-
# Perform element-wise multiplication in the frequency domain
|
| 50 |
-
Y_conv = Y * H
|
| 51 |
-
|
| 52 |
-
# Perform inverse FFT to get the convolution result
|
| 53 |
-
y_conv = torch.fft.ifft(Y_conv, fft_size, dim=2)
|
| 54 |
-
|
| 55 |
-
# Take the real part of the result
|
| 56 |
-
y_conv = y_conv[:, :, :N].real
|
| 57 |
-
|
| 58 |
-
# Squeeze the unnecessary dimensions
|
| 59 |
-
y_conv = y_conv.squeeze(1)
|
| 60 |
-
|
| 61 |
-
return y_conv
|
|
|
|
| 22 |
minimum_phase_h = minimum_phase_h[: -T_orig]
|
| 23 |
return minimum_phase_h
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|