forked from richardassar/SampleRNN_torch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_sample.lua
235 lines (192 loc) · 7.45 KB
/
fast_sample.lua
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
--[[
MIT License
Copyright (c) 2017 Richard Assar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]--
require 'rnn'
require 'cudnn'
require 'audio'
require 'SeqGRU_WN'
require 'SeqLSTM_WN'
require 'utils'
--
local cmd = torch.CmdLine()
cmd:text('fast_sample.lua - Samples a model generating a single audio file')
cmd:text('')
cmd:text('Session:')
cmd:option('-session','default','The name of the session in which to locate the model to be sampled')
cmd:text('')
cmd:text('Sampling:')
cmd:option('-sample_length',20,'The duration of generated samples')
cmd:option('-sampling_temperature',1,'The sampling temperature')
cmd:text('')
cmd:text('Output:')
cmd:option('-output_path','sample.wav','The path of the output audio file')
cmd:text('')
local args = cmd:parse(arg)
assert(args.session:len() > 0, "session must be provided")
local session_path = 'sessions/'..args.session
local session = torch.load(session_path..'/session.t7')
for k,v in pairs(session) do
args[k] = v
end
--
local linear_type = args.linear_type
local cudnn_rnn = args.cudnn_rnn
local rnn_type = args.rnn_type
local big_frame_size = args.big_frame_size
local frame_size = args.frame_size
local big_dim = args.hidden_dim
local dim = big_dim
local q_levels = args.q_levels
local q_zero = math.floor(q_levels / 2)
local q_type = args.q_type or 'linear'
local emb_size = args.embedding_size
local dropout = args.dropout
local audio_data_path = 'datasets/'..args.dataset..'/data'
local aud,sample_rate = audio.load(audio_data_path..'/p0001.wav')
local sample_length = args.sample_length*sample_rate
local sampling_temperature = args.sampling_temperature
local output_path = args.output_path
--
local big_rnn, frame_rnn
if cudnn_rnn then
big_rnn = cudnn[rnn_type](big_frame_size, big_dim, 1, true, dropout, true)
frame_rnn = cudnn[rnn_type](dim, dim, 1, true, dropout, true)
else
big_rnn = nn['Seq'..rnn_type..'_WN'](big_frame_size, big_dim)
frame_rnn = nn['Seq'..rnn_type..'_WN'](dim, dim)
big_rnn:remember('both')
frame_rnn:remember('both')
end
local linearType = linear_type == 'WN' and 'LinearWeightNorm' or 'Linear'
local LinearLayer = nn[linearType]
local big_frame_level_rnn = nn.Sequential()
:add(nn.AddConstant(-1))
:add(nn.MulConstant(4/(q_levels-1)))
:add(nn.AddConstant(-2))
:add(nn.View(1,-1):setNumInputDims(1))
:add(big_rnn)
:add(nn.View(-1):setNumInputDims(1))
:add(LinearLayer(big_dim, dim * big_frame_size / frame_size))
:add(nn.View(-1,dim):setNumInputDims(2))
local frame_level_rnn = nn.Sequential()
:add(nn.ParallelTable()
:add(nn.Identity())
:add(nn.Sequential()
:add(nn.AddConstant(-1))
:add(nn.MulConstant(4/(q_levels-1)))
:add(nn.AddConstant(-2))
:add(nn.Contiguous())
:add(LinearLayer(frame_size, dim))
)
)
:add(nn.CAddTable())
:add(nn.View(1,-1):setNumInputDims(1))
:add(frame_rnn)
:add(nn.View(-1):setNumInputDims(1))
:add(LinearLayer(dim, dim * frame_size))
:add(nn.View(-1,dim):setNumInputDims(2))
local sample_level_predictor = nn.Sequential()
:add(nn.ParallelTable()
:add(nn.Identity())
:add(nn.Sequential()
:add(nn.Contiguous())
:add(nn.View(1,-1))
:add(nn.LookupTable(q_levels, emb_size))
:add(nn.View(-1,frame_size*emb_size))
:add(LinearLayer(frame_size*emb_size, dim, false))
)
)
:add(nn.CAddTable())
:add(nn.Bottle(nn.Sequential()
:add(LinearLayer(dim,dim))
:add(cudnn.ReLU())
:add(LinearLayer(dim,dim))
:add(cudnn.ReLU())
:add(LinearLayer(dim,q_levels))
:add(cudnn.LogSoftMax())
))
local net = nn.Sequential()
:add(nn.ParallelTable()
:add(big_frame_level_rnn)
:add(nn.Identity())
:add(nn.Identity())
)
:add(nn.ConcatTable()
:add(nn.Sequential()
:add(nn.ConcatTable()
:add(nn.SelectTable(1))
:add(nn.SelectTable(2))
)
:add(frame_level_rnn)
)
:add(nn.SelectTable(3))
)
:add(sample_level_predictor)
:cuda()
local param,dparam = net:getParameters()
param:copy(torch.load(session_path.."/params.t7"))
cudnn.RNN.forget = cudnn.RNN.resetStates
function resetStates()
local rnn_lookup = cudnn_rnn and ('cudnn.'..rnn_type) or ('nn.Seq'..rnn_type..'_WN')
local grus = net:findModules(rnn_lookup)
for i=1,#grus do
grus[i]:forget()
end
end
function sample()
print("Sampling...")
net:evaluate()
resetStates()
local samples = torch.CudaTensor(sample_length):fill(0)
local big_frame_level_outputs, frame_level_outputs
samples[{{1,big_frame_size}}] = q_zero
local start_time = sys.clock()
for t = big_frame_size + 1, sample_length do
if (t-1) % big_frame_size == 0 then
local big_frames = samples[{{t - big_frame_size, t - 1}}]:view(1,-1)
big_frame_level_outputs = big_frame_level_rnn:forward(big_frames)
end
if (t-1) % frame_size == 0 then
local frames = samples[{{t - frame_size, t - 1}}]:view(1,-1)
local _t = (((t-1) / frame_size) % (big_frame_size / frame_size)) + 1
frame_level_outputs = frame_level_rnn:forward({big_frame_level_outputs[{{_t}}],frames})
end
local prev_samples = samples[{{t - frame_size, t - 1}}]
local _t = (t-1) % frame_size + 1
local inp = {frame_level_outputs[{{_t}}], prev_samples}
local sample = sample_level_predictor:forward(inp)
sample:div(sampling_temperature)
sample:exp()
sample = torch.multinomial(sample,1)
samples[t] = sample:typeAs(samples)
xlua.progress(t-big_frame_size,sample_length-big_frame_size)
end
local stop_time = sys.clock()
print("Generated "..(sample_length / sample_rate).." seconds of audio in "..(stop_time - start_time).." seconds.")
if q_type == 'mu-law' then
samples = mu2linear(samples - 1)
samples:add(1)
samples:div(2)
elseif q_type == 'linear' then
samples = (samples - 1) / (q_levels - 1)
end
local audioOut = -0x80000000 + 0xFFFF0000 * samples
audio.save(output_path, audioOut:view(-1,1):double(), sample_rate)
end
sample()