SHARE
TWEET

Untitled

a guest Oct 21st, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import tensorflow as tf
  2. import numpy as np
  3.  
  4.  
  5. def image_to_patches(image, patch_size, stride):
  6.     target_width = ((image.shape[1] - patch_size) // stride + 1) * stride + patch_size + 1
  7.     target_height = ((image.shape[0] - patch_size) // stride + 1) * stride + patch_size + 1
  8.  
  9.     image = np.pad(
  10.         image,
  11.         ((0, target_height - image.shape[0]), (0, target_width - image.shape[1]), (0, 0)),
  12.         mode='constant'
  13.     )
  14.     print(f'Last pixels of the bottom row of the image:\n {image[-1, -5:]}')
  15.     # here, the last row of `image` is all zeros
  16.     batched_image = np.expand_dims(image, 0)
  17.     patches = tf.image.extract_patches(
  18.         images=batched_image,
  19.         sizes=[1, patch_size, patch_size, 1],
  20.         strides=[1, stride, stride, 1],
  21.         rates=[1, 1, 1, 1],
  22.         padding='VALID',
  23.         name=None
  24.     )
  25.     patches = np.array(patches)
  26.     patches = np.resize(patches, (patches.shape[0] * patches.shape[1] * patches.shape[2], -1))
  27.     patches = np.resize(patches, (patches.shape[0], patch_size, patch_size, image.shape[-1]))
  28.     print(f'Last pixels of the bottom row of the last patch:\n {patches[-1, -1, -5:]}')
  29.     return patches
  30.  
  31.  
  32. def main():
  33.     image = 255 * np.ones((200, 100, 3))
  34.     _ = image_to_patches(image, patch_size=60, stride=50)
  35.  
  36.  
  37. if __name__ == '__main__':
  38.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top