Last active
October 5, 2018 07:22
-
-
Save lmassaron/0d31c744dc6584d6ea48726cf6b8a21c to your computer and use it in GitHub Desktop.
random_crop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def random_crop(img, random_crop_size): | |
| # Note: image_data_format is 'channel_last' | |
| assert img.shape[2] == 3 | |
| height, width = img.shape[0], img.shape[1] | |
| dy, dx = random_crop_size | |
| x = np.random.randint(0, width - dx + 1) | |
| y = np.random.randint(0, height - dy + 1) | |
| return img[y:(y+dy), x:(x+dx), :] | |
| def crop_generator(batches, crop_length): | |
| """Take as input a Keras ImageGen (Iterator) and generate random | |
| crops from the image batches generated by the original iterator. | |
| """ | |
| while True: | |
| batch_x, batch_y = next(batches) | |
| batch_crops = np.zeros((batch_x.shape[0], crop_length, crop_length, 3)) | |
| for i in range(batch_x.shape[0]): | |
| batch_crops[i] = random_crop(batch_x[i], (crop_length, crop_length)) | |
| yield (batch_crops, batch_y) | |
| # TensorFlow. 'x' = A placeholder for an image. | |
| original_size = [height, width, channels] | |
| x = tf.placeholder(dtype = tf.float32, shape = original_size) | |
| # Use the following commands to perform random crops | |
| crop_size = [new_height, new_width, channels] | |
| seed = np.random.randint(1234) | |
| x = tf.random_crop(x, size = crop_size, seed = seed) | |
| output = tf.images.resize_images(x, size = original_size) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment