Python源码示例:imgaug.augmenters.AdditiveGaussianNoise()
示例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 example_augment_images_and_keypoints():
print("Example: Augment Images and Keypoints")
import numpy as np
import imgaug.augmenters as iaa
images = np.zeros((2, 128, 128, 3), dtype=np.uint8) # two example images
images[:, 64, 64, :] = 255
points = [
[(10.5, 20.5)], # points on first image
[(50.5, 50.5), (60.5, 60.5), (70.5, 70.5)] # points on second image
]
seq = iaa.Sequential([
iaa.AdditiveGaussianNoise(scale=0.05*255),
iaa.Affine(translate_px={"x": (1, 5)})
])
# augment keypoints and images
images_aug, points_aug = seq(images=images, keypoints=points)
print("Image 1 center", np.argmax(images_aug[0, 64, 64:64+6, 0]))
print("Image 2 center", np.argmax(images_aug[1, 64, 64:64+6, 0]))
print("Points 1", points_aug[0])
print("Points 2", points_aug[1])
示例3
def example_augment_images_and_bounding_boxes():
print("Example: Augment Images and Bounding Boxes")
import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa
images = np.zeros((2, 128, 128, 3), dtype=np.uint8) # two example images
images[:, 64, 64, :] = 255
bbs = [
[ia.BoundingBox(x1=10.5, y1=15.5, x2=30.5, y2=50.5)],
[ia.BoundingBox(x1=10.5, y1=20.5, x2=50.5, y2=50.5),
ia.BoundingBox(x1=40.5, y1=75.5, x2=70.5, y2=100.5)]
]
seq = iaa.Sequential([
iaa.AdditiveGaussianNoise(scale=0.05*255),
iaa.Affine(translate_px={"x": (1, 5)})
])
images_aug, bbs_aug = seq(images=images, bounding_boxes=bbs)
示例4
def example_augment_images_and_polygons():
print("Example: Augment Images and Polygons")
import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa
images = np.zeros((2, 128, 128, 3), dtype=np.uint8) # two example images
images[:, 64, 64, :] = 255
polygons = [
[ia.Polygon([(10.5, 10.5), (50.5, 10.5), (50.5, 50.5)])],
[ia.Polygon([(0.0, 64.5), (64.5, 0.0), (128.0, 128.0), (64.5, 128.0)])]
]
seq = iaa.Sequential([
iaa.AdditiveGaussianNoise(scale=0.05*255),
iaa.Affine(translate_px={"x": (1, 5)})
])
images_aug, polygons_aug = seq(images=images, polygons=polygons)
示例5
def example_augment_images_and_linestrings():
print("Example: Augment Images and LineStrings")
import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa
images = np.zeros((2, 128, 128, 3), dtype=np.uint8) # two example images
images[:, 64, 64, :] = 255
ls = [
[ia.LineString([(10.5, 10.5), (50.5, 10.5), (50.5, 50.5)])],
[ia.LineString([(0.0, 64.5), (64.5, 0.0), (128.0, 128.0), (64.5, 128.0),
(128.0, 0.0)])]
]
seq = iaa.Sequential([
iaa.AdditiveGaussianNoise(scale=0.05*255),
iaa.Affine(translate_px={"x": (1, 5)})
])
images_aug, ls_aug = seq(images=images, line_strings=ls)
示例6
def test_seed_affects_augmenters_created_before_its_call(self):
image = np.full((50, 50, 3), 128, dtype=np.uint8)
images_aug = []
for _ in np.arange(5):
aug = iaa.AdditiveGaussianNoise(scale=50, per_channel=True)
iarandom.seed(100)
images_aug.append(aug(image=image))
# assert all images identical
for other_image_aug in images_aug[1:]:
assert np.array_equal(images_aug[0], other_image_aug)
# but different seed must lead to different image
aug = iaa.AdditiveGaussianNoise(scale=50, per_channel=True)
iarandom.seed(101)
image_aug = aug(image=image)
assert not np.array_equal(images_aug[0], image_aug)
示例7
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)
示例8
def create_augmenter(stage: str = "train"):
if stage == "train":
return iaa.Sequential([
iaa.Fliplr(0.5),
iaa.CropAndPad(px=(0, 112), sample_independently=False),
iaa.Affine(translate_percent={"x": (-0.4, 0.4), "y": (-0.4, 0.4)}),
iaa.SomeOf((0, 3), [
iaa.AddToHueAndSaturation((-10, 10)),
iaa.Affine(scale={"x": (0.9, 1.1), "y": (0.9, 1.1)}),
iaa.GaussianBlur(sigma=(0, 1.0)),
iaa.AdditiveGaussianNoise(scale=0.05 * 255)
])
])
elif stage == "val":
return iaa.Sequential([
iaa.CropAndPad(px=(0, 112), sample_independently=False),
iaa.Affine(translate_percent={"x": (-0.4, 0.4), "y": (-0.4, 0.4)}),
])
elif stage == "test":
return iaa.Sequential([])
示例9
def amaugimg(image):
#数据增强
image = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)
seq = iaa.Sequential([
# iaa.Affine(rotate=(-5, 5),
# shear=(-5, 5),
# mode='edge'),
iaa.SomeOf((0, 2), #选择数据增强
[
iaa.GaussianBlur((0, 1.5)),
iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.01 * 255), per_channel=0.5),
# iaa.AddToHueAndSaturation((-5, 5)), # change hue and saturation
iaa.PiecewiseAffine(scale=(0.01, 0.03)),
iaa.PerspectiveTransform(scale=(0.01, 0.1))
],
random_order=True
)
])
image = seq.augment_image(image)
image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
return image
示例10
def augment(image, bbox):
x = random.randint(-50, 50)
y = random.randint(-50, 50)
aug = iaa.Sequential([iaa.Multiply(random.uniform(0.5, 1.5)),
iaa.AdditiveGaussianNoise(random.uniform(0.01, 0.1) * 255),
iaa.Affine(translate_px={"x": x, "y": y},
scale=random.uniform(0.5, 1.5),
rotate=random.uniform(-45, 45),
cval=(0, 255))])
bbs = ia.BoundingBoxesOnImage([ia.BoundingBox(x1=bbox[0], y1=bbox[1], x2=bbox[2], y2=bbox[3])], shape=image.shape)
aug = aug.to_deterministic()
image_aug = aug.augment_image(image)
bbs_aug = aug.augment_bounding_boxes([bbs])[0]
b = bbs_aug.bounding_boxes
bbs_aug = [b[0].x1, b[0].y1, b[0].x2, b[0].y2]
bbs_aug = np.asarray(bbs_aug)
bbs_aug[0] = bbs_aug[0] if bbs_aug[0] > 0 else 0
bbs_aug[1] = bbs_aug[1] if bbs_aug[1] > 0 else 0
bbs_aug[2] = bbs_aug[2] if bbs_aug[2] < size else size
bbs_aug[3] = bbs_aug[3] if bbs_aug[3] < size else size
return image_aug, bbs_aug
示例11
def augment(image, bbox):
x = random.randint(-60, 60)
y = random.randint(-60, 60)
aug = iaa.Sequential([iaa.AdditiveGaussianNoise(scale=random.uniform(.001, .01) * 255), # gaussian noise
iaa.Multiply(random.uniform(0.5, 1.5)), # brightness
iaa.Affine(translate_px={"x": x, "y": y}, # translation
scale=random.uniform(0.5, 1.5), # zoom in and out
rotate=random.uniform(-25, 25), # rotation
shear=random.uniform(-5, 5), # shear transformation
cval=(0, 255))]) # fill the empty space with color
aug.add(iaa.Salt(.001))
bbs = ia.BoundingBoxesOnImage([ia.BoundingBox(x1=bbox[0], y1=bbox[1], x2=bbox[2], y2=bbox[3])], shape=image.shape)
aug = aug.to_deterministic()
image_aug = aug.augment_image(image)
bbs_aug = aug.augment_bounding_boxes([bbs])[0]
b = bbs_aug.bounding_boxes
bbs_aug = [b[0].x1, b[0].y1, b[0].x2, b[0].y2]
bbs_aug = np.asarray(bbs_aug)
bbs_aug[0] = bbs_aug[0] if bbs_aug[0] > 0 else 0
bbs_aug[1] = bbs_aug[1] if bbs_aug[1] > 0 else 0
bbs_aug[2] = bbs_aug[2] if bbs_aug[2] < size else size
bbs_aug[3] = bbs_aug[3] if bbs_aug[3] < size else size
return image_aug, bbs_aug
示例12
def noise(image, prob, keys):
""" Adding noise """
aug = iaa.Sequential([iaa.Multiply(random.uniform(0.25, 1.5)),
iaa.AdditiveGaussianNoise(scale=0.05 * 255)])
seq_det = aug.to_deterministic()
image_aug = seq_det.augment_images([image])[0]
keys = ia.KeypointsOnImage([ia.Keypoint(x=keys[0], y=keys[1]),
ia.Keypoint(x=keys[2], y=keys[3]),
ia.Keypoint(x=keys[4], y=keys[5]),
ia.Keypoint(x=keys[6], y=keys[7]),
ia.Keypoint(x=keys[8], y=keys[9])], shape=image.shape)
keys_aug = seq_det.augment_keypoints([keys])[0]
k = keys_aug.keypoints
output = [k[0].x, k[0].y, k[1].x, k[1].y, k[2].x, k[2].y, k[3].x, k[3].y, k[4].x, k[4].y]
index = 0
for i in range(0, len(prob)):
output[index] = output[index] * prob[i]
output[index + 1] = output[index + 1] * prob[i]
index = index + 2
output = np.array(output)
return image_aug, output
示例13
def chapter_augmenters_sequential():
aug = iaa.Sequential([
iaa.Affine(translate_px={"x":-40}),
iaa.AdditiveGaussianNoise(scale=0.2*255)
])
run_and_save_augseq(
"sequential.jpg", aug,
[ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2
)
aug = iaa.Sequential([
iaa.Affine(translate_px={"x":-40}),
iaa.AdditiveGaussianNoise(scale=0.2*255)
], random_order=True)
run_and_save_augseq(
"sequential_random_order.jpg", aug,
[ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2
)
示例14
def chapter_augmenters_additivegaussiannoise():
aug = iaa.AdditiveGaussianNoise(scale=(0, 0.2*255))
run_and_save_augseq(
"additivegaussiannoise.jpg", aug,
[ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2,
quality=90
)
aug = iaa.AdditiveGaussianNoise(scale=0.2*255)
run_and_save_augseq(
"additivegaussiannoise_large.jpg", aug,
[ia.quokka(size=(512, 512)) for _ in range(1)], cols=1, rows=1,
quality=90
)
aug = iaa.AdditiveGaussianNoise(scale=0.2*255, per_channel=True)
run_and_save_augseq(
"additivegaussiannoise_per_channel.jpg", aug,
[ia.quokka(size=(512, 512)) for _ in range(1)], cols=1, rows=1,
quality=90
)
示例15
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)
示例16
def __init__(self, loc=0, scale=(0.01*255, 0.05*255), prob=0.5):
super().__init__(prob)
self.processor = iaa.AdditiveGaussianNoise(loc, scale)
示例17
def __init__(self, loc=0, scale=(0.01*255, 0.05*255), prob=0.5):
super().__init__(prob)
self.processor = iaa.AdditiveGaussianNoise(loc, scale)
示例18
def __init__(self, loc=0, scale=(0.01 * 255, 0.05 * 255), prob=0.5):
super().__init__(prob)
self.processor = iaa.AdditiveGaussianNoise(loc, scale)
示例19
def salt(image, prob, keys):
""" Adding salt noise """
r = random.uniform(1, 5) * 0.05
aug = iaa.Sequential([iaa.Dropout(p=(0, r)), iaa.CoarseDropout(p=0.001, size_percent=0.01),
iaa.Salt(0.001), iaa.AdditiveGaussianNoise(scale=0.1 * 255)])
aug.add(iaa.Multiply(random.uniform(0.25, 1.5)))
x = random.randrange(-10, 10) * .01
y = random.randrange(-10, 10) * .01
aug.add(iaa.Affine(scale=random.uniform(.7, 1.1), translate_percent={"x": x, "y": y}, cval=(0, 255)))
seq_det = aug.to_deterministic()
image_aug = seq_det.augment_images([image])[0]
keys = ia.KeypointsOnImage([ia.Keypoint(x=keys[0], y=keys[1]),
ia.Keypoint(x=keys[2], y=keys[3]),
ia.Keypoint(x=keys[4], y=keys[5]),
ia.Keypoint(x=keys[6], y=keys[7]),
ia.Keypoint(x=keys[8], y=keys[9])], shape=image.shape)
keys_aug = seq_det.augment_keypoints([keys])[0]
k = keys_aug.keypoints
output = [k[0].x, k[0].y, k[1].x, k[1].y, k[2].x, k[2].y, k[3].x, k[3].y, k[4].x, k[4].y]
index = 0
for i in range(0, len(prob)):
output[index] = output[index] * prob[i]
output[index + 1] = output[index + 1] * prob[i]
index = index + 2
output = np.array(output)
return image_aug, output
示例20
def chapter_augmenters_someof():
aug = iaa.SomeOf(2, [
iaa.Affine(rotate=45),
iaa.AdditiveGaussianNoise(scale=0.2*255),
iaa.Add(50, per_channel=True),
iaa.Sharpen(alpha=0.5)
])
run_and_save_augseq(
"someof.jpg", aug,
[ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2
)
aug = iaa.SomeOf((0, None), [
iaa.Affine(rotate=45),
iaa.AdditiveGaussianNoise(scale=0.2*255),
iaa.Add(50, per_channel=True),
iaa.Sharpen(alpha=0.5)
])
run_and_save_augseq(
"someof_0_to_none.jpg", aug,
[ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2
)
aug = iaa.SomeOf(2, [
iaa.Affine(rotate=45),
iaa.AdditiveGaussianNoise(scale=0.2*255),
iaa.Add(50, per_channel=True),
iaa.Sharpen(alpha=0.5)
], random_order=True)
run_and_save_augseq(
"someof_random_order.jpg", aug,
[ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2
)
示例21
def chapter_augmenters_oneof():
aug = iaa.OneOf([
iaa.Affine(rotate=45),
iaa.AdditiveGaussianNoise(scale=0.2*255),
iaa.Add(50, per_channel=True),
iaa.Sharpen(alpha=0.5)
])
run_and_save_augseq(
"oneof.jpg", aug,
[ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2
)
示例22
def example_hooks():
print("Example: Hooks")
import imgaug as ia
from imgaug import augmenters as iaa
import numpy as np
# images and heatmaps, just arrays filled with value 30
images = np.ones((16, 128, 128, 3), dtype=np.uint8) * 30
heatmaps = np.ones((16, 128, 128, 21), dtype=np.uint8) * 30
# add vertical lines to see the effect of flip
images[:, 16:128-16, 120:124, :] = 120
heatmaps[:, 16:128-16, 120:124, :] = 120
seq = iaa.Sequential([
iaa.Fliplr(0.5, name="Flipper"),
iaa.GaussianBlur((0, 3.0), name="GaussianBlur"),
iaa.Dropout(0.02, name="Dropout"),
iaa.AdditiveGaussianNoise(scale=0.01*255, name="MyLittleNoise"),
iaa.AdditiveGaussianNoise(loc=32, scale=0.0001*255, name="SomeOtherNoise"),
iaa.Affine(translate_px={"x": (-40, 40)}, name="Affine")
])
# change the activated augmenters for heatmaps
def activator_heatmaps(images, augmenter, parents, default):
if augmenter.name in ["GaussianBlur", "Dropout", "MyLittleNoise"]:
return False
else:
# default value for all other augmenters
return default
hooks_heatmaps = ia.HooksImages(activator=activator_heatmaps)
seq_det = seq.to_deterministic() # call this for each batch again, NOT only once at the start
images_aug = seq_det.augment_images(images)
heatmaps_aug = seq_det.augment_images(heatmaps, hooks=hooks_heatmaps)
# -----------
ia.show_grid(images_aug)
ia.show_grid(heatmaps_aug[..., 0:3])
示例23
def processor(self):
return iaa.AdditiveGaussianNoise(self.loc, self.scale, self.per_channel)
示例24
def additive_Gaussian_noise(images, scale):
transformer = iaa.AdditiveGaussianNoise(scale=(0,scale*255), deterministic=True)
return augment_on_df(images, transformer)
示例25
def example_hooks():
print("Example: Hooks")
import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa
# Images and heatmaps, just arrays filled with value 30.
# We define the heatmaps here as uint8 arrays as we are going to feed them
# through the pipeline similar to normal images. In that way, every
# augmenter is applied to them.
images = np.full((16, 128, 128, 3), 30, dtype=np.uint8)
heatmaps = np.full((16, 128, 128, 21), 30, dtype=np.uint8)
# add vertical lines to see the effect of flip
images[:, 16:128-16, 120:124, :] = 120
heatmaps[:, 16:128-16, 120:124, :] = 120
seq = iaa.Sequential([
iaa.Fliplr(0.5, name="Flipper"),
iaa.GaussianBlur((0, 3.0), name="GaussianBlur"),
iaa.Dropout(0.02, name="Dropout"),
iaa.AdditiveGaussianNoise(scale=0.01*255, name="MyLittleNoise"),
iaa.AdditiveGaussianNoise(loc=32, scale=0.0001*255, name="SomeOtherNoise"),
iaa.Affine(translate_px={"x": (-40, 40)}, name="Affine")
])
# change the activated augmenters for heatmaps,
# we only want to execute horizontal flip, affine transformation and one of
# the gaussian noises
def activator_heatmaps(images, augmenter, parents, default):
if augmenter.name in ["GaussianBlur", "Dropout", "MyLittleNoise"]:
return False
else:
# default value for all other augmenters
return default
hooks_heatmaps = ia.HooksImages(activator=activator_heatmaps)
# call to_deterministic() once per batch, NOT only once at the start
seq_det = seq.to_deterministic()
images_aug = seq_det(images=images)
heatmaps_aug = seq_det(images=heatmaps, hooks=hooks_heatmaps)
# -----------
ia.show_grid(images_aug)
ia.show_grid(heatmaps_aug[..., 0:3])
示例26
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
示例27
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
示例28
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
示例29
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
示例30
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