-
Notifications
You must be signed in to change notification settings - Fork 12
/
model_apply.cpp
314 lines (247 loc) · 10.6 KB
/
model_apply.cpp
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "crosstransformer.hpp"
#include "dsp.hpp"
#include "encdec.hpp"
#include "layers.hpp"
#include "model.hpp"
#include "tensor.hpp"
#include <Eigen/Dense>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <random>
#include <sstream>
#include <string>
#include <tuple>
#include <unsupported/Eigen/FFT>
#include <unsupported/Eigen/MatrixFunctions>
#include <vector>
static std::tuple<int, int>
symmetric_zero_padding(Eigen::MatrixXf &padded, const Eigen::MatrixXf &original,
int total_padding)
{
int left_padding = std::floor((float)total_padding / 2.0f);
int right_padding = total_padding - left_padding;
int N = original.cols(); // The original number of columns
// Copy the original mix into the middle of padded_mix
padded.block(0, left_padding, 2, N) = original;
// Zero padding on the left
padded.block(0, 0, 2, left_padding) =
Eigen::MatrixXf::Zero(2, left_padding);
// Zero padding on the right
padded.block(0, N + left_padding, 2, right_padding) =
Eigen::MatrixXf::Zero(2, right_padding);
// return left, right padding as tuple
return std::make_tuple(left_padding, right_padding);
}
// forward declaration of inner fns
static Eigen::Tensor3dXf
shift_inference(struct demucscpp::demucs_model &model,
Eigen::MatrixXf &full_audio);
static Eigen::Tensor3dXf
split_inference(struct demucscpp::demucs_model &model,
Eigen::MatrixXf &full_audio);
static Eigen::Tensor3dXf
segment_inference(struct demucscpp::demucs_model &model,
Eigen::MatrixXf chunk, int segment_sample,
struct demucscpp::demucs_segment_buffers &buffers,
struct demucscpp::stft_buffers &stft_buf);
Eigen::Tensor3dXf demucscpp::demucs_inference(struct demucs_model &model,
Eigen::MatrixXf &full_audio)
{
std::cout << std::fixed << std::setprecision(20) << std::endl;
demucscppdebug::debug_matrix_xf(full_audio, "full_audio");
// first, normalize the audio to mean and std
// ref = wav.mean(0)
// wav = (wav - ref.mean()) / ref.std()
// Calculate the overall mean and standard deviation
// Compute the mean and standard deviation separately for each channel
Eigen::VectorXf ref_mean_0 = full_audio.colwise().mean();
float ref_mean = ref_mean_0.mean();
float ref_std = std::sqrt((ref_mean_0.array() - ref_mean).square().sum() /
(ref_mean_0.size() - 1));
// Normalize the audio
Eigen::MatrixXf normalized_audio =
(full_audio.array() - ref_mean) / ref_std;
demucscppdebug::debug_matrix_xf(normalized_audio, "normalized_audio");
full_audio = normalized_audio;
int length = full_audio.cols();
Eigen::Tensor3dXf waveform_outputs = shift_inference(model, full_audio);
// now inverse the normalization in Eigen C++
// sources = sources * ref.std() + ref.mean()
waveform_outputs = (waveform_outputs * ref_std).eval() + ref_mean;
return waveform_outputs;
}
static Eigen::Tensor3dXf
shift_inference(struct demucscpp::demucs_model &model,
Eigen::MatrixXf &full_audio)
{
demucscppdebug::debug_matrix_xf(full_audio, "mix in shift!");
// first, apply shifts for time invariance
// we simply only support shift=1, the demucs default
// shifts (int): if > 0, will shift in time `mix` by a random amount between
// 0 and 0.5 sec
// and apply the oppositve shift to the output. This is repeated
// `shifts` time and all predictions are averaged. This effectively
// makes the model time equivariant and improves SDR by up to 0.2
// points.
int max_shift =
(int)(demucscpp::MAX_SHIFT_SECS * demucscpp::SUPPORTED_SAMPLE_RATE);
int length = full_audio.cols();
Eigen::MatrixXf padded_mix(2, length + 2 * max_shift);
symmetric_zero_padding(padded_mix, full_audio, 2 * max_shift);
demucscppdebug::debug_matrix_xf(padded_mix, "padded_mix");
//int offset = rand() % max_shift;
int offset = 1337;
std::cout << "1., apply model w/ shift, offset: " << offset << std::endl;
Eigen::MatrixXf shifted_audio =
padded_mix.block(0, offset, 2, length + max_shift - offset);
int shifted_length = shifted_audio.cols();
demucscppdebug::debug_matrix_xf(shifted_audio, "shifted_audio");
Eigen::Tensor3dXf waveform_outputs =
split_inference(model, shifted_audio);
demucscppdebug::debug_tensor_3dxf(waveform_outputs, "waveform_outputs");
int nb_out_sources = model.is_4sources ? 4 : 6;
// trim the output to the original length
// waveform_outputs = waveform_outputs[..., max_shift:max_shift + length]
Eigen::Tensor3dXf trimmed_waveform_outputs =
waveform_outputs
.reshape(
Eigen::array<int, 3>({nb_out_sources, 2, waveform_outputs.dimension(2)}))
.slice(Eigen::array<int, 3>({0, 0, max_shift - offset}),
Eigen::array<int, 3>({nb_out_sources, 2, length}));
return trimmed_waveform_outputs;
}
static Eigen::Tensor3dXf
split_inference(struct demucscpp::demucs_model &model,
Eigen::MatrixXf &full_audio)
{
std::cout << "in split inference!" << std::endl;
demucscppdebug::debug_matrix_xf(full_audio, "full_audio");
// calculate segment in samples
int segment_samples =
(int)(demucscpp::SEGMENT_LEN_SECS * demucscpp::SUPPORTED_SAMPLE_RATE);
int nb_out_sources = model.is_4sources ? 4 : 6;
// let's create reusable buffers with padded sizes
struct demucscpp::demucs_segment_buffers buffers(2, segment_samples, nb_out_sources);
struct demucscpp::stft_buffers stft_buf(buffers.padded_segment_samples);
// next, use splits with weighted transition and overlap
// split (bool): if True, the input will be broken down in 8 seconds
// extracts
// and predictions will be performed individually on each and
// concatenated. Useful for model with large memory footprint like
// Tasnet.
int stride_samples = (int)((1 - demucscpp::OVERLAP) * segment_samples);
int length = full_audio.cols();
// create an output tensor of zeros for four source waveforms
Eigen::Tensor3dXf out = Eigen::Tensor3dXf(nb_out_sources, 2, length);
out.setZero();
// create weight tensor
Eigen::VectorXf weight(segment_samples);
weight.setZero();
weight.head(segment_samples / 2) =
Eigen::VectorXf::LinSpaced(segment_samples / 2, 1, segment_samples / 2);
weight.tail(segment_samples / 2) =
weight.head(segment_samples / 2).reverse();
weight /= weight.maxCoeff();
weight = weight.array().pow(demucscpp::TRANSITION_POWER);
Eigen::VectorXf sum_weight(length);
sum_weight.setZero();
// for loop from 0 to length with stride stride_samples
/* ------------------------------------------------------*/
/* ----> SERIOUS PARALLELISM CAN BE ACHIEVED HERE?? <----*/
/* ------------------------------------------------------*/
for (int offset = 0; offset < length; offset += stride_samples)
{
// create a chunk of the padded_full_audio
int chunk_end = std::min(segment_samples, length - offset);
Eigen::MatrixXf chunk = full_audio.block(0, offset, 2, chunk_end);
int chunk_length = chunk.cols();
std::cout << "2., apply model w/ split, offset: " << offset
<< ", chunk shape: (" << chunk.rows() << ", " << chunk.cols()
<< ")" << std::endl;
Eigen::Tensor3dXf chunk_out = segment_inference(
model, chunk, segment_samples, buffers, stft_buf);
demucscppdebug::debug_tensor_3dxf(
chunk_out, "chunk_out for offset: " + std::to_string(offset));
// add the weighted chunk to the output
// out[..., offset:offset + segment] += (weight[:chunk_length] *
// chunk_out).to(mix.device)
for (int i = 0; i < nb_out_sources; ++i)
{
for (int j = 0; j < 2; ++j)
{
for (int k = 0; k < chunk_length; ++k)
{
if (offset + k >= length)
{
break;
}
out(i, j, offset + k) +=
weight(k % chunk_length) * chunk_out(i, j, k);
}
}
}
// sum_weight[offset:offset + segment] +=
// weight[:chunk_length].to(mix.device)
for (int k = 0; k < chunk_length; ++k)
{
if (offset + k >= length)
{
break;
}
sum_weight(offset + k) += weight(k % chunk_length);
}
}
demucscppdebug::assert_(sum_weight.minCoeff() > 0);
for (int i = 0; i < nb_out_sources; ++i)
{
for (int j = 0; j < 2; ++j)
{
for (int k = 0; k < length; ++k)
{
out(i, j, k) /= sum_weight[k];
}
}
}
demucscppdebug::debug_tensor_3dxf(out, "out");
return out;
}
static Eigen::Tensor3dXf
segment_inference(struct demucscpp::demucs_model &model,
Eigen::MatrixXf chunk, int segment_samples,
struct demucscpp::demucs_segment_buffers &buffers,
struct demucscpp::stft_buffers &stft_buf)
{
std::cout << "in segment inference!" << std::endl;
demucscppdebug::debug_matrix_xf(chunk, "chunk");
int chunk_length = chunk.cols();
// copy chunk into buffers.mix with symmetric zero-padding
// assign two ints to tuple return value
std::tuple<int, int> padding = symmetric_zero_padding(
buffers.mix, chunk, segment_samples - chunk_length);
// apply demucs inference
demucscpp::model_inference(model, buffers, stft_buf);
int nb_out_sources = model.is_4sources ? 4 : 6;
// copy from buffers.targets_out into chunk_out with center trimming
Eigen::Tensor3dXf chunk_out = Eigen::Tensor3dXf(nb_out_sources, 2, chunk_length);
chunk_out.setZero();
demucscppdebug::debug_tensor_3dxf(chunk_out, "chunk_out");
demucscppdebug::debug_tensor_3dxf(buffers.targets_out,
"buffers.targets_out");
for (int i = 0; i < nb_out_sources; ++i)
{
for (int j = 0; j < 2; ++j)
{
for (int k = 0; k < chunk_length; ++k)
{
// undoing center_trim
chunk_out(i, j, k) =
buffers.targets_out(i, j, k + std::get<0>(padding));
}
}
}
demucscppdebug::debug_tensor_3dxf(chunk_out, "chunk_out");
return chunk_out;
}