-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpredict.py
61 lines (46 loc) · 1.57 KB
/
predict.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
import argparse
import os
import librosa
import numpy as np
import soundfile
import torch
from model.waveunet import WaveUNet
def main(args):
path_model = os.path.expanduser(args.path_model)
path_song = os.path.expanduser(args.path_song)
path_to_save = os.path.normpath(args.path_to_save)
os.makedirs(path_to_save, exist_ok=True)
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using {device} device")
model = WaveUNet().to(device)
model.load_state_dict(torch.load(path_model))
song_np, _ = librosa.load(path_song)
song = torch.Tensor(song_np.reshape(1, 1, -1)).to(device)
model.eval()
pred = None
with torch.no_grad():
sep_length = 16384
start = 0
while start < song.shape[2]:
if pred == None:
pred = model(song[:, :, start : start + sep_length])
else:
pred = torch.cat(
(pred, model(song[:, :, start : start + sep_length])), 2
)
start += sep_length
pred = pred.cpu()
for i in range(4):
soundfile.write(
os.path.join(path_to_save, f"song_out_{i}.wav"),
pred[0][i],
22050,
format="wav",
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="predict")
parser.add_argument("--path_model", type=str, default="model.pt")
parser.add_argument("--path_song", type=str, required=True)
parser.add_argument("--path_to_save", type=str, default="./output")
args = parser.parse_args()
main(args)