|
| 1 | +# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved |
| 2 | +import numpy as np |
| 3 | +import random |
| 4 | +import cv2 |
| 5 | +from fvcore.transforms.transform import Transform |
| 6 | + |
| 7 | +from detectron2.data.transforms import TransformGen |
| 8 | + |
| 9 | + |
| 10 | +class ColorAugSSD(TransformGen): |
| 11 | + def __init__(self): |
| 12 | + super().__init__() |
| 13 | + |
| 14 | + def get_transform(self, img): |
| 15 | + return ColorAugSSDTransform() |
| 16 | + |
| 17 | + |
| 18 | +class ColorAugSSDTransform(Transform): |
| 19 | + """ |
| 20 | + A color related data augmentation used in Single Shot Multibox Detector (SSD). |
| 21 | +
|
| 22 | + Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy, |
| 23 | + Scott Reed, Cheng-Yang Fu, Alexander C. Berg. |
| 24 | + SSD: Single Shot MultiBox Detector. ECCV 2016. |
| 25 | +
|
| 26 | + Implementation based on: |
| 27 | +
|
| 28 | + https://github.com/weiliu89/caffe/blob |
| 29 | + /4817bf8b4200b35ada8ed0dc378dceaf38c539e4 |
| 30 | + /src/caffe/util/im_transforms.cpp |
| 31 | +
|
| 32 | + https://github.com/chainer/chainercv/blob |
| 33 | + /7159616642e0be7c5b3ef380b848e16b7e99355b/chainercv |
| 34 | + /links/model/ssd/transforms.py |
| 35 | + """ |
| 36 | + |
| 37 | + def __init__( |
| 38 | + self, |
| 39 | + brightness_delta=32, |
| 40 | + contrast_low=0.5, |
| 41 | + contrast_high=1.5, |
| 42 | + saturation_low=0.5, |
| 43 | + saturation_high=1.5, |
| 44 | + hue_delta=18, |
| 45 | + ): |
| 46 | + super().__init__() |
| 47 | + self._set_attributes(locals()) |
| 48 | + |
| 49 | + def apply_coords(self, coords): |
| 50 | + return coords |
| 51 | + |
| 52 | + def apply_segmentation(self, segmentation): |
| 53 | + return segmentation |
| 54 | + |
| 55 | + def apply_image(self, img, interp=None): |
| 56 | + img = self.brightness(img) |
| 57 | + if random.randrange(2): |
| 58 | + img = self.contrast(img) |
| 59 | + img = self.saturation(img) |
| 60 | + img = self.hue(img) |
| 61 | + else: |
| 62 | + img = self.saturation(img) |
| 63 | + img = self.hue(img) |
| 64 | + img = self.contrast(img) |
| 65 | + return img |
| 66 | + |
| 67 | + def convert(self, img, alpha=1, beta=0): |
| 68 | + img = img.astype(np.float32) * alpha + beta |
| 69 | + img = np.clip(img, 0, 255) |
| 70 | + return img.astype(np.uint8) |
| 71 | + |
| 72 | + def brightness(self, img): |
| 73 | + if random.randrange(2): |
| 74 | + return self.convert( |
| 75 | + img, beta=random.uniform(-self.brightness_delta, self.brightness_delta) |
| 76 | + ) |
| 77 | + return img |
| 78 | + |
| 79 | + def contrast(self, img): |
| 80 | + if random.randrange(2): |
| 81 | + return self.convert(img, alpha=random.uniform(self.contrast_low, self.contrast_high)) |
| 82 | + return img |
| 83 | + |
| 84 | + def saturation(self, img): |
| 85 | + if random.randrange(2): |
| 86 | + img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) |
| 87 | + img[:, :, 1] = self.convert( |
| 88 | + img[:, :, 1], alpha=random.uniform(self.saturation_low, self.saturation_high) |
| 89 | + ) |
| 90 | + return cv2.cvtColor(img, cv2.COLOR_HSV2BGR) |
| 91 | + return img |
| 92 | + |
| 93 | + def hue(self, img): |
| 94 | + if random.randrange(2): |
| 95 | + img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) |
| 96 | + img[:, :, 0] = ( |
| 97 | + img[:, :, 0].astype(int) + random.randint(-self.hue_delta, self.hue_delta) |
| 98 | + ) % 180 |
| 99 | + return cv2.cvtColor(img, cv2.COLOR_HSV2BGR) |
| 100 | + return img |
0 commit comments