question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`STFT` layer output shape deviates from `STFTTflite` layer in batch dimension

See original GitHub issue

Use Case

I want to convert a STFT layer in my model to a STFTTflite to deploy it to my mobile device. In the documentation I found that another dimension is added to account for complex numbers. But I also encountered a behaviour that is not documented.

Expected Behaviour

input_shape = (2048, 1)  # mono signal

model = keras.models.Sequential()  # TFLite incompatible model
model.add(kapre.STFT(n_fft=1024, hop_length=512, input_shape=input_shape))

tflite_model = keras.models.Sequential()  # TFLite compatible model
tflite_model.add(kapre.STFTTflite(n_fft=1024, hop_length=512, input_shape=input_shape))

model has the output shape (None, 3, 513, 1). Therefore, tflite_model should have the output shape (None, 3, 513, 1, 2).

Observed Behaviour

The output shape of tflite_model is (1, 3, 513, 1, 2) instead of (None, 3, 513, 1, 2).

Problem Solution

  • If this behaviour is unwanted:
    • Change the model output format so that the batch dimension is correctly shaped.
  • Otherwise:
    • Explain in the documentation why the batch dimension is shaped to 1.
    • Explain in the documentation how to include this layer into models which expect the batch dimension to be shaped None.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kenders2000commented, Aug 25, 2021

Hi,

I can see in the model summary that while the input is None theSTFTTflite layers still have a batch size of 1. So while I would expect this model to convert and run fine (when you provide a batch size of one), you would still need resize the input dimension of the resulting tflite file to have a batch size of 1.

E.g. using resize_tensor_input()

Cheers

Paul

0reactions
keunwoochoicommented, Nov 20, 2021

Thank you so much for everyone!

Read more comments on GitHub >

github_iconTop Results From Across the Web

time_frequency_tflite — Kapre 2017 documentation
A Short-time Fourier transform layer (tflite compatible). ... If output_data_format == 'channels_last' , the output shape is (batch, time, freq, channel, ...
Read more >
Need tf.signal.rfft op in TFLite · Issue #27303 - GitHub
Changing the output tensor to the pcm tensor instead of stft works fine converter = tf.lite.TFLiteConverter.from_session(sess, [waveform], ...
Read more >
Tensorflow STFT Layer - Kaggle
TensorFlow STFT and STFT Inverse Layers. This notebook is based on TensorFlow tutorial. The tutorial is about dealing with Audio data, with the...
Read more >
tf.signal.stft | TensorFlow v2.11.0
Computes the [Short-time Fourier Transform][stft] of signals. ... The size of the FFT to apply. If not provided, uses the smallest power of ......
Read more >
STFT output shape - PyTorch Forums
Hi,. I am confused about the output shape from STFT. Given print (y.shape) s = torch.stft(y, frame_length=128, hop=32) print (s.shape).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found