Need help to get torchaudio.Transforms.TimeStretch to work

ForBo7: <p>Hello.</p>
<p>I’m trying to use <code>torchaudio.Transforms.TimeStretch</code>, on a logarithmic spectrogram by following the <a href="https://pytorch.org/audio/stable/tutorials/audio_feature_augmentation_tutorial.html#timestretch" rel="noopener nofollow ugc">PyTorch docs</a>, but get the following error:</p>
<p><code>RuntimeError: The size of tensor a (1025) must match the size of tensor b (201) at non-singleton dimension 1</code></p>
<details>
<summary>
Full Traceback</summary>
<pre><code class="lang-auto">---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_27/3556733881.py in &lt;module&gt;
      1 str = T.TimeStretch()
      2 rate = 1.2
----&gt; 3 spec_ = str(lg_spec_db, rate)

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1192         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1193                 or _global_forward_hooks or _global_forward_pre_hooks):
-&gt; 1194             return forward_call(*input, **kwargs)
   1195         # Do not call functions when jit is used
   1196         full_backward_hooks, non_full_backward_hooks = [], []

/opt/conda/lib/python3.7/site-packages/torchaudio/transforms/_transforms.py in forward(self, complex_specgrams, overriding_rate)
   1100         else:
   1101             rate = overriding_rate
-&gt; 1102         return F.phase_vocoder(complex_specgrams, rate, self.phase_advance)
   1103 
   1104 

/opt/conda/lib/python3.7/site-packages/torchaudio/functional/functional.py in phase_vocoder(complex_specgrams, rate, phase_advance)
    777     norm_1 = complex_specgrams_1.abs()
    778 
--&gt; 779     phase = angle_1 - angle_0 - phase_advance
    780     phase = phase - 2 * math.pi * torch.round(phase / (2 * math.pi))
    781 
</code></pre>
</details>
<h2>
<a name="how-i-created-the-logarithmic-spectrogram-1" class="anchor" href="#how-i-created-the-logarithmic-spectrogram-1"></a>How I Created the Logarithmic Spectrogram</h2>
<pre><code class="lang-auto">import torchaudio

wvfrm, sr = torchaudio.load(aud_files[0])
</code></pre>
<pre><code class="lang-auto">import torchaudio.transfroms as T

trans = T.Spectrogram(n_fft=2048)
spec = trans(wvfrm); spec.shape
</code></pre>
<pre><code class="lang-auto">spec_db = T.AmplitudeToDB()(spec)
lg_spec_db = (spec_db + torch.abs(spec_db.min())).log1p()
</code></pre>
<h2>
<a name="how-i-tried-to-timestretch-2" class="anchor" href="#how-i-tried-to-timestretch-2"></a>How I Tried to TimeStretch</h2>
<pre><code class="lang-auto">str = T.TimeStretch()
rate = 1.2
spec_ = str(lg_spec_db, rate)
</code></pre>
<p>However, this results in the error I pasted above.</p>
<p>The shape of my spectrogram tensor is <code>torch. Size([1, 1025, 2074])</code>. I’ve tried playing around with the dimensions such as excluding some of them, but with no success.</p>
<p>I’m not quite sure how I should go about correctly doing this.</p>
<p>I’d appreciate any input or suggestions! If anymore information is needed please do let me know!</p>

ptrblck: <p>Based on the error message it seem the number of filter banks defined by <code>n_freq</code> in <code>TimeStretch</code> is not correct and you would have to correct it.</p>

ForBo7: <p>Thanks for the response!</p>
<p>Right, I see. So I fiddled around with <code>n_freq</code> and figured out I have to set it <code>1025</code>, so it matches the size of the second dimension in the spectrogram tensor.</p>
<p>However, after doing that, I get a complex64 tensor. I can’t use <code>plt.imsave</code> to convert the tensor to an image since it’s complex64, so I tried using <code>torch.view_as_real</code>. Not sure if that’s right/correct, but it does seem to do the trick I think.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/5/c59dac6566365bdd1ea2139b128e4152ccefc0c7.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/c59dac6566365bdd1ea2139b128e4152ccefc0c7" title="Untitled"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c59dac6566365bdd1ea2139b128e4152ccefc0c7_2_353x500.jpeg" alt="Untitled" data-base62-sha1="scbSh8UwqNQ32ztFg8Rqsp74DJl" width="353" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c59dac6566365bdd1ea2139b128e4152ccefc0c7_2_353x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c59dac6566365bdd1ea2139b128e4152ccefc0c7_2_529x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c59dac6566365bdd1ea2139b128e4152ccefc0c7_2_706x1000.jpeg 2x" data-dominant-color="D4E3D4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Untitled</span><span class="informations">2116×2993 363 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>