-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_from_model.py
65 lines (46 loc) · 1.69 KB
/
generate_from_model.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
# generate music using a particular model
from model import SampleRNN, Predictor, Generator
import os, sys, time
import torch
from librosa.output import write_wav
# edit the modeldir and other variables below
modeldir = "colab_results/"
modelname = "ep1-it625"
audio_pref = "r001_{}"
save_raw = False
n_samples = 1
sample_length = 800
sample_rate = 16000
samples_path = os.path.join(modeldir, "gen_samples")
os.makedirs(samples_path, exist_ok=True)
# sys.stderr.write("available models are: {}".format(listdir(modeldir)))
modelpath = os.path.join(modeldir, modelname)
srnn_model1 = SampleRNN(frame_sizes=[4, 16], n_rnn=2, dim=1024, learn_h0=True,
q_levels=256, weight_norm=True)
if torch.cuda.is_available():
srnn_model1 = srnn_model1.cuda()
predictor1 = Predictor(srnn_model1)
if torch.cuda.is_available():
predictor1 = predictor1.cuda()
if torch.cuda.is_available():
predictor1.load_state_dict(torch.load(modelpath)['model'])
else:
predictor1.load_state_dict(torch.load(modelpath, map_location='cpu')['model'])
print("model loaded successfully!")
generate = Generator(srnn_model1, True)
import time
s_time = time.time()
sys.stderr.write("Generating {} sequences, each of length {}."\
.format(n_samples, sample_length))
samples = generate(n_samples, sample_length).cpu().float().numpy()
sys.stderr.write("Total time taken = {}".format(time.time() - s_time))
for i in range(n_samples):
if save_raw:
samples.tofile('debug_seq_{}.csv'.format(i),
sep=',', format='%10.5f')
write_wav(
os.path.join(
samples_path, audio_pref.format(i + 1)
),
samples[i, :], sr=sample_rate, norm=True
)