Delete dreamvoice/src/utils/.ipynb_checkpoints
Browse files
dreamvoice/src/utils/.ipynb_checkpoints/__init__-checkpoint.py
DELETED
@@ -1 +0,0 @@
|
|
1 |
-
from .utils import *
|
|
|
|
dreamvoice/src/utils/.ipynb_checkpoints/utils-checkpoint.py
DELETED
@@ -1,76 +0,0 @@
|
|
1 |
-
import numpy as np
|
2 |
-
import matplotlib.pyplot as plt
|
3 |
-
from scipy.io import wavfile
|
4 |
-
import torch
|
5 |
-
|
6 |
-
|
7 |
-
def rescale_noise_cfg(noise_cfg, noise_pred_text, guidance_rescale=0.0):
|
8 |
-
"""
|
9 |
-
Rescale `noise_cfg` according to `guidance_rescale`. Based on findings of [Common Diffusion Noise Schedules and
|
10 |
-
Sample Steps are Flawed](https://arxiv.org/pdf/2305.08891.pdf). See Section 3.4
|
11 |
-
"""
|
12 |
-
std_text = noise_pred_text.std(dim=list(range(1, noise_pred_text.ndim)), keepdim=True)
|
13 |
-
std_cfg = noise_cfg.std(dim=list(range(1, noise_cfg.ndim)), keepdim=True)
|
14 |
-
# rescale the results from guidance (fixes overexposure)
|
15 |
-
noise_pred_rescaled = noise_cfg * (std_text / std_cfg)
|
16 |
-
# mix with the original results from guidance by factor guidance_rescale to avoid "plain looking" images
|
17 |
-
noise_cfg = guidance_rescale * noise_pred_rescaled + (1 - guidance_rescale) * noise_cfg
|
18 |
-
return noise_cfg
|
19 |
-
|
20 |
-
|
21 |
-
def scale_shift(x, scale, shift):
|
22 |
-
return (x+shift) * scale
|
23 |
-
|
24 |
-
|
25 |
-
def scale_shift_re(x, scale, shift):
|
26 |
-
return (x/scale) - shift
|
27 |
-
|
28 |
-
|
29 |
-
def align_seq(source, target_length, mapping_method='hard'):
|
30 |
-
source_len = source.shape[1]
|
31 |
-
if mapping_method == 'hard':
|
32 |
-
mapping_idx = np.round(np.arange(target_length) * source_len / target_length)
|
33 |
-
output = source[:, mapping_idx]
|
34 |
-
else:
|
35 |
-
# TBD
|
36 |
-
raise NotImplementedError
|
37 |
-
|
38 |
-
return output
|
39 |
-
|
40 |
-
|
41 |
-
def save_plot(tensor, savepath):
|
42 |
-
tensor = tensor.squeeze().cpu()
|
43 |
-
plt.style.use('default')
|
44 |
-
fig, ax = plt.subplots(figsize=(12, 3))
|
45 |
-
im = ax.imshow(tensor, aspect="auto", origin="lower", interpolation='none')
|
46 |
-
plt.colorbar(im, ax=ax)
|
47 |
-
plt.tight_layout()
|
48 |
-
fig.canvas.draw()
|
49 |
-
plt.savefig(savepath)
|
50 |
-
plt.close()
|
51 |
-
|
52 |
-
|
53 |
-
def save_audio(file_path, sampling_rate, audio):
|
54 |
-
audio = np.clip(audio.cpu().squeeze().numpy(), -0.999, 0.999)
|
55 |
-
wavfile.write(file_path, sampling_rate, (audio * 32767).astype("int16"))
|
56 |
-
|
57 |
-
|
58 |
-
def minmax_norm_diff(tensor: torch.Tensor, vmax: float = 2.5, vmin: float = -12) -> torch.Tensor:
|
59 |
-
tensor = torch.clip(tensor, vmin, vmax)
|
60 |
-
tensor = 2 * (tensor - vmin) / (vmax - vmin) - 1
|
61 |
-
return tensor
|
62 |
-
|
63 |
-
|
64 |
-
def reverse_minmax_norm_diff(tensor: torch.Tensor, vmax: float = 2.5, vmin: float = -12) -> torch.Tensor:
|
65 |
-
tensor = torch.clip(tensor, -1.0, 1.0)
|
66 |
-
tensor = (tensor + 1) / 2
|
67 |
-
tensor = tensor * (vmax - vmin) + vmin
|
68 |
-
return tensor
|
69 |
-
|
70 |
-
|
71 |
-
if __name__ == "__main__":
|
72 |
-
|
73 |
-
a = torch.rand(2, 10)
|
74 |
-
target_len = 15
|
75 |
-
|
76 |
-
b = align_seq(a, target_len)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|