def augment(self, n, hflip=False, vflip=False, scale_to_percent=1.0, scale_axis_equally=True, rotation_deg=0, shear_deg=0, translation_x_px=0, translation_y_px=0, brightness_change=0.0, noise_mean=0.0, noise_std=0.0): """Generates randomly augmented versions of the image. Also augments the keypoints accordingly. Args: n Number of augmentations to generate. hflip Allow horizontal flipping (yes/no). vflip Allow vertical flipping (yes/no) scale_to_percent How much scaling/zooming to allow. Values are around 1.0. E.g. 1.1 is -10% to +10% E.g. (0.7, 1.05) is -30% to 5%. scale_axis_equally Whether to enforce equal scaling of x and y axis. rotation_deg How much rotation to allow. E.g. 5 is -5 degrees to +5 degrees. shear_deg How much shearing to allow. translation_x_px How many pixels of translation along the x axis to allow. translation_y_px How many pixels of translation along the y axis to allow. brightness_change How much change in brightness to allow. Values are around 0.0. E.g. 0.2 is -20% to +20%. noise_mean Mean value of gaussian noise to add. noise_std Standard deviation of gaussian noise to add. Returns: List of ImageWithKeypoints """ assert n >= 0 result = [] if n == 0: return result matrices = create_aug_matrices(n, img_width_px=self.get_width(), img_height_px=self.get_height(), scale_to_percent=scale_to_percent, scale_axis_equally=scale_axis_equally, rotation_deg=rotation_deg, shear_deg=shear_deg, translation_x_px=translation_x_px, translation_y_px=translation_y_px) for i in range(n): img = self.copy() matrix = matrices[i] # random horizontal / vertical flip if hflip and random.random() > 0.5: img.image_arr = np.fliplr(img.image_arr) img.keypoints.fliplr(img) if vflip and random.random() > 0.5: img.image_arr = np.flipud(img.image_arr) img.keypoints.flipud(img) # random brightness adjustment by_percent = random.uniform(1.0 - brightness_change, 1.0 + brightness_change) img.image_arr = img.image_arr * by_percent # gaussian noise # numpy requires a std above 0 if noise_std > 0: img.image_arr = img.image_arr \ + (255 * np.random.normal(noise_mean, noise_std, (img.image_arr.shape))) # clip to 0-255 img.image_arr = np.clip(img.image_arr, 0, 255).astype(np.uint8) arr = tf.warp(img.image_arr, matrix, mode="constant") # projects to float 0-1 img.image_arr = np.array(arr * 255, dtype=np.uint8) img.keypoints.warp(img, matrix) result.append(img) return result
def augment( self, n, hflip=False, vflip=False, scale_to_percent=1.0, scale_axis_equally=True, rotation_deg=0, shear_deg=0, translation_x_px=0, translation_y_px=0, brightness_change=0.0, noise_mean=0.0, noise_std=0.0, ): """Generates randomly augmented versions of the image. Also augments the keypoints accordingly. Args: n Number of augmentations to generate. hflip Allow horizontal flipping (yes/no). vflip Allow vertical flipping (yes/no) scale_to_percent How much scaling/zooming to allow. Values are around 1.0. E.g. 1.1 is -10% to +10% E.g. (0.7, 1.05) is -30% to 5%. scale_axis_equally Whether to enforce equal scaling of x and y axis. rotation_deg How much rotation to allow. E.g. 5 is -5 degrees to +5 degrees. shear_deg How much shearing to allow. translation_x_px How many pixels of translation along the x axis to allow. translation_y_px How many pixels of translation along the y axis to allow. brightness_change How much change in brightness to allow. Values are around 0.0. E.g. 0.2 is -20% to +20%. noise_mean Mean value of gaussian noise to add. noise_std Standard deviation of gaussian noise to add. Returns: List of ImageWithKeypoints """ assert n >= 0 result = [] if n == 0: return result matrices = create_aug_matrices( n, img_width_px=self.get_width(), img_height_px=self.get_height(), scale_to_percent=scale_to_percent, scale_axis_equally=scale_axis_equally, rotation_deg=rotation_deg, shear_deg=shear_deg, translation_x_px=translation_x_px, translation_y_px=translation_y_px, ) for i in range(n): img = self.copy() matrix = matrices[i] # random horizontal / vertical flip if hflip and random.random() > 0.5: img.image_arr = np.fliplr(img.image_arr) img.keypoints.fliplr(img) if vflip and random.random() > 0.5: img.image_arr = np.flipud(img.image_arr) img.keypoints.flipud(img) # random brightness adjustment by_percent = random.uniform(1.0 - brightness_change, 1.0 + brightness_change) img.image_arr = img.image_arr * by_percent # gaussian noise # numpy requires a std above 0 if noise_std > 0: img.image_arr = img.image_arr + (255 * np.random.normal(noise_mean, noise_std, (img.image_arr.shape))) # clip to 0-255 img.image_arr = np.clip(img.image_arr, 0, 255).astype(np.uint8) arr = tf.warp(img.image_arr, matrix, mode="nearest") # projects to float 0-1 img.image_arr = np.array(arr * 255, dtype=np.uint8) img.keypoints.warp(img, matrix) result.append(img) return result
def main(): """Measure time required to generate augmentations matrices and to apply them. """ batch_size = 64 nb_runs = 20 # Measure time required to generate 100k augmentation matrices """ print("Generating 100 times 1000 augmentation matrices of size 64x64...") start = time.time() for _ in range(100): create_aug_matrices(1000, 64, 64, scale_to_percent=1.5, scale_axis_equally=False, rotation_deg=20, shear_deg=20, translation_x_px=5, translation_y_px=5) print("Done in %.8f" % (time.time() - start,)) """ # Test Performance on 64 images of size 512x512 pixels image = data.lena() images = np.resize( image, (batch_size, image.shape[0], image.shape[1], image.shape[2])) augmenter = ImageAugmenter(image.shape[0], image.shape[1], hflip=True, vflip=True, scale_to_percent=1.3, scale_axis_equally=False, rotation_deg=25, shear_deg=10, translation_x_px=5, translation_y_px=5) print("Running tests on %d images of shape %s" % (batch_size, str(image.shape))) run_tests(augmenter, images, nb_runs) print("") print("Running tests on %d images of shape %s" % (batch_size, str(image.shape))) print("(With 1000 pregenerated matrices)") augmenter.pregenerate_matrices(1000) run_tests(augmenter, images, nb_runs) print("") # Test Performance on 64 images of size 64x64 pixels image = data.lena() image = misc.imresize(image, (64, 64)) images = np.resize( image, (batch_size, image.shape[0], image.shape[1], image.shape[2])) augmenter = ImageAugmenter(image.shape[0], image.shape[1], hflip=True, vflip=True, scale_to_percent=1.3, scale_axis_equally=False, rotation_deg=25, shear_deg=10, translation_x_px=5, translation_y_px=5) print("Running tests on %d images of shape %s" % (batch_size, str(image.shape))) run_tests(augmenter, images, nb_runs) print("Running tests on %d images of shape %s" % (batch_size, str(image.shape))) print("(With 1000 pregenerated matrices)") augmenter.pregenerate_matrices(1000) run_tests(augmenter, images, nb_runs) print("") # Time required to augment 1,000,000 images of size 32x32 print("Augmenting 1000 batches of 1000 lena images (1 million total)" \ ", each of size 32x32...") image = data.lena() image = misc.imresize(image, (32, 32)) batch_size = 1000 images = np.resize( image, (batch_size, image.shape[0], image.shape[1], image.shape[2])) augmenter = ImageAugmenter(image.shape[1], image.shape[0], hflip=True, vflip=True, scale_to_percent=1.3, scale_axis_equally=False, rotation_deg=25, shear_deg=10, translation_x_px=5, translation_y_px=5) augmenter.pregenerate_matrices(1000) start = time.time() for _ in range(1000): augmenter.augment_batch(images) print("Done in %.8fs" % (time.time() - start, )) print("") # Time required to augment 1,000,000 images of size 32x32 # but using only one matrix without the class (no library overhead from # ImageAugmenter) # Notice that this does not include horizontal and vertical flipping, # which is done via numpy in the ImageAugmenter class. print("Augmenting 1000 batches of 1000 lena images (1 million total)" \ ", each of size 32x32, using one matrix directly (no ImageAugmenter " \ "class)...") matrices = create_aug_matrices(1, image.shape[1], image.shape[0], scale_to_percent=1.3, scale_axis_equally=False, rotation_deg=25, shear_deg=10, translation_x_px=5, translation_y_px=5) matrix = matrices[0] start = time.time() for _ in range(1000): for image in images: augmented_image = tf.warp(image, matrix) print("Done in %.8fs" % (time.time() - start, ))
def main(): """Measure time required to generate augmentations matrices and to apply them. """ batch_size = 64 nb_runs = 20 # Measure time required to generate 100k augmentation matrices """ print("Generating 100 times 1000 augmentation matrices of size 64x64...") start = time.time() for _ in range(100): create_aug_matrices(1000, 64, 64, scale_to_percent=1.5, scale_axis_equally=False, rotation_deg=20, shear_deg=20, translation_x_px=5, translation_y_px=5) print("Done in %.8f" % (time.time() - start,)) """ # Test Performance on 64 images of size 512x512 pixels image = data.lena() images = np.resize(image, (batch_size, image.shape[0], image.shape[1], image.shape[2])) augmenter = ImageAugmenter(image.shape[0], image.shape[1], hflip=True, vflip=True, scale_to_percent=1.3, scale_axis_equally=False, rotation_deg=25, shear_deg=10, translation_x_px=5, translation_y_px=5) print("Running tests on %d images of shape %s" % (batch_size, str(image.shape))) run_tests(augmenter, images, nb_runs) print("") print("Running tests on %d images of shape %s" % (batch_size, str(image.shape))) print("(With 1000 pregenerated matrices)") augmenter.pregenerate_matrices(1000) run_tests(augmenter, images, nb_runs) print("") # Test Performance on 64 images of size 64x64 pixels image = data.lena() image = misc.imresize(image, (64, 64)) images = np.resize(image, (batch_size, image.shape[0], image.shape[1], image.shape[2])) augmenter = ImageAugmenter(image.shape[0], image.shape[1], hflip=True, vflip=True, scale_to_percent=1.3, scale_axis_equally=False, rotation_deg=25, shear_deg=10, translation_x_px=5, translation_y_px=5) print("Running tests on %d images of shape %s" % (batch_size, str(image.shape))) run_tests(augmenter, images, nb_runs) print("Running tests on %d images of shape %s" % (batch_size, str(image.shape))) print("(With 1000 pregenerated matrices)") augmenter.pregenerate_matrices(1000) run_tests(augmenter, images, nb_runs) print("") # Time required to augment 1,000,000 images of size 32x32 print("Augmenting 1000 batches of 1000 lena images (1 million total)" \ ", each of size 32x32...") image = data.lena() image = misc.imresize(image, (32, 32)) batch_size = 1000 images = np.resize(image, (batch_size, image.shape[0], image.shape[1], image.shape[2])) augmenter = ImageAugmenter(image.shape[1], image.shape[0], hflip=True, vflip=True, scale_to_percent=1.3, scale_axis_equally=False, rotation_deg=25, shear_deg=10, translation_x_px=5, translation_y_px=5) augmenter.pregenerate_matrices(1000) start = time.time() for _ in range(1000): augmenter.augment_batch(images) print("Done in %.8fs" % (time.time() - start,)) print("") # Time required to augment 1,000,000 images of size 32x32 # but using only one matrix without the class (no library overhead from # ImageAugmenter) # Notice that this does not include horizontal and vertical flipping, # which is done via numpy in the ImageAugmenter class. print("Augmenting 1000 batches of 1000 lena images (1 million total)" \ ", each of size 32x32, using one matrix directly (no ImageAugmenter " \ "class)...") matrices = create_aug_matrices(1, image.shape[1], image.shape[0], scale_to_percent=1.3, scale_axis_equally=False, rotation_deg=25, shear_deg=10, translation_x_px=5, translation_y_px=5) matrix = matrices[0] start = time.time() for _ in range(1000): for image in images: augmented_image = tf.warp(image, matrix) print("Done in %.8fs" % (time.time() - start,))