-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* working on example * fix window function in backend * add release note; bump version Co-authored-by: Keunwoo Choi <[email protected]>
- Loading branch information
1 parent
1988e84
commit 3ca2a9c
Showing
6 changed files
with
125 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,62 @@ | ||
One-shot example | ||
^^^^^^^^^^^^^^^^ | ||
Examples | ||
======== | ||
|
||
How To Import | ||
------------- | ||
|
||
.. code-block:: python | ||
import kapre # to import the whole library | ||
from kapre import ( # `time_frequency` layers can be directly imported from `kapre` | ||
STFT, | ||
InverseSTFT, | ||
Magnitude, | ||
Phase, | ||
MagnitudeToDecibel, | ||
ApplyFilterbank, | ||
Delta, | ||
ConcatenateFrequencyMap, | ||
) | ||
from kapre import ( # `signal` layers can be also directly imported from kapre | ||
Frame, | ||
Energy, | ||
MuLawEncoding, | ||
MuLawDecoding, | ||
LogmelToMFCC, | ||
) | ||
# from kapre import backend # we can do this, but `backend` might be a too general name | ||
import kapre.backend # for namespace sanity, you might prefer this | ||
from kapre import backend as kapre_backend # or maybe this | ||
from kapre.composed import ( # function names in `composed` are purposefully verbose. | ||
get_stft_magnitude_layer, | ||
get_melspectrogram_layer, | ||
get_log_frequency_spectrogram_layer, | ||
get_perfectly_reconstructing_stft_istft, | ||
get_stft_mag_phase, | ||
get_frequency_aware_conv2d, | ||
) | ||
Use STFT Magnitude | ||
------------------ | ||
|
||
.. code-block:: python | ||
from tensorflow.keras.models import Sequential | ||
from tensorflow.keras.layers import Conv2D, BatchNormalization, ReLU, GlobalAveragePooling2D, Dense, Softmax | ||
from kapre import STFT, Magnitude, MagnitudeToDecibel | ||
from kapre.composed import get_melspectrogram_layer, get_log_frequency_spectrogram_layer, get_stft_magnitude_layer | ||
# 6 channels (!), maybe 1-sec audio signal, for an example. | ||
input_shape = (6, 44100) | ||
sr = 44100 | ||
model = Sequential() | ||
# A STFT layer | ||
model.add(STFT(n_fft=2048, win_length=2018, hop_length=1024, | ||
window_fn=None, pad_end=False, | ||
input_data_format='channels_last', output_data_format='channels_last', | ||
input_shape=input_shape)) | ||
model.add(Magnitude()) | ||
model.add(MagnitudeToDecibel()) # these three layers can be replaced with get_stft_magnitude_layer() | ||
# Alternatively, you may want to use a melspectrogram layer | ||
# melgram_layer = get_melspectrogram_layer() | ||
# or log-frequency layer | ||
# log_stft_layer = get_log_frequency_spectrogram_layer() | ||
# add more layers as you want | ||
model.add(Conv2D(32, (3, 3), strides=(2, 2))) | ||
model.add(BatchNormalization()) | ||
model.add(ReLU()) | ||
model.add(GlobalAveragePooling2D()) | ||
model.add(Dense(10)) | ||
model.add(Softmax()) | ||
# Compile the model | ||
model.compile('adam', 'categorical_crossentropy') # if single-label classification | ||
# train it with raw audio sample inputs | ||
# for example, you may have functions that load your data as below. | ||
x = load_x() # e.g., x.shape = (10000, 6, 44100) | ||
y = load_y() # e.g., y.shape = (10000, 10) if it's 10-class classification | ||
# then.. | ||
model.fit(x, y) | ||
# Done! | ||
from tensorflow.keras.models import Sequential | ||
from kapre import STFT, Magnitude, MagnitudeToDecibel | ||
from kapre.composed import get_stft_magnitude_layer | ||
|
||
sampling_rate = 16000 # sampling rate of your input audio | ||
duration = 20.0 # duration of the audio | ||
num_channel = 2 # number of channels of the audio | ||
input_shape = (num_channel, int(sampling_rate * duration)) # let's follow `channels_last` convention even for audio | ||
|
||
model = Sequential() | ||
model.add(STFT(n_fft=2048, win_length=2018, hop_length=1024, | ||
window_name='hann_window', pad_end=False, | ||
input_data_format='channels_last', output_data_format='channels_last', | ||
input_shape=input_shape)) | ||
model.add(Magnitude()) | ||
model.add(MagnitudeToDecibel()) # these three layers can be replaced with get_stft_magnitude_layer() | ||
|
||
|
||
* See the Jupyter notebook at the `example folder <https://github.com/keunwoochoi/kapre/tree/master/examples>`_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
__version__ = '0.3.3' | ||
__version__ = '0.3.4' | ||
VERSION = __version__ | ||
|
||
from . import composed | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters