This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.py
86 lines (64 loc) · 2.39 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright (c) 2021 Chanjung Kim. All rights reserved.
# Licensed under the MIT License.
import librosa
import numpy as np
import soundfile as sf
import tensorflow as tf
import youtube_dl
from absl import app
from absl import flags
from pathlib import Path
from os import path, listdir
from tasnet import TasNet, TasNetParam
from dataset import Dataset
FLAGS = flags.FLAGS
flags.DEFINE_string("checkpoint", None,
"Directory containing saved weights", required=True)
flags.DEFINE_string("video_id", None, "YouTube video ID", required=True)
def youtube_dl_hook(d):
if d["status"] == "finished":
print("Done downloading...")
def main(argv):
checkpoint_dir = FLAGS.checkpoint
if not path.exists(checkpoint_dir):
raise ValueError(f"'{checkpoint_dir}' does not exist")
checkpoints = [name for name in listdir(checkpoint_dir) if "ckpt" in name]
if not checkpoints:
raise ValueError(f"No checkpoint exists")
checkpoints.sort()
checkpoint_name = checkpoints[-1].split(".")[0]
param = TasNetParam.load(f"{checkpoint_dir}/config.txt")
print(param.get_config)
tasnet = TasNet.make(param)
tasnet.load_weights(f"{checkpoint_dir}/{checkpoint_name}.ckpt")
video_id = FLAGS.video_id
ydl_opts = {
"format": "bestaudio/best",
"postprocessors": [{
"key": "FFmpegExtractAudio",
"preferredcodec": "wav",
"preferredquality": "44100",
}],
"outtmpl": "%(title)s.wav",
"progress_hooks": [youtube_dl_hook],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(video_id, download=False)
status = ydl.download([video_id])
title = info.get("title", None)
filename = title + ".wav"
audio, sr = librosa.load(filename, sr=44100, mono=True)
num_samples = audio.shape[0]
num_portions = num_samples // (param.K * param.L)
num_samples = num_portions * (param.K * param.L)
print("predicting...")
audio = audio[:num_samples]
audio = np.reshape(audio, [num_portions, param.K, param.L])
separated = tasnet.predict(audio)
separated = np.transpose(separated, (1, 0, 2, 3))
separated = np.reshape(separated, (param.C, num_samples))
print("saving...")
for idx, stem in enumerate(Dataset.STEMS):
sf.write(f"{title}_{stem}.wav", separated[idx], sr)
if __name__ == '__main__':
app.run(main)