imgaug_support.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import numpy as np
  15. import copy
  16. def execute_imgaug(augmenter, im, bboxes=None, polygons=None,
  17. segment_map=None):
  18. # 预处理,将bboxes, polygons转换成imgaug格式
  19. import imgaug.augmentables.kps as kps
  20. import imgaug.augmentables.bbs as bbs
  21. aug_im = im.astype('uint8')
  22. aug_im = augmenter.augment(image=aug_im).astype('float32')
  23. return aug_im
  24. # TODO imgaug的标注处理逻辑与paddlex已存的transform存在部分差异
  25. # 目前仅支持对原图进行处理,因此只能使用pixlevel的imgaug增强操作
  26. # 以下代码暂不会执行
  27. aug_bboxes = None
  28. if bboxes is not None:
  29. aug_bboxes = list()
  30. for i in range(len(bboxes)):
  31. x1 = bboxes[i, 0]
  32. y1 = bboxes[i, 1]
  33. x2 = bboxes[i, 2]
  34. y2 = bboxes[i, 3]
  35. aug_bboxes.append(bbs.BoundingBox(x1, y1, x2, y2))
  36. aug_points = None
  37. if polygons is not None:
  38. aug_points = list()
  39. for i in range(len(polygons)):
  40. num = len(polygons[i])
  41. for j in range(num):
  42. tmp = np.reshape(polygons[i][j], (-1, 2))
  43. for k in range(len(tmp)):
  44. aug_points.append(kps.Keypoint(tmp[k, 0], tmp[k, 1]))
  45. aug_segment_map = None
  46. if segment_map is not None:
  47. if len(segment_map.shape) == 2:
  48. h, w = segment_map.shape
  49. aug_segment_map = np.reshape(segment_map, (1, h, w, 1))
  50. elif len(segment_map.shape) == 3:
  51. h, w, c = segment_map.shape
  52. aug_segment_map = np.reshape(segment_map, (1, h, w, c))
  53. else:
  54. raise Exception(
  55. "Only support 2-dimensions for 3-dimensions for segment_map")
  56. unnormalized_batch = augmenter.augment(
  57. image=aug_im,
  58. bounding_boxes=aug_bboxes,
  59. keypoints=aug_points,
  60. segmentation_maps=aug_segment_map,
  61. return_batch=True)
  62. aug_im = unnormalized_batch.images_aug[0]
  63. aug_bboxes = unnormalized_batch.bounding_boxes_aug
  64. aug_points = unnormalized_batch.keypoints_aug
  65. aug_seg_map = unnormalized_batch.segmentation_maps_aug
  66. aug_im = aug_im.astype('float32')
  67. if aug_bboxes is not None:
  68. converted_bboxes = list()
  69. for i in range(len(aug_bboxes)):
  70. converted_bboxes.append([
  71. aug_bboxes[i].x1, aug_bboxes[i].y1, aug_bboxes[i].x2,
  72. aug_bboxes[i].y2
  73. ])
  74. aug_bboxes = converted_bboxes
  75. aug_polygons = None
  76. if aug_points is not None:
  77. aug_polygons = copy.deepcopy(polygons)
  78. idx = 0
  79. for i in range(len(aug_polygons)):
  80. num = len(aug_polygons[i])
  81. for j in range(num):
  82. num_points = len(aug_polygons[i][j]) // 2
  83. for k in range(num_points):
  84. aug_polygons[i][j][k * 2] = aug_points[idx].x
  85. aug_polygons[i][j][k * 2 + 1] = aug_points[idx].y
  86. idx += 1
  87. result = [aug_im]
  88. if aug_bboxes is not None:
  89. result.append(np.array(aug_bboxes))
  90. if aug_polygons is not None:
  91. result.append(aug_polygons)
  92. if aug_seg_map is not None:
  93. n, h, w, c = aug_seg_map.shape
  94. if len(segment_map.shape) == 2:
  95. aug_seg_map = np.reshape(aug_seg_map, (h, w))
  96. elif len(segment_map.shape) == 3:
  97. aug_seg_map = np.reshape(aug_seg_map, (h, w, c))
  98. result.append(aug_seg_map)
  99. return result