赞
踩
- from imgaug import augmenters as iaa
- import imgaug as ia
- import matplotlib.pyplot as plt
-
- augmentation = iaa.SomeOf((0, 3), [
- iaa.Fliplr(0.5),
- iaa.Flipud(0.5),
- iaa.OneOf([iaa.Affine(rotate=90),
- iaa.Affine(rotate=90),
- iaa.Affine(rotate=270),
- iaa.Affine(rotate=180),
- iaa.Affine(rotate=180),
- iaa.Affine(rotate=270)]),
- iaa.Multiply((0.8, 1.5)),
- iaa.GaussianBlur(sigma=(0.0, 3.0))
- ])
-
- temp_aug_bbox = []
- for bbox in bboxes_resize1:
- temp_aug_bbox.append(ia.BoundingBox(x1=bbox[1],
- x2=bbox[3],
- y1=bbox[0],
- y2=bbox[2]))
- bbs = ia.BoundingBoxesOnImage(temp_aug_bbox, shape=image.shape)
- # if debug:
- # pass
- # plt.figure()
- # plt.imshow(bbs.draw_on_image(image, thickness=2))
-
-
- seq_det = augmentation.to_deterministic()
-
- image = seq_det.augment_image(image)
- bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
- # plt.figure()
- # plt.imshow(bbs_aug.draw_on_image(image_aug, thickness=2))
-
- bboxes_resize1 = []
- for one in bbs_aug.bounding_boxes:
- bboxes_resize1.append([ one.y1, one.x1, one.y2, one.x2])
- if debug:
- pass
- debug_show(image, bboxes_resize1)
分割任务
- from imgaug import augmenters as iaa
- import imgaug as ia
- import matplotlib.pyplot as plt
-
- def img_aug2(image, mask_img):
- image = np.array(image)
- mask_img = np.array(mask_img)
- debug = False
- sometimes = lambda aug: iaa.Sometimes(0.8, aug)
- imput_rate = max(image.shape[0] * 1.0 / image.shape[1], image.shape[1] * 1.0 / image.shape[0])
- augmentation = iaa.SomeOf((3, 5), [
- iaa.Fliplr(0.5),
- # iaa.Flipud(0.1),
- # iaa.OneOf([iaa.Affine(rotate=90),
- # iaa.Affine(rotate=90),
- # iaa.Affine(rotate=270),
- # iaa.Affine(rotate=180),
- # iaa.Affine(rotate=180),
- # iaa.Affine(rotate=270)]),
-
- # # crop some of the images by 0-10% of their height/width
- # sometimes(iaa.Crop(percent=(0.0, 0.1))),
-
- # Apply affine transformations to some of the images
- # - scale to 80-120% of image height/width (each axis independently)
- # - translate by -20 to +20 relative to height/width (per axis)
- # - rotate by -45 to +45 degrees
- # - shear by -16 to +16 degrees
- # - order: use nearest neighbour or bilinear interpolation (fast)
- # - mode: use any available mode to fill newly created pixels
- # see API or scikit-image for which modes are available
- # - cval: if the mode is constant, then use a random brightness
- # for the newly created pixels (e.g. sometimes black,
- # sometimes white)
- sometimes(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=(-10 - int(max(0, -0.5*imput_rate + 1.5) * 20), 10 + int(max(0, -0.5*imput_rate + 1.5) * 20)),
- shear=(-16, 16),
- order=[0, 1],
- cval=(0, 255),
- # mode=ia.ALL,
- mode="constant",
- )),
-
- # # Add gaussian noise to some images.
- # # In 50% of these cases, the noise is randomly sampled per
- # # channel and pixel.
- # # In the other 50% of all cases it is sampled once per
- # # pixel (i.e. brightness change).
- # iaa.AdditiveGaussianNoise(
- # loc=0, scale=(0.0, 0.10 * 255), per_channel=0.5
- # ),
-
- # Either drop randomly 1 to 10% of all pixels (i.e. set
- # them to black) or drop them on an image with 2-5% percent
- # of the original size, leading to large dropped
- # rectangles.
- # iaa.OneOf([
- # # iaa.Dropout((0.01, 0.1), per_channel=0.5),
- # iaa.CoarseDropout(
- # (0.001, 0.003), size_percent=(0.001, 0.01),
- # per_channel=0.9
- # ),
- # ]),
-
- # # Convert some images into their superpixel representation,
- # # sample between 20 and 200 superpixels per image, but do
- # # not replace all superpixels with their average, only
- # # some of them (p_replace).
- # sometimes(
- # iaa.Superpixels(
- # p_replace=(0, 1.0),
- # n_segments=(20, 200)
- # )
- # ),
-
- # # Blur each image with varying strength using
- # # gaussian blur (sigma between 0 and 3.0),
- # # average/uniform blur (kernel size between 2x2 and 7x7)
- # # median blur (kernel size between 3x3 and 11x11).
- # iaa.OneOf([
- # iaa.GaussianBlur((0, 4.0)),
- # iaa.AverageBlur(k=(2, 7)),
- # iaa.MedianBlur(k=(3, 11)),
- # ]),
-
- # # Sharpen each image, overlay the result with the original
- # # image using an alpha between 0 (no sharpening) and 1
- # # (full sharpening effect).
- # iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
-
- # # Same as sharpen, but for an embossing effect.
- # iaa.Emboss(alpha=(0.0, 0.6), strength=(0, 2.0)),
-
- # # Improve or worsen the contrast of images.
- # iaa.LinearContrast((0.7, 1.3), per_channel=0.5),
-
- # # Change brightness of images (50-150% of original value).
- # iaa.Multiply((0.6, 1.3)),
-
- # # Invert each image's channel with 5% probability.
- # # This sets each pixel value v to 255-v.
- # iaa.Invert(0.05, per_channel=True), # invert color channels
-
- # # Add a value of -10 to 10 to each pixel.
- # iaa.Add((-0, 20), per_channel=0.5),
-
- # # Convert each image to grayscale and then overlay the
- # # result with the original with random alpha. I.e. remove
- # # colors with varying strengths.
- # iaa.Grayscale(alpha=(0.01, 0.4)),
-
- # iaa.WithColorspace('HSV',
- # from_colorspace='RGB',
- # children=iaa.WithChannels(1, iaa.Add((10, 200))),
- # name=None,
- # deterministic=False,
- # random_state=None),
-
- # 亮度
- # iaa.AddToBrightness((-100, 100)),
-
- ])
-
- # temp_aug_bbox = []
- # temp_aug_points = []
- # bboxes_resize1 = [[100, 100, 200, 200]]
- # for bbox in bboxes_resize1:
- # temp_aug_bbox.append(ia.BoundingBox(x1=bbox[1],
- # x2=bbox[3],
- # y1=bbox[0],
- # y2=bbox[2]))
- # temp_points = [[100, 100], [100, 200], [200, 200], [200, 100]]
- # temp_aug_points.append(ia.Polygon(temp_points))
- # bbs = ia.BoundingBoxesOnImage(temp_aug_bbox, shape=image.shape)
-
- # if debug:
- # pass
- # plt.figure()
- # plt.imshow(bbs.draw_on_image(image, thickness=2))
-
- # seq_det = augmentation.to_deterministic()
- # aug_image = seq_det.augment_image(image)
-
- image = np.expand_dims(image, axis=0)
- mask_img = np.expand_dims(mask_img, axis=0)
- aug_image, aug_mask_img = augmentation(images=image, segmentation_maps=mask_img)
-
- # bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
- # # bbs_aug = seq_det.augment_polygons([bbs])[0]
- # aug_mask_img = seq_det.augment_image(mask_img)
- # plt.figure()
- # plt.imshow(bbs_aug.draw_on_image(image_aug, thickness=2))
-
- # bboxes_resize1 = []
- # for one in bbs_aug.bounding_boxes:
- # bboxes_resize1.append([one.y1, one.x1, one.y2, one.x2])
- if debug:
- pass
- # debug_show(image, bboxes_resize1)
- aug_image = np.squeeze(aug_image)
- aug_mask_img = np.squeeze(aug_mask_img)
- return aug_image, aug_mask_img
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。