Error in reconstruction patches cnn

randino: <p>I want to reconstruct the original image</p>
<pre><code class="lang-auto">patch_size = (5,5)

new_image = image.reconstruct_from_patches_2d(val_x, ((5, 5, 1)))
plt.subplot(1, 2, 1)
plt.title('Image')
plt.imshow(new_image,cmap='gray' )
</code></pre>
<p>error:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/4/c4d1f77ba51e664dfbff11d42881f001781abb50.png" data-download-href="https://discuss.pytorch.org/uploads/default/c4d1f77ba51e664dfbff11d42881f001781abb50" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/4/c4d1f77ba51e664dfbff11d42881f001781abb50.png" alt="image" data-base62-sha1="s59r1q25ZV2utZadlsYwOnr23Be" width="690" height="324" data-dominant-color="F6F6FA"><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">732×344 12.6 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>help me plz</p>

ptrblck: <p>I guess you are trying to pass <code>val_x</code> as a tensor, while <code>sklearn</code> expects a numpy array, so you could try to pass it as <code>val_x.numpy()</code>.</p>

randino: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>  the size of original image is (100*100), so I took 40%test. in 40%test I divised image into 3678 patches. But is the same problem</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/6/d62e8d5cce7dbd488438f59909fd501f55661376.png" data-download-href="https://discuss.pytorch.org/uploads/default/d62e8d5cce7dbd488438f59909fd501f55661376" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/d/6/d62e8d5cce7dbd488438f59909fd501f55661376.png" alt="image" data-base62-sha1="uyJUOfHsfJIq6NSeT1H8zFaNG8C" width="690" height="340" data-dominant-color="F6F6FA"><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">740×365 12.7 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>
<pre><code class="lang-auto">new_image = image.reconstruct_from_patches_2d(val_x.numpy(),(100*100))
plt.subplot(1, 2, 1)
plt.title('Image')
plt.imshow(new_image,cmap='gray' )
</code></pre>

ptrblck: <p>The passed <code>image_size</code> is wrong. The <a href="https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.image.reconstruct_from_patches_2d.html">docs</a> explain the input as:</p>
<blockquote>
<p>image_sizetuple of int (image_height, image_width) or (image_height, image_width, n_channels)<br>
The size of the image that will be reconstructed.</p>
</blockquote>

randino: <p><a class="mention" href="/u/ptrblck">@ptrblck</a></p>
<pre><code class="lang-auto">patch_size = (5,5)

new_image = image.reconstruct_from_patches_2d(val_x.numpy(),(50,50))
plt.subplot(1, 2, 1)
plt.title('Image')
plt.imshow(new_image,cmap='gray' )
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/0/40458a6687e47a1e8434cd6845245cb66dcc6b76.png" data-download-href="https://discuss.pytorch.org/uploads/default/40458a6687e47a1e8434cd6845245cb66dcc6b76" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/0/40458a6687e47a1e8434cd6845245cb66dcc6b76.png" alt="image" data-base62-sha1="9azxZkYdayZypinWqNoJ7KNhtZQ" width="690" height="284" data-dominant-color="F7F7FA"><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">899×371 20.9 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>

ptrblck: <p>It seems your shapes are still wrong, so please check the <a href="https://scikit-learn.org/stable/modules/feature_extraction.html#image-feature-extraction">example</a> and make sure to use the expected shape requirements.</p>

randino: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>  plz , my tensor is equal zeros, but in the image attach values are not equal to tensors</p>
<pre><code class="lang-auto">patch_size = (5,5)
print(val_x.shape)
new_image = image.reconstruct_from_patches_2d(val_x.numpy(),(1,5,5))
plt.subplot(5, 5, 1)
plt.title('Image')
plt.imshow(np.squeeze(new_image),cmap='gray' )
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/5/8549abc78a6b4db0e27bc0f4438f863e233bf2e1.png" data-download-href="https://discuss.pytorch.org/uploads/default/8549abc78a6b4db0e27bc0f4438f863e233bf2e1" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/8/5/8549abc78a6b4db0e27bc0f4438f863e233bf2e1.png" alt="image" data-base62-sha1="j17lgrGS6oUNVNIPHodfqLmG4aR" width="690" height="366" data-dominant-color="F9F9F9"><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">854×453 18.3 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>image1 = image.extract_patches_2d(mat_diff[‘nouvelle’], ((5,5)))<br>
I toolk 40% of image1 for testing (val_x), I find 3687 patches , then I realised the reconstrcution of this val_x .</p>