-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathwhisper.py
More file actions
137 lines (114 loc) · 4.63 KB
/
whisper.py
File metadata and controls
137 lines (114 loc) · 4.63 KB
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
"""LogMelSpectrogram feature extractor for Whisper models."""
import numpy as np
from onnxscript import FLOAT, INT64, script
from onnxscript import opset17 as op
from preprocessors.fbanks import melscale_fbanks
from preprocessors.stft import conv_power_spectrogram, stft_conv_weights
chunk_length = 30
sample_rate = 16_000
n_fft = 400
win_length = 400
hop_length = 160
clamp_min = 1e-10
ln10 = 2.302585092994046
features_length = np.array([chunk_length * sample_rate // hop_length])
melscale_fbanks80 = melscale_fbanks(n_fft // 2 + 1, 0, sample_rate // 2, 80, sample_rate, "slaney", "slaney").astype(
np.float32
)
melscale_fbanks128 = melscale_fbanks(n_fft // 2 + 1, 0, sample_rate // 2, 128, sample_rate, "slaney", "slaney").astype(
np.float32
)
stft_conv_weights_whisper = stft_conv_weights(np.hanning(win_length + 1)[:-1].astype(np.float32))
@script()
def whisper_preprocessor(
waveforms: FLOAT["batch_size", "N"],
waveforms_lens: INT64["batch_size"],
melscale_fbanks: FLOAT[n_fft // 2 + 1, "M"],
):
waveforms = op.Pad(
waveforms,
pads=(chunk_length * sample_rate - op.Shape(waveforms, start=1, end=2)) * op.Constant(value=[0, 0, 0, 1]),
)
waveforms = op.Pad(
waveforms,
pads=op.Constant(value=[0, n_fft // 2, 0, n_fft // 2]),
mode="reflect",
)
hann_window = op.HannWindow(win_length)
image = op.STFT(waveforms, hop_length, hann_window)[:, :-1]
spectrogram = op.ReduceSumSquare(image, axes=[-1], keepdims=0)
mel_spectrogram = op.MatMul(spectrogram, melscale_fbanks)
log_mel_spectrogram = op.Log(op.Clip(mel_spectrogram, clamp_min)) / ln10
log_mel_spectrogram = (op.Max(log_mel_spectrogram, op.ReduceMax(log_mel_spectrogram) - 8) + 4) / 4.0
return op.Transpose(log_mel_spectrogram, perm=[0, 2, 1]), op.ConstantOfShape(
op.Shape(waveforms_lens), value=features_length
)
@script(doc_string="LogMelSpectrogram feature extractor for Whisper models", default_opset=op)
def WhisperPreprocessor80(
waveforms: FLOAT["batch_size", "N"],
waveforms_lens: INT64["batch_size"],
) -> tuple[FLOAT["batch_size", 80, "T"], INT64["batch_size"]]:
features, features_lens = whisper_preprocessor(
waveforms,
waveforms_lens,
melscale_fbanks80,
)
return features, features_lens
@script(doc_string="LogMelSpectrogram feature extractor for Whisper models", default_opset=op)
def WhisperPreprocessor128(
waveforms: FLOAT["batch_size", "N"],
waveforms_lens: INT64["batch_size"],
) -> tuple[FLOAT["batch_size", 128, "T"], INT64["batch_size"]]:
features, features_lens = whisper_preprocessor(
waveforms,
waveforms_lens,
melscale_fbanks128,
)
return features, features_lens
@script()
def whisper_preprocessor_conv(
waveforms: FLOAT["batch_size", "N"],
waveforms_lens: INT64["batch_size"],
melscale_fbanks: FLOAT[n_fft // 2 + 1, "M"],
conv_weights: FLOAT["channels", 1, n_fft],
):
waveforms = op.Pad(
waveforms,
pads=(chunk_length * sample_rate - op.Shape(waveforms, start=1, end=2)) * op.Constant(value=[0, 0, 0, 1]),
)
waveforms = op.Pad(
waveforms,
pads=op.Constant(value=[0, n_fft // 2, 0, n_fft // 2]),
mode="reflect",
)
spectrogram = conv_power_spectrogram(waveforms, conv_weights)[:, :-1]
mel_spectrogram = op.MatMul(spectrogram, melscale_fbanks)
log_mel_spectrogram = op.Log(op.Clip(mel_spectrogram, clamp_min)) / ln10
log_mel_spectrogram = (op.Max(log_mel_spectrogram, op.ReduceMax(log_mel_spectrogram) - 8) + 4) / 4.0
return op.Transpose(log_mel_spectrogram, perm=[0, 2, 1]), op.ConstantOfShape(
op.Shape(waveforms_lens), value=features_length
)
@script(doc_string="LogMelSpectrogram feature extractor for Whisper models (Conv-based STFT)", default_opset=op)
def WhisperPreprocessor80Conv(
waveforms: FLOAT["batch_size", "N"],
waveforms_lens: INT64["batch_size"],
) -> tuple[FLOAT["batch_size", 80, "T"], INT64["batch_size"]]:
features, features_lens = whisper_preprocessor_conv(
waveforms,
waveforms_lens,
melscale_fbanks80,
stft_conv_weights_whisper,
)
return features, features_lens
@script(doc_string="LogMelSpectrogram feature extractor for Whisper models (Conv-based STFT)", default_opset=op)
def WhisperPreprocessor128Conv(
waveforms: FLOAT["batch_size", "N"],
waveforms_lens: INT64["batch_size"],
) -> tuple[FLOAT["batch_size", 128, "T"], INT64["batch_size"]]:
features, features_lens = whisper_preprocessor_conv(
waveforms,
waveforms_lens,
melscale_fbanks128,
stft_conv_weights_whisper,
)
return features, features_lens