Python源码示例:imgaug.augmenters.Superpixels()
示例1
def main():
image = data.astronaut()[..., ::-1] # rgb2bgr
print(image.shape)
cv2.namedWindow("aug", cv2.WINDOW_NORMAL)
cv2.imshow("aug", image)
cv2.waitKey(TIME_PER_STEP)
for n_segments in cycle(reversed(np.arange(1, 200, SEGMENTS_PER_STEP))):
aug = iaa.Superpixels(p_replace=0.75, n_segments=n_segments)
time_start = time.time()
img_aug = aug.augment_image(image)
print("augmented %d in %.4fs" % (n_segments, time.time() - time_start))
img_aug = ia.draw_text(img_aug, x=5, y=5, text="%d" % (n_segments,))
cv2.imshow("aug", img_aug)
cv2.waitKey(TIME_PER_STEP)
示例2
def test_zero_sized_axes(self):
shapes = [
(0, 0),
(0, 1),
(1, 0),
(0, 1, 0),
(1, 0, 0),
(0, 1, 1),
(1, 0, 1)
]
for shape, use_np_replace in itertools.product(shapes, _NP_REPLACE):
with self.subTest(shape=shape, use_np_replace=use_np_replace):
with _create_replace_np_context(use_np_replace):
image = np.full(shape, 128, dtype=np.uint8)
aug = iaa.Superpixels(p_replace=1.0, n_segments=10)
image_aug = aug(image=image)
assert image_aug.dtype.name == "uint8"
assert image_aug.shape == shape
示例3
def test_unusual_channel_numbers(self):
shapes = [
(1, 1, 4),
(1, 1, 5),
(1, 1, 512),
(1, 1, 513)
]
for shape, use_np_replace in itertools.product(shapes, _NP_REPLACE):
with self.subTest(shape=shape, use_np_replace=use_np_replace):
with _create_replace_np_context(use_np_replace):
image = np.full(shape, 128, dtype=np.uint8)
aug = iaa.Superpixels(p_replace=1.0, n_segments=10)
image_aug = aug(image=image)
assert image_aug.dtype.name == "uint8"
assert image_aug.shape == shape
示例4
def main():
image = data.astronaut()[...,::-1] # rgb2bgr
print(image.shape)
cv2.namedWindow("aug", cv2.WINDOW_NORMAL)
cv2.imshow("aug", image)
cv2.waitKey(TIME_PER_STEP)
for n_segments in cycle(reversed(np.arange(1, 200, SEGMENTS_PER_STEP))):
aug = iaa.Superpixels(p_replace=0.75, n_segments=n_segments)
time_start = time.time()
img_aug = aug.augment_image(image)
print("augmented %d in %.4fs" % (n_segments, time.time() - time_start))
img_aug = ia.draw_text(img_aug, x=5, y=5, text="%d" % (n_segments,))
cv2.imshow("aug", img_aug)
cv2.waitKey(TIME_PER_STEP)
示例5
def test_p_replace_0_n_segments_2(self):
for use_np_replace in _NP_REPLACE:
with self.subTest(use_np_replace=use_np_replace):
with _create_replace_np_context(use_np_replace):
aug = iaa.Superpixels(p_replace=0, n_segments=2)
observed = aug.augment_image(self.base_img)
expected = self.base_img
assert np.allclose(observed, expected)
示例6
def test_p_replace_1_n_segments_2(self):
for use_np_replace in _NP_REPLACE:
with self.subTest(use_np_replace=use_np_replace):
with _create_replace_np_context(use_np_replace):
aug = iaa.Superpixels(p_replace=1.0, n_segments=2)
observed = aug.augment_image(self.base_img)
expected = self.base_img_superpixels
assert self._array_equals_tolerant(observed, expected, 2)
示例7
def test_p_replace_1_n_segments_stochastic_parameter(self):
for use_np_replace in _NP_REPLACE:
with self.subTest(use_np_replace=use_np_replace):
with _create_replace_np_context(use_np_replace):
aug = iaa.Superpixels(
p_replace=1.0, n_segments=iap.Deterministic(2)
)
observed = aug.augment_image(self.base_img)
expected = self.base_img_superpixels
assert self._array_equals_tolerant(observed, expected, 2)
示例8
def test_p_replace_stochastic_parameter_n_segments_2(self):
for use_np_replace in _NP_REPLACE:
with self.subTest(use_np_replace=use_np_replace):
with _create_replace_np_context(use_np_replace):
aug = iaa.Superpixels(
p_replace=iap.Binomial(iap.Choice([0.0, 1.0])),
n_segments=2
)
observed = aug.augment_image(self.base_img)
assert (
np.allclose(observed, self.base_img)
or self._array_equals_tolerant(
observed, self.base_img_superpixels, 2)
)
示例9
def test_failure_on_invalid_datatype_for_p_replace(self):
# note that assertRaisesRegex does not exist in 2.7
got_exception = False
try:
_ = iaa.Superpixels(p_replace="test", n_segments=100)
except Exception as exc:
assert "Expected " in str(exc)
got_exception = True
assert got_exception
示例10
def test_failure_on_invalid_datatype_for_n_segments(self):
# note that assertRaisesRegex does not exist in 2.7
got_exception = False
try:
_ = iaa.Superpixels(p_replace=1, n_segments="test")
except Exception as exc:
assert "Expected " in str(exc)
got_exception = True
assert got_exception
示例11
def test_get_parameters(self):
aug = iaa.Superpixels(
p_replace=0.5, n_segments=2, max_size=100, interpolation="nearest")
params = aug.get_parameters()
assert params[0] is aug.p_replace
assert is_parameter_instance(params[0].p, iap.Deterministic)
assert params[1] is aug.n_segments
assert 0.5 - 1e-4 < params[0].p.value < 0.5 + 1e-4
assert params[1].value == 2
assert params[2] == 100
assert params[3] == "nearest"
示例12
def test_other_dtypes_float(self):
# currently, no float dtype is actually accepted
for dtype in []:
def _allclose(a, b):
atol = 1e-4 if dtype == np.float16 else 1e-8
return np.allclose(a, b, atol=atol, rtol=0)
isize = np.dtype(dtype).itemsize
for value in [0, 1.0, 10.0, 1000 ** (isize - 1)]:
v1 = (-1) * value
v2 = value
aug = iaa.Superpixels(p_replace=1.0, n_segments=2)
img = np.array([
[v1, v1, v2, v2],
[v1, v1, v2, v2]
], dtype=dtype)
img_aug = aug.augment_image(img)
assert img_aug.dtype == np.dtype(dtype)
assert _allclose(img_aug, img)
aug = iaa.Superpixels(p_replace=1.0, n_segments=1)
img = np.array([
[v2, v2, v2, v2],
[v1, v2, v2, v2]
], dtype=dtype)
img_aug = aug.augment_image(img)
assert img_aug.dtype == np.dtype(dtype)
assert _allclose(img_aug, (7/8)*v2 + (1/8)*v1)
示例13
def test_pickleable(self):
aug = iaa.Superpixels(p_replace=0.5, seed=1)
runtest_pickleable_uint8_img(aug, iterations=10, shape=(25, 25, 1))
示例14
def __init__(self, p_replace=0.1, n_segments=100, prob=0.5):
super().__init__(prob)
self.processor = iaa.Superpixels(p_replace=p_replace, n_segments=n_segments)
示例15
def __init__(self, p_replace=0.1, n_segments=100, prob=0.5):
super().__init__(prob)
self.processor = iaa.Superpixels(p_replace=p_replace, n_segments=n_segments)
示例16
def __init__(self, p_replace=0.1, n_segments=100, prob=0.5):
super().__init__(prob)
self.processor = iaa.Superpixels(p_replace=p_replace, n_segments=n_segments)
示例17
def chapter_augmenters_superpixels():
aug = iaa.Superpixels(p_replace=0.5, n_segments=64)
run_and_save_augseq(
"superpixels_50_64.jpg", aug,
[ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2
)
aug = iaa.Superpixels(p_replace=(0.1, 1.0), n_segments=(16, 128))
run_and_save_augseq(
"superpixels.jpg", aug,
[ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2
)
#ps = [1/8*i for i in range(8)]
ps = np.linspace(0, 1.0, num=8)
run_and_save_augseq(
"superpixels_vary_p.jpg",
[iaa.Superpixels(p_replace=p, n_segments=64) for p in ps],
[ia.quokka(size=(64, 64)) for _ in range(8)], cols=8, rows=1,
quality=75
)
ns = [16*i for i in range(1, 9)]
run_and_save_augseq(
"superpixels_vary_n.jpg",
[iaa.Superpixels(p_replace=1.0, n_segments=n) for n in ns],
[ia.quokka(size=(64, 64)) for _ in range(8)], cols=8, rows=1,
quality=75
)
示例18
def processor(self):
return iaa.Superpixels(p_replace=self.p_replace, n_segments=self.n_segments)
示例19
def test_other_dtypes_uint_int(self):
dtypes = ["uint8", "uint16", "uint32",
"int8", "int16", "int32"]
for dtype in dtypes:
for use_np_replace in _NP_REPLACE:
with self.subTest(dtype=dtype, use_np_replace=use_np_replace):
with _create_replace_np_context(use_np_replace):
dtype = np.dtype(dtype)
min_value, center_value, max_value = \
iadt.get_value_range_of_dtype(dtype)
if np.dtype(dtype).kind == "i":
values = [
int(center_value), int(0.1 * max_value),
int(0.2 * max_value), int(0.5 * max_value),
max_value-100
]
values = [((-1)*value, value) for value in values]
else:
values = [(0, int(center_value)),
(10, int(0.1 * max_value)),
(10, int(0.2 * max_value)),
(10, int(0.5 * max_value)),
(0, max_value),
(int(center_value),
max_value)]
for v1, v2 in values:
aug = iaa.Superpixels(p_replace=1.0, n_segments=2)
img = np.array([
[v1, v1, v2, v2],
[v1, v1, v2, v2]
], dtype=dtype)
img_aug = aug.augment_image(img)
assert img_aug.dtype.name == dtype.name
assert np.array_equal(img_aug, img)
aug = iaa.Superpixels(p_replace=1.0, n_segments=1)
img = np.array([
[v2, v2, v2, v2],
[v1, v2, v2, v2]
], dtype=dtype)
img_aug = aug.augment_image(img)
assert img_aug.dtype.name == dtype.name
assert np.all(img_aug == int((7/8)*v2 + (1/8)*v1))
示例20
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