Skip to content

Instantly share code, notes, and snippets.

@alasin
Created August 10, 2017 08:40
Show Gist options
  • Save alasin/44bf7cf3a6fd76ae61c31b658cddf261 to your computer and use it in GitHub Desktop.
Save alasin/44bf7cf3a6fd76ae61c31b658cddf261 to your computer and use it in GitHub Desktop.
Gets a batch of random patches from images in memory
def get_random_patch(input_image, target_image, patch_size):
start_x = np.random.randint(input_image.shape[0] - patch_size)
start_y = np.random.randint(input_image.shape[1] - patch_size)
end_x = start_x + patch_size
end_y = start_y + patch_size
input_patch = input_image[start_x:end_x, start_y:end_y]
target_patch = target_image[start_x:end_x, start_y:end_y]
return [input_patch, target_patch]
def get_random_batch(batch_size):
index_list = np.random.randint(num_images, size=batch_size)
input_batch = np.empty(
[batch_size, train_patch_size, train_patch_size], dtype=np.int32)
target_batch = np.empty(
[batch_size, train_patch_size, train_patch_size], dtype=np.int32)
for i, index in enumerate(index_list):
[input_patch, target_patch] = get_random_patch(
input_images[index], target_images[index], train_patch_size)
input_batch[i, :, :] = input_patch
target_batch[i, :, :] = target_patch
input_batch = np.reshape(input_batch, (batch_size, -1))
target_batch = np.reshape(target_batch, (batch_size, -1))
return [input_batch, target_batch]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment