def load_images(self, ids: List[int]) -> Tensor: """ Returns: (l, c, w, h) Tensor: Tensor of images. """ images: List[Tensor] = [ imread(self.frame_template.format(i)) for i in ids ] # Using deterministic parameters to apply exactly # the same augmentations to all images in the sequence. if self.augment: augmenter = Sequential([ LinearContrast(Deterministic(Uniform(0.7, 1.3))), Multiply( Deterministic(Uniform(0.7, 1.3)), per_channel=Deterministic(DiscreteUniform(0, 1)), ), Sometimes( Deterministic(DiscreteUniform(0, 1)), GaussianBlur(sigma=Deterministic(Uniform(0, 0.7))), ), ], random_order=True) images = augmenter(images=images) for i in range(len(images)): image = images[i].astype("float32") / 255 images[i] = tensor(image).permute(2, 0, 1).unsqueeze_(0) return cat(images, dim=0)
def prepare_augmentation_pipeline(self, config): from imgaug.parameters import DiscreteUniform br_change = config['trn']['brightness_augmentation_amount'] # (was 70) contrast_min = config['trn']['augmentation_contrast_min'] contrast_max = config['trn']['augmentation_contrast_max'] rotation = config['trn']['augmentation_rotation'] if rotation == 'uniform': rotation = DiscreteUniform(0, 360) translation = config['trn']['augmentation_translation'] scale_min = config['trn']['augmentation_scale_min'] scale_max = config['trn']['augmentation_scale_max'] shear = config['trn']['augmentation_shear'] from imgaug import augmenters as iaa return iaa.Sequential( [ iaa.Add( (-br_change, br_change), per_channel=False ), # change brightness of images (by -10 to 10 of original value) iaa.contrast.LinearContrast( (contrast_min, contrast_max), per_channel=False), # improve or worsen the contrast iaa.Affine(rotate=rotation, translate_percent={ "x": (-translation, translation), 'y': (-translation, translation) }, scale=(scale_min, scale_max), shear=(-shear, shear), cval=128), iaa.Fliplr(p=0.5) ], random_order=True)
def _draw_sample_point(self, box_w, box_h, img_w, img_h, seed): x1 = math.ceil(box_w / 2) y1 = math.ceil(box_h / 2) x2 = math.floor(img_w - x1) y2 = math.floor(img_h - y1) random_state = ia.new_random_state(seed) x = DiscreteUniform(x1, x2) y = DiscreteUniform(y1, y2) center_x = x.draw_sample(random_state=random_state) center_y = y.draw_sample(random_state=random_state) return center_x, center_y
def main(): params = [ ("Binomial(0.1)", Binomial(0.1)), ("Choice", Choice([0, 1, 2])), ("Choice with p", Choice([0, 1, 2], p=[0.1, 0.2, 0.7])), ("DiscreteUniform(0, 10)", DiscreteUniform(0, 10)), ("Poisson(0)", Poisson(0)), ("Poisson(5)", Poisson(5)), ("Discretize(Poisson(5))", Discretize(Poisson(5))), ("Normal(0, 1)", Normal(0, 1)), ("Normal(1, 1)", Normal(1, 1)), ("Normal(1, 2)", Normal(0, 2)), ("Normal(Choice([-1, 1]), 2)", Normal(Choice([-1, 1]), 2)), ("Discretize(Normal(0, 1.0))", Discretize(Normal(0, 1.0))), ("Positive(Normal(0, 1.0))", Positive(Normal(0, 1.0))), ("Positive(Normal(0, 1.0), mode='reroll')", Positive(Normal(0, 1.0), mode="reroll")), ("Negative(Normal(0, 1.0))", Negative(Normal(0, 1.0))), ("Negative(Normal(0, 1.0), mode='reroll')", Negative(Normal(0, 1.0), mode="reroll")), ("Laplace(0, 1.0)", Laplace(0, 1.0)), ("Laplace(1.0, 3.0)", Laplace(1.0, 3.0)), ("Laplace([-1.0, 1.0], 1.0)", Laplace([-1.0, 1.0], 1.0)), ("ChiSquare(1)", ChiSquare(1)), ("ChiSquare([1, 6])", ChiSquare([1, 6])), ("Weibull(0.5)", Weibull(0.5)), ("Weibull((1.0, 3.0))", Weibull((1.0, 3.0))), ("Uniform(0, 10)", Uniform(0, 10)), ("Beta(0.5, 0.5)", Beta(0.5, 0.5)), ("Deterministic(1)", Deterministic(1)), ("Clip(Normal(0, 1), 0, None)", Clip(Normal(0, 1), minval=0, maxval=None)), ("Multiply(Uniform(0, 10), 2)", Multiply(Uniform(0, 10), 2)), ("Add(Uniform(0, 10), 5)", Add(Uniform(0, 10), 5)), ("Absolute(Normal(0, 1))", Absolute(Normal(0, 1))), ("RandomSign(Poisson(1))", RandomSign(Poisson(1))), ("RandomSign(Poisson(1), 0.9)", RandomSign(Poisson(1), 0.9)) ] params_arithmetic = [ ("Normal(0, 1.0)", Normal(0.0, 1.0)), ("Normal(0, 1.0) + 5", Normal(0.0, 1.0) + 5), ("Normal(0, 1.0) * 10", Normal(0.0, 1.0) * 10), ("Normal(0, 1.0) / 10", Normal(0.0, 1.0) / 10), ("Normal(0, 1.0) ** 2", Normal(0.0, 1.0) ** 2) ] params_noise = [ ("SimplexNoise", SimplexNoise()), ("Sigmoid(SimplexNoise)", Sigmoid(SimplexNoise())), ("SimplexNoise(linear)", SimplexNoise(upscale_method="linear")), ("SimplexNoise(nearest)", SimplexNoise(upscale_method="nearest")), ("FrequencyNoise((-4, 4))", FrequencyNoise(exponent=(-4, 4))), ("FrequencyNoise(-2)", FrequencyNoise(exponent=-2)), ("FrequencyNoise(2)", FrequencyNoise(exponent=2)) ] images_params = [param.draw_distribution_graph() for (title, param) in params] images_arithmetic = [param.draw_distribution_graph() for (title, param) in params_arithmetic] images_noise = [param.draw_distribution_graph(size=(1000, 10, 10)) for (title, param) in params_noise] misc.imshow(np.vstack(images_params)) misc.imshow(np.vstack(images_arithmetic)) misc.imshow(np.vstack(images_noise))