Use Update # 2 . One small example for your task: (TF 1.0)
Given the size image (4.4.1), converted to size patches (4.2.2.1) and restored them back to the image.
import tensorflow as tf image = tf.constant([[[1], [2], [3], [4]], [[5], [6], [7], [8]], [[9], [10], [11], [12]], [[13], [14], [15], [16]]]) patch_size = [1,2,2,1] patches = tf.extract_image_patches([image], patch_size, patch_size, [1, 1, 1, 1], 'VALID') patches = tf.reshape(patches, [4, 2, 2, 1]) reconstructed = tf.reshape(patches, [1, 4, 4, 1]) rec_new = tf.space_to_depth(reconstructed,2) rec_new = tf.reshape(rec_new,[4,4,1]) sess = tf.Session() I,P,R_n = sess.run([image,patches,rec_new]) print(I) print(I.shape) print(P.shape) print(R_n) print(R_n.shape)
Output:
[[[ 1][ 2][ 3][ 4]] [[ 5][ 6][ 7][ 8]] [[ 9][10][11][12]] [[13][14][15][16]]] (4, 4, 1) (4, 2, 2, 1) [[[ 1][ 2][ 3][ 4]] [[ 5][ 6][ 7][ 8]] [[ 9][10][11][12]] [[13][14][15][16]]] (4,4,1)
Update - for 3 channels (debugging ..)
only works for p = sqrt (h)
import tensorflow as tf import numpy as np c = 3 h = 1024 p = 32 image = tf.random_normal([h,h,c]) patch_size = [1,p,p,1] patches = tf.extract_image_patches([image], patch_size, patch_size, [1, 1, 1, 1], 'VALID') patches = tf.reshape(patches, [h, p, p, c]) reconstructed = tf.reshape(patches, [1, h, h, c]) rec_new = tf.space_to_depth(reconstructed,p) rec_new = tf.reshape(rec_new,[h,h,c]) sess = tf.Session() I,P,R_n = sess.run([image,patches,rec_new]) print(I.shape) print(P.shape) print(R_n.shape) err = np.sum((R_n-I)**2) print(err)
Output:
(1024, 1024, 3) (1024, 32, 32, 3) (1024, 1024, 3) 0.0
Update 2
Reconstructing from the output of extract_image_patches seems complicated. Other functions are used to extract patches and reverse the process for recovery, which seems simpler.
import tensorflow as tf import numpy as np c = 3 h = 1024 p = 128 image = tf.random_normal([1,h,h,c])
Output:
(1, 1024, 1024, 3) (64, 128, 128, 3) (1, 1024, 1024, 3) 0.0
Here you can see other interesting tensor conversion functions: https://www.tensorflow.org/api_guides/python/array_ops