How does torchaudio.transforms.DownmixMono work?

wzehui: <p>Hello,</p>
<p>I am getting confused when I use torchaudio.transforms.DownmixMono.<br>
first, I load my data with sound = torchaudio.load(). This is correct that sound[0] is two channel data with torch.Size is ([2, 132300]) and sound[1] = 22050, which is the sample rate.Then I use soundData = torchaudio.transforms.DownmixMono(sound[0]) to downsample. But the result looks weird with torch.Size([2, 1]). If I understand it correctly, I can get soundData, which has only one channel? What’s wrong with that?</p>
<p>I check the document as well,  the input format should be: tensor (Tensor): Tensor of audio of size (c x n) or (n x c), what does (c x n) mean?</p>

ptrblck: <p>It looks like you are passing your data as <code>[channels, length]</code>, so you should pass <code>channels_first=True</code>. While the docs says:</p>
<blockquote>
<p>channels_first (bool): Downmix across channels dimension.  Default: <code>True</code></p>
</blockquote>
<p>The default seems to be in fact <code>None</code>, which results in dim1 as the default channel dimension:</p>
<pre data-code-wrap="python"><code class="lang-python">channels_first = None
ch_dim = int(not channels_first)
print(ch_dim)
&gt; 1
</code></pre>
<p><code>c x n</code> should correspond to <code>[channels, length]</code>.</p>
<p>Thanks for reporting this problem! I’ve created an issue <a href="https://github.com/pytorch/audio/issues/93">here</a>.</p>

wzehui: <p>Thanks for your reply! This is exactly where the problem is!</p>

100deep1001: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>,<br>
How should I pass the <em>channels_first</em>  keyword to the DownmixMono  function?</p>
<p>I get the following error.<br>
<code>torchaudio.transforms.DownmixMono()(sound[0],channels_first = True)</code><br>
<code>__init__() got an unexpected keyword argument 'channels_first'</code></p>
<p>However this works but I get wrong dimensions as mentioned by OP:<br>
<code>torchaudio.transforms.DownmixMono()(sound[0])</code></p>

ptrblck: <p>You should pass it while initializing the transformation:</p>
<pre><code class="lang-python">transform = torchaudio.transforms.DownmixMono(channels_first=True)
</code></pre>

100deep1001: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Thanks for the reply;<br>
I tried that too… but can’t get it work<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/7/7751c33e5bb0ca6cf18c6786a6dde2bd95d1e037.png" data-download-href="https://discuss.pytorch.org/uploads/default/7751c33e5bb0ca6cf18c6786a6dde2bd95d1e037" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/2X/7/7751c33e5bb0ca6cf18c6786a6dde2bd95d1e037.png" alt="image" data-base62-sha1="h1xZFC2HGvoCD27770bTwHV62b5" width="690" height="190" data-dominant-color="F4F3F5"><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">image</span><span class="informations">795×220 8.61 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>
<p><code>transform = torchaudio.transforms.DownmixMono(channels_first=True) __init__() got an unexpected keyword argument 'channels_first'</code></p>
<p>Just to add up; I downloaded torchaudio as follows:</p>
<pre><code class="lang-auto">!git clone https://github.com/pytorch/audio.git
os.chdir("audio")
!git checkout 301e2e9
!python setup.py install
</code></pre>

ptrblck: <p>This argument was introduced after your specified commit hash.<br>
If you look at the file, you’ll see that the <code>__init__</code> method just contains a <code>pass</code> statement.</p>

100deep1001: <p>Sure… got it! Thanks for the help, I will look for more recent hashes.</p>

100deep1001: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>  Tried all possible hashes but didn’t succeed.</p>
<p>Could you guide to a stable version/hash? I am on Google Colab.</p>

ptrblck: <p>Isn’t the current master working (<code>5c9d33d</code>)?</p>

100deep1001: <p>No,</p>
<p>Here’s  what I tried:</p>
<pre><code class="lang-auto">!apt-get install sox libsox-dev libsox-fmt-all
!git clone https://github.com/pytorch/audio.git
import os
os.chdir("audio")
!git checkout 5c9d33d  #301e2e9 d92de5b
!python setup.py install
import torchaudio
</code></pre>
<p>Gives me following error:<br>
<code>RuntimeError: Failed to parse the argument list of a type annotation: name 'Optional' is not defined</code></p>

ptrblck: <p>I just installed the current master without a problem and it seems your error might be related to <a href="https://github.com/pytorch/audio/issues/121" rel="nofollow noopener">this issue</a>.</p>