当前位置:   article > 正文

图像增强库imgaug的目标检测数据使用笔记_imgaug.augmenters to_deterministic()

imgaug.augmenters to_deterministic()
  1. from imgaug import augmenters as iaa
  2. import imgaug as ia
  3. import matplotlib.pyplot as plt
  4. augmentation = iaa.SomeOf((0, 3), [
  5. iaa.Fliplr(0.5),
  6. iaa.Flipud(0.5),
  7. iaa.OneOf([iaa.Affine(rotate=90),
  8. iaa.Affine(rotate=90),
  9. iaa.Affine(rotate=270),
  10. iaa.Affine(rotate=180),
  11. iaa.Affine(rotate=180),
  12. iaa.Affine(rotate=270)]),
  13. iaa.Multiply((0.8, 1.5)),
  14. iaa.GaussianBlur(sigma=(0.0, 3.0))
  15. ])
  16. temp_aug_bbox = []
  17. for bbox in bboxes_resize1:
  18. temp_aug_bbox.append(ia.BoundingBox(x1=bbox[1],
  19. x2=bbox[3],
  20. y1=bbox[0],
  21. y2=bbox[2]))
  22. bbs = ia.BoundingBoxesOnImage(temp_aug_bbox, shape=image.shape)
  23. # if debug:
  24. # pass
  25. # plt.figure()
  26. # plt.imshow(bbs.draw_on_image(image, thickness=2))
  27. seq_det = augmentation.to_deterministic()
  28. image = seq_det.augment_image(image)
  29. bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
  30. # plt.figure()
  31. # plt.imshow(bbs_aug.draw_on_image(image_aug, thickness=2))
  32. bboxes_resize1 = []
  33. for one in bbs_aug.bounding_boxes:
  34. bboxes_resize1.append([ one.y1, one.x1, one.y2, one.x2])
  35. if debug:
  36. pass
  37. debug_show(image, bboxes_resize1)

 分割任务

  1. from imgaug import augmenters as iaa
  2. import imgaug as ia
  3. import matplotlib.pyplot as plt
  4. def img_aug2(image, mask_img):
  5. image = np.array(image)
  6. mask_img = np.array(mask_img)
  7. debug = False
  8. sometimes = lambda aug: iaa.Sometimes(0.8, aug)
  9. imput_rate = max(image.shape[0] * 1.0 / image.shape[1], image.shape[1] * 1.0 / image.shape[0])
  10. augmentation = iaa.SomeOf((3, 5), [
  11. iaa.Fliplr(0.5),
  12. # iaa.Flipud(0.1),
  13. # iaa.OneOf([iaa.Affine(rotate=90),
  14. # iaa.Affine(rotate=90),
  15. # iaa.Affine(rotate=270),
  16. # iaa.Affine(rotate=180),
  17. # iaa.Affine(rotate=180),
  18. # iaa.Affine(rotate=270)]),
  19. # # crop some of the images by 0-10% of their height/width
  20. # sometimes(iaa.Crop(percent=(0.0, 0.1))),
  21. # Apply affine transformations to some of the images
  22. # - scale to 80-120% of image height/width (each axis independently)
  23. # - translate by -20 to +20 relative to height/width (per axis)
  24. # - rotate by -45 to +45 degrees
  25. # - shear by -16 to +16 degrees
  26. # - order: use nearest neighbour or bilinear interpolation (fast)
  27. # - mode: use any available mode to fill newly created pixels
  28. # see API or scikit-image for which modes are available
  29. # - cval: if the mode is constant, then use a random brightness
  30. # for the newly created pixels (e.g. sometimes black,
  31. # sometimes white)
  32. sometimes(iaa.Affine(
  33. scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
  34. translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
  35. rotate=(-10 - int(max(0, -0.5*imput_rate + 1.5) * 20), 10 + int(max(0, -0.5*imput_rate + 1.5) * 20)),
  36. shear=(-16, 16),
  37. order=[0, 1],
  38. cval=(0, 255),
  39. # mode=ia.ALL,
  40. mode="constant",
  41. )),
  42. # # Add gaussian noise to some images.
  43. # # In 50% of these cases, the noise is randomly sampled per
  44. # # channel and pixel.
  45. # # In the other 50% of all cases it is sampled once per
  46. # # pixel (i.e. brightness change).
  47. # iaa.AdditiveGaussianNoise(
  48. # loc=0, scale=(0.0, 0.10 * 255), per_channel=0.5
  49. # ),
  50. # Either drop randomly 1 to 10% of all pixels (i.e. set
  51. # them to black) or drop them on an image with 2-5% percent
  52. # of the original size, leading to large dropped
  53. # rectangles.
  54. # iaa.OneOf([
  55. # # iaa.Dropout((0.01, 0.1), per_channel=0.5),
  56. # iaa.CoarseDropout(
  57. # (0.001, 0.003), size_percent=(0.001, 0.01),
  58. # per_channel=0.9
  59. # ),
  60. # ]),
  61. # # Convert some images into their superpixel representation,
  62. # # sample between 20 and 200 superpixels per image, but do
  63. # # not replace all superpixels with their average, only
  64. # # some of them (p_replace).
  65. # sometimes(
  66. # iaa.Superpixels(
  67. # p_replace=(0, 1.0),
  68. # n_segments=(20, 200)
  69. # )
  70. # ),
  71. # # Blur each image with varying strength using
  72. # # gaussian blur (sigma between 0 and 3.0),
  73. # # average/uniform blur (kernel size between 2x2 and 7x7)
  74. # # median blur (kernel size between 3x3 and 11x11).
  75. # iaa.OneOf([
  76. # iaa.GaussianBlur((0, 4.0)),
  77. # iaa.AverageBlur(k=(2, 7)),
  78. # iaa.MedianBlur(k=(3, 11)),
  79. # ]),
  80. # # Sharpen each image, overlay the result with the original
  81. # # image using an alpha between 0 (no sharpening) and 1
  82. # # (full sharpening effect).
  83. # iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
  84. # # Same as sharpen, but for an embossing effect.
  85. # iaa.Emboss(alpha=(0.0, 0.6), strength=(0, 2.0)),
  86. # # Improve or worsen the contrast of images.
  87. # iaa.LinearContrast((0.7, 1.3), per_channel=0.5),
  88. # # Change brightness of images (50-150% of original value).
  89. # iaa.Multiply((0.6, 1.3)),
  90. # # Invert each image's channel with 5% probability.
  91. # # This sets each pixel value v to 255-v.
  92. # iaa.Invert(0.05, per_channel=True), # invert color channels
  93. # # Add a value of -10 to 10 to each pixel.
  94. # iaa.Add((-0, 20), per_channel=0.5),
  95. # # Convert each image to grayscale and then overlay the
  96. # # result with the original with random alpha. I.e. remove
  97. # # colors with varying strengths.
  98. # iaa.Grayscale(alpha=(0.01, 0.4)),
  99. # iaa.WithColorspace('HSV',
  100. # from_colorspace='RGB',
  101. # children=iaa.WithChannels(1, iaa.Add((10, 200))),
  102. # name=None,
  103. # deterministic=False,
  104. # random_state=None),
  105. # 亮度
  106. # iaa.AddToBrightness((-100, 100)),
  107. ])
  108. # temp_aug_bbox = []
  109. # temp_aug_points = []
  110. # bboxes_resize1 = [[100, 100, 200, 200]]
  111. # for bbox in bboxes_resize1:
  112. # temp_aug_bbox.append(ia.BoundingBox(x1=bbox[1],
  113. # x2=bbox[3],
  114. # y1=bbox[0],
  115. # y2=bbox[2]))
  116. # temp_points = [[100, 100], [100, 200], [200, 200], [200, 100]]
  117. # temp_aug_points.append(ia.Polygon(temp_points))
  118. # bbs = ia.BoundingBoxesOnImage(temp_aug_bbox, shape=image.shape)
  119. # if debug:
  120. # pass
  121. # plt.figure()
  122. # plt.imshow(bbs.draw_on_image(image, thickness=2))
  123. # seq_det = augmentation.to_deterministic()
  124. # aug_image = seq_det.augment_image(image)
  125. image = np.expand_dims(image, axis=0)
  126. mask_img = np.expand_dims(mask_img, axis=0)
  127. aug_image, aug_mask_img = augmentation(images=image, segmentation_maps=mask_img)
  128. # bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
  129. # # bbs_aug = seq_det.augment_polygons([bbs])[0]
  130. # aug_mask_img = seq_det.augment_image(mask_img)
  131. # plt.figure()
  132. # plt.imshow(bbs_aug.draw_on_image(image_aug, thickness=2))
  133. # bboxes_resize1 = []
  134. # for one in bbs_aug.bounding_boxes:
  135. # bboxes_resize1.append([one.y1, one.x1, one.y2, one.x2])
  136. if debug:
  137. pass
  138. # debug_show(image, bboxes_resize1)
  139. aug_image = np.squeeze(aug_image)
  140. aug_mask_img = np.squeeze(aug_mask_img)
  141. return aug_image, aug_mask_img

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/148877
推荐阅读
相关标签
  

闽ICP备14008679号