View source on GitHub
|
Layer to compute the Short-Time Fourier Transform (STFT) on a 1D signal.
Inherits From: Layer, Operation
tf.keras.layers.STFTSpectrogram(
mode='log',
frame_length=256,
frame_step=None,
fft_length=None,
window='hann',
periodic=False,
scaling='density',
padding='valid',
expand_dims=False,
data_format=None,
**kwargs
)
A layer that computes Spectrograms of the input signal to produce a spectrogram. This layers utilizes Short-Time Fourier Transform (STFT) by The layer computes Spectrograms based on STFT by utilizing convolution kernels, which allows parallelization on GPUs and trainable kernels for fine-tuning support. This layer allows different modes of output (e.g., log-scaled magnitude, phase, power spectral density, etc.) and provides flexibility in windowing, padding, and scaling options for the STFT calculation.
Examples:
Apply it as a non-trainable preprocessing layer on 3 audio tracks of 1 channel, 10 seconds and sampled at 16 kHz.
layer = keras.layers.STFTSpectrogram(mode='log',frame_length=256,frame_step=128, # 50% overlapfft_length=512,window="hann",padding="valid",trainable=False, # non-trainable, preprocessing only)layer(keras.random.uniform(shape=(3, 160000, 1))).shape(3, 1249, 257)
Apply it as a trainable processing layer on 3 stereo audio tracks of 2 channels, 10 seconds and sampled at 16 kHz. This is initialized as the non-trainable layer, but then can be trained jointly within a model.
layer = keras.layers.STFTSpectrogram(mode='log',frame_length=256,frame_step=128, # 50% overlapfft_length=512,window="hamming", # hamming windowing functionpadding="same", # padding to preserve the time dimensiontrainable=True, # trainable, this is the default in keras)layer(keras.random.uniform(shape=(3, 160000, 2))).shape(3, 1250, 514)
Similar to the last example, but add an extra dimension so the output is an image to be used with image models. We apply this here on a signal of 3 input channels to output an image tensor, hence is directly applicable with an image model.
layer = keras.layers.STFTSpectrogram(mode='log',frame_length=256,frame_step=128,fft_length=512,padding="same",expand_dims=True, # this adds the extra dimension)layer(keras.random.uniform(shape=(3, 160000, 3))).shape(3, 1250, 257, 3)
Raises | |
|---|---|
ValueError
|
If an invalid value is provided for "mode", "scaling",
"padding", or other input arguments.
|
TypeError
|
If the input data type is not one of "float16",
"float32", or "float64".
|
Input shape | |
|---|---|
A 3D tensor of shape (batch_size, time_length, input_channels), if
data_format=="channels_last", and of shape
(batch_size, input_channels, time_length) if
data_format=="channels_first", where time_length is the length of
the input signal, and input_channels is the number of input channels.
The same kernels are applied to each channel independently.
|
Output shape | |
|---|---|
If data_format=="channels_first" and not expand_dims, a 3D tensor:
(batch_size, input_channels * freq_channels, new_time_length)
If data_format=="channels_last" and not expand_dims, a 3D tensor:
(batch_size, new_time_length, input_channels * freq_channels)
If data_format=="channels_first" and expand_dims, a 4D tensor:
(batch_size, input_channels, new_time_length, freq_channels)
If data_format=="channels_last" and expand_dims, a 4D tensor:
(batch_size, new_time_length, freq_channels, input_channels)
where |
Methods
from_config
@classmethodfrom_config( config )
Creates an operation from its config.
This method is the reverse of get_config, capable of instantiating the
same operation from the config dictionary.
if "dtype" in config and isinstance(config["dtype"], dict):
policy = dtype_policies.deserialize(config["dtype"])
| Args | |
|---|---|
config
|
A Python dictionary, typically the output of get_config.
|
| Returns | |
|---|---|
| An operation instance. |
View source on GitHub