I have an image form (466,394,1) that I want to split into 7x7 patches.
image = tf.placeholder(dtype=tf.float32, shape=[1, 466, 394, 1])
Using
image_patches = tf.extract_image_patches(image, [1, 7, 7, 1], [1, 7, 7, 1], [1, 1, 1, 1], 'VALID') # shape (1, 66, 56, 49) image_patches_reshaped = tf.reshape(image_patches, [-1, 7, 7, 1]) # shape (3696, 7, 7, 1)
unfortunately, this does not work in practice, since image_patches_reshaped mixes the order of pixels (if you see images_patches_reshaped , you will only see noise).
So my new approach was to use tf.split :
image_hsplits = tf.split(1, 4, image_resized) # [<tf.Tensor 'split_255:0' shape=(462, 7, 1) dtype=float32>,...] image_patches = [] for split in image_hsplits: image_patches.extend(tf.split(0, 66, split)) image_patches # [<tf.Tensor 'split_317:0' shape=(7, 7, 1) dtype=float32>, ...]
it really preserves the pixel order of the image, unfortunately, it creates a lot of OP, which is not very good.
How to split the image into smaller patches with less OP?
Update1:
I put the answer of this question to numpy in tensorflow:
def image_to_patches(image, image_height, image_width, patch_height, patch_width): height = math.ceil(image_height/patch_height)*patch_height width = math.ceil(image_width/patch_width)*patch_width image_resized = tf.squeeze(tf.image.resize_image_with_crop_or_pad(image, height, width)) image_reshaped = tf.reshape(image_resized, [height // patch_height, patch_height, -1, patch_width]) image_transposed = tf.transpose(image_reshaped, [0, 2, 1, 3]) return tf.reshape(image_transposed, [-1, patch_height, patch_width, 1])
but I think there is still room for improvement.
Update2:
This will convert the corrections back to the original image.
def patches_to_image(patches, image_height, image_width, patch_height, patch_width): height = math.ceil(image_height/patch_height)*patch_height width = math.ceil(image_width/patch_width)*patch_width image_reshaped = tf.reshape(tf.squeeze(patches), [height // patch_height, width // patch_width, patch_height, patch_width]) image_transposed = tf.transpose(image_reshaped, [0, 2, 1, 3]) image_resized = tf.reshape(image_transposed, [height, width, 1]) return tf.image.resize_image_with_crop_or_pad(image_resized, image_height, image_width)