Reconstructing an image after using extract_image_patches : <p>I have an autoencoder that takes an image as an input and produces a new image as an output.</p>

<p>The input image (1x1024x1024x3) is split into patches (1024x32x32x3) before being fed to the network.</p>

<p>Once I have the output, also a batch of patches size 1024x32x32x3, I want to be able to reconstruct a 1024x1024x3 image. I thought I had this sussed by simply reshaping, but here's what happened.</p>

<p>First, the image as read by Tensorflow:
<img src="https://i.imgur.com/euaZxKf.png" alt="Input image"></p>

<p>I patched the image with the following code</p>

<pre><code>patch_size = [1, 32, 32, 1]
patches = tf.extract_image_patches([image],
    patch_size, patch_size, [1, 1, 1, 1], 'VALID')
patches = tf.reshape(patches, [1024, 32, 32, 3])
</code></pre>

<p>Here are a couple of patches from this image:</p>

<p><img src="https://i.imgur.com/oseI4Cv.png[/img]" alt="Patched input #168"> <img src="https://i.imgur.com/s7g3l2B.png[/img]" alt="Patched input #169"></p>

<p>But it's when I reshape this patch data back into an image that things go pear-shaped.</p>

<pre><code>reconstructed = tf.reshape(patches, [1, 1024, 1024, 3])
converted = tf.image.convert_image_dtype(reconstructed, tf.uint8)
encoded = tf.image.encode_png(converted)
</code></pre>

<p><img src="https://i.imgur.com/XI1CZwp.jpg[/img]" alt="Reconstructed output"></p>

<p>In this example, no processing has been done between patching and reconstructing. I have made a <a href="https://pastebin.com/vPyRGcQG" rel="noreferrer">version of the code</a> you can use to test this behaviour. To use it, run the following:</p>

<pre><code>echo "/path/to/test-image.png" &gt; inputs.txt
mkdir images
python3 image_test.py inputs.txt images
</code></pre>

<p>The code will make one input image, one patch image, and one output image for each of the 1024 patches in each input image, so comment out the lines that create input and output images if you're only concerned in saving all the patches.</p>

<p>Somebody please explain what happened :(</p>
