Tensor into image

randino: <p>how I transform tensor to an image</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/4/44bfa98725edcbea45020b56e57e442c86199496.png" alt="image" data-base62-sha1="9Ob6lCIRE7xq4sD0O6PMsYfz9pc" width="316" height="252"></p>
<pre><code class="lang-auto">torch.Size([3687, 1, 5, 5])
</code></pre>

arman-yekkehkhani: <p>Your tensor is already a set of images. The size <code>(3687, 1, 5, 5)</code> means the tensor contains a <code> batch</code> of 3687 images of size <code>(5, 5)</code> with 1 channel.</p>

randino: <p><a class="mention" href="/u/arman-yekkehkhani">@arman-yekkehkhani</a>  yes ,but I want to recover the first and the second in order to concatenate them to result in a final image</p>

arman-yekkehkhani: <p>You can pick any slice from that tensor,  for example:</p>
<pre><code class="lang-python">a = torch.ones((3678, 1, 5, 5))
first = a[0] # size (1, 5,  5)
second = a[1] # size (1, 5, 5)
</code></pre>

randino: <p><a class="mention" href="/u/arman-yekkehkhani">@arman-yekkehkhani</a>  thanks for reply, but I want to display part of the image of size 100 * 100 with concatenation these  tensors 3678</p>

arman-yekkehkhani: <p>Could please explain the problem in more detail. What is that tensor and what do you want to obtain from that?</p>

randino: <p>image1= image.extract_patches_2d(imag[‘taille100’], ((5,5)))<br>
<a class="mention" href="/u/arman-yekkehkhani">@arman-yekkehkhani</a>   I want to get the (i,j) of each patches : I want to retrieve the coordinates of x, y of patches, which is retrieved from the first image (imag)</p>

arman-yekkehkhani: <p>Assuming you are using sklearn for extracting patches, you can use <a href="https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.image.reconstruct_from_patches_2d.html#sklearn.feature_extraction.image.reconstruct_from_patches_2d" rel="noopener nofollow ugc">reconstruct_from_patches_2d</a> to reconstruct your image.</p>
<p>P.S. on the first edition of your response, you used <code>torch.exp</code> mistakenly instead of <code>torch.nn.softmax()</code> for computing softmax.</p>

randino: <p><a class="mention" href="/u/arman-yekkehkhani">@arman-yekkehkhani</a>  how to get the values of x, y of the patch?<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/0/6/06e1553b885c8577ba57c280e27febe7d966d4de.png" alt="image" data-base62-sha1="YRDA5cX2fSqhu3LHnobhx28zbE" width="322" height="129"></p>

arman-yekkehkhani: <p>Assume your image is of size <code>h x w </code>. Then the number of total patches with patch size <code>a x b</code> is calculated as <code>[ h - a + 1 ] x [ w - b + 1]</code>.  So given the index of a patch (what you have), you can compute the location of the top-left corner of a patch according to the following formula:</p>
<pre><code class="lang-python">x = idx % [h - a + 1]
y = idx // [h - a + 1]
</code></pre>

randino: <p><a class="mention" href="/u/arman-yekkehkhani">@arman-yekkehkhani</a> the h*w it’s the size of original image?  what does mean  idx?</p>

arman-yekkehkhani: <p>yes. And <code>idx</code> is the index of a batch. In your example, there are 3687 patches.</p>

randino: <p><a class="mention" href="/u/arman-yekkehkhani">@arman-yekkehkhani</a><br>
x = idx % [h - a + 1]<br>
y = idx // [h - a + 1]</p>
<p>I want to calculate x, y for each patches of image (xa, ya), but just one patch</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/8/a/8a04d133f21d6b740c858c13b452bbf7bf3e0451.png" alt="image" data-base62-sha1="jGYd7RvJzBMx7sVRSCa3CfZmlMt" width="217" height="291"></p>