forked from Stanford-TML/EDGE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
128 lines (113 loc) · 4.78 KB
/
test.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import glob
import os
from functools import cmp_to_key
from pathlib import Path
from tempfile import TemporaryDirectory
import random
import jukemirlib
import numpy as np
import torch
from tqdm import tqdm
from args import parse_test_opt
from data.slice import slice_audio
from EDGE import EDGE
from data.audio_extraction.baseline_features import extract as baseline_extract
from data.audio_extraction.jukebox_features import extract as juke_extract
# sort filenames that look like songname_slice{number}.ext
key_func = lambda x: int(os.path.splitext(x)[0].split("_")[-1].split("slice")[-1])
def stringintcmp_(a, b):
aa, bb = "".join(a.split("_")[:-1]), "".join(b.split("_")[:-1])
ka, kb = key_func(a), key_func(b)
if aa < bb:
return -1
if aa > bb:
return 1
if ka < kb:
return -1
if ka > kb:
return 1
return 0
stringintkey = cmp_to_key(stringintcmp_)
def test(opt):
feature_func = juke_extract if opt.feature_type == "jukebox" else baseline_extract
sample_length = opt.out_length
sample_size = int(sample_length / 2.5) - 1
temp_dir_list = []
all_cond = []
all_filenames = []
if opt.use_cached_features:
print("Using precomputed features")
# all subdirectories
dir_list = glob.glob(os.path.join(opt.feature_cache_dir, "*/"))
for dir in dir_list:
file_list = sorted(glob.glob(f"{dir}/*.wav"), key=stringintkey)
juke_file_list = sorted(glob.glob(f"{dir}/*.npy"), key=stringintkey)
assert len(file_list) == len(juke_file_list)
# random chunk after sanity check
rand_idx = random.randint(0, len(file_list) - sample_size)
file_list = file_list[rand_idx : rand_idx + sample_size]
juke_file_list = juke_file_list[rand_idx : rand_idx + sample_size]
cond_list = [np.load(x) for x in juke_file_list]
all_filenames.append(file_list)
all_cond.append(torch.from_numpy(np.array(cond_list)))
else:
print("Computing features for input music")
for wav_file in glob.glob(os.path.join(opt.music_dir, "*.wav")):
# create temp folder (or use the cache folder if specified)
if opt.cache_features:
songname = os.path.splitext(os.path.basename(wav_file))[0]
save_dir = os.path.join(opt.feature_cache_dir, songname)
Path(save_dir).mkdir(parents=True, exist_ok=True)
dirname = save_dir
else:
temp_dir = TemporaryDirectory()
temp_dir_list.append(temp_dir)
dirname = temp_dir.name
# slice the audio file
print(f"Slicing {wav_file}")
slice_audio(wav_file, 2.5, 5.0, dirname)
file_list = sorted(glob.glob(f"{dirname}/*.wav"), key=stringintkey)
# randomly sample a chunk of length at most sample_size
rand_idx = random.randint(0, len(file_list) - sample_size)
cond_list = []
# generate juke representations
print(f"Computing features for {wav_file}")
for idx, file in enumerate(tqdm(file_list)):
# if not caching then only calculate for the interested range
if (not opt.cache_features) and (not (rand_idx <= idx < rand_idx + sample_size)):
continue
# audio = jukemirlib.load_audio(file)
# reps = jukemirlib.extract(
# audio, layers=[66], downsample_target_rate=30
# )[66]
reps, _ = feature_func(file)
# save reps
if opt.cache_features:
featurename = os.path.splitext(file)[0] + ".npy"
np.save(featurename, reps)
# if in the random range, put it into the list of reps we want
# to actually use for generation
if rand_idx <= idx < rand_idx + sample_size:
cond_list.append(reps)
cond_list = torch.from_numpy(np.array(cond_list))
all_cond.append(cond_list)
all_filenames.append(file_list[rand_idx : rand_idx + sample_size])
model = EDGE(opt.feature_type, opt.checkpoint)
model.eval()
# directory for optionally saving the dances for eval
fk_out = None
if opt.save_motions:
fk_out = opt.motion_save_dir
print("Generating dances")
for i in range(len(all_cond)):
data_tuple = None, all_cond[i], all_filenames[i]
model.render_sample(
data_tuple, "test", opt.render_dir, render_count=-1, fk_out=fk_out, render=not opt.no_render
)
print("Done")
torch.cuda.empty_cache()
for temp_dir in temp_dir_list:
temp_dir.cleanup()
if __name__ == "__main__":
opt = parse_test_opt()
test(opt)