def _draw_samples(self, size, random_state): seed = random_state.randint(0, 10**6, 1)[0] samples = self.other_param.draw_samples( size, random_state=eu.new_random_state(seed)) elementwise = self.elementwise and not isinstance( self.val, Deterministic) if elementwise: val_samples = self.val.draw_samples( size, random_state=eu.new_random_state(seed + 1)) # prevent division by zero val_samples[val_samples == 0] = 1 return np.multiply(force_np_float_dtype(samples), force_np_float_dtype(val_samples)) else: val_sample = self.val.draw_sample( random_state=eu.new_random_state(seed + 1)) # prevent division by zero if val_sample == 0: val_sample = 1 return force_np_float_dtype(samples) / float(val_sample)
def _draw_samples(self, size, random_state): seed = random_state.randint(0, 10**6, 1)[0] samples = self.other_param.draw_samples( size, random_state=eu.new_random_state(seed)) elementwise = self.elementwise and not isinstance( self.val, Deterministic) if elementwise: exponents = self.val.draw_samples( size, random_state=eu.new_random_state(seed + 1)) else: exponents = self.val.draw_sample( random_state=eu.new_random_state(seed + 1)) # without this we get int results in the case of # Power(<int>, <stochastic float param>) samples, exponents = both_np_float_if_one_is_float(samples, exponents) samples_dtype = samples.dtype # float_power requires numpy>=1.12 #result = np.float_power(samples, exponents) # TODO why was float32 type here replaced with complex number # formulation? result = np.power(samples.astype(np.complex), exponents).real if result.dtype != samples_dtype: result = result.astype(samples_dtype) return result
def reseed(self, random_state=None, deterministic_too=False): """Reseed this augmentor and all of its children(if it has any). This function is useful, when augmentatons are run in the background. Parameters: random_state: None or it or np.random.RandState, optional A RandomState that is used to sample seeds per augmentor. If int, the parameter will be used as a seed for a new RandomState. If None, a new RandomState will automatically be creatd. deterministic_too: bool, optional Whether to also change the seed of an augmentor 'A', if 'A' is deterministic. This is the case both when this augmentor object is 'A' or one of its children is 'A'. """ eu.do_assert(isinstance(deterministic_too, bool)) if random_state is None: random_state = eu.current_random_state() elif isinstance(random_state, np.random.RandomState): pass else: random_state = eu.new_random_state(random_state) if not self.deterministic or deterministic_too: seed = random_state.randint(0, 10**6, 1)[0] self.random_state = eu.new_random_state(seed) for lst in self.get_children_lists(): for aug in lst: aug.reseed(random_state=random_state, deterministic_too=deterministic_too)
def _draw_samples(self, size, random_state): if any([isinstance(a_i, StochasticParameter) for a_i in self.a]): seed = random_state.randint(0, 10**6, 1)[0] samples = eu.new_random_state(seed).choice(self.a, np.prod(size), replace=self.replace, p=self.p) # collect the sampled parameters and how many samples must be taken # from each of them params_counter = defaultdict(lambda: 0) #params_keys = set() for sample in samples: if isinstance(sample, StochasticParameter): key = str(sample) params_counter[key] += 1 #params_keys.add(key) # collect per parameter once the required number of samples # iterate here over self.a to always use the same seed for # the same parameter # TODO this might fail if the same parameter is added # multiple times to self.a? # TODO this will fail if a parameter cant handle size=(N,) param_to_samples = dict() for i, param in enumerate(self.a): key = str(param) if key in params_counter: #print("[Choice] sampling %d from %s" % (params_counter[key], key)) param_to_samples[key] = param.draw_samples( size=(params_counter[key], ), random_state=eu.new_random_state(seed + 1 + i)) # assign the values sampled from the parameters to the `samples` # array by replacing the respective parameter param_to_readcount = defaultdict(lambda: 0) for i, sample in enumerate(samples): #if i%10 == 0: # print("[Choice] assigning sample %d" % (i,)) if isinstance(sample, StochasticParameter): key = str(sample) readcount = param_to_readcount[key] #if readcount%10==0: # print("[Choice] readcount %d for %s" % (readcount, key)) samples[i] = param_to_samples[key][readcount] param_to_readcount[key] += 1 samples = samples.reshape(size) else: samples = random_state.choice(self.a, size, replace=self.replace, p=self.p) return samples
def _augment_images(self, images, random_state, parents, hooks): input_dtypes = eu.copy_dtypes_for_restore(images) result = images nb_images = len(images) seeds = random_state.randint(0, 10**6, (nb_images, )) for i in range(nb_images): image = images[i].astype(np.int32) rs_image = eu.new_random_state(seeds[i]) per_channel = self.per_channel.draw_sample(random_state=rs_image) if per_channel == 1: nb_channels = image.shape[2] samples = self.value.draw_samples((nb_channels, ), random_state=rs_image) for c, sample in enumerate(samples): # TODO make value range more flexible eu.do_assert(-255 <= sample <= 255) image[..., c] += sample else: sample = self.value.draw_sample(random_state=rs_image) # TODO make value range more flexible eu.do_assert(-255 <= sample <= 255) image += sample result[i] = image # TODO make value range more flexible eu.clip_augmented_images_(result, 0, 255) eu.restore_augmented_images_dtypes_(result, input_dtypes) return result
def _to_deterministic(self): augs = [aug.to_deterministic() for aug in self] seq = self.copy() seq[:] = augs seq.random_state = eu.new_random_state() seq.deterministic = True return seq
def _draw_samples(self, size, random_state): seed = random_state.randint(0, 10**6, 1)[0] samples = self.other_param.draw_samples( size, random_state=eu.new_random_state(seed)) elementwise = self.elementwise and not isinstance( self.val, Deterministic) if elementwise: val_samples = self.val.draw_samples( size, random_state=eu.new_random_state(seed + 1)) else: val_samples = self.val.draw_sample( random_state=eu.new_random_state(seed + 1)) if elementwise: return np.subtract(samples, val_samples) else: return samples - val_samples
def __init__(self, name=None, deterministic=False, random_state=None): """ Create a new Augmenter instance. Parameters ---------- name : None or string, optional(default=None) Name given to an Augmenter object. This name is used in print() statements as well as find and remove functions. If None, `UnnamedX` will be used as the name, where X is the Augmenter's class name. deterministic : bool, optional(default=False) Whether the augmenter instance's random state will be saved before augmenting images and then reset to that saved state after an augmentation (of multiple images/keypoints) is finished. I.e. if set to True, each batch of images will be augmented in the same way (e.g. first image might always be flipped horizontally, second image will never be flipped etc.). This is useful when you want to transform multiple batches of images in the same way, or when you want to augment images and keypoints on these images. Usually, there is no need to set this variable by hand. Instead, instantiate the augmenter with the defaults and then use `augmenter.to_deterministic()`. random_state : None or int or np.random.RandomState, optional(default=None) The random state to use for this augmenter. * If int, a new np.random.RandomState will be created using this value as the seed. * If np.random.RandomState instance, the instance will be used directly. * If None, imgaug's default RandomState will be used, which's state can be controlled using imgaug.seed(int). Usually there is no need to set this variable by hand. Instead, instantiate the augmenter with the defaults and then use `augmenter.to_deterministic()`. """ super(Augmentor, self).__init__() self.name = name if name is None: self.name = "Unnamed" + self.__class__.__name__ self.deterministic = deterministic if random_state is None: if self.deterministic: self.random_state = eu.new_random_state() else: self.random_state = eu.current_random_state() elif isinstance(random_state, np.random.RandomState): self.random_state = random_state else: self.random_state = np.random.RandomState(random_state) self.activated = True
def _to_deterministic(self): aug = self.copy() aug.children = aug.children.to_deterministic() aug.deterministic = True aug.random_state = eu.new_random_state() return aug
def _to_deterministic(self): aug = self.copy() aug.random_state = eu.new_random_state() aug.deterministic = True return aug