-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdenoiser_onnx_test.py
331 lines (273 loc) · 13.7 KB
/
denoiser_onnx_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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import io
import os
import time
import zipfile
import librosa
import numpy as np
import onnxruntime
import torch
import torchaudio
from scipy.io import wavfile
import torchaudio.transforms as transforms
# torch-> numpy dönüşümü
def to_numpy(tensor):
if torch.is_tensor(tensor):
return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
else:
return tensor
def write(wav, filename, sr=16_000):
# Normalize audio if it prevents clipping
wav = wav / max(wav.abs().max().item(), 1)
torchaudio.save(filename, wav.cpu(), sr)
def load_onnx_from_zip(onnx_tt_model_path):
# .zip arşivini yükleyin (örnek olarak "model.zip" adını varsayalım)
with open(onnx_tt_model_path, "rb") as file:
zip_file = file.read()
# Zip dosyasını bellekte açın
zip_buffer = io.BytesIO(zip_file)
# Zip arşivini açın
with zipfile.ZipFile(zip_buffer, "r") as archive:
# ONNX model dosyasını yükleyin
model_bytes = archive.read("model.onnx")
return model_bytes
def is_zip_file(file_path):
_, file_extension = os.path.splitext(file_path)
return file_extension.lower() == ".zip"
def split_audio_into_frames(audio, block_len, block_shift):
hop_size = block_len - block_shift
print("blockSize:", block_len)
print("overlapSize:", block_shift)
print("hopSize:", hop_size)
chunks = []
i = 0
data_len = audio.shape[1]
count = 1
while i < data_len:
start = i
end = min(i + block_len, data_len)
chunk = audio[:, start:end]
chunks.append(chunk)
i += hop_size
count += 1
return chunks
def test_audio_denoising(noisy, onnx_tt_model_path, block_len, block_shift, sr, out_file):
# Bu kısmın iyileştirme yapıp yapmadığına emin değilim.
session_options = onnxruntime.SessionOptions()
# session_options.intra_op_num_threads = 1
# session_options.inter_op_num_threads = 1
session_options.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL
session_options.enable_profiling = False
session_options.profile_file_prefix = "profile_onnx_dns48_buff50_quantized"
# onnx runtime session oluşturulması
if is_zip_file(onnx_tt_quantized_model_path):
model_bytes = load_onnx_from_zip(onnx_tt_model_path)
ort_session = onnxruntime.InferenceSession(model_bytes, session_options)
else:
ort_session = onnxruntime.InferenceSession(onnx_tt_model_path, session_options)
# burada onnx modelinin giriş değerlerinin isimleri alınır.
input_audio_frame_name = ort_session.get_inputs()[0].name # ses dizisini ifade eder
# onnx modelinin çıktısındaki değerlerin isimleri
out_frame_name = ort_session.get_outputs()[0].name # burası çıkış audio array idir.
total_duration = (len(noisy) / sr) * 1000 # ses dosyasının ms cinsinden uzunluğu
print(f"noisy shape: {noisy.shape}")
chunks = split_audio_into_frames(noisy, block_len, block_shift)
outs = []
total_frame = 0
total_inference_time = 0
frame_in_ms = (block_len / sr) * 1000
# output_audio = np.zeros(noisy.shape, dtype=np.float32)
output_audio = np.zeros(noisy.shape, dtype=np.float32)
for i, chunk in enumerate(chunks):
print(f"frame number {i + 1}")
# frame = chunk.expand(1, 1, block_len)
start_time = time.time()
# onnx modelinin çalışmsı
ort_inputs = {input_audio_frame_name: to_numpy(chunk)}
out = ort_session.run(None, ort_inputs)[0]
print(f"input chunk shape {to_numpy(chunk).shape}")
print(f"out shape {out.shape}, type {type(out)}")
end_time = time.time()
inference_time = (end_time - start_time) * 1000
total_inference_time += inference_time
rtf = inference_time / frame_in_ms
offset = i * block_len - i * block_shift # calculate the offset of the current block, taking overlap into account
end = min(offset + block_len, output_audio.shape[1])
slice_length = end - offset
print(f"offset {offset}, end {end}, slice length {slice_length}")
if slice_length != block_len:
out = out[:, :, :slice_length]
reshaped_out = np.reshape(out, (1, slice_length))
print(f"reshaped out {reshaped_out.shape}")
output_audio[:, offset:end] = reshaped_out[:, :slice_length]
total_frame += 1
print(f"inference time in ms for frame {total_frame}, noisy frame in ms: {frame_in_ms}, "
f"{inference_time} ms. rtf: {rtf}")
print("**********")
prof = ort_session.end_profiling()
average_inference_time = total_inference_time / total_frame
print(f"average inference time in ms: {average_inference_time:.6f}")
print(f"average rtf : {average_inference_time / frame_in_ms:.6f}")
write(torch.from_numpy(output_audio), out_file, sr=16000) # wav dosyası olarak yazılır.
def test_audio_denoising_with_3sized_input(noisy, onnx_tt_model_path, block_len, block_shift, sr, out_file):
# Bu kısmın iyileştirme yapıp yapmadığına emin değilim.
session_options = onnxruntime.SessionOptions()
# session_options.intra_op_num_threads = 1
# session_options.inter_op_num_threads = 1
session_options.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL
session_options.enable_profiling = False
session_options.profile_file_prefix = "profile_onnx_dns48_buff50_quantized"
# onnx runtime session oluşturulması
if is_zip_file(onnx_tt_quantized_model_path):
model_bytes = load_onnx_from_zip(onnx_tt_model_path)
ort_session = onnxruntime.InferenceSession(model_bytes, session_options)
else:
ort_session = onnxruntime.InferenceSession(onnx_tt_model_path, session_options)
# burada onnx modelinin giriş değerlerinin isimleri alınır.
input_audio_frame_name = ort_session.get_inputs()[0].name # ses dizisini ifade eder
# onnx modelinin çıktısındaki değerlerin isimleri
out_frame_name = ort_session.get_outputs()[0].name # burası çıkış audio array idir.
total_duration = (len(noisy) / sr) * 1000 # ses dosyasının ms cinsinden uzunluğu
print(f"noisy shape: {noisy.shape}")
chunks = split_audio_into_frames(noisy, block_len, block_shift)
outs = []
total_frame = 0
total_inference_time = 0
frame_in_ms = (block_len / sr) * 1000
# output_audio = np.zeros(noisy.shape, dtype=np.float32)
output_audio = np.zeros(noisy.shape, dtype=np.float32)
for i, chunk in enumerate(chunks):
print(f"frame number {i + 1}")
if chunk.shape[1] < block_len:
frame = chunk.expand(1, 1, chunk.shape[1])
else:
frame = chunk.expand(1, 1, block_len)
print(f"frame input size: {frame.shape}")
start_time = time.time()
# onnx modelinin çalışmsı
ort_inputs = {input_audio_frame_name: to_numpy(frame)}
out = ort_session.run(None, ort_inputs)[0]
block_size = block_len
if out.shape[2] < block_len:
block_size = out.shape[2]
print(f"block len: {block_size}")
print(f"input chunk shape {to_numpy(frame).shape}")
print(f"out shape {out.shape}, type {type(out)}")
end_time = time.time()
inference_time = (end_time - start_time) * 1000
total_inference_time += inference_time
rtf = inference_time / frame_in_ms
offset = i * block_size - i * block_shift # calculate the offset of the current block, taking overlap into account
end = min(offset + block_size, output_audio.shape[1])
slice_length = end - offset
print(f"offset {offset}, end {end}, slice length {slice_length}")
if slice_length != block_size:
out = out[:, :, :slice_length]
reshaped_out = np.reshape(out, (1, slice_length))
print(f"reshaped out {reshaped_out.shape}")
output_audio[:, offset:end] = reshaped_out[:, :slice_length]
total_frame += 1
print(f"inference time in ms for frame {total_frame}, noisy frame in ms: {frame_in_ms}, "
f"{inference_time} ms. rtf: {rtf}")
print("**********")
prof = ort_session.end_profiling()
average_inference_time = total_inference_time / total_frame
print(f"average inference time in ms: {average_inference_time:.6f}")
print(f"average rtf : {average_inference_time / frame_in_ms:.6f}")
write(torch.from_numpy(output_audio), out_file, sr=16000) # wav dosyası olarak yazılır.
print(f"output audio shape : {output_audio.shape}")
def test_audio_denoising_with_fix_size_input(noisy, onnx_tt_model_path, block_len, block_shift, sr, out_file):
# Bu kısmın iyileştirme yapıp yapmadığına emin değilim.
session_options = onnxruntime.SessionOptions()
# session_options.intra_op_num_threads = 1
# session_options.inter_op_num_threads = 1
session_options.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL
session_options.enable_profiling = False
session_options.profile_file_prefix = "profile_onnx_dns48_buff50_quantized"
# onnx runtime session oluşturulması
if is_zip_file(onnx_tt_quantized_model_path):
model_bytes = load_onnx_from_zip(onnx_tt_model_path)
ort_session = onnxruntime.InferenceSession(model_bytes, session_options)
else:
ort_session = onnxruntime.InferenceSession(onnx_tt_model_path, session_options)
# burada onnx modelinin giriş değerlerinin isimleri alınır.
input_audio_frame_name = ort_session.get_inputs()[0].name # ses dizisini ifade eder
# onnx modelinin çıktısındaki değerlerin isimleri
out_frame_name = ort_session.get_outputs()[0].name # burası çıkış audio array idir.
total_duration = (len(noisy) / sr) * 1000 # ses dosyasının ms cinsinden uzunluğu
print(f"noisy shape: {noisy.shape}")
chunks = split_audio_into_frames(noisy, block_len, block_shift)
outs = []
total_frame = 0
total_inference_time = 0
frame_in_ms = (block_len / sr) * 1000
# output_audio = np.zeros(noisy.shape, dtype=np.float32)
output_audio = np.zeros(noisy.shape, dtype=np.float32)
for i, chunk in enumerate(chunks):
print(f"frame number {i + 1}")
if chunk.shape[1] < block_len:
num_zeros = block_len - chunk.shape[1]
# Create an array of zeros to be appended to the chunk.
zeros_to_add = np.zeros((1, num_zeros), dtype=np.float32)
# Append zeros to the chunk to expand it to block_len.
chunk = np.hstack((chunk, zeros_to_add))
frame = np.expand_dims(chunk, axis=0)
# frame = chunk.expand(1, 1, block_len)
print(f"frame input size : {frame.shape}")
start_time = time.time()
# onnx modelinin çalışmsı
ort_inputs = {input_audio_frame_name: to_numpy(frame)}
out = ort_session.run(None, ort_inputs)[0]
block_size = block_len
if out.shape[2] < block_len:
block_size = out.shape[2]
print(f"block len: {block_size}")
print(f"input chunk shape {to_numpy(frame).shape}")
print(f"out shape {out.shape}, type {type(out)}")
end_time = time.time()
inference_time = (end_time - start_time) * 1000
total_inference_time += inference_time
rtf = inference_time / frame_in_ms
offset = i * block_size - i * block_shift # calculate the offset of the current block, taking overlap into account
end = min(offset + block_size, output_audio.shape[1])
slice_length = end - offset
print(f"offset {offset}, end {end}, slice length {slice_length}")
if slice_length != block_size:
out = out[:, :, :slice_length]
reshaped_out = np.reshape(out, (1, slice_length))
print(f"reshaped out {reshaped_out.shape}")
output_audio[:, offset:end] = reshaped_out[:, :slice_length]
total_frame += 1
print(f"inference time in ms for frame {total_frame}, noisy frame in ms: {frame_in_ms}, "
f"{inference_time} ms. rtf: {rtf}")
print("**********")
prof = ort_session.end_profiling()
average_inference_time = total_inference_time / total_frame
print(f"average inference time in ms: {average_inference_time:.6f}")
print(f"average rtf : {average_inference_time / frame_in_ms:.6f}")
write(torch.from_numpy(output_audio), out_file, sr=16000) # wav dosyası olarak yazılır.
if __name__ == '__main__':
onnx_tt_quantized_model_path = 'D:/zeynep/data/noise-cancelling/denoiser/dns/hidden=48-depth=4/dns48_depth=4.onnx'
# zip modeli kullanmak isterseniz.
noisy_audio = 'D:/zeynep/data/noise-cancelling/DNS-Challenge/test-set/noisy_testset_wav_16k'
out_dir = 'D:/zeynep/data/noise-cancelling/DNS-Challenge/test-set/dns48-depth=4-batch-onnx/'
block_len = 480 # her bir frame uzunluğu
block_shift = 160 # shift uzunluğu
if not os.path.exists(out_dir):
os.mkdir(out_dir)
if os.path.isfile(noisy_audio):
noisy, sr = torchaudio.load(str(noisy_audio))
name = os.path.basename(noisy_audio)
out_file = out_dir + name
print(f"inference starts for {noisy_audio}")
test_audio_denoising_with_3sized_input(noisy, onnx_tt_quantized_model_path, block_len,
block_shift, sr, out_file)
else:
noisy_files = librosa.util.find_files(noisy_audio, ext='wav')
for noisy_f in noisy_files:
name = os.path.basename(noisy_f)
out_file = out_dir + name
noisy, sr = torchaudio.load(str(noisy_f))
print(f"inference starts for {noisy_f}")
test_audio_denoising_with_3sized_input(noisy, onnx_tt_quantized_model_path, block_len,
block_shift, sr, out_file)
print(f"inference done for {noisy_f}.")