Python源码示例:imgaug.augmenters.Flipud()
示例1
def __init__(self, dataset_path,scale,k_fold_test=1, mode='train'):
super().__init__()
self.mode = mode
self.img_path=dataset_path+'/img'
self.mask_path=dataset_path+'/mask'
self.image_lists,self.label_lists=self.read_list(self.img_path,k_fold_test=k_fold_test)
self.flip =iaa.SomeOf((2,4),[
iaa.Fliplr(0.5),
iaa.Flipud(0.5),
iaa.Affine(rotate=(-30, 30)),
iaa.AdditiveGaussianNoise(scale=(0.0,0.08*255))], random_order=True)
# resize
self.resize_label = transforms.Resize(scale, Image.NEAREST)
self.resize_img = transforms.Resize(scale, Image.BILINEAR)
# normalization
self.to_tensor = transforms.ToTensor()
示例2
def img_aug(img, mask):
mask = np.where(mask > 0, 0, 255).astype(np.uint8)
flipper = iaa.Fliplr(0.5).to_deterministic()
mask = flipper.augment_image(mask)
img = flipper.augment_image(img)
vflipper = iaa.Flipud(0.5).to_deterministic()
img = vflipper.augment_image(img)
mask = vflipper.augment_image(mask)
if random.random() < 0.5:
rot_time = random.choice([1, 2, 3])
for i in range(rot_time):
img = np.rot90(img)
mask = np.rot90(mask)
if random.random() < 0.5:
translater = iaa.Affine(translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
shear=(-8, 8),
cval=(255)
).to_deterministic()
img = translater.augment_image(img)
mask = translater.augment_image(mask)
# if random.random() < 0.5:
# img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
mask = np.where(mask > 0, 0, 255).astype(np.uint8)
return img, mask
示例3
def example_single_augmenters():
print("Example: Single Augmenters")
from imgaug import augmenters as iaa
images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)
flipper = iaa.Fliplr(1.0) # always horizontally flip each input image
images[0] = flipper.augment_image(images[0]) # horizontally flip image 0
vflipper = iaa.Flipud(0.9) # vertically flip each input image with 90% probability
images[1] = vflipper.augment_image(images[1]) # probably vertically flip image 1
blurer = iaa.GaussianBlur(3.0)
images[2] = blurer.augment_image(images[2]) # blur image 2 by a sigma of 3.0
images[3] = blurer.augment_image(images[3]) # blur image 3 by a sigma of 3.0 too
translater = iaa.Affine(translate_px={"x": -16}) # move each input image by 16px to the left
images[4] = translater.augment_image(images[4]) # move image 4 to the left
scaler = iaa.Affine(scale={"y": (0.8, 1.2)}) # scale each input image to 80-120% on the y axis
images[5] = scaler.augment_image(images[5]) # scale image 5 by 80-120% on the y axis
示例4
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)))])
])
示例5
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)
示例6
def test_returns_flipud(self):
aug = iaa.VerticalFlip(0.5)
assert isinstance(aug, iaa.Flipud)
assert np.allclose(aug.p.p.value, 0.5)
示例7
def create_aug(self, *args, **kwargs):
return iaa.Flipud(*args, **kwargs)
示例8
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
示例9
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
示例10
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
示例11
def chapter_augmenters_flipud():
aug = iaa.Flipud(0.5)
run_and_save_augseq(
"flipud.jpg", aug,
[ia.quokka(size=(64, 64)) for _ in range(16)], cols=8, rows=2
)
示例12
def test_find():
reseed()
noop1 = iaa.Noop(name="Noop")
fliplr = iaa.Fliplr(name="Fliplr")
flipud = iaa.Flipud(name="Flipud")
noop2 = iaa.Noop(name="Noop2")
seq2 = iaa.Sequential([flipud, noop2], name="Seq2")
seq1 = iaa.Sequential([noop1, fliplr, seq2], name="Seq")
augs = seq1.find_augmenters_by_name("Seq")
assert len(augs) == 1
assert augs[0] == seq1
augs = seq1.find_augmenters_by_name("Seq2")
assert len(augs) == 1
assert augs[0] == seq2
augs = seq1.find_augmenters_by_names(["Seq", "Seq2"])
assert len(augs) == 2
assert augs[0] == seq1
assert augs[1] == seq2
augs = seq1.find_augmenters_by_name(r"Seq.*", regex=True)
assert len(augs) == 2
assert augs[0] == seq1
assert augs[1] == seq2
augs = seq1.find_augmenters(lambda aug, parents: aug.name in ["Seq", "Seq2"])
assert len(augs) == 2
assert augs[0] == seq1
assert augs[1] == seq2
augs = seq1.find_augmenters(lambda aug, parents: aug.name in ["Seq", "Seq2"] and len(parents) > 0)
assert len(augs) == 1
assert augs[0] == seq2
augs = seq1.find_augmenters(lambda aug, parents: aug.name in ["Seq", "Seq2"], flat=False)
assert len(augs) == 2
assert augs[0] == seq1
assert augs[1] == [seq2]
示例13
def test_remove():
reseed()
def get_seq():
noop1 = iaa.Noop(name="Noop")
fliplr = iaa.Fliplr(name="Fliplr")
flipud = iaa.Flipud(name="Flipud")
noop2 = iaa.Noop(name="Noop2")
seq2 = iaa.Sequential([flipud, noop2], name="Seq2")
seq1 = iaa.Sequential([noop1, fliplr, seq2], name="Seq")
return seq1
augs = get_seq()
augs = augs.remove_augmenters(lambda aug, parents: aug.name == "Seq2")
seqs = augs.find_augmenters_by_name(r"Seq.*", regex=True)
assert len(seqs) == 1
assert seqs[0].name == "Seq"
augs = get_seq()
augs = augs.remove_augmenters(lambda aug, parents: aug.name == "Seq2" and len(parents) == 0)
seqs = augs.find_augmenters_by_name(r"Seq.*", regex=True)
assert len(seqs) == 2
assert seqs[0].name == "Seq"
assert seqs[1].name == "Seq2"
augs = get_seq()
augs = augs.remove_augmenters(lambda aug, parents: True)
assert augs is not None
assert isinstance(augs, iaa.Noop)
augs = get_seq()
augs = augs.remove_augmenters(lambda aug, parents: True, noop_if_topmost=False)
assert augs is None
示例14
def __init__(self):
self.imgaug_transform = iaa.Flipud(p=1)
self.augmentor_op = Operations.Flip(probability=1, top_bottom_left_right="TOP_BOTTOM")
self.solt_stream = slc.Stream([slt.RandomFlip(p=1, axis=0)])
示例15
def processor(self):
return iaa.Flipud(1)
示例16
def train(model, dataset_dir, subset):
"""Train the model."""
# Training dataset.
dataset_train = NucleusDataset()
dataset_train.load_nucleus(dataset_dir, subset)
dataset_train.prepare()
# Validation dataset
dataset_val = NucleusDataset()
dataset_val.load_nucleus(dataset_dir, "val")
dataset_val.prepare()
# Image augmentation
# http://imgaug.readthedocs.io/en/latest/source/augmenters.html
augmentation = iaa.SomeOf((0, 2), [
iaa.Fliplr(0.5),
iaa.Flipud(0.5),
iaa.OneOf([iaa.Affine(rotate=90),
iaa.Affine(rotate=180),
iaa.Affine(rotate=270)]),
iaa.Multiply((0.8, 1.5)),
iaa.GaussianBlur(sigma=(0.0, 5.0))
])
# *** This training schedule is an example. Update to your needs ***
# If starting from imagenet, train heads only for a bit
# since they have random weights
print("Train network heads")
model.train(dataset_train, dataset_val,
learning_rate=config.LEARNING_RATE,
epochs=20,
augmentation=augmentation,
layers='heads')
print("Train all layers")
model.train(dataset_train, dataset_val,
learning_rate=config.LEARNING_RATE,
epochs=40,
augmentation=augmentation,
layers='all')
############################################################
# RLE Encoding
############################################################
示例17
def __getitem__(self, item):
if not self.aug:
uuid = self.list[item]
else:
uuid = self.list[item // test_aug_sz]
colors = ['red', 'green', 'blue', 'yellow']
flags = cv2.IMREAD_GRAYSCALE
img = [cv2.imread(os.path.join(self.default_path, uuid + '_' + color + self.ext), flags) for color in colors]
if self.resize:
img = [cv2.resize(x, (1024, 1024)) for x in img]
img = np.stack(img, axis=-1)
# TODO : data augmentation zoom/shear/brightness
if 'train' in self.setname:
augment_img = iaa.Sequential([
iaa.OneOf([
iaa.Affine(rotate=0),
iaa.Affine(rotate=90),
iaa.Affine(rotate=180),
iaa.Affine(rotate=270),
iaa.Fliplr(0.5),
iaa.Flipud(0.5),
])
], random_order=True)
img = augment_img.augment_image(img)
# cutout
if C.get()['cutout_p'] > 0.0:
img = cutout(C.get()['cutout_size'], C.get()['cutout_p'], False)(img)
# TODO : channel drop(except green)?
# d_ch = random.choice([0, 2, 3])
# img[:, :, d_ch] = 0
if self.aug:
# teat-time aug. : tta
tta_list = list(itertools.product(
[iaa.Affine(rotate=0), iaa.Affine(rotate=90), iaa.Affine(rotate=180), iaa.Affine(rotate=270)],
[iaa.Fliplr(0.0), iaa.Fliplr(1.0), iaa.Flipud(1.0), iaa.Sequential([iaa.Fliplr(1.0), iaa.Flipud(1.0)])]
))
tta_idx = item % len(tta_list)
img = tta_list[tta_idx][0].augment_image(img)
img = tta_list[tta_idx][1].augment_image(img)
img = img.astype(np.float32)
img /= 255. # TODO : different normalization?
img = np.transpose(img, (2, 0, 1))
img = np.ascontiguousarray(img)
if self.setname == 'tests':
lb = np.zeros(len(name_label_dict), dtype=np.int)
else:
lb = [int(x) for x in self.labels.loc[uuid]['Target'].split()]
lb = np.eye(len(name_label_dict), dtype=np.float)[lb].sum(axis=0)
return img, lb
示例18
def load_annoataion(p, im=None):
'''
load annotation from the text file
:param p:
:return:
'''
text_polys = []
text_tags = []
if not os.path.exists(p):
return np.array(text_polys, dtype=np.float32)
with open(p, 'r') as f:
# reader = csv.reader(f)
reader = f.readlines()
for line in reader:
line = line.split(',')
label = line[-1]
# strip BOM. \ufeff for python3, \xef\xbb\bf for python2
line = [i.strip('\ufeff').strip('\xef\xbb\xbf') for i in line]
# print(line)
x1, y1, x2, y2, x3, y3, x4, y4 = list(map(float, line[:8]))
text_polys.append([[x1, y1], [x2, y2], [x3, y3], [x4, y4]])
if label == '*' or label == '###':
text_tags.append(True)
else:
text_tags.append(False)
# 执行数据增广操作
random_value = np.random.random()
if random_value < 0.2:
# 执行旋转操作
angle = np.random.random() * 10
operation_obj = iaa.Affine(rotate=(-angle, angle), random_state=np.random.randint(0, 10000))
im, text_polys = data_agumentation(im, text_polys, operation_obj)
random_value = np.random.random()
if random_value < 0.1:
# 水平镜像
operation_obj = iaa.Sequential([iaa.Flipud(0.5, random_state=np.random.randint(0, 10000))])
im, text_polys = data_agumentation(im, text_polys, operation_obj)
random_value = np.random.random()
if random_value < 0.1:
# 垂直镜像
operation_obj = iaa.Sequential([iaa.Fliplr(0.5, random_state=np.random.randint(0, 10000))])
# operation_obj = iaa.Affine(shear=(-10, 10))
im, text_polys = data_agumentation(im, text_polys, operation_obj)
random_value = np.random.random()
if random_value < 0.1:
# 随机Dropout
operation_obj = iaa.Sequential([iaa.Dropout(p=(0, 0.1), random_state=np.random.randint(0, 10000))])
im, text_polys = data_agumentation(im, text_polys, operation_obj)
random_value = np.random.random()
if random_value < 0.1:
# 随机增加噪声
operation_obj = iaa.Sequential([iaa.AdditiveGaussianNoise(scale=np.random.random() * 30,
random_state=np.random.randint(0,
10000))])
im, text_polys = data_agumentation(im, text_polys, operation_obj)
return np.array(text_polys, dtype=np.float32), np.array(text_tags, dtype=np.bool), im
示例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 train(init_with="coco", weights=None, fine=0, last_epoch=0, epochs=1,gpus=1):
class TrainConfig(CrowdAiConfig):
GPU_COUNT = gpus
config=TrainConfig()
config.DILATION=[5]
print (config.display())
model = modellib.MaskRCNN(mode="training", config=config,
model_dir=MODEL_DIR)
if init_with == "coco":
# Load weights trained on MS COCO, but skip layers that
# are different due to the different number of classes
# See README for instructions to download the COCO weights
print ('Loading coco model')
model.load_weights(weights, by_name=True,
exclude=["mrcnn_class_logits", "mrcnn_bbox_fc",
"mrcnn_bbox", "mrcnn_mask"])
elif init_with == "last":
if weights is None:
weight_path=model.find_last()[1]
else:
weight_path = weights
print ('loaded weight ..',weights)
# Load the last model you trained and continue training
model.load_weights(weight_path, by_name=True)
dataset_val = get_dataset(''.join([HOME_DIR,'/val']),fltarea=None)
dataset_train = get_dataset(''.join([HOME_DIR,'/train']),fltarea=None)
print('train samples::{}'.format(dataset_train.num_images), 'valid samples::{}'.format(dataset_val.num_images))
config.STEPS_PER_EPOCH=int(dataset_train.num_images/ (config.IMAGES_PER_GPU*config.GPU_COUNT))
config.VALIDATION_STEPS = int(dataset_val.num_images/ (config.IMAGES_PER_GPU*config.GPU_COUNT))
print ('STEPS_PER_EPOCH', config.STEPS_PER_EPOCH)
print ('VALIDATION_STEPS', config.VALIDATION_STEPS)
if DEBUG:
config.STEPS_PER_EPOCH = TRAIN_STEP
config.VALIDATION_STEPS = VAL_STEP
augmentation = iaa.Sequential([
iaa.Fliplr(0.5),
iaa.Flipud(0.3)
])
model.epoch = last_epoch
model.train(dataset_train, dataset_val,
learning_rate=config.LEARNING_RATE,
epochs=epochs,
layers='all',augmentation=augmentation)
if fine>0:
print ('current lr:', config.LEARNING_RATE)
print ('new lr :', config.LEARNING_RATE / 10)
model.train(dataset_train, dataset_val,
learning_rate=config.LEARNING_RATE/10 ,
epochs=model.epoch+fine,
layers="all",augmentation=augmentation)
del model,dataset_train,dataset_val
gc.collect()
示例21
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
示例22
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)