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 activator_lbls(images, augmenter, parents, default): white_lists = (iaa.Affine, iaa.Sequential, iaa.Sometimes) if not isinstance(augmenter, white_lists): return False if isinstance(augmenter, iaa.Affine): augmenter.order = Deterministic(0) augmenter.cval = Deterministic(-1) return True
def __init__(self, p): super(BinomialColumns, self).__init__() if isinstance(p, StochasticParameter): self.p = p elif ia.is_single_number(p): assert 0 <= p <= 1.0, "Expected probability p to be in range [0.0, 1.0], got %s." % ( p, ) self.p = Deterministic(float(p)) else: raise Exception( "Expected StochasticParameter or float/int value, got %s." % (type(p), ))
class BinomialColumns(StochasticParameter): def __init__(self, p): super(BinomialColumns, self).__init__() if isinstance(p, StochasticParameter): self.p = p elif ia.is_single_number(p): assert 0 <= p <= 1.0, "Expected probability p to be in range [0.0, 1.0], got %s." % ( p, ) self.p = Deterministic(float(p)) else: raise Exception( "Expected StochasticParameter or float/int value, got %s." % (type(p), )) def _draw_samples(self, size, random_state): p = self.p.draw_sample(random_state=random_state) assert 0 <= p <= 1.0, "Expected probability p to be in range [0.0, 1.0], got %s." % ( p, ) h, w, c = size drops = random_state.binomial(1, p, (1, w, c)) drops_columns = np.tile(drops, (h, 1, 1)) return drops_columns def __repr__(self): return self.__str__() def __str__(self): if isinstance(self.p, float): return "BinomialColumns(%.4f)" % (self.p, ) else: return "BinomialColumns(%s)" % (self.p, )
def _scale_if_necessary(self, obj, random_state, parents, hooks): h, w = obj.shape[:2] box_w, box_h = self.size ratio = max(box_w / w, box_h / h) if ratio <= 1: return obj self.scale.size = (Deterministic(math.ceil(h * ratio)), Deterministic(math.ceil(w * ratio))) if isinstance(obj, ia.Keypoint): obj = self.scale._augment_keypoints([obj], random_state, parents, hooks) else: obj = self.scale._augment_images([obj], random_state, parents, hooks) return obj[0]
def transform(self, image, mode): # sample angle if mode == ImageMaskTransformMode.Image: scale = np.random.uniform(self.from_val, self.to_val) self._augmentor.scale = Deterministic(scale) image = self._augmentor.augment_image(image) return image
def transform(self, image, mode): # sample angle if mode == ImageMaskTransformMode.Image: angle = np.random.choice(self._angles) self._augmentor.rotate = Deterministic(angle) image = self._augmentor.augment_image(image) return image
class BinomialColumns(StochasticParameter): def __init__(self, p): super(BinomialColumns, self).__init__() self.p = Deterministic(float(p)) def _draw_samples(self, size, random_state): p = self.p.draw_sample(random_state=random_state) h, w, c = size drops = random_state.binomial(1, p, (1, w, c)) drops_columns = np.tile(drops, (h, 1, 1)) return drops_columns
def _augment_keypoints(self, keypoints_on_images, random_state, parents, hooks): result = [] nb_images = len(keypoints_on_images) for i in sm.xrange(nb_images): keypoint = keypoints_on_images[i] h, w = keypoint.shape[:2] scale, top_pad, right_pad, bottom_pad, left_pad = self._calculate_scale_pad( h, w) self.scale.size = (Deterministic(int(h * scale)), Deterministic(int(w * scale)) ) #Deterministic(scale) self.pad.top = Deterministic(top_pad) self.pad.right = Deterministic(right_pad) self.pad.bottom = Deterministic(bottom_pad) self.pad.left = Deterministic(left_pad) scaled = self.scale._augment_keypoints([keypoint], random_state, parents, hooks) padded = self.pad._augment_keypoints(scaled, random_state, parents, hooks) result += padded return result
def _augment_images(self, images, random_state, parents, hooks): size = self.size result = [] nb_images = len(images) seeds = random_state.randint(0, 10**6, (nb_images, )) for i in sm.xrange(nb_images): image = images[i] image = self._scale_if_necessary(image, random_state, parents, hooks) h, w = image.shape[:2] top_px, left_px, bottom_px, right_px = self._crop_points( size[0], size[1], w, h, seeds[i]) self.crop.top = Deterministic(top_px) self.crop.left = Deterministic(left_px) self.crop.bottom = Deterministic(bottom_px) self.crop.right = Deterministic(right_px) cropped = self.crop._augment_images([image], random_state, parents, hooks) result += cropped return result
def __init__(self, shear=(-40, 41), cval=255, vertical=False, name=None, deterministic=False, random_state=None): """Initialize the augmentator # Arguments shear [float or tuple of 2 floats]: if it is a single number, then image will be sheared in that degree. If it is a tuple of 2 numbers, then the shear value will be chosen randomly cval [int]: fill-in value to new pixels """ super(ItalicizeLine, self).__init__(name=name, deterministic=deterministic, random_state=random_state) if isinstance(shear, StochasticParameter): self.shear = shear elif ia.is_single_number(shear): self.shear = Deterministic(shear) elif ia.is_iterable(shear): ia.do_assert( len(shear) == 2, "Expected rotate tuple/list with 2 entries, got {} entries.". format((len(shear)))) ia.do_assert(all([ia.is_single_number(val) for val in shear]), "Expected floats/ints in shear tuple/list.") self.shear = Uniform(shear[0], shear[1]) else: raise Exception( "Expected float, int, tuple/list with 2 entries or " "StochasticParameter. Got {}.".format(type(shear))) self.cval = cval self.vertical = vertical
def __init__(self, angle=(-10, 10), cval=255, name=None, deterministic=False, random_state=None): """Initialize the augmentator # Arguments angle [float or tuple of 2 floats]: if it is a single number, then image will be rotated in that degree. If it is a tuple of 2 numbers, then the angle value will be chosen randomly cval [int]: fill-in value to new pixels """ super(RotateLine, self).__init__(name=name, deterministic=deterministic, random_state=random_state) if isinstance(angle, StochasticParameter): self.angle = angle elif ia.is_single_number(angle): self.angle = Deterministic(angle) elif ia.is_iterable(angle): ia.do_assert( len(angle) == 2, "Expected rotate tuple/list with 2 entries, got {} entries.". format((len(angle)))) ia.do_assert(all([ia.is_single_number(val) for val in angle]), "Expected floats/ints in angle tuple/list.") self.angle = Uniform(angle[0], angle[1]) else: raise Exception( "Expected float, int, tuple/list with 2 entries or " "StochasticParameter. Got {}.".format(type(angle))) self.cval = cval
def activator_imgs(images, augmenter, parents, default): if isinstance(augmenter, iaa.Affine): augmenter.order = Deterministic(1) augmenter.cval = Deterministic(0) return True
def __init__(self, p): super(BinomialColumns, self).__init__() self.p = Deterministic(float(p))
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))