ssd_augment.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. import cv2
  2. import numpy as np
  3. import torch
  4. from numpy import random
  5. def intersect(box_a, box_b):
  6. max_xy = np.minimum(box_a[:, 2:], box_b[2:])
  7. min_xy = np.maximum(box_a[:, :2], box_b[:2])
  8. inter = np.clip((max_xy - min_xy), a_min=0, a_max=np.inf)
  9. return inter[:, 0] * inter[:, 1]
  10. def jaccard_numpy(box_a, box_b):
  11. """Compute the jaccard overlap of two sets of boxes. The jaccard overlap
  12. is simply the intersection over union of two boxes.
  13. E.g.:
  14. A ∩ B / A ∪ B = A ∩ B / (area(A) + area(B) - A ∩ B)
  15. Args:
  16. box_a: Multiple bounding boxes, Shape: [num_boxes,4]
  17. box_b: Single bounding box, Shape: [4]
  18. Return:
  19. jaccard overlap: Shape: [box_a.shape[0], box_a.shape[1]]
  20. """
  21. inter = intersect(box_a, box_b)
  22. area_a = ((box_a[:, 2]-box_a[:, 0]) *
  23. (box_a[:, 3]-box_a[:, 1])) # [A,B]
  24. area_b = ((box_b[2]-box_b[0]) *
  25. (box_b[3]-box_b[1])) # [A,B]
  26. union = area_a + area_b - inter
  27. return inter / union # [A,B]
  28. class Compose(object):
  29. """Composes several augmentations together.
  30. Args:
  31. transforms (List[Transform]): list of transforms to compose.
  32. Example:
  33. >>> augmentations.Compose([
  34. >>> transforms.CenterCrop(10),
  35. >>> transforms.ToTensor(),
  36. >>> ])
  37. """
  38. def __init__(self, transforms):
  39. self.transforms = transforms
  40. def __call__(self, img, boxes=None, labels=None):
  41. for t in self.transforms:
  42. img, boxes, labels = t(img, boxes, labels)
  43. return img, boxes, labels
  44. class ConvertFromInts(object):
  45. def __call__(self, image, boxes=None, labels=None):
  46. return image.astype(np.float32), boxes, labels
  47. class ConvertColor(object):
  48. def __init__(self, current='BGR', transform='HSV'):
  49. self.transform = transform
  50. self.current = current
  51. def __call__(self, image, boxes=None, labels=None):
  52. if self.current == 'BGR' and self.transform == 'HSV':
  53. image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
  54. elif self.current == 'HSV' and self.transform == 'BGR':
  55. image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR)
  56. else:
  57. raise NotImplementedError
  58. return image, boxes, labels
  59. class Resize(object):
  60. def __init__(self, img_size=640):
  61. self.img_size = img_size
  62. def __call__(self, image, boxes=None, labels=None):
  63. orig_h, orig_w = image.shape[:2]
  64. image = cv2.resize(image, (self.img_size, self.img_size))
  65. # rescale bbox
  66. if boxes is not None:
  67. img_h, img_w = image.shape[:2]
  68. boxes[..., [0, 2]] = boxes[..., [0, 2]] / orig_w * img_w
  69. boxes[..., [1, 3]] = boxes[..., [1, 3]] / orig_h * img_h
  70. return image, boxes, labels
  71. class RandomSaturation(object):
  72. def __init__(self, lower=0.5, upper=1.5):
  73. self.lower = lower
  74. self.upper = upper
  75. assert self.upper >= self.lower, "contrast upper must be >= lower."
  76. assert self.lower >= 0, "contrast lower must be non-negative."
  77. def __call__(self, image, boxes=None, labels=None):
  78. if random.randint(2):
  79. image[:, :, 1] *= random.uniform(self.lower, self.upper)
  80. return image, boxes, labels
  81. class RandomHue(object):
  82. def __init__(self, delta=18.0):
  83. assert delta >= 0.0 and delta <= 360.0
  84. self.delta = delta
  85. def __call__(self, image, boxes=None, labels=None):
  86. if random.randint(2):
  87. image[:, :, 0] += random.uniform(-self.delta, self.delta)
  88. image[:, :, 0][image[:, :, 0] > 360.0] -= 360.0
  89. image[:, :, 0][image[:, :, 0] < 0.0] += 360.0
  90. return image, boxes, labels
  91. class RandomLightingNoise(object):
  92. def __init__(self):
  93. self.perms = ((0, 1, 2), (0, 2, 1),
  94. (1, 0, 2), (1, 2, 0),
  95. (2, 0, 1), (2, 1, 0))
  96. def __call__(self, image, boxes=None, labels=None):
  97. if random.randint(2):
  98. swap = self.perms[random.randint(len(self.perms))]
  99. shuffle = SwapChannels(swap) # shuffle channels
  100. image = shuffle(image)
  101. return image, boxes, labels
  102. class RandomContrast(object):
  103. def __init__(self, lower=0.5, upper=1.5):
  104. self.lower = lower
  105. self.upper = upper
  106. assert self.upper >= self.lower, "contrast upper must be >= lower."
  107. assert self.lower >= 0, "contrast lower must be non-negative."
  108. # expects float image
  109. def __call__(self, image, boxes=None, labels=None):
  110. if random.randint(2):
  111. alpha = random.uniform(self.lower, self.upper)
  112. image *= alpha
  113. return image, boxes, labels
  114. class RandomBrightness(object):
  115. def __init__(self, delta=32):
  116. assert delta >= 0.0
  117. assert delta <= 255.0
  118. self.delta = delta
  119. def __call__(self, image, boxes=None, labels=None):
  120. if random.randint(2):
  121. delta = random.uniform(-self.delta, self.delta)
  122. image += delta
  123. return image, boxes, labels
  124. class RandomSampleCrop(object):
  125. """Crop
  126. Arguments:
  127. img (Image): the image being input during training
  128. boxes (Tensor): the original bounding boxes in pt form
  129. labels (Tensor): the class labels for each bbox
  130. mode (float tuple): the min and max jaccard overlaps
  131. Return:
  132. (img, boxes, classes)
  133. img (Image): the cropped image
  134. boxes (Tensor): the adjusted bounding boxes in pt form
  135. labels (Tensor): the class labels for each bbox
  136. """
  137. def __init__(self):
  138. self.sample_options = (
  139. # using entire original input image
  140. None,
  141. # sample a patch s.t. MIN jaccard w/ obj in .1,.3,.4,.7,.9
  142. (0.1, None),
  143. (0.3, None),
  144. (0.7, None),
  145. (0.9, None),
  146. # randomly sample a patch
  147. (None, None),
  148. )
  149. def __call__(self, image, boxes=None, labels=None):
  150. height, width, _ = image.shape
  151. # check
  152. if len(boxes) == 0:
  153. return image, boxes, labels
  154. while True:
  155. # randomly choose a mode
  156. sample_id = np.random.randint(len(self.sample_options))
  157. mode = self.sample_options[sample_id]
  158. if mode is None:
  159. return image, boxes, labels
  160. min_iou, max_iou = mode
  161. if min_iou is None:
  162. min_iou = float('-inf')
  163. if max_iou is None:
  164. max_iou = float('inf')
  165. # max trails (50)
  166. for _ in range(50):
  167. current_image = image
  168. w = random.uniform(0.3 * width, width)
  169. h = random.uniform(0.3 * height, height)
  170. # aspect ratio constraint b/t .5 & 2
  171. if h / w < 0.5 or h / w > 2:
  172. continue
  173. left = random.uniform(width - w)
  174. top = random.uniform(height - h)
  175. # convert to integer rect x1,y1,x2,y2
  176. rect = np.array([int(left), int(top), int(left+w), int(top+h)])
  177. # calculate IoU (jaccard overlap) b/t the cropped and gt boxes
  178. overlap = jaccard_numpy(boxes, rect)
  179. # is min and max overlap constraint satisfied? if not try again
  180. if overlap.min() < min_iou and max_iou < overlap.max():
  181. continue
  182. # cut the crop from the image
  183. current_image = current_image[rect[1]:rect[3], rect[0]:rect[2],
  184. :]
  185. # keep overlap with gt box IF center in sampled patch
  186. centers = (boxes[:, :2] + boxes[:, 2:]) / 2.0
  187. # mask in all gt boxes that above and to the left of centers
  188. m1 = (rect[0] < centers[:, 0]) * (rect[1] < centers[:, 1])
  189. # mask in all gt boxes that under and to the right of centers
  190. m2 = (rect[2] > centers[:, 0]) * (rect[3] > centers[:, 1])
  191. # mask in that both m1 and m2 are true
  192. mask = m1 * m2
  193. # have any valid boxes? try again if not
  194. if not mask.any():
  195. continue
  196. # take only matching gt boxes
  197. current_boxes = boxes[mask, :].copy()
  198. # take only matching gt labels
  199. current_labels = labels[mask]
  200. # should we use the box left and top corner or the crop's
  201. current_boxes[:, :2] = np.maximum(current_boxes[:, :2],
  202. rect[:2])
  203. # adjust to crop (by substracting crop's left,top)
  204. current_boxes[:, :2] -= rect[:2]
  205. current_boxes[:, 2:] = np.minimum(current_boxes[:, 2:],
  206. rect[2:])
  207. # adjust to crop (by substracting crop's left,top)
  208. current_boxes[:, 2:] -= rect[:2]
  209. return current_image, current_boxes, current_labels
  210. class Expand(object):
  211. def __call__(self, image, boxes, labels):
  212. if random.randint(2):
  213. return image, boxes, labels
  214. height, width, depth = image.shape
  215. ratio = random.uniform(1, 4)
  216. left = random.uniform(0, width*ratio - width)
  217. top = random.uniform(0, height*ratio - height)
  218. expand_image = np.zeros(
  219. (int(height*ratio), int(width*ratio), depth),
  220. dtype=image.dtype)
  221. expand_image[int(top):int(top + height),
  222. int(left):int(left + width)] = image
  223. image = expand_image
  224. boxes = boxes.copy()
  225. boxes[:, :2] += (int(left), int(top))
  226. boxes[:, 2:] += (int(left), int(top))
  227. return image, boxes, labels
  228. class RandomHorizontalFlip(object):
  229. def __call__(self, image, boxes, classes):
  230. _, width, _ = image.shape
  231. if random.randint(2):
  232. image = image[:, ::-1]
  233. boxes = boxes.copy()
  234. boxes[:, 0::2] = width - boxes[:, 2::-2]
  235. return image, boxes, classes
  236. class SwapChannels(object):
  237. """Transforms a tensorized image by swapping the channels in the order
  238. specified in the swap tuple.
  239. Args:
  240. swaps (int triple): final order of channels
  241. eg: (2, 1, 0)
  242. """
  243. def __init__(self, swaps):
  244. self.swaps = swaps
  245. def __call__(self, image):
  246. """
  247. Args:
  248. image (Tensor): image tensor to be transformed
  249. Return:
  250. a tensor with channels swapped according to swap
  251. """
  252. # if torch.is_tensor(image):
  253. # image = image.data.cpu().numpy()
  254. # else:
  255. # image = np.array(image)
  256. image = image[:, :, self.swaps]
  257. return image
  258. class PhotometricDistort(object):
  259. def __init__(self):
  260. self.pd = [
  261. RandomContrast(),
  262. ConvertColor(transform='HSV'),
  263. RandomSaturation(),
  264. RandomHue(),
  265. ConvertColor(current='HSV', transform='BGR'),
  266. RandomContrast()
  267. ]
  268. self.rand_brightness = RandomBrightness()
  269. def __call__(self, image, boxes, labels):
  270. im = image.copy()
  271. im, boxes, labels = self.rand_brightness(im, boxes, labels)
  272. if random.randint(2):
  273. distort = Compose(self.pd[:-1])
  274. else:
  275. distort = Compose(self.pd[1:])
  276. im, boxes, labels = distort(im, boxes, labels)
  277. return im, boxes, labels
  278. # ----------------------- Main Functions -----------------------
  279. ## SSD-style Augmentation
  280. class SSDAugmentation(object):
  281. def __init__(self, img_size=640):
  282. self.img_size = img_size
  283. self.augment = Compose([
  284. ConvertFromInts(), # 将int类型转换为float32类型
  285. PhotometricDistort(), # 图像颜色增强
  286. Expand(), # 扩充增强
  287. RandomSampleCrop(), # 随机剪裁
  288. RandomHorizontalFlip(), # 随机水平翻转
  289. Resize(self.img_size) # resize操作
  290. ])
  291. def __call__(self, image, target, mosaic=False):
  292. orig_h, orig_w = image.shape[:2]
  293. ratio = [self.img_size / orig_w, self.img_size / orig_h]
  294. # augment
  295. boxes = target['boxes'].copy()
  296. labels = target['labels'].copy()
  297. image, boxes, labels = self.augment(image, boxes, labels)
  298. # to tensor
  299. img_tensor = torch.from_numpy(image).permute(2, 0, 1).contiguous().float()
  300. target['boxes'] = torch.from_numpy(boxes).float()
  301. target['labels'] = torch.from_numpy(labels).float()
  302. return img_tensor, target, ratio
  303. ## SSD-style valTransform
  304. class SSDBaseTransform(object):
  305. def __init__(self, img_size):
  306. self.img_size = img_size
  307. def __call__(self, image, target=None, mosaic=False):
  308. # resize
  309. orig_h, orig_w = image.shape[:2]
  310. ratio = [self.img_size / orig_w, self.img_size / orig_h]
  311. image = cv2.resize(image, (self.img_size, self.img_size)).astype(np.float32)
  312. # scale targets
  313. if target is not None:
  314. boxes = target['boxes'].copy()
  315. labels = target['labels'].copy()
  316. img_h, img_w = image.shape[:2]
  317. boxes[..., [0, 2]] = boxes[..., [0, 2]] / orig_w * img_w
  318. boxes[..., [1, 3]] = boxes[..., [1, 3]] / orig_h * img_h
  319. target['boxes'] = boxes
  320. # to tensor
  321. img_tensor = torch.from_numpy(image).permute(2, 0, 1).contiguous().float()
  322. if target is not None:
  323. target['boxes'] = torch.from_numpy(boxes).float()
  324. target['labels'] = torch.from_numpy(labels).float()
  325. return img_tensor, target, ratio