Swapping dimensions and then swapping back messes up the images

kinwai_cheuk: <p>I have few spectrograms which look like this:</p>
<pre><code class="lang-auto">x = torch.load('input')
fig, axs = plt.subplots(2, 2, figsize=(24,8))
axs = axs.flat
for idx, i in enumerate(x.cpu().detach().numpy()):
    i = scaler.fit_transform(i)
    axs[idx].imshow(i, origin='lower', cmap='jet')
    axs[idx].axis('off')
    
fig.tight_layout()
fig.suptitle('Correct image', size=30)
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/5/f596a7f0b44435ff976d92e4c6f602d11f0500b5.png" data-download-href="https://discuss.pytorch.org/uploads/default/f596a7f0b44435ff976d92e4c6f602d11f0500b5" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/5/f596a7f0b44435ff976d92e4c6f602d11f0500b5_2_690x223.png" alt="image" data-base62-sha1="z2zL4Hl70ZpQ2vpKRxhtg0sTYWN" width="690" height="223" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/5/f596a7f0b44435ff976d92e4c6f602d11f0500b5_2_690x223.png, https://discuss.pytorch.org/uploads/default/optimized/3X/f/5/f596a7f0b44435ff976d92e4c6f602d11f0500b5_2_1035x334.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/5/f596a7f0b44435ff976d92e4c6f602d11f0500b5_2_1380x446.png 2x" data-dominant-color="050C78"><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">1720×558 318 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>However, my neural network requires some axes swapping along the last two dimension. I realized that swapping the images distort the images,</p>
<pre><code class="lang-auto">x = x.transpose(-1,-2)

fig, axs = plt.subplots(2, 2, figsize=(24,8))
axs = axs.flat
for idx, i in enumerate(x.cpu().detach().numpy()):
    i = scaler.fit_transform(i)
    axs[idx].imshow(i.swapaxes(0,1), origin='lower', cmap='jet')
    axs[idx].axis('off')
    
fig.tight_layout()
fig.suptitle('Wrong image', size=30)
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/2/625d8496b7f06a46662c4d142ea6330bde32eba6.png" data-download-href="https://discuss.pytorch.org/uploads/default/625d8496b7f06a46662c4d142ea6330bde32eba6" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/2/625d8496b7f06a46662c4d142ea6330bde32eba6_2_690x223.png" alt="image" data-base62-sha1="e2b9vpJHeHcd21zTPtnOydeqiPk" width="690" height="223" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/2/625d8496b7f06a46662c4d142ea6330bde32eba6_2_690x223.png, https://discuss.pytorch.org/uploads/default/optimized/3X/6/2/625d8496b7f06a46662c4d142ea6330bde32eba6_2_1035x334.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/6/2/625d8496b7f06a46662c4d142ea6330bde32eba6_2_1380x446.png 2x" data-dominant-color="0D2994"><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">1720×558 552 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>What am I doing wrong here?<br>
You can download my images below<br>
<a href="https://sutdapac-my.sharepoint.com/:u:/g/personal/kinwai_cheuk_mymail_sutd_edu_sg/Eb711s15rkFDhcTTUh4vRXkB55rZ3WCY_OS2f0rDsbK8qg?e=SR94eu" class="onebox" target="_blank" rel="noopener nofollow ugc">https://sutdapac-my.sharepoint.com/:u:/g/personal/kinwai_cheuk_mymail_sutd_edu_sg/Eb711s15rkFDhcTTUh4vRXkB55rZ3WCY_OS2f0rDsbK8qg?e=SR94eu</a></p>

ptrblck: <p>Using your code snippet I get the same output for this dummy example:</p>
<pre><code class="lang-python"># Create dummy images
x = torch.zeros(4, 100, 100)
x[0, 25, :] = 1.
x[1, 75, :] = 1.
x[2, :, 25] = 1.
x[3, :, 75] = 1.

fig, axs = plt.subplots(2, 2, figsize=(24,8))
axs = axs.flat
for idx, i in enumerate(x.cpu().detach().numpy()):
    axs[idx].imshow(i, origin='lower', cmap='jet')
    axs[idx].axis('off')
fig.tight_layout()
fig.suptitle('Correct image', size=30)


x = x.transpose(-1,-2)
fig, axs = plt.subplots(2, 2, figsize=(24,8))
axs = axs.flat
for idx, i in enumerate(x.cpu().detach().numpy()):
    axs[idx].imshow(i.swapaxes(0,1), origin='lower', cmap='jet')
    axs[idx].axis('off')
    
fig.tight_layout()
fig.suptitle('Wrong image', size=30)
</code></pre>

kinwai_cheuk: <p>Hi ptrblck, I realized that there is something wrong with the normalization.<br>
But I don’t understand how swapping the axes affect the normalization. (I am normalizing each of the image to range from 0 to 1)</p>

ptrblck: <p>If you are using <a href="https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html">sklearn.preprocessing.StandardScaler</a>, note that the input is expected to have the shape <code>[n_samples, n_features]</code>, while the mean and var have the shape <code>[n_features,]</code>. Passing your original input and the transposed one would thus result in a different scaling.</p>