AudioSR.AXERA full board package: models + bin + python + samples
Browse files- .gitattributes +10 -0
- README.md +38 -46
- bin/audiosr_ax650 +0 -0
- configuration.json +17 -0
- models/audiosr_ddim_10.npz +3 -0
- models/mel_filters_256x1025.bin +3 -0
- models/model_config.json +5 -0
- models/model_meta.json +8 -0
- python/audiosr_sdk.py +184 -0
- python/demo.py +23 -0
- python/requirements.txt +5 -0
- requirements.txt +5 -0
- run_ax650.sh +24 -0
- samples/listen/16k/speech_2_input_16k.wav +3 -0
- samples/listen/8k/OSR_cn_000_0072_input_8k.wav +0 -0
- samples/listen/8k/OSR_cn_000_0072_input_8k_5s.wav +0 -0
- samples/listen/8k/OSR_us_000_0010_input_8k.wav +0 -0
- samples/listen/8k/OSR_us_000_0010_input_8k_5s.wav +0 -0
- samples/listen/output/OSR_cn_000_0072_output_48k.wav +3 -0
- samples/listen/output/OSR_us_000_0010_output_48k.wav +3 -0
- samples/listen/output/speech_2_output_48k.wav +3 -0
- samples/listen/spectrograms/OSR_cn_000_0072_input_8k_spec.png +3 -0
- samples/listen/spectrograms/OSR_cn_000_0072_output_48k_spec.png +3 -0
- samples/listen/spectrograms/OSR_us_000_0010_input_8k_spec.png +3 -0
- samples/listen/spectrograms/OSR_us_000_0010_output_48k_spec.png +3 -0
- samples/listen/spectrograms/speech_2_input_16k_spec.png +3 -0
- samples/listen/spectrograms/speech_2_output_48k_spec.png +3 -0
.gitattributes
CHANGED
|
@@ -45,3 +45,13 @@ models/voc_up0a_mix.axmodel filter=lfs diff=lfs merge=lfs -text
|
|
| 45 |
models/voc_up0b_mix.axmodel filter=lfs diff=lfs merge=lfs -text
|
| 46 |
models/voc_up1a_mix.axmodel filter=lfs diff=lfs merge=lfs -text
|
| 47 |
models/voc_up1b_mix.axmodel filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
models/voc_up0b_mix.axmodel filter=lfs diff=lfs merge=lfs -text
|
| 46 |
models/voc_up1a_mix.axmodel filter=lfs diff=lfs merge=lfs -text
|
| 47 |
models/voc_up1b_mix.axmodel filter=lfs diff=lfs merge=lfs -text
|
| 48 |
+
samples/listen/16k/speech_2_input_16k.wav filter=lfs diff=lfs merge=lfs -text
|
| 49 |
+
samples/listen/output/OSR_cn_000_0072_output_48k.wav filter=lfs diff=lfs merge=lfs -text
|
| 50 |
+
samples/listen/output/OSR_us_000_0010_output_48k.wav filter=lfs diff=lfs merge=lfs -text
|
| 51 |
+
samples/listen/output/speech_2_output_48k.wav filter=lfs diff=lfs merge=lfs -text
|
| 52 |
+
samples/listen/spectrograms/OSR_cn_000_0072_input_8k_spec.png filter=lfs diff=lfs merge=lfs -text
|
| 53 |
+
samples/listen/spectrograms/OSR_cn_000_0072_output_48k_spec.png filter=lfs diff=lfs merge=lfs -text
|
| 54 |
+
samples/listen/spectrograms/OSR_us_000_0010_input_8k_spec.png filter=lfs diff=lfs merge=lfs -text
|
| 55 |
+
samples/listen/spectrograms/OSR_us_000_0010_output_48k_spec.png filter=lfs diff=lfs merge=lfs -text
|
| 56 |
+
samples/listen/spectrograms/speech_2_input_16k_spec.png filter=lfs diff=lfs merge=lfs -text
|
| 57 |
+
samples/listen/spectrograms/speech_2_output_48k_spec.png filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,25 +1,31 @@
|
|
| 1 |
---
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
tags:
|
| 4 |
-
- audio-super-resolution
|
| 5 |
-
- speech
|
| 6 |
-
- ax650
|
| 7 |
- axera
|
| 8 |
-
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
# AudioSR.AXERA
|
| 13 |
|
| 14 |
-
**
|
| 15 |
|
| 16 |
-
   [。
|
| 112 |
|
| 113 |
## 数据来源
|
| 114 |
|
| 115 |
- 上游 AudioSR:<https://github.com/haoheliu/versatile_audio_super_resolution>
|
| 116 |
- 示例英文 / 中文语音:Open Speech Repository、公开 16 kHz 人声样例
|
| 117 |
-
- 示例输出由本项目 AX650
|
| 118 |
|
| 119 |
## License
|
| 120 |
|
|
|
|
| 1 |
---
|
| 2 |
+
library_name: axera
|
| 3 |
+
pipeline_tag: audio-to-audio
|
| 4 |
+
base_model: haoheliu/audiosr_speech
|
| 5 |
+
language:
|
| 6 |
+
- en
|
| 7 |
+
- zh
|
| 8 |
tags:
|
|
|
|
|
|
|
|
|
|
| 9 |
- axera
|
| 10 |
+
- ax650
|
| 11 |
+
- npu3
|
| 12 |
+
- audiosr
|
| 13 |
+
- speech-super-resolution
|
| 14 |
+
- audio-super-resolution
|
| 15 |
+
- python
|
| 16 |
+
- cpp
|
| 17 |
---
|
| 18 |
|
| 19 |
# AudioSR.AXERA
|
| 20 |
|
| 21 |
+
**低采样率语音 → 48 kHz · AX650 NPU3 · vocoder 板端相似度 0.9984 · 完整链路 vs PyTorch 0.9917**
|
| 22 |
|
| 23 |
+
   [](https://github.com/ZY-2012/AudioSR_AXERA)
|
| 24 |
|
| 25 |
+
- GitHub: https://github.com/ZY-2012/AudioSR_AXERA
|
| 26 |
+
- 上游: https://github.com/haoheliu/versatile_audio_super_resolution
|
| 27 |
+
- 输入: 8 / 16 / 24 / 48 kHz WAV;输出: 48 kHz
|
| 28 |
+
- 5.12 s 输入,AudioSR DDIM-10
|
|
|
|
| 29 |
|
| 30 |
## 目录
|
| 31 |
|
|
|
|
| 38 |
|
| 39 |
## 快速开始(AX650 板端)
|
| 40 |
|
| 41 |
+
下载整个仓库后,在包根目录运行:
|
| 42 |
|
| 43 |
```bash
|
| 44 |
+
# C++(默认)
|
| 45 |
+
bash run_ax650.sh samples/listen/16k/speech_2_input_16k.wav output.wav
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# Python SDK
|
| 48 |
+
bash run_ax650.sh samples/listen/16k/speech_2_input_16k.wav output.wav --python
|
|
|
|
| 49 |
```
|
| 50 |
|
| 51 |
+
输入 8 / 16 / 24 / 48 kHz,输出固定 48 kHz。C++ 二进制位于 `bin/audiosr_ax650`,模型位于 `models/`。
|
| 52 |
|
| 53 |
## 工程结构
|
| 54 |
|
| 55 |
```text
|
| 56 |
AudioSR_AXERA/
|
| 57 |
+
├── bin/ # C++ 板端可执行文件
|
| 58 |
+
├── models/ # axmodel + DDIM 常量 + mel filter
|
| 59 |
├── python/ # Python 板端推理 SDK(axengine)
|
| 60 |
+
├── samples/ # 中英文示例输入 / 输出 / 频谱图
|
| 61 |
+
├── run_ax650.sh # 一键 C++/Python 运行
|
| 62 |
+
├── configuration.json
|
| 63 |
└── requirements.txt
|
| 64 |
```
|
| 65 |
|
|
|
|
| 69 |
|
| 70 |
| 输入 | AudioSR 输出 |
|
| 71 |
|---|---|
|
| 72 |
+
| <audio controls style="width:220px" src="https://huggingface.co/HY-2012/AudioSR.AXERA/resolve/main/samples/listen/16k/speech_2_input_16k.wav"></audio><br><img src="samples/listen/spectrograms/speech_2_input_16k_spec.png" width="360"> | <audio controls style="width:220px" src="https://huggingface.co/HY-2012/AudioSR.AXERA/resolve/main/samples/listen/output/speech_2_output_48k.wav"></audio><br><img src="samples/listen/spectrograms/speech_2_output_48k_spec.png" width="360"> |
|
| 73 |
|
| 74 |
### English 8 kHz
|
| 75 |
|
| 76 |
| 输入 | AudioSR 输出 |
|
| 77 |
|---|---|
|
| 78 |
+
| <audio controls style="width:220px" src="https://huggingface.co/HY-2012/AudioSR.AXERA/resolve/main/samples/listen/8k/OSR_us_000_0010_input_8k.wav"></audio><br><img src="samples/listen/spectrograms/OSR_us_000_0010_input_8k_spec.png" width="360"> | <audio controls style="width:220px" src="https://huggingface.co/HY-2012/AudioSR.AXERA/resolve/main/samples/listen/output/OSR_us_000_0010_output_48k.wav"></audio><br><img src="samples/listen/spectrograms/OSR_us_000_0010_output_48k_spec.png" width="360"> |
|
| 79 |
|
| 80 |
### 中文 8 kHz
|
| 81 |
|
| 82 |
| 输入 | AudioSR 输出 |
|
| 83 |
|---|---|
|
| 84 |
+
| <audio controls style="width:220px" src="https://huggingface.co/HY-2012/AudioSR.AXERA/resolve/main/samples/listen/8k/OSR_cn_000_0072_input_8k.wav"></audio><br><img src="samples/listen/spectrograms/OSR_cn_000_0072_input_8k_spec.png" width="360"> | <audio controls style="width:220px" src="https://huggingface.co/HY-2012/AudioSR.AXERA/resolve/main/samples/listen/output/OSR_cn_000_0072_output_48k.wav"></audio><br><img src="samples/listen/spectrograms/OSR_cn_000_0072_output_48k_spec.png" width="360"> |
|
| 85 |
|
| 86 |
### 精度
|
| 87 |
|
|
|
|
| 97 |
|
| 98 |
5.12 s 输入、DDIM-10、AX650 NPU3:
|
| 99 |
|
| 100 |
+
| 运行时 | 时间 | RTF |
|
| 101 |
+
|---|---:|---:|
|
| 102 |
+
| Python SDK | ~20–22 s | ~4.0–4.3 |
|
| 103 |
+
| **C++** | **~7.3 s** | **~1.43** |
|
|
|
|
|
|
|
| 104 |
|
| 105 |
## 数据来源
|
| 106 |
|
| 107 |
- 上游 AudioSR:<https://github.com/haoheliu/versatile_audio_super_resolution>
|
| 108 |
- 示例英文 / 中文语音:Open Speech Repository、公开 16 kHz 人声样例
|
| 109 |
+
- 示例输出由本项目 AX650 SDK 生成
|
| 110 |
|
| 111 |
## License
|
| 112 |
|
bin/audiosr_ax650
ADDED
|
Binary file (92.9 kB). View file
|
|
|
configuration.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"framework": "axera",
|
| 3 |
+
"model": "AudioSR-speech",
|
| 4 |
+
"target_hardware": "AX650",
|
| 5 |
+
"npu_mode": "NPU3",
|
| 6 |
+
"compiler": "Pulsar2 8.0",
|
| 7 |
+
"task": "audio-to-audio",
|
| 8 |
+
"sample_rate_out": 48000,
|
| 9 |
+
"models": {
|
| 10 |
+
"unet": "models/audiosr_unet.axmodel",
|
| 11 |
+
"vae_encoder": "models/audiosr_vae_encoder_nhwc.axmodel",
|
| 12 |
+
"vae_decoder": "models/audiosr_vae_decoder.axmodel",
|
| 13 |
+
"vocoder": "models/voc_*.axmodel"
|
| 14 |
+
},
|
| 15 |
+
"python_demo": "python/demo.py",
|
| 16 |
+
"cpp_bin": "bin/audiosr_ax650"
|
| 17 |
+
}
|
models/audiosr_ddim_10.npz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ef451a92d0b86fc9999e66fbaf99f5312a4676d54963e96a7b7387480559ee86
|
| 3 |
+
size 10074
|
models/mel_filters_256x1025.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8dd24b6b1a81dc8f70ed479186fe66b1ccb4fc2dcffc3133dfd01daa93618c1d
|
| 3 |
+
size 1049600
|
models/model_config.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"scale_factor": 0.3342401385307312,
|
| 3 |
+
"unconditional_cond": -11.4981,
|
| 4 |
+
"vocoder_split": ["pre", "up0a", "up0b", "up1a", "up1b", "up2", "up3", "up4", "post"]
|
| 5 |
+
}
|
models/model_meta.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "AudioSR-speech",
|
| 3 |
+
"sample_rate": 48000,
|
| 4 |
+
"input_duration": 5.12,
|
| 5 |
+
"ddim_steps": 10,
|
| 6 |
+
"model_count": 12,
|
| 7 |
+
"compiler": "Pulsar2 8.0"
|
| 8 |
+
}
|
python/audiosr_sdk.py
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""AudioSR speech super-resolution on AX650 (axengine).
|
| 2 |
+
|
| 3 |
+
The SDK accepts 8/16/24/48 kHz mono/stereo WAV and produces 48 kHz output.
|
| 4 |
+
It implements the upstream AudioSR preprocessing (5.12 s padding, log-mel,
|
| 5 |
+
low-pass conditioning, DDIM-10) in NumPy/librosa, then runs the AX650
|
| 6 |
+
axmodels with axengine.
|
| 7 |
+
"""
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
import argparse
|
| 10 |
+
import math
|
| 11 |
+
import os
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
|
| 14 |
+
import numpy as np
|
| 15 |
+
import soundfile as sf
|
| 16 |
+
from scipy.signal import butter, resample_poly, sosfiltfilt
|
| 17 |
+
import librosa
|
| 18 |
+
|
| 19 |
+
SR = 48000
|
| 20 |
+
N_FFT = 2048
|
| 21 |
+
HOP = 480
|
| 22 |
+
WIN = 2048
|
| 23 |
+
N_MEL = 256
|
| 24 |
+
FMIN = 20
|
| 25 |
+
FMAX = 24000
|
| 26 |
+
SEG = 5.12
|
| 27 |
+
SCALE = 0.3342401385307312
|
| 28 |
+
UNCOND = -11.4981
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def _normalize(y):
|
| 32 |
+
y = y - np.mean(y)
|
| 33 |
+
return (y / (np.max(np.abs(y)) + 1e-8) * 0.5).astype(np.float32)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def _pad_wav(y, target):
|
| 37 |
+
out = np.zeros(target, dtype=np.float32)
|
| 38 |
+
out[:min(target, len(y))] = y[:target]
|
| 39 |
+
return out
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def _mel(y):
|
| 43 |
+
# same reflect padding as audiosr.utils.mel_spectrogram_train
|
| 44 |
+
p = (N_FFT - HOP) // 2
|
| 45 |
+
yy = np.pad(y, (p, p), mode="reflect")
|
| 46 |
+
stft = librosa.stft(yy, n_fft=N_FFT, hop_length=HOP, win_length=WIN,
|
| 47 |
+
window="hann", center=False)
|
| 48 |
+
mag = np.abs(stft)
|
| 49 |
+
basis = librosa.filters.mel(sr=SR, n_fft=N_FFT, n_mels=N_MEL, fmin=FMIN, fmax=FMAX)
|
| 50 |
+
mel = np.log(np.maximum(basis @ mag, 1e-5))
|
| 51 |
+
return mel.astype(np.float32), mag.astype(np.float32)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _cutoff(stft_mag, percentile=0.985):
|
| 55 |
+
energy = np.cumsum(stft_mag.sum(axis=1)) # [freq], sum over time
|
| 56 |
+
idx = 0
|
| 57 |
+
for i in range(1, energy.shape[0]):
|
| 58 |
+
if energy[-i] < energy[-1] * percentile:
|
| 59 |
+
idx = energy.shape[0] - i
|
| 60 |
+
break
|
| 61 |
+
cutoff = idx / 1024.0 * 24000.0
|
| 62 |
+
return 24000.0 if cutoff < 1000.0 else cutoff
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def _lowpass(y, cutoff):
|
| 66 |
+
sos = butter(8, cutoff, btype="lowpass", fs=SR, output="sos")
|
| 67 |
+
return sosfiltfilt(sos, y).astype(np.float32)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def make_batch(path):
|
| 71 |
+
y, sr = sf.read(path, always_2d=False)
|
| 72 |
+
if y.ndim > 1:
|
| 73 |
+
y = y.mean(axis=1)
|
| 74 |
+
y = np.asarray(y, dtype=np.float32)
|
| 75 |
+
if sr != SR:
|
| 76 |
+
y = resample_poly(y, SR, sr).astype(np.float32)
|
| 77 |
+
duration = len(y) / SR
|
| 78 |
+
pad_duration = duration + ((SEG - duration % SEG) if duration % SEG else 0.0)
|
| 79 |
+
target_frame = int(pad_duration * 100)
|
| 80 |
+
y = _normalize(y)
|
| 81 |
+
y = _pad_wav(y, int(SR * pad_duration))
|
| 82 |
+
|
| 83 |
+
mel, stft = _mel(y)
|
| 84 |
+
mel = mel.T # [T, 256]
|
| 85 |
+
stft = stft.T
|
| 86 |
+
if mel.shape[0] < target_frame:
|
| 87 |
+
mel = np.pad(mel, ((0, target_frame - mel.shape[0]), (0, 0)))
|
| 88 |
+
stft = np.pad(stft, ((0, target_frame - stft.shape[0]), (0, 0)))
|
| 89 |
+
else:
|
| 90 |
+
mel, stft = mel[:target_frame], stft[:target_frame]
|
| 91 |
+
if mel.shape[-1] % 2:
|
| 92 |
+
mel, stft = mel[..., :-1], stft[..., :-1]
|
| 93 |
+
|
| 94 |
+
cutoff = _cutoff(stft, 0.985)
|
| 95 |
+
low = _lowpass(y, cutoff)
|
| 96 |
+
low_mel, _ = _mel(low)
|
| 97 |
+
low_mel = low_mel.T
|
| 98 |
+
if low_mel.shape[0] < target_frame:
|
| 99 |
+
low_mel = np.pad(low_mel, ((0, target_frame - low_mel.shape[0]), (0, 0)))
|
| 100 |
+
else:
|
| 101 |
+
low_mel = low_mel[:target_frame]
|
| 102 |
+
if low_mel.shape[-1] % 2:
|
| 103 |
+
low_mel = low_mel[..., :-1]
|
| 104 |
+
return mel[None, None], low_mel[None, None], low, cutoff
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
class AudioSR:
|
| 108 |
+
def __init__(self, model_dir, seed=42, ddim="models/audiosr_ddim_10.npz"):
|
| 109 |
+
import axengine
|
| 110 |
+
self.axengine = axengine
|
| 111 |
+
self.dir = Path(model_dir)
|
| 112 |
+
self.enc = axengine.InferenceSession(str(self.dir / "audiosr_vae_encoder_nhwc.axmodel"))
|
| 113 |
+
self.unet = axengine.InferenceSession(str(self.dir / "audiosr_unet.axmodel"))
|
| 114 |
+
self.dec = axengine.InferenceSession(str(self.dir / "audiosr_vae_decoder.axmodel"))
|
| 115 |
+
self.voc = [axengine.InferenceSession(str(self.dir / n)) for n in [
|
| 116 |
+
"voc_pre_fp32.axmodel", "voc_up0a_mix.axmodel", "voc_up0b_mix.axmodel",
|
| 117 |
+
"voc_up1a_mix.axmodel", "voc_up1b_mix.axmodel", "voc_split_nhwc_up2_mix.axmodel",
|
| 118 |
+
"voc_split_nhwc_up3_mix.axmodel", "voc_split_nhwc_up4_mix.axmodel",
|
| 119 |
+
"voc_split_nhwc_post_fp32.axmodel"]]
|
| 120 |
+
self.d = np.load(ddim)
|
| 121 |
+
self.rng = np.random.RandomState(seed)
|
| 122 |
+
|
| 123 |
+
def _encode(self, mel):
|
| 124 |
+
o = self.enc.run(None, {"mel": np.ascontiguousarray(mel[0, 0, :, :, None].astype(np.float32))})
|
| 125 |
+
return (o[0] + self.rng.randn(*o[0].shape).astype(np.float32) *
|
| 126 |
+
np.exp(0.5 * o[1])).astype(np.float32) * SCALE
|
| 127 |
+
|
| 128 |
+
def enhance(self, path):
|
| 129 |
+
mel, low_mel, low_wav, _ = make_batch(path)
|
| 130 |
+
zc = self._encode(mel)
|
| 131 |
+
zl = self._encode(low_mel)
|
| 132 |
+
x = self.rng.randn(1, 16, 64, 32).astype(np.float32)
|
| 133 |
+
uncond = np.full((1, 16, 64, 32), UNCOND, np.float32)
|
| 134 |
+
ts = np.flip(self.d["timesteps"])
|
| 135 |
+
n = len(ts)
|
| 136 |
+
for i, step in enumerate(ts):
|
| 137 |
+
t = np.array([float(step)], np.float32)
|
| 138 |
+
def run(c):
|
| 139 |
+
return self.unet.run(None, {"x": np.ascontiguousarray(np.concatenate([x, c], 1)), "t": t})[0]
|
| 140 |
+
vu, vc = run(uncond), run(zc)
|
| 141 |
+
v = vu + 3.5 * (vc - vu)
|
| 142 |
+
sc = float(self.d["sqrt_alphas_cumprod"][step])
|
| 143 |
+
so = float(self.d["sqrt_one_minus_alphas_cumprod"][step])
|
| 144 |
+
et = sc * v + so * x
|
| 145 |
+
px0 = sc * x - so * v
|
| 146 |
+
idx = n - 1 - i
|
| 147 |
+
at, ap = float(self.d["alphas"][idx]), float(self.d["alphas_prev"][idx])
|
| 148 |
+
sig = float(self.d["sigmas"][idx])
|
| 149 |
+
x = (np.sqrt(ap) * px0 + np.sqrt(max(0.0, 1.0 - ap - sig * sig)) * et +
|
| 150 |
+
sig * self.rng.randn(*x.shape).astype(np.float32)).astype(np.float32)
|
| 151 |
+
dm = self.dec.run(None, {"z": np.ascontiguousarray(x / SCALE)})[0]
|
| 152 |
+
# mel bin cutoff, using upstream mel_replace_ops
|
| 153 |
+
e = np.cumsum(np.exp(low_mel[0]).sum(axis=1))
|
| 154 |
+
limit = e[-1] * 0.985
|
| 155 |
+
cb = 0
|
| 156 |
+
for i in range(1, e.shape[0]):
|
| 157 |
+
if e[-i] < limit:
|
| 158 |
+
cb = e.shape[0] - i
|
| 159 |
+
break
|
| 160 |
+
dm[0, :, :cb, :] = low_mel[0, :cb, :]
|
| 161 |
+
voc = dm.squeeze(1).transpose(0, 2, 1).astype(np.float32) # [1, 256, 512]
|
| 162 |
+
x = np.ascontiguousarray(voc.transpose(0, 2, 1)) # NHWC [1, 512, 256]
|
| 163 |
+
for s in self.voc:
|
| 164 |
+
x = s.run(None, {s.get_inputs()[0].name: x})[0]
|
| 165 |
+
wav = x[0, 0][:low_wav.size]
|
| 166 |
+
return postprocess(wav, low_wav), SR
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
def postprocess(out, src):
|
| 170 |
+
stft_gt = librosa.stft(src)
|
| 171 |
+
stft_out = librosa.stft(out)
|
| 172 |
+
e = np.cumsum(np.sum(np.abs(stft_gt), axis=-1))
|
| 173 |
+
limit = e[-1] * 0.985
|
| 174 |
+
c = 0
|
| 175 |
+
for i in range(1, e.shape[0]):
|
| 176 |
+
if e[-i] < limit:
|
| 177 |
+
c = e.shape[0] - i
|
| 178 |
+
break
|
| 179 |
+
er = np.mean(np.sum(np.abs(stft_gt[c])) / np.sum(np.abs(stft_out[c])))
|
| 180 |
+
er = min(max(er, 0.8), 1.2)
|
| 181 |
+
stft_out[:c] = stft_gt[:c] / er
|
| 182 |
+
out = librosa.istft(stft_out, length=out.shape[0])
|
| 183 |
+
out = 0.5 * out / max(float(np.max(np.abs(out))), 1e-8)
|
| 184 |
+
return (out - float(np.mean(out))).astype(np.float32)
|
python/demo.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import argparse, os, sys, time
|
| 3 |
+
import numpy as np, soundfile as sf
|
| 4 |
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 5 |
+
from audiosr_sdk import AudioSR
|
| 6 |
+
|
| 7 |
+
def main():
|
| 8 |
+
p=argparse.ArgumentParser(description="AudioSR speech super-resolution on AX650")
|
| 9 |
+
p.add_argument("input")
|
| 10 |
+
p.add_argument("-o","--output",default=None)
|
| 11 |
+
p.add_argument("--models",default=os.path.join(os.path.dirname(__file__),"..","models"))
|
| 12 |
+
p.add_argument("--ddim",default=os.path.join(os.path.dirname(__file__),"..","models","audiosr_ddim_10.npz"))
|
| 13 |
+
p.add_argument("--seed",type=int,default=42)
|
| 14 |
+
a=p.parse_args()
|
| 15 |
+
m=AudioSR(os.path.abspath(a.models),seed=a.seed,ddim=os.path.abspath(a.ddim))
|
| 16 |
+
t=time.time(); y,sr=m.enhance(a.input); dt=time.time()-t
|
| 17 |
+
out=a.output or os.path.splitext(a.input)[0]+"_48k.wav"
|
| 18 |
+
sf.write(out,y,sr,subtype="PCM_16")
|
| 19 |
+
d=len(y)/sr
|
| 20 |
+
print(f"AudioSR: {sr} Hz, {d:.2f}s, {dt:.2f}s inference, RTF {dt/max(d,1e-9):.3f}")
|
| 21 |
+
print("output:",out)
|
| 22 |
+
|
| 23 |
+
if __name__=="__main__": main()
|
python/requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy>=1.24
|
| 2 |
+
scipy>=1.10
|
| 3 |
+
soundfile>=0.12
|
| 4 |
+
librosa>=0.10
|
| 5 |
+
pyaxengine
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy>=1.24
|
| 2 |
+
scipy>=1.10
|
| 3 |
+
soundfile>=0.12
|
| 4 |
+
librosa>=0.10
|
| 5 |
+
pyaxengine
|
run_ax650.sh
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# AudioSR.AXERA one-click demo.
|
| 3 |
+
# bash run_ax650.sh [input.wav] [output.wav] [--python]
|
| 4 |
+
set -e
|
| 5 |
+
cd "$(dirname "$0")"
|
| 6 |
+
export LD_LIBRARY_PATH=/soc/lib:${LD_LIBRARY_PATH:-}
|
| 7 |
+
export PYTHONDONTWRITEBYTECODE=1
|
| 8 |
+
INPUT="${1:-samples/listen/16k/speech_2_input_16k.wav}"
|
| 9 |
+
OUTPUT="${2:-output.wav}"
|
| 10 |
+
if [ $# -ge 2 ]; then shift 2; else shift $#; fi
|
| 11 |
+
MODE=cpp; EXTRA=()
|
| 12 |
+
for a in "$@"; do
|
| 13 |
+
case "$a" in
|
| 14 |
+
--python) MODE=python ;;
|
| 15 |
+
--cpp) MODE=cpp ;;
|
| 16 |
+
*) EXTRA+=("$a") ;;
|
| 17 |
+
esac
|
| 18 |
+
done
|
| 19 |
+
if [ ! -x ./bin/audiosr_ax650 ]; then MODE=python; fi
|
| 20 |
+
if [ "$MODE" = cpp ]; then
|
| 21 |
+
exec ./bin/audiosr_ax650 models "$INPUT" "$OUTPUT" "${EXTRA[@]}"
|
| 22 |
+
else
|
| 23 |
+
exec python3 python/demo.py "$INPUT" -o "$OUTPUT" --models models --ddim models/audiosr_ddim_10.npz "${EXTRA[@]}"
|
| 24 |
+
fi
|
samples/listen/16k/speech_2_input_16k.wav
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0c7773c82ce75aa5e8a14d5bb53ee5915dbb65f47272ae9f527ac767c7b1f80c
|
| 3 |
+
size 163884
|
samples/listen/8k/OSR_cn_000_0072_input_8k.wav
ADDED
|
Binary file (82 kB). View file
|
|
|
samples/listen/8k/OSR_cn_000_0072_input_8k_5s.wav
ADDED
|
Binary file (82 kB). View file
|
|
|
samples/listen/8k/OSR_us_000_0010_input_8k.wav
ADDED
|
Binary file (82 kB). View file
|
|
|
samples/listen/8k/OSR_us_000_0010_input_8k_5s.wav
ADDED
|
Binary file (82 kB). View file
|
|
|
samples/listen/output/OSR_cn_000_0072_output_48k.wav
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:aed320d8f74c684f6838eeaa0abbad322ae0be2122e9b7810c376b277dc8c45e
|
| 3 |
+
size 491564
|
samples/listen/output/OSR_us_000_0010_output_48k.wav
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a076dc77eb4e308a14cd1d7aa6e2b062e09f9734abf44d44a612af658ed43bb3
|
| 3 |
+
size 491564
|
samples/listen/output/speech_2_output_48k.wav
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5e8a83e7a346cb3c301b4c9fedc001a3ca74dfd634c6f21581a5e671633def6c
|
| 3 |
+
size 491564
|
samples/listen/spectrograms/OSR_cn_000_0072_input_8k_spec.png
ADDED
|
Git LFS Details
|
samples/listen/spectrograms/OSR_cn_000_0072_output_48k_spec.png
ADDED
|
Git LFS Details
|
samples/listen/spectrograms/OSR_us_000_0010_input_8k_spec.png
ADDED
|
Git LFS Details
|
samples/listen/spectrograms/OSR_us_000_0010_output_48k_spec.png
ADDED
|
Git LFS Details
|
samples/listen/spectrograms/speech_2_input_16k_spec.png
ADDED
|
Git LFS Details
|
samples/listen/spectrograms/speech_2_output_48k_spec.png
ADDED
|
Git LFS Details
|