Python源码示例:imgaug.augmenters.Sometimes()
示例1
def _load_augmentation_aug_non_geometric():
return iaa.Sequential([
iaa.Sometimes(0.3, iaa.Multiply((0.5, 1.5), per_channel=0.5)),
iaa.Sometimes(0.2, iaa.JpegCompression(compression=(70, 99))),
iaa.Sometimes(0.2, iaa.GaussianBlur(sigma=(0, 3.0))),
iaa.Sometimes(0.2, iaa.MotionBlur(k=15, angle=[-45, 45])),
iaa.Sometimes(0.2, iaa.MultiplyHue((0.5, 1.5))),
iaa.Sometimes(0.2, iaa.MultiplySaturation((0.5, 1.5))),
iaa.Sometimes(0.34, iaa.MultiplyHueAndSaturation((0.5, 1.5),
per_channel=True)),
iaa.Sometimes(0.34, iaa.Grayscale(alpha=(0.0, 1.0))),
iaa.Sometimes(0.2, iaa.ChangeColorTemperature((1100, 10000))),
iaa.Sometimes(0.1, iaa.GammaContrast((0.5, 2.0))),
iaa.Sometimes(0.2, iaa.SigmoidContrast(gain=(3, 10),
cutoff=(0.4, 0.6))),
iaa.Sometimes(0.1, iaa.CLAHE()),
iaa.Sometimes(0.1, iaa.HistogramEqualization()),
iaa.Sometimes(0.2, iaa.LinearContrast((0.5, 2.0), per_channel=0.5)),
iaa.Sometimes(0.1, iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)))
])
示例2
def __init__(self):
self.seq = iaa.Sequential([
iaa.Sometimes(0.5, iaa.OneOf([
iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0
iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7
iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7
])),
iaa.Sometimes(0.5, iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5)),
iaa.Sometimes(0.5, iaa.Add((-10, 10), per_channel=0.5)),
iaa.Sometimes(0.5, iaa.AddToHueAndSaturation((-20, 20))),
iaa.Sometimes(0.5, iaa.FrequencyNoiseAlpha(
exponent=(-4, 0),
first=iaa.Multiply((0.5, 1.5), per_channel=True),
second=iaa.LinearContrast((0.5, 2.0))
)),
iaa.Sometimes(0.5, iaa.PiecewiseAffine(scale=(0.01, 0.05))),
iaa.Sometimes(0.5, iaa.PerspectiveTransform(scale=(0.01, 0.1)))
], random_order=True)
示例3
def __init__(self, augmentation_rate):
self.augs = iaa.Sometimes(
augmentation_rate,
iaa.SomeOf(
(4, 7),
[
iaa.Affine(rotate=(-10, 10)),
iaa.Fliplr(0.2),
iaa.AverageBlur(k=(2, 10)),
iaa.Add((-10, 10), per_channel=0.5),
iaa.Multiply((0.75, 1.25), per_channel=0.5),
iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5),
iaa.Crop(px=(0, 20))
],
random_order=True
)
)
示例4
def init_augmentations(self):
if self.transform_probability > 0 and self.use_imgaug:
augmentations = iaa.Sometimes(
self.transform_probability,
iaa.Sequential([
iaa.SomeOf(
(1, None),
[
iaa.AddToHueAndSaturation(iap.Uniform(-20, 20), per_channel=True),
iaa.GaussianBlur(sigma=(0, 1.0)),
iaa.LinearContrast((0.75, 1.0)),
iaa.PiecewiseAffine(scale=(0.01, 0.02), mode='edge'),
],
random_order=True
),
iaa.Resize(
{"height": (16, self.image_size.height), "width": "keep-aspect-ratio"},
interpolation=imgaug.ALL
),
])
)
else:
augmentations = None
return augmentations
示例5
def chapter_augmenters_sometimes():
aug = iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=2.0))
run_and_save_augseq(
"sometimes.jpg", aug,
[ia.quokka(size=(64, 64)) for _ in range(16)], cols=8, rows=2,
seed=2
)
aug = iaa.Sometimes(
0.5,
iaa.GaussianBlur(sigma=2.0),
iaa.Sequential([iaa.Affine(rotate=45), iaa.Sharpen(alpha=1.0)])
)
run_and_save_augseq(
"sometimes_if_else.jpg", aug,
[ia.quokka(size=(64, 64)) for _ in range(16)], cols=8, rows=2
)
示例6
def _rectify_augmenter(self, augment):
import netharn as nh
if augment is True:
augment = 'simple'
if not augment:
augmenter = None
elif augment == 'simple':
augmenter = iaa.Sequential([
iaa.Crop(percent=(0, .2)),
iaa.Fliplr(p=.5)
])
elif augment == 'complex':
augmenter = iaa.Sequential([
iaa.Sometimes(0.2, nh.data.transforms.HSVShift(hue=0.1, sat=1.5, val=1.5)),
iaa.Crop(percent=(0, .2)),
iaa.Fliplr(p=.5)
])
else:
raise KeyError('Unknown augmentation {!r}'.format(augment))
return augmenter
示例7
def _rectify_augmenter(self, augmenter):
import netharn as nh
if augmenter is True:
augmenter = 'simple'
if not augmenter:
augmenter = None
elif augmenter == 'simple':
augmenter = iaa.Sequential([
iaa.Crop(percent=(0, .2)),
iaa.Fliplr(p=.5)
])
elif augmenter == 'complex':
augmenter = iaa.Sequential([
iaa.Sometimes(0.2, nh.data.transforms.HSVShift(hue=0.1, sat=1.5, val=1.5)),
iaa.Crop(percent=(0, .2)),
iaa.Fliplr(p=.5)
])
else:
raise KeyError('Unknown augmentation {!r}'.format(self.augment))
return augmenter
示例8
def __init__(self,data_dir, back_dir,
batch_size=50,gan=True,imsize=128,
res_x=640,res_y=480,
**kwargs):
'''
data_dir: Folder that contains cropped image+xyz
back_dir: Folder that contains random background images
batch_size: batch size for training
gan: if False, gt for GAN is not yielded
'''
self.data_dir = data_dir
self.back_dir = back_dir
self.imsize=imsize
self.batch_size = batch_size
self.gan = gan
self.backfiles = os.listdir(back_dir)
data_list = os.listdir(data_dir)
self.datafiles=[]
self.res_x=res_x
self.res_y=res_y
for file in data_list:
if(file.endswith(".npy")):
self.datafiles.append(file)
self.n_data = len(self.datafiles)
self.n_background = len(self.backfiles)
print("Total training views:", self.n_data)
self.seq_syn= iaa.Sequential([
iaa.WithChannels(0, iaa.Add((-15, 15))),
iaa.WithChannels(1, iaa.Add((-15, 15))),
iaa.WithChannels(2, iaa.Add((-15, 15))),
iaa.ContrastNormalization((0.8, 1.3)),
iaa.Multiply((0.8, 1.2),per_channel=0.5),
iaa.GaussianBlur(sigma=(0.0, 0.5)),
iaa.Sometimes(0.1, iaa.AdditiveGaussianNoise(scale=10, per_channel=True)),
iaa.Sometimes(0.5, iaa.ContrastNormalization((0.5, 2.2), per_channel=0.3)),
], random_order=True)
示例9
def _load_augmentation_aug_geometric():
return iaa.OneOf([
iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.2)]),
iaa.CropAndPad(percent=(-0.05, 0.1),
pad_mode='constant',
pad_cval=(0, 255)),
iaa.Crop(percent=(0.0, 0.1)),
iaa.Crop(percent=(0.3, 0.5)),
iaa.Crop(percent=(0.3, 0.5)),
iaa.Crop(percent=(0.3, 0.5)),
iaa.Sequential([
iaa.Affine(
# scale images to 80-120% of their size,
# individually per axis
scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
# translate by -20 to +20 percent (per axis)
translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
rotate=(-45, 45), # rotate by -45 to +45 degrees
shear=(-16, 16), # shear by -16 to +16 degrees
# use nearest neighbour or bilinear interpolation (fast)
order=[0, 1],
# if mode is constant, use a cval between 0 and 255
mode='constant',
cval=(0, 255),
# use any of scikit-image's warping modes
# (see 2nd image from the top for examples)
),
iaa.Sometimes(0.3, iaa.Crop(percent=(0.3, 0.5)))])
])
示例10
def _load_augmentation_aug_all2():
return iaa.Sequential([
iaa.Sometimes(0.65, _load_augmentation_aug_non_geometric()),
iaa.Sometimes(0.65, _load_augmentation_aug_geometric())
])
示例11
def example_using_augmenters_only_once():
print("Example: Using Augmenters Only Once")
from imgaug import augmenters as iaa
import numpy as np
images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)
# always horizontally flip each input image
images_aug = iaa.Fliplr(1.0)(images=images)
# vertically flip each input image with 90% probability
images_aug = iaa.Flipud(0.9)(images=images)
# blur 50% of all images using a gaussian kernel with a sigma of 3.0
images_aug = iaa.Sometimes(0.5, iaa.GaussianBlur(3.0))(images=images)
示例12
def apply_augment_sequence(image_set_x, image_set_y):
"""
Randomly flip and rotate the images in both set with deterministic order. This turns 1 image into 8 images.
Parameters:
image_set_x: List of Images (X) to augment
image_set_y: List of corresponding Y image to augment in the same deterministic order applied to image_set_x
Returns:
image_setx_aug, image_sety_aug : augmented versions of the inputs
"""
# Sometimes(0.5, ...) applies the given augmenter in 50% of all cases,
# e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second image.
sometimes = lambda aug: iaa.Sometimes(0.5, aug)
seq = iaa.Sequential(
[
iaa.Fliplr(0.5),
iaa.Flipud(0.5),
sometimes(iaa.Affine(
rotate=(90, 90),
))
],
random_order=False)
seq_det = seq.to_deterministic()
image_setx_aug = seq_det.augment_images(image_set_x)
image_sety_aug = seq_det.augment_images(image_set_y)
return image_setx_aug, image_sety_aug
示例13
def augment_soft(img):
# Sometimes(0.5, ...) applies the given augmenter in 50% of all cases,
# e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second image.
sometimes = lambda aug: iaa.Sometimes(0.5, aug)
# Define our sequence of augmentation steps that will be applied to every image
# All augmenters with per_channel=0.5 will sample one value _per image_
# in 50% of all cases. In all other cases they will sample new values
# _per channel_.
seq = iaa.Sequential(
[
# apply the following augmenters to most images
iaa.Fliplr(0.5), # horizontally flip 50% of all images
# crop images by -5% to 10% of their height/width
iaa.Crop(
percent=(0, 0.2),
),
iaa.Scale({"height": CROP_SIZE, "width": CROP_SIZE }),
],
random_order=False
)
if img.ndim == 3:
img = seq.augment_images(np.expand_dims(img, axis=0)).squeeze(axis=0)
else:
img = seq.augment_images(img)
return img
示例14
def imgaug(args):
# Number of batches and batch size for this example
filename, root, fold_A = args
img = cv2.imread(os.path.join(root,filename))
print('image opened ' + os.path.join(root,filename))
batch_size = 4
for i in range(0,batch_size):
imageio.imwrite(os.path.join(root, os.path.splitext(filename)[0] + '_' + str(i) + '.jpg'), img) #convert the current image in B into a jpg from png
nb_batches = 1
# Example augmentation sequence to run in the background
sometimes = lambda aug: iaa.Sometimes(0.4, aug)
augseq = iaa.Sequential(
[
iaa.PiecewiseAffine(scale=(0.01, 0.01005))
]
)
# Make batches out of the example image (here: 10 batches, each 32 times
# the example image)
batches = []
for _ in range(nb_batches):
batches.append(Batch(images=[img] * batch_size))
#Save images
for batch in augseq.augment_batches(batches, background=False):
count = 0
for img in batch.images_aug:
path = os.path.join(fold_A,root.rsplit('/', 1)[-1], os.path.splitext(filename)[0] + '_' + str(count) + '.jpg')
cv2.imwrite(path, img)
print('image saved as: ' + path)
count +=1
示例15
def aug_on_fly(img, det_mask, cls_mask):
"""Do augmentation with different combination on each training batch
"""
def image_basic_augmentation(image, masks, ratio_operations=0.9):
# without additional operations
# according to the paper, operations such as shearing, fliping horizontal/vertical,
# rotating, zooming and channel shifting will be apply
sometimes = lambda aug: iaa.Sometimes(ratio_operations, aug)
hor_flip_angle = np.random.uniform(0, 1)
ver_flip_angle = np.random.uniform(0, 1)
seq = iaa.Sequential([
sometimes(
iaa.SomeOf((0, 5), [
iaa.Fliplr(hor_flip_angle),
iaa.Flipud(ver_flip_angle),
iaa.Affine(shear=(-16, 16)),
iaa.Affine(scale={'x': (1, 1.6), 'y': (1, 1.6)}),
iaa.PerspectiveTransform(scale=(0.01, 0.1))
]))
])
det_mask, cls_mask = masks[0], masks[1]
seq_to_deterministic = seq.to_deterministic()
aug_img = seq_to_deterministic.augment_images(image)
aug_det_mask = seq_to_deterministic.augment_images(det_mask)
aug_cls_mask = seq_to_deterministic.augment_images(cls_mask)
return aug_img, aug_det_mask, aug_cls_mask
aug_image, aug_det_mask, aug_cls_mask = image_basic_augmentation(image=img, masks=[det_mask, cls_mask])
return aug_image, aug_det_mask, aug_cls_mask
示例16
def aug_on_fly(img, det_mask, cls_mask):
"""Do augmentation with different combination on each training batch
"""
def image_basic_augmentation(image, masks, ratio_operations=0.9):
# without additional operations
# according to the paper, operations such as shearing, fliping horizontal/vertical,
# rotating, zooming and channel shifting will be apply
sometimes = lambda aug: iaa.Sometimes(ratio_operations, aug)
hor_flip_angle = np.random.uniform(0, 1)
ver_flip_angle = np.random.uniform(0, 1)
seq = iaa.Sequential([
sometimes(
iaa.SomeOf((0, 5), [
iaa.Fliplr(hor_flip_angle),
iaa.Flipud(ver_flip_angle),
iaa.Affine(shear=(-16, 16)),
iaa.Affine(scale={'x': (1, 1.6), 'y': (1, 1.6)}),
iaa.PerspectiveTransform(scale=(0.01, 0.1))
]))
])
det_mask, cls_mask = masks[0], masks[1]
seq_to_deterministic = seq.to_deterministic()
aug_img = seq_to_deterministic.augment_images(image)
aug_det_mask = seq_to_deterministic.augment_images(det_mask)
aug_cls_mask = seq_to_deterministic.augment_images(cls_mask)
return aug_img, aug_det_mask, aug_cls_mask
aug_image, aug_det_mask, aug_cls_mask = image_basic_augmentation(image=img, masks=[det_mask, cls_mask])
return aug_image, aug_det_mask, aug_cls_mask
示例17
def __init__(self, devkit_dpath=None, split='train', years=[2007, 2012],
base_wh=[416, 416], scales=[-3, 6], factor=32):
super(YoloVOCDataset, self).__init__(devkit_dpath, split=split,
years=years)
self.split = split
self.factor = factor # downsample factor of yolo grid
self.base_wh = np.array(base_wh, dtype=np.int)
assert np.all(self.base_wh % self.factor == 0)
self.multi_scale_inp_size = np.array([
self.base_wh + (self.factor * i) for i in range(*scales)])
self.multi_scale_out_size = self.multi_scale_inp_size // self.factor
self.augmenter = None
if 'train' in split:
augmentors = [
# Order used in lightnet is hsv, rc, rf, lb
# lb is applied externally to augmenters
iaa.Sometimes(.9, HSVShift(hue=0.1, sat=1.5, val=1.5)),
iaa.Crop(percent=(0, .2), keep_size=False),
iaa.Fliplr(p=.5),
]
self.augmenter = iaa.Sequential(augmentors)
# Used to resize images to the appropriate inp_size without changing
# the aspect ratio.
self.letterbox = nh.data.transforms.Resize(None, mode='letterbox')
示例18
def __init__(self, sampler, augment='simple', input_dims=[416, 416],
scales=[-3, 6], factor=32):
super(DetectDataset, self).__init__()
self.sampler = sampler
self.factor = factor # downsample factor of yolo grid
self.input_dims = np.array(input_dims, dtype=np.int)
assert np.all(self.input_dims % self.factor == 0)
self.multi_scale_inp_size = np.array([
self.input_dims + (self.factor * i) for i in range(*scales)])
self.multi_scale_out_size = self.multi_scale_inp_size // self.factor
import imgaug.augmenters as iaa
self.augmenter = None
if not augment:
self.augmenter = None
elif augment == 'simple':
augmentors = [
# Order used in lightnet is hsv, rc, rf, lb
# lb is applied externally to augmenters
# iaa.Sometimes(.9, HSVShift(hue=0.1, sat=1.5, val=1.5)),
iaa.Crop(percent=(0, .2), keep_size=False),
iaa.Fliplr(p=.5),
]
self.augmenter = iaa.Sequential(augmentors)
else:
raise KeyError(augment)
# Used to resize images to the appropriate inp_size without changing
# the aspect ratio.
self.letterbox = nh.data.transforms.Resize(None, mode='letterbox')
self.input_id = ub.hash_data([
self.sampler._depends()
])
示例19
def _create_augment_pipeline():
from imgaug import augmenters as iaa
### augmentors by https://github.com/aleju/imgaug
sometimes = lambda aug: iaa.Sometimes(0.5, aug)
# Define our sequence of augmentation steps that will be applied to every image
# All augmenters with per_channel=0.5 will sample one value _per image_
# in 50% of all cases. In all other cases they will sample new values
# _per channel_.
aug_pipe = iaa.Sequential(
[
# apply the following augmenters to most images
#iaa.Fliplr(0.5), # horizontally flip 50% of all images
#iaa.Flipud(0.2), # vertically flip 20% of all images
#sometimes(iaa.Crop(percent=(0, 0.1))), # crop images by 0-10% of their height/width
sometimes(iaa.Affine(
#scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
#translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
#rotate=(-5, 5), # rotate by -45 to +45 degrees
#shear=(-5, 5), # shear by -16 to +16 degrees
#order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
#cval=(0, 255), # if mode is constant, use a cval between 0 and 255
#mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
)),
# execute 0 to 5 of the following (less important) augmenters per image
# don't execute all of them, as that would often be way too strong
iaa.SomeOf((0, 5),
[
#sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation
iaa.OneOf([
iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0
iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7
iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7
]),
iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images
#iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images
# search either for all edges or for directed edges
#sometimes(iaa.OneOf([
# iaa.EdgeDetect(alpha=(0, 0.7)),
# iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)),
#])),
iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images
iaa.OneOf([
iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels
#iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
]),
#iaa.Invert(0.05, per_channel=True), # invert color channels
iaa.Add((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value)
iaa.Multiply((0.5, 1.5), per_channel=0.5), # change brightness of images (50-150% of original value)
iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast
#iaa.Grayscale(alpha=(0.0, 1.0)),
#sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths)
#sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))) # sometimes move parts of the image around
],
random_order=True
)
],
random_order=True
)
return aug_pipe
示例20
def medium(image_iteration):
iteration = image_iteration/(120*1.5)
frequency_factor = 0.05 + float(iteration)/1000000.0
color_factor = float(iteration)/1000000.0
dropout_factor = 0.198667 + (0.03856658 - 0.198667) / (1 + (iteration / 196416.6) ** 1.863486)
blur_factor = 0.5 + (0.5*iteration/100000.0)
add_factor = 10 + 10*iteration/150000.0
multiply_factor_pos = 1 + (2.5*iteration/500000.0)
multiply_factor_neg = 1 - (0.91 * iteration / 500000.0)
contrast_factor_pos = 1 + (0.5*iteration/500000.0)
contrast_factor_neg = 1 - (0.5 * iteration / 500000.0)
#print 'Augment Status ',frequency_factor,color_factor,dropout_factor,blur_factor,add_factor,\
# multiply_factor_pos,multiply_factor_neg,contrast_factor_pos,contrast_factor_neg
augmenter = iaa.Sequential([
iaa.Sometimes(frequency_factor, iaa.GaussianBlur((0, blur_factor))),
# blur images with a sigma between 0 and 1.5
iaa.Sometimes(frequency_factor, iaa.AdditiveGaussianNoise(loc=0, scale=(0.0,dropout_factor ),
per_channel=color_factor)),
# add gaussian noise to images
iaa.Sometimes(frequency_factor, iaa.CoarseDropout((0.0, dropout_factor), size_percent=(
0.08, 0.2), per_channel=color_factor)),
# randomly remove up to X% of the pixels
iaa.Sometimes(frequency_factor, iaa.Dropout((0.0, dropout_factor), per_channel=color_factor)),
# randomly remove up to X% of the pixels
iaa.Sometimes(frequency_factor,
iaa.Add((-add_factor, add_factor), per_channel=color_factor)),
# change brightness of images (by -X to Y of original value)
iaa.Sometimes(frequency_factor,
iaa.Multiply((multiply_factor_neg, multiply_factor_pos), per_channel=color_factor)),
# change brightness of images (X-Y% of original value)
iaa.Sometimes(frequency_factor, iaa.ContrastNormalization((contrast_factor_neg, contrast_factor_pos),
per_channel=color_factor)),
# improve or worsen the contrast
iaa.Sometimes(frequency_factor, iaa.Grayscale((0.0, 1))), # put grayscale
],
random_order=True # do all of the above in random order
)
return augmenter
示例21
def soft(image_iteration):
iteration = image_iteration/(120*1.5)
frequency_factor = 0.05 + float(iteration)/1200000.0
color_factor = float(iteration)/1200000.0
dropout_factor = 0.198667 + (0.03856658 - 0.198667) / (1 + (iteration / 196416.6) ** 1.863486)
blur_factor = 0.5 + (0.5*iteration/120000.0)
add_factor = 10 + 10*iteration/170000.0
multiply_factor_pos = 1 + (2.5*iteration/800000.0)
multiply_factor_neg = 1 - (0.91 * iteration / 800000.0)
contrast_factor_pos = 1 + (0.5*iteration/800000.0)
contrast_factor_neg = 1 - (0.5 * iteration / 800000.0)
#print ('iteration',iteration,'Augment Status ',frequency_factor,color_factor,dropout_factor,blur_factor,add_factor,
# multiply_factor_pos,multiply_factor_neg,contrast_factor_pos,contrast_factor_neg)
augmenter = iaa.Sequential([
iaa.Sometimes(frequency_factor, iaa.GaussianBlur((0, blur_factor))),
# blur images with a sigma between 0 and 1.5
iaa.Sometimes(frequency_factor, iaa.AdditiveGaussianNoise(loc=0, scale=(0.0,dropout_factor ),
per_channel=color_factor)),
# add gaussian noise to images
iaa.Sometimes(frequency_factor, iaa.CoarseDropout((0.0, dropout_factor), size_percent=(
0.08, 0.2), per_channel=color_factor)),
# randomly remove up to X% of the pixels
iaa.Sometimes(frequency_factor, iaa.Dropout((0.0, dropout_factor), per_channel=color_factor)),
# randomly remove up to X% of the pixels
iaa.Sometimes(frequency_factor,
iaa.Add((-add_factor, add_factor), per_channel=color_factor)),
# change brightness of images (by -X to Y of original value)
iaa.Sometimes(frequency_factor,
iaa.Multiply((multiply_factor_neg, multiply_factor_pos), per_channel=color_factor)),
# change brightness of images (X-Y% of original value)
iaa.Sometimes(frequency_factor, iaa.ContrastNormalization((contrast_factor_neg, contrast_factor_pos),
per_channel=color_factor)),
# improve or worsen the contrast
iaa.Sometimes(frequency_factor, iaa.Grayscale((0.0, 1))), # put grayscale
],
random_order=True # do all of the above in random order
)
return augmenter
示例22
def high(image_iteration):
iteration = image_iteration/(120*1.5)
frequency_factor = 0.05 + float(iteration)/800000.0
color_factor = float(iteration)/800000.0
dropout_factor = 0.198667 + (0.03856658 - 0.198667) / (1 + (iteration / 196416.6) ** 1.863486)
blur_factor = 0.5 + (0.5*iteration/80000.0)
add_factor = 10 + 10*iteration/120000.0
multiply_factor_pos = 1 + (2.5*iteration/350000.0)
multiply_factor_neg = 1 - (0.91 * iteration / 400000.0)
contrast_factor_pos = 1 + (0.5*iteration/350000.0)
contrast_factor_neg = 1 - (0.5 * iteration / 400000.0)
#print ('iteration',iteration,'Augment Status ',frequency_factor,color_factor,dropout_factor,blur_factor,add_factor,
# multiply_factor_pos,multiply_factor_neg,contrast_factor_pos,contrast_factor_neg)
augmenter = iaa.Sequential([
iaa.Sometimes(frequency_factor, iaa.GaussianBlur((0, blur_factor))),
# blur images with a sigma between 0 and 1.5
iaa.Sometimes(frequency_factor, iaa.AdditiveGaussianNoise(loc=0, scale=(0.0,dropout_factor ),
per_channel=color_factor)),
# add gaussian noise to images
iaa.Sometimes(frequency_factor, iaa.CoarseDropout((0.0, dropout_factor), size_percent=(
0.08, 0.2), per_channel=color_factor)),
# randomly remove up to X% of the pixels
iaa.Sometimes(frequency_factor, iaa.Dropout((0.0, dropout_factor), per_channel=color_factor)),
# randomly remove up to X% of the pixels
iaa.Sometimes(frequency_factor,
iaa.Add((-add_factor, add_factor), per_channel=color_factor)),
# change brightness of images (by -X to Y of original value)
iaa.Sometimes(frequency_factor,
iaa.Multiply((multiply_factor_neg, multiply_factor_pos), per_channel=color_factor)),
# change brightness of images (X-Y% of original value)
iaa.Sometimes(frequency_factor, iaa.ContrastNormalization((contrast_factor_neg, contrast_factor_pos),
per_channel=color_factor)),
# improve or worsen the contrast
iaa.Sometimes(frequency_factor, iaa.Grayscale((0.0, 1))), # put grayscale
],
random_order=True # do all of the above in random order
)
return augmenter
示例23
def medium_harder(image_iteration):
iteration = image_iteration / 120
frequency_factor = 0.05 + float(iteration)/1000000.0
color_factor = float(iteration)/1000000.0
dropout_factor = 0.198667 + (0.03856658 - 0.198667) / (1 + (iteration / 196416.6) ** 1.863486)
blur_factor = 0.5 + (0.5*iteration/100000.0)
add_factor = 10 + 10*iteration/150000.0
multiply_factor_pos = 1 + (2.5*iteration/500000.0)
multiply_factor_neg = 1 - (0.91 * iteration / 500000.0)
contrast_factor_pos = 1 + (0.5*iteration/500000.0)
contrast_factor_neg = 1 - (0.5 * iteration / 500000.0)
#print 'Augment Status ',frequency_factor,color_factor,dropout_factor,blur_factor,add_factor,\
# multiply_factor_pos,multiply_factor_neg,contrast_factor_pos,contrast_factor_neg
augmenter = iaa.Sequential([
iaa.Sometimes(frequency_factor, iaa.GaussianBlur((0, blur_factor))),
# blur images with a sigma between 0 and 1.5
iaa.Sometimes(frequency_factor, iaa.AdditiveGaussianNoise(loc=0, scale=(0.0,dropout_factor ),
per_channel=color_factor)),
# add gaussian noise to images
iaa.Sometimes(frequency_factor, iaa.CoarseDropout((0.0, dropout_factor), size_percent=(
0.08, 0.2), per_channel=color_factor)),
# randomly remove up to X% of the pixels
iaa.Sometimes(frequency_factor, iaa.Dropout((0.0, dropout_factor), per_channel=color_factor)),
# randomly remove up to X% of the pixels
iaa.Sometimes(frequency_factor,
iaa.Add((-add_factor, add_factor), per_channel=color_factor)),
# change brightness of images (by -X to Y of original value)
iaa.Sometimes(frequency_factor,
iaa.Multiply((multiply_factor_neg, multiply_factor_pos), per_channel=color_factor)),
# change brightness of images (X-Y% of original value)
iaa.Sometimes(frequency_factor, iaa.ContrastNormalization((contrast_factor_neg, contrast_factor_pos),
per_channel=color_factor)),
# improve or worsen the contrast
iaa.Sometimes(frequency_factor, iaa.Grayscale((0.0, 1))), # put grayscale
],
random_order=True # do all of the above in random order
)
return augmenter
示例24
def hard_harder(image_iteration):
iteration = image_iteration / 120
frequency_factor = min(0.05 + float(iteration)/200000.0, 1.0)
color_factor = float(iteration)/1000000.0
dropout_factor = 0.198667 + (0.03856658 - 0.198667) / (1 + (iteration / 196416.6) ** 1.863486)
blur_factor = 0.5 + (0.5*iteration/100000.0)
add_factor = 10 + 10*iteration/100000.0
multiply_factor_pos = 1 + (2.5*iteration/200000.0)
multiply_factor_neg = 1 - (0.91 * iteration / 500000.0)
contrast_factor_pos = 1 + (0.5*iteration/500000.0)
contrast_factor_neg = 1 - (0.5 * iteration / 500000.0)
#print 'Augment Status ',frequency_factor,color_factor,dropout_factor,blur_factor,add_factor,\
# multiply_factor_pos,multiply_factor_neg,contrast_factor_pos,contrast_factor_neg
augmenter = iaa.Sequential([
iaa.Sometimes(frequency_factor, iaa.GaussianBlur((0, blur_factor))),
# blur images with a sigma between 0 and 1.5
iaa.Sometimes(frequency_factor, iaa.AdditiveGaussianNoise(loc=0, scale=(0.0,dropout_factor ),
per_channel=color_factor)),
# add gaussian noise to images
iaa.Sometimes(frequency_factor, iaa.CoarseDropout((0.0, dropout_factor), size_percent=(
0.08, 0.2), per_channel=color_factor)),
# randomly remove up to X% of the pixels
iaa.Sometimes(frequency_factor, iaa.Dropout((0.0, dropout_factor), per_channel=color_factor)),
# randomly remove up to X% of the pixels
iaa.Sometimes(frequency_factor,
iaa.Add((-add_factor, add_factor), per_channel=color_factor)),
# change brightness of images (by -X to Y of original value)
iaa.Sometimes(frequency_factor,
iaa.Multiply((multiply_factor_neg, multiply_factor_pos), per_channel=color_factor)),
# change brightness of images (X-Y% of original value)
iaa.Sometimes(frequency_factor, iaa.ContrastNormalization((contrast_factor_neg, contrast_factor_pos),
per_channel=color_factor)),
# improve or worsen the contrast
iaa.Sometimes(frequency_factor, iaa.Grayscale((0.0, 1))), # put grayscale
],
random_order=True # do all of the above in random order
)
return augmenter
示例25
def build_augmentation_pipeline(self, height=None, width=None, apply_prob=0.5):
sometimes = lambda aug: iaa.Sometimes(apply_prob, aug)
pipeline = iaa.Sequential(random_order=False)
cfg = self.cfg
if cfg.get("fliplr", False):
opt = cfg.get("fliplr", False)
if type(opt) == int:
pipeline.add(sometimes(iaa.Fliplr(opt)))
else:
pipeline.add(sometimes(iaa.Fliplr(0.5)))
if cfg.get("rotation", False):
opt = cfg.get("rotation", False)
if type(opt) == int:
pipeline.add(sometimes(iaa.Affine(rotate=(-opt, opt))))
else:
pipeline.add(sometimes(iaa.Affine(rotate=(-10, 10))))
if cfg.get("hist_eq", False):
pipeline.add(sometimes(iaa.AllChannelsHistogramEqualization()))
if cfg.get("motion_blur", False):
opts = cfg.get("motion_blur", False)
if type(opts) == list:
opts = dict(opts)
pipeline.add(sometimes(iaa.MotionBlur(**opts)))
else:
pipeline.add(sometimes(iaa.MotionBlur(k=7, angle=(-90, 90))))
if cfg.get("covering", False):
pipeline.add(
sometimes(iaa.CoarseDropout((0, 0.02), size_percent=(0.01, 0.05)))
) # , per_channel=0.5)))
if cfg.get("elastic_transform", False):
pipeline.add(sometimes(iaa.ElasticTransformation(sigma=5)))
if cfg.get("gaussian_noise", False):
opt = cfg.get("gaussian_noise", False)
if type(opt) == int or type(opt) == float:
pipeline.add(
sometimes(
iaa.AdditiveGaussianNoise(
loc=0, scale=(0.0, opt), per_channel=0.5
)
)
)
else:
pipeline.add(
sometimes(
iaa.AdditiveGaussianNoise(
loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5
)
)
)
if height is not None and width is not None:
pipeline.add(
iaa.Sometimes(
cfg.cropratio, iaa.CropAndPad(percent=(-0.3, 0.1), keep_size=False)
)
)
pipeline.add(iaa.Resize({"height": height, "width": width}))
return pipeline
示例26
def heavy_aug_on_fly(img, det_mask):
"""Do augmentation with different combination on each training batch
"""
def image_heavy_augmentation(image, det_masks, ratio_operations=0.6):
# according to the paper, operations such as shearing, fliping horizontal/vertical,
# rotating, zooming and channel shifting will be apply
sometimes = lambda aug: iaa.Sometimes(ratio_operations, aug)
edge_detect_sometime = lambda aug: iaa.Sometimes(0.1, aug)
elasitic_sometime = lambda aug:iaa.Sometimes(0.2, aug)
add_gauss_noise = lambda aug: iaa.Sometimes(0.15, aug)
hor_flip_angle = np.random.uniform(0, 1)
ver_flip_angle = np.random.uniform(0, 1)
seq = iaa.Sequential([
iaa.SomeOf((0, 5), [
iaa.Fliplr(hor_flip_angle),
iaa.Flipud(ver_flip_angle),
iaa.Affine(shear=(-16, 16)),
iaa.Affine(scale={'x': (1, 1.6), 'y': (1, 1.6)}),
iaa.PerspectiveTransform(scale=(0.01, 0.1)),
# These are additional augmentation.
#iaa.ContrastNormalization((0.75, 1.5))
]),
edge_detect_sometime(iaa.OneOf([
iaa.EdgeDetect(alpha=(0, 0.7)),
iaa.DirectedEdgeDetect(alpha=(0,0.7), direction=(0.0, 1.0)
)
])),
add_gauss_noise(iaa.AdditiveGaussianNoise(loc=0,
scale=(0.0, 0.05*255),
per_channel=0.5)
),
iaa.Sometimes(0.3,
iaa.GaussianBlur(sigma=(0, 0.5))
),
elasitic_sometime(
iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25))
])
seq_to_deterministic = seq.to_deterministic()
aug_img = seq_to_deterministic.augment_images(image)
aug_det_mask = seq_to_deterministic.augment_images(det_masks)
return aug_img, aug_det_mask
aug_image, aug_det_mask = image_heavy_augmentation(image=img, det_masks=det_mask)
return aug_image, aug_det_mask
示例27
def get_augmentations():
# applies the given augmenter in 50% of all cases,
sometimes = lambda aug: iaa.Sometimes(0.5, aug)
# Define our sequence of augmentation steps that will be applied to every image
seq = iaa.Sequential([
# execute 0 to 5 of the following (less important) augmenters per image
iaa.SomeOf((0, 5),
[
iaa.OneOf([
iaa.GaussianBlur((0, 3.0)),
iaa.AverageBlur(k=(2, 7)),
iaa.MedianBlur(k=(3, 11)),
]),
iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),
# search either for all edges or for directed edges,
# blend the result with the original image using a blobby mask
iaa.SimplexNoiseAlpha(iaa.OneOf([
iaa.EdgeDetect(alpha=(0.5, 1.0)),
iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)),
])),
iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
iaa.OneOf([
iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels
iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
]),
iaa.Add((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value)
iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation
# either change the brightness of the whole image (sometimes
# per channel) or change the brightness of subareas
iaa.OneOf([
iaa.Multiply((0.5, 1.5), per_channel=0.5),
iaa.FrequencyNoiseAlpha(
exponent=(-4, 0),
first=iaa.Multiply((0.5, 1.5), per_channel=True),
second=iaa.ContrastNormalization((0.5, 2.0))
)
]),
iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast
sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths)
],
random_order=True
)
],
random_order=True
)
return seq
### data transforms
示例28
def chapter_examples_basics_simple():
import imgaug as ia
from imgaug import augmenters as iaa
# Example batch of images.
# The array has shape (32, 64, 64, 3) and dtype uint8.
images = np.array(
[ia.quokka(size=(64, 64)) for _ in range(32)],
dtype=np.uint8
)
seq = iaa.Sequential([
iaa.Fliplr(0.5), # horizontal flips
iaa.Crop(percent=(0, 0.1)), # random crops
# Small gaussian blur with random sigma between 0 and 0.5.
# But we only blur about 50% of all images.
iaa.Sometimes(0.5,
iaa.GaussianBlur(sigma=(0, 0.5))
),
# Strengthen or weaken the contrast in each image.
iaa.ContrastNormalization((0.75, 1.5)),
# Add gaussian noise.
# For 50% of all images, we sample the noise once per pixel.
# For the other 50% of all images, we sample the noise per pixel AND
# channel. This can change the color (not only brightness) of the
# pixels.
iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
# Make some images brighter and some darker.
# In 20% of all cases, we sample the multiplier once per channel,
# which can end up changing the color of the images.
iaa.Multiply((0.8, 1.2), per_channel=0.2),
# Apply affine transformations to each image.
# Scale/zoom them, translate/move them, rotate them and shear them.
iaa.Affine(
scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
rotate=(-25, 25),
shear=(-8, 8)
)
], random_order=True) # apply augmenters in random order
ia.seed(1)
images_aug = seq.augment_images(images)
# ------------
save(
"examples_basics",
"simple.jpg",
grid(images_aug, cols=8, rows=4)
)
示例29
def example_heavy_augmentations():
print("Example: Heavy Augmentations")
import imgaug as ia
from imgaug import augmenters as iaa
# random example images
images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)
# Sometimes(0.5, ...) applies the given augmenter in 50% of all cases,
# e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second image.
st = lambda aug: iaa.Sometimes(0.5, aug)
# Define our sequence of augmentation steps that will be applied to every image
# All augmenters with per_channel=0.5 will sample one value _per image_
# in 50% of all cases. In all other cases they will sample new values
# _per channel_.
seq = iaa.Sequential([
iaa.Fliplr(0.5), # horizontally flip 50% of all images
iaa.Flipud(0.5), # vertically flip 50% of all images
st(iaa.Crop(percent=(0, 0.1))), # crop images by 0-10% of their height/width
st(iaa.GaussianBlur((0, 3.0))), # blur images with a sigma between 0 and 3.0
st(iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5)), # add gaussian noise to images
st(iaa.Dropout((0.0, 0.1), per_channel=0.5)), # randomly remove up to 10% of the pixels
st(iaa.Add((-10, 10), per_channel=0.5)), # change brightness of images (by -10 to 10 of original value)
st(iaa.Multiply((0.5, 1.5), per_channel=0.5)), # change brightness of images (50-150% of original value)
st(iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5)), # improve or worsen the contrast
st(iaa.Grayscale((0.0, 1.0))), # blend with grayscale image
st(iaa.Affine(
scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
translate_px={"x": (-16, 16), "y": (-16, 16)}, # translate by -16 to +16 pixels (per axis)
rotate=(-45, 45), # rotate by -45 to +45 degrees
shear=(-16, 16), # shear by -16 to +16 degrees
order=[0, 1], # use scikit-image's interpolation orders 0 (nearest neighbour) and 1 (bilinear)
cval=(0, 255), # if mode is constant, use a cval between 0 and 1.0
mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
)),
st(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)) # apply elastic transformations with random strengths
],
random_order=True # do all of the above in random order
)
images_aug = seq.augment_images(images)
# -----
# Make sure that the example really does something
assert not np.array_equal(images, images_aug)
示例30
def __init__(self, sampler, workdir=None, augment=False, dim=416):
print('make AnnotCocoDataset')
cacher = ub.Cacher('aid_pccs_v2', cfgstr=sampler.dset.tag, verbose=True)
aid_pccs = cacher.tryload()
if aid_pccs is None:
aid_pccs = extract_ggr_pccs(sampler.dset)
cacher.save(aid_pccs)
self.aid_pccs = aid_pccs
self.sampler = sampler
self.aids = sorted(ub.flatten(self.aid_pccs))
self.aid_to_index = aid_to_index = {aid: index for index, aid in enumerate(self.aids)}
# index pccs
self.index_pccs = [frozenset(aid_to_index[aid] for aid in pcc)
for pcc in self.aid_pccs]
self.nx_to_aidpcc = {nx: pcc for nx, pcc in enumerate( self.aid_pccs)}
self.nx_to_indexpcc = {nx: pcc for nx, pcc in enumerate(self.index_pccs)}
self.aid_to_nx = { aid: nx for nx, pcc in self.nx_to_aidpcc.items() for aid in pcc}
self.index_to_nx = {index: nx for nx, pcc in self.nx_to_indexpcc.items() for index in pcc}
self.aid_to_tx = {aid: tx for tx, aid in enumerate(sampler.regions.targets['aid'])}
window_dim = dim
self.dim = window_dim
self.window_dim = window_dim
self.dims = (window_dim, window_dim)
self.rng = nh.util.ensure_rng(0)
if augment:
import imgaug.augmenters as iaa
self.independent = iaa.Sequential([
iaa.Sometimes(0.2, nh.data.transforms.HSVShift(hue=0.1, sat=1.5, val=1.5)),
iaa.Crop(percent=(0, .2)),
])
self.dependent = iaa.Sequential([
iaa.Fliplr(p=.5)
])
# NOTE: we are only using `self.augmenter` to make a hyper hashid
# in __getitem__ we invoke transform explicitly for fine control
self.augmenter = iaa.Sequential([
self.independent,
self.dependent,
])
else:
self.augmenter = None
self.letterbox = nh.data.transforms.Resize(
target_size=self.dims,
fill_color=0,
mode='letterbox'
)