|
@@ -1,36 +1,13 @@
|
|
|
|
|
+# ------------------------------------------------------------
|
|
|
|
|
+# Data preprocessor for SSD
|
|
|
|
|
+# ------------------------------------------------------------
|
|
|
import cv2
|
|
import cv2
|
|
|
import numpy as np
|
|
import numpy as np
|
|
|
import torch
|
|
import torch
|
|
|
from numpy import random
|
|
from numpy import random
|
|
|
|
|
|
|
|
|
|
|
|
|
-def intersect(box_a, box_b):
|
|
|
|
|
- max_xy = np.minimum(box_a[:, 2:], box_b[2:])
|
|
|
|
|
- min_xy = np.maximum(box_a[:, :2], box_b[:2])
|
|
|
|
|
- inter = np.clip((max_xy - min_xy), a_min=0, a_max=np.inf)
|
|
|
|
|
- return inter[:, 0] * inter[:, 1]
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-def jaccard_numpy(box_a, box_b):
|
|
|
|
|
- """Compute the jaccard overlap of two sets of boxes. The jaccard overlap
|
|
|
|
|
- is simply the intersection over union of two boxes.
|
|
|
|
|
- E.g.:
|
|
|
|
|
- A ∩ B / A ∪ B = A ∩ B / (area(A) + area(B) - A ∩ B)
|
|
|
|
|
- Args:
|
|
|
|
|
- box_a: Multiple bounding boxes, Shape: [num_boxes,4]
|
|
|
|
|
- box_b: Single bounding box, Shape: [4]
|
|
|
|
|
- Return:
|
|
|
|
|
- jaccard overlap: Shape: [box_a.shape[0], box_a.shape[1]]
|
|
|
|
|
- """
|
|
|
|
|
- inter = intersect(box_a, box_b)
|
|
|
|
|
- area_a = ((box_a[:, 2]-box_a[:, 0]) *
|
|
|
|
|
- (box_a[:, 3]-box_a[:, 1])) # [A,B]
|
|
|
|
|
- area_b = ((box_b[2]-box_b[0]) *
|
|
|
|
|
- (box_b[3]-box_b[1])) # [A,B]
|
|
|
|
|
- union = area_a + area_b - inter
|
|
|
|
|
- return inter / union # [A,B]
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
+# ------------------------- Augmentations -------------------------
|
|
|
class Compose(object):
|
|
class Compose(object):
|
|
|
"""Composes several augmentations together.
|
|
"""Composes several augmentations together.
|
|
|
Args:
|
|
Args:
|
|
@@ -50,12 +27,12 @@ class Compose(object):
|
|
|
img, boxes, labels = t(img, boxes, labels)
|
|
img, boxes, labels = t(img, boxes, labels)
|
|
|
return img, boxes, labels
|
|
return img, boxes, labels
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+## Convert Image to float type
|
|
|
class ConvertFromInts(object):
|
|
class ConvertFromInts(object):
|
|
|
def __call__(self, image, boxes=None, labels=None):
|
|
def __call__(self, image, boxes=None, labels=None):
|
|
|
return image.astype(np.float32), boxes, labels
|
|
return image.astype(np.float32), boxes, labels
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+## Convert color format
|
|
|
class ConvertColor(object):
|
|
class ConvertColor(object):
|
|
|
def __init__(self, current='BGR', transform='HSV'):
|
|
def __init__(self, current='BGR', transform='HSV'):
|
|
|
self.transform = transform
|
|
self.transform = transform
|
|
@@ -70,7 +47,7 @@ class ConvertColor(object):
|
|
|
raise NotImplementedError
|
|
raise NotImplementedError
|
|
|
return image, boxes, labels
|
|
return image, boxes, labels
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+## Resize image
|
|
|
class Resize(object):
|
|
class Resize(object):
|
|
|
def __init__(self, img_size=640):
|
|
def __init__(self, img_size=640):
|
|
|
self.img_size = img_size
|
|
self.img_size = img_size
|
|
@@ -86,7 +63,7 @@ class Resize(object):
|
|
|
|
|
|
|
|
return image, boxes, labels
|
|
return image, boxes, labels
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+## Random Saturation
|
|
|
class RandomSaturation(object):
|
|
class RandomSaturation(object):
|
|
|
def __init__(self, lower=0.5, upper=1.5):
|
|
def __init__(self, lower=0.5, upper=1.5):
|
|
|
self.lower = lower
|
|
self.lower = lower
|
|
@@ -100,7 +77,7 @@ class RandomSaturation(object):
|
|
|
|
|
|
|
|
return image, boxes, labels
|
|
return image, boxes, labels
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+## Random Hue
|
|
|
class RandomHue(object):
|
|
class RandomHue(object):
|
|
|
def __init__(self, delta=18.0):
|
|
def __init__(self, delta=18.0):
|
|
|
assert delta >= 0.0 and delta <= 360.0
|
|
assert delta >= 0.0 and delta <= 360.0
|
|
@@ -113,7 +90,7 @@ class RandomHue(object):
|
|
|
image[:, :, 0][image[:, :, 0] < 0.0] += 360.0
|
|
image[:, :, 0][image[:, :, 0] < 0.0] += 360.0
|
|
|
return image, boxes, labels
|
|
return image, boxes, labels
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+## Random Lighting noise
|
|
|
class RandomLightingNoise(object):
|
|
class RandomLightingNoise(object):
|
|
|
def __init__(self):
|
|
def __init__(self):
|
|
|
self.perms = ((0, 1, 2), (0, 2, 1),
|
|
self.perms = ((0, 1, 2), (0, 2, 1),
|
|
@@ -127,7 +104,7 @@ class RandomLightingNoise(object):
|
|
|
image = shuffle(image)
|
|
image = shuffle(image)
|
|
|
return image, boxes, labels
|
|
return image, boxes, labels
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+## Random Contrast
|
|
|
class RandomContrast(object):
|
|
class RandomContrast(object):
|
|
|
def __init__(self, lower=0.5, upper=1.5):
|
|
def __init__(self, lower=0.5, upper=1.5):
|
|
|
self.lower = lower
|
|
self.lower = lower
|
|
@@ -142,7 +119,7 @@ class RandomContrast(object):
|
|
|
image *= alpha
|
|
image *= alpha
|
|
|
return image, boxes, labels
|
|
return image, boxes, labels
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+## Random Brightness
|
|
|
class RandomBrightness(object):
|
|
class RandomBrightness(object):
|
|
|
def __init__(self, delta=32):
|
|
def __init__(self, delta=32):
|
|
|
assert delta >= 0.0
|
|
assert delta >= 0.0
|
|
@@ -155,7 +132,7 @@ class RandomBrightness(object):
|
|
|
image += delta
|
|
image += delta
|
|
|
return image, boxes, labels
|
|
return image, boxes, labels
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+## Random SampleCrop
|
|
|
class RandomSampleCrop(object):
|
|
class RandomSampleCrop(object):
|
|
|
"""Crop
|
|
"""Crop
|
|
|
Arguments:
|
|
Arguments:
|
|
@@ -182,6 +159,21 @@ class RandomSampleCrop(object):
|
|
|
(None, None),
|
|
(None, None),
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+ def intersect(self, box_a, box_b):
|
|
|
|
|
+ max_xy = np.minimum(box_a[:, 2:], box_b[2:])
|
|
|
|
|
+ min_xy = np.maximum(box_a[:, :2], box_b[:2])
|
|
|
|
|
+ inter = np.clip((max_xy - min_xy), a_min=0, a_max=np.inf)
|
|
|
|
|
+ return inter[:, 0] * inter[:, 1]
|
|
|
|
|
+
|
|
|
|
|
+ def compute_iou(self, box_a, box_b):
|
|
|
|
|
+ inter = self.intersect(box_a, box_b)
|
|
|
|
|
+ area_a = ((box_a[:, 2]-box_a[:, 0]) *
|
|
|
|
|
+ (box_a[:, 3]-box_a[:, 1])) # [A,B]
|
|
|
|
|
+ area_b = ((box_b[2]-box_b[0]) *
|
|
|
|
|
+ (box_b[3]-box_b[1])) # [A,B]
|
|
|
|
|
+ union = area_a + area_b - inter
|
|
|
|
|
+ return inter / union # [A,B]
|
|
|
|
|
+
|
|
|
def __call__(self, image, boxes=None, labels=None):
|
|
def __call__(self, image, boxes=None, labels=None):
|
|
|
height, width, _ = image.shape
|
|
height, width, _ = image.shape
|
|
|
# check
|
|
# check
|
|
@@ -219,7 +211,7 @@ class RandomSampleCrop(object):
|
|
|
rect = np.array([int(left), int(top), int(left+w), int(top+h)])
|
|
rect = np.array([int(left), int(top), int(left+w), int(top+h)])
|
|
|
|
|
|
|
|
# calculate IoU (jaccard overlap) b/t the cropped and gt boxes
|
|
# calculate IoU (jaccard overlap) b/t the cropped and gt boxes
|
|
|
- overlap = jaccard_numpy(boxes, rect)
|
|
|
|
|
|
|
+ overlap = self.compute_iou(boxes, rect)
|
|
|
|
|
|
|
|
# is min and max overlap constraint satisfied? if not try again
|
|
# is min and max overlap constraint satisfied? if not try again
|
|
|
if overlap.min() < min_iou and max_iou < overlap.max():
|
|
if overlap.min() < min_iou and max_iou < overlap.max():
|
|
@@ -264,7 +256,7 @@ class RandomSampleCrop(object):
|
|
|
|
|
|
|
|
return current_image, current_boxes, current_labels
|
|
return current_image, current_boxes, current_labels
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+## Random scaling
|
|
|
class Expand(object):
|
|
class Expand(object):
|
|
|
def __call__(self, image, boxes, labels):
|
|
def __call__(self, image, boxes, labels):
|
|
|
if random.randint(2):
|
|
if random.randint(2):
|
|
@@ -288,7 +280,7 @@ class Expand(object):
|
|
|
|
|
|
|
|
return image, boxes, labels
|
|
return image, boxes, labels
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+## Random HFlip
|
|
|
class RandomHorizontalFlip(object):
|
|
class RandomHorizontalFlip(object):
|
|
|
def __call__(self, image, boxes, classes):
|
|
def __call__(self, image, boxes, classes):
|
|
|
_, width, _ = image.shape
|
|
_, width, _ = image.shape
|
|
@@ -298,7 +290,7 @@ class RandomHorizontalFlip(object):
|
|
|
boxes[:, 0::2] = width - boxes[:, 2::-2]
|
|
boxes[:, 0::2] = width - boxes[:, 2::-2]
|
|
|
return image, boxes, classes
|
|
return image, boxes, classes
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+## Random swap channels
|
|
|
class SwapChannels(object):
|
|
class SwapChannels(object):
|
|
|
"""Transforms a tensorized image by swapping the channels in the order
|
|
"""Transforms a tensorized image by swapping the channels in the order
|
|
|
specified in the swap tuple.
|
|
specified in the swap tuple.
|
|
@@ -324,7 +316,7 @@ class SwapChannels(object):
|
|
|
image = image[:, :, self.swaps]
|
|
image = image[:, :, self.swaps]
|
|
|
return image
|
|
return image
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+## Random color jitter
|
|
|
class PhotometricDistort(object):
|
|
class PhotometricDistort(object):
|
|
|
def __init__(self):
|
|
def __init__(self):
|
|
|
self.pd = [
|
|
self.pd = [
|
|
@@ -348,11 +340,14 @@ class PhotometricDistort(object):
|
|
|
return im, boxes, labels
|
|
return im, boxes, labels
|
|
|
|
|
|
|
|
|
|
|
|
|
-# ----------------------- Main Functions -----------------------
|
|
|
|
|
|
|
+# ------------------------- Preprocessers -------------------------
|
|
|
## SSD-style Augmentation
|
|
## SSD-style Augmentation
|
|
|
class SSDAugmentation(object):
|
|
class SSDAugmentation(object):
|
|
|
def __init__(self, img_size=640):
|
|
def __init__(self, img_size=640):
|
|
|
self.img_size = img_size
|
|
self.img_size = img_size
|
|
|
|
|
+ self.pixel_mean = [0., 0., 0.]
|
|
|
|
|
+ self.pixel_std = [1., 1., 1.]
|
|
|
|
|
+ self.color_format = 'bgr'
|
|
|
self.augment = Compose([
|
|
self.augment = Compose([
|
|
|
ConvertFromInts(), # 将int类型转换为float32类型
|
|
ConvertFromInts(), # 将int类型转换为float32类型
|
|
|
PhotometricDistort(), # 图像颜色增强
|
|
PhotometricDistort(), # 图像颜色增强
|
|
@@ -384,6 +379,9 @@ class SSDAugmentation(object):
|
|
|
class SSDBaseTransform(object):
|
|
class SSDBaseTransform(object):
|
|
|
def __init__(self, img_size):
|
|
def __init__(self, img_size):
|
|
|
self.img_size = img_size
|
|
self.img_size = img_size
|
|
|
|
|
+ self.pixel_mean = [0., 0., 0.]
|
|
|
|
|
+ self.pixel_std = [1., 1., 1.]
|
|
|
|
|
+ self.color_format = 'bgr'
|
|
|
|
|
|
|
|
def __call__(self, image, target=None, mosaic=False):
|
|
def __call__(self, image, target=None, mosaic=False):
|
|
|
# resize
|
|
# resize
|