-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf_helper.py
50 lines (40 loc) · 1.6 KB
/
tf_helper.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
import numpy as np
import tensorflow as tf
# Set the seed value for experiment reproducibility.
seed = 42
tf.random.set_seed(seed)
np.random.seed(seed)
def get_spectrogram(waveform):
# Zero-padding for an audio waveform with less than 16,000 samples.
input_len = 16000
waveform = waveform[:input_len]
zero_padding = tf.zeros(
[16000] - tf.shape(waveform),
dtype=tf.float32)
# Cast the waveform tensors' dtype to float32.
waveform = tf.cast(waveform, dtype=tf.float32)
# Concatenate the waveform with `zero_padding`, which ensures all audio
# clips are of the same length.
equal_length = tf.concat([waveform, zero_padding], 0)
# Convert the waveform to a spectrogram via a STFT.
spectrogram = tf.signal.stft(
equal_length, frame_length=255, frame_step=128)
# Obtain the magnitude of the STFT.
spectrogram = tf.abs(spectrogram)
# Add a `channels` dimension, so that the spectrogram can be used
# as image-like input data with convolution layers (which expect
# shape (`batch_size`, `height`, `width`, `channels`).
spectrogram = spectrogram[..., tf.newaxis]
return spectrogram
def preprocess_audiobuffer(waveform):
"""
waveform: ndarray of size (16000, )
output: Spectogram Tensor of size: (1, `height`, `width`, `channels`)
"""
# normalize from [-32768, 32767] to [-1, 1]
waveform = waveform / 32768
waveform = tf.convert_to_tensor(waveform, dtype=tf.float32)
spectogram = get_spectrogram(waveform)
# add one dimension
spectogram = tf.expand_dims(spectogram, 0)
return spectogram