forked from acids-ircam/RAVE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconstruct.py
78 lines (57 loc) · 1.81 KB
/
reconstruct.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import torch
torch.set_grad_enabled(False)
from tqdm import tqdm
from rave import RAVE
from rave.core import search_for_run
from effortless_config import Config
from os import path, makedirs, environ
from pathlib import Path
import librosa as li
import GPUtil as gpu
import soundfile as sf
class args(Config):
CKPT = None # PATH TO YOUR PRETRAINED CHECKPOINT
WAV_FOLDER = None # PATH TO YOUR WAV FOLDER
OUT = "./reconstruction/"
args.parse_args()
# GPU DISCOVERY
CUDA = gpu.getAvailable(maxMemory=.05)
if len(CUDA):
environ["CUDA_VISIBLE_DEVICES"] = str(CUDA[0])
use_gpu = 1
elif torch.cuda.is_available():
print("Cuda is available but no fully free GPU found.")
print("Reconstruction may be slower due to concurrent processes.")
use_gpu = 1
else:
print("No GPU found.")
use_gpu = 0
device = torch.device("cuda:0" if use_gpu else "cpu")
# LOAD RAVE
rave = RAVE.load_from_checkpoint(
search_for_run(args.CKPT),
strict=False,
).eval().to(device)
# COMPUTE LATENT COMPRESSION RATIO
x = torch.randn(1, 1, 2**14).to(device)
z = rave.encode(x)
ratio = x.shape[-1] // z.shape[-1]
# SEARCH FOR WAV FILES
audios = tqdm(list(Path(args.WAV_FOLDER).rglob("*.wav")))
# RECONSTRUCTION
makedirs(args.OUT, exist_ok=True)
for audio in audios:
audio_name = path.splitext(path.basename(audio))[0]
audios.set_description(audio_name)
# LOAD AUDIO TO TENSOR
x, sr = li.load(audio, sr=rave.sr)
x = torch.from_numpy(x).reshape(1, 1, -1).float().to(device)
# PAD AUDIO
n_sample = x.shape[-1]
pad = (ratio - (n_sample % ratio)) % ratio
x = torch.nn.functional.pad(x, (0, pad))
# ENCODE / DECODE
y = rave.decode(rave.encode(x))
y = y.reshape(-1).cpu().numpy()[:n_sample]
# WRITE AUDIO
sf.write(path.join(args.OUT, f"{audio_name}_reconstruction.wav"), y, sr)