InverseMelScale

username: Mukesh1729
cooked: <p>If i get the below error what should my input tensor look like to the inverse mel scale function:</p>
<p>inv_mel = torchaudio.transforms.InverseMelScale(n_stft=1025)</p>
<p>inv_mel(s)</p>
<hr>
<p>TypeError                                 Traceback (most recent call last)</p>
<p> in ()<br>
2 plt.imshow(s)<br>
3 inv_mel = torchaudio.transforms.InverseMelScale(n_stft=1025)<br>
----&gt; 4 inv_mel(s)</p>
<p>1 frames</p>
<p>/usr/local/lib/python3.7/dist-packages/torchaudio/transforms.py in forward(self, melspec)<br>
388         “”"<br>
389         # pack batch<br>
 → 390         shape = melspec.size()<br>
391         melspec = melspec.view(-1, shape[-2], shape[-1])<br>
392</p>
<p>TypeError: ‘int’ object is not callable</p>

username: ptrblck
cooked: <p>Based on the error message it seems that you are using a <code>np.array</code> instead of a tensor, as the former returns an <code>int</code> from the <code>.size</code> attribute:</p>
<pre><code class="lang-python">x = np.random.randn(10)
x.size()
&gt; TypeError: 'int' object is not callable

x = torch.randn(10)
x.size()
&gt; torch.Size([10])
</code></pre>
<p>so you might want to use tensors.</p>

username: Mukesh1729
cooked: <p>Hey, Patrick thanks for getting back. When I use a tensor then i get the following error. I am bit confused what the input should look like given an mel spectrogram of shape: <strong>torch.Size([288, 432, 4])</strong></p>
<hr>
<p>AssertionError                            Traceback (most recent call last)</p>
<p> in ()<br>
7<br>
8 inv_mel = torchaudio.transforms.InverseMelScale(n_stft=1023)<br>
----&gt; 9 inv_mel(sample)</p>
<p>1 frames</p>
<p>/usr/local/lib/python3.7/dist-packages/torchaudio/transforms.py in forward(self, melspec)<br>
394         freq, _ = self.fb.size()  # (freq, n_mels)<br>
395         melspec = melspec.transpose(-1, -2)<br>
 → 396         assert self.n_mels == n_mels<br>
397<br>
398         specgram = torch.rand(melspec.size()[0], time, freq, requires_grad=True,</p>
<p>AssertionError:</p>

username: ptrblck
cooked: <p>I guess the input shape might be wrong.<br>
From the <a href="https://pytorch.org/audio/stable/transforms.html#torchaudio.transforms.InverseMelScale">docs</a>:</p>
<ul>
<li>
<strong>n_mels</strong> (<a href="https://docs.python.org/3/library/functions.html#int"><em>int</em></a><em>,</em> <em>optional</em>) – Number of mel filterbanks. (Default: <code>128</code>)</li>
<li>…</li>
<li>
<strong>melspec</strong> (<em>Tensor</em>) – A Mel frequency spectrogram of dimension (…, <code>n_mels</code>, time)</li>
</ul>
<p>Based on this it seems the <code>n_mels</code> argument is set to 128 by default, while your input tensor has a value of 432 in this dimension.</p>

username: Mukesh1729
cooked: <p>Hey Patrick, so my input tensor can be of shape <strong>torch.Size([288, 432, 4])</strong>  so what do you think my input tensor shape should be in this case?</p>
<p>Why are there three dots here?: A Mel frequency spectrogram of dimension (…, <code>n_mels</code>, time)<br>
does it imply something?</p>
<p>Thanks</p>

username: ptrblck
cooked: <p>The three dots should indicate additional dimensions while the last two dimensions would represent <code>n_mels</code> and <code>time</code>.<br>
Could you set <code>n_mels</code> to <code>432</code> in <code>InverseMelScale</code> as it should work:</p>
<pre><code class="lang-python">inv_mel = torchaudio.transforms.InverseMelScale(n_stft=1023, n_mels=432)
x = torch.randn(288, 432, 4)
out = inv_mel(x)
print(out.shape)
</code></pre>

username: Mukesh1729
cooked: <p>Hey Patrick,<br>
I actually tried the above step you mentioned before by setting n_mels= 432 and then my cell (jupyter nb) ran for a very long time with no outputs.</p>

username: ptrblck
cooked: <p>I think a long runtime is expected, since <code>InverseMelScale</code> uses <code>SGD</code> to solve the mapping.<br>
From the docs:</p>
<blockquote>
<p>Solve for a normal STFT from a mel frequency STFT, using a conversion matrix. This uses triangular filter banks.<br>
It minimizes the euclidian norm between the input mel-spectrogram and the product between the estimated spectrogram and the filter banks using SGD.</p>
</blockquote>
<p>Corresponding <a href="https://github.com/pytorch/audio/blob/e39ece66987b53d33f1976f37db9905370c892d4/torchaudio/transforms.py#L383-L426">lines of code</a>.</p>
<p>You could change the tolerance to stop the optimization earlier, if needed.</p>

username: Mukesh1729
cooked: <p>Hey Patrick, thanks for getting back. I will try it out again <img src="https://discuss.pytorch.org/images/emoji/apple/grinning.png?v=9" title=":grinning:" class="emoji" alt=":grinning:"></p>