def __init__(self, value=0, per_channel=False, name=None, deterministic=False, random_state=None): super(AddCPU, self).__init__(name=name, deterministic=deterministic, random_state=random_state) #elif ia.is_iterable(value): # ia.do_assert(len(value) == 2, "Expected tuple/list with 2 entries, got %d entries." % (len(value),)) if isinstance(value, numbers.Integral): ch.do_assert( -255 <= value <= 255, "Expected value to have range [-255, 255], got value %d." % (value, )) self.value = randomizer.Deterministic(value) else: self.value = randomizer.DiscreteUniform(value[0], value[1]) #elif isinstance(value, StochasticParameter): # self.value = value #else: # raise Exception("Expected float or int, tuple/list with 2 entries or StochasticParameter. Got %s." % (type(value),)) if per_channel in [True, False, 0, 1, 0.0, 1.0]: self.per_channel = randomizer.Deterministic(int(per_channel)) else: # ia.is_single_number(per_channel): #ia.do_assert(0 <= per_channel <= 1.0, "Expected bool, or number in range [0, 1.0] for per_channel, got %s." % (type(per_channel),)) self.per_channel = randomizer.Binomial(per_channel)
def __call__(self, images): result = images nb_images = len(images) seeds = randomizer.current_random_state().randint( 0, 10**6, (nb_images, )) for i in sm.xrange(nb_images): image = images[i] rs_image = randomizer.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.mul.draw_samples((nb_channels, ), random_state=rs_image) for c, sample in enumerate(samples): ch.do_assert(sample >= 0) image = F.multiply(image[c, ...], torch.ones(1) * float(sample)) else: sample = self.mul.draw_sample(random_state=rs_image) ch.do_assert(sample >= 0) image = F.multiply(image, torch.ones(1) * float(sample)) #image = meta.clip_augmented_image_(image, 0, 255) # TODO make value range more flexible #image = meta.restore_augmented_image_dtype_(image, input_dtypes[i]) result[i] = image return result
def __call__(self, images): result = images nb_images = len(images) # Generate new seeds con seeds = randomizer.current_random_state().randint(0, 10**6, (nb_images,)) rs_image = randomizer.new_random_state(seeds[0]) per_channel = self.per_channel.draw_sample(random_state=rs_image) if per_channel == 1: nb_channels = images.shape[1] #Considering (NXCXMXN) for c in range(nb_channels): samples = self.mul.draw_samples((nb_images,), random_state=rs_image).astype( np.float32) ch.do_assert(samples.all() >= 0) # This is ugly how can I fix this transposition ? Put it somewhere else. # TODO: document how to do the transposition. result[ c:(c+1), ...] = F.multiply(images[c:(c+1), ...], torch.cuda.FloatTensor(samples)) else: samples = self.mul.draw_samples((nb_images,), random_state=rs_image).astype(np.float32) ch.do_assert(samples.all() >= 0) result = F.multiply(images, torch.cuda.FloatTensor(samples))#torch.from_numpy(sample)) return result
def __call__(self, images): #input_dtypes = copy_dtypes_for_restore(images, force_list=True) result = images nb_images = len(images) random_seeds = randomizer.current_random_state().randint(0, 10 ** 6, (nb_images,)) for i in sm.xrange(nb_images): image = images[i] rs_image = randomizer.new_random_state(random_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 ch.do_assert(-255 <= sample <= 255) image = F.add(image[c, ...].cuda(), torch.ones(1).cuda() * float(sample)) else: sample = self.value.draw_sample(random_state=rs_image) ch.do_assert(-255 <= sample <= 255) # TODO make value range more flexible image = F.add(image.cuda(), torch.ones(1).cuda() * float(sample)) #image = meta.clip_augmented_image_(image, 0, 255) # TODO make value range more flexible #image = meta.restore_augmented_image_dtype_(image, input_dtypes[i]) result[i] = image return result
def __init__(self, mul=1.0, per_channel=False, name=None, deterministic=False, random_state=None): super(MultiplyCPU, self).__init__(name=name, deterministic=deterministic, random_state=random_state) if isinstance(mul, numbers.Real) or isinstance(mul, numbers.Integral): ch.do_assert( mul >= 0.0, "Expected multiplier to have range [0, inf), got value %.4f." % (mul, )) self.mul = randomizer.Deterministic(mul) else: ch.do_assert( len(mul) == 2, "Expected tuple/list with 2 entries, got %d entries." % (len(mul), )) self.mul = randomizer.Uniform(mul[0], mul[1]) if per_channel in [True, False, 0, 1, 0.0, 1.0]: self.per_channel = randomizer.Deterministic(int(per_channel)) else: ch.do_assert(0 <= per_channel <= 1.0) self.per_channel = randomizer.Binomial(per_channel)
def __init__(self, mul=1.0, per_channel=False, name=None, deterministic=False, random_state=None): super(MultiplyElementwise, self).__init__(name=name, deterministic=deterministic, random_state=random_state) if ch.is_single_number(mul): ch.do_assert(mul >= 0.0, "Expected multiplier to have range [0, inf), got value %.4f." % (mul,)) self.mul = randomizer.Deterministic(mul) elif ch.is_iterable(mul): ch.do_assert(len(mul) == 2, "Expected tuple/list with 2 entries, got %d entries." % (len(mul),)) self.mul = randomizer.Uniform(mul[0], mul[1]) else: raise Exception("Expected float or int, tuple/list with 2 entries or StochasticParameter. Got %s." % (type(mul),)) if per_channel in [True, False, 0, 1, 0.0, 1.0]: self.per_channel = randomizer.Deterministic(int(per_channel)) elif ch.is_single_number(per_channel): ch.do_assert(0 <= per_channel <= 1.0) self.per_channel = randomizer.Binomial(per_channel) else: raise Exception("Expected per_channel to be boolean or number or StochasticParameter")
def Dropout(p=0, per_channel=False, name=None, deterministic=False, random_state=None): """ Augmenter that sets a certain fraction of pixels in images to zero. Parameters ---------- p : float or tuple of two floats or StochasticParameter, optional(default=0) The probability of any pixel being dropped (i.e. set to zero). * If a float, then that value will be used for all images. A value of 1.0 would mean that all pixels will be dropped and 0.0 that no pixels would be dropped. A value of 0.05 corresponds to 5 percent of all pixels dropped. * If a tuple (a, b), then a value p will be sampled from the range a <= p <= b per image and be used as the pixel's dropout probability. * If a StochasticParameter, then this parameter will be used to determine per pixel whether it should be dropped (sampled value of 0) or shouldn't (sampled value of 1). per_channel : bool or float, optional(default=False) Whether to use the same value (is dropped / is not dropped) for all channels of a pixel (False) or to sample a new value for each channel (True). If this value is a float p, then for p percent of all images `per_channel` will be treated as True, otherwise as False. name : string, optional(default=None) See `Augmenter.__init__()` deterministic : bool, optional(default=False) See `Augmenter.__init__()` random_state : int or np.random.RandomState or None, optional(default=None) See `Augmenter.__init__()` Examples -------- >>> aug = iaa.Dropout(0.02) drops 2 percent of all pixels. >>> aug = iaa.Dropout((0.0, 0.05)) drops in each image a random fraction of all pixels, where the fraction is in the range 0.0 <= x <= 0.05. >>> aug = iaa.Dropout(0.02, per_channel=True) drops 2 percent of all pixels in a channel-wise fashion, i.e. it is unlikely for any pixel to have all channels set to zero (black pixels). >>> aug = iaa.Dropout(0.02, per_channel=0.5) same as previous example, but the `per_channel` feature is only active for 50 percent of all images. """ if ch.is_single_number(p): p2 = randomizer.Binomial(1 - p) elif isinstance(p, (tuple, list)): ch.do_assert(len(p) == 2) ch.do_assert(p[0] < p[1]) ch.do_assert(0 <= p[0] <= 1.0) ch.do_assert(0 <= p[1] <= 1.0) p2 = randomizer.Binomial(randomizer.Uniform(1 - p[1], 1 - p[0])) else: raise Exception("Expected p to be float or int or StochasticParameter, got %s." % (type(p),)) return MultiplyElementwise(p2, per_channel=per_channel, name=name, deterministic=deterministic, random_state=random_state)