Problem with torch.view

username: SilSever
cooked: <p>Sorry, I’m new and this is my first question.</p>
<p>I’m trying to plot a PyTorch tensor image, which shape is (channels, width, height), so for example (3, 256, 256). To plot such image I’m using pyplot.imshow of matplotlib, but this method requires a ndarray which shape is (width, height, channels), so (256, 256, 3).</p>
<p>To do this transformation I always used torch.view. However, is figured out that such method repeats the image as you can see in the left image.</p>
<p>Instead, I have no problem transforming the tensor in numpy and then using img = np.einsum(“xyz → yzx”, img) as you can see in the right image.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/a/cae43bbe76c3168dfac85eb09a1cd2399764a85b.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/cae43bbe76c3168dfac85eb09a1cd2399764a85b" title="combine_images"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/a/cae43bbe76c3168dfac85eb09a1cd2399764a85b_2_690x345.jpeg" alt="combine_images" data-base62-sha1="sWRr4ZfyjC11E4ptUKfQze5ks0X" width="690" height="345" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/a/cae43bbe76c3168dfac85eb09a1cd2399764a85b_2_690x345.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/c/a/cae43bbe76c3168dfac85eb09a1cd2399764a85b.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/c/a/cae43bbe76c3168dfac85eb09a1cd2399764a85b.jpeg 2x" data-dominant-color="C6C4C0"><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">combine_images</span><span class="informations">942×472 121 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>Could you help me to understand the differences between the methods? Moreover, torch.view could be used for transpose? Thanks.</p>
<pre><code class="lang-auto">   ### This code produces the second image ###

    # Shape: out[2] = (3, 256, 256)
    img = out[2].numpy()
    img = np.einsum("xyz -&gt; yzx", img)
    plt.imshow(img)
    plt.show()
    plt.clf()
    plt.close()


   ### This code produces the first image ###

    # Shape: out[2] = (3, 256, 256)
    img = out[2].view(256, 256, 3)
    plt.imshow(img)
    plt.show()
    plt.clf()
    plt.close()
</code></pre>

username: AladdinPerzon
cooked: <p>My thinking is that what we want is to permute the axis so we want to do</p>
<pre><code class="lang-auto">x = torch.randn((3, 256, 256))
x = x.permute(1,2,0) # shape (256,256,3)
</code></pre>
<p>And using einsum is equivalent to permuting the axis. This is not what <code>torch.view</code> does. I think one explanation of what view does is that it will rearrange the dimensions to match, not swap them which is what permute is doing. You might want to check out <a href="https://discuss.pytorch.org/t/tensor-view-is-misleading/20553/2">this</a> post to see a better example of what <code>view</code> is doing</p>

username: SilSever
cooked: <p>Thanks for your explanation!</p>