Skip to content

Commit 47c34bc

Browse files
Alexander Kirillovfacebook-github-bot
authored andcommitted
SSD Color augmentation
Reviewed By: rbgirshick Differential Revision: D19529633 fbshipit-source-id: c9e7028c840007eb5435a0a4d9ff4ecfd1286a6f
1 parent 9725ea1 commit 47c34bc

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

projects/PointRend/configs/SemanticSegmentation/pointrend_semantic_R_50_FPN_1x_cityscapes.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ INPUT:
2828
TYPE: "absolute"
2929
SIZE: (512, 1024)
3030
SINGLE_CATEGORY_MAX_AREA: 0.75
31+
COLOR_AUG_SSD: True
32+
DATALOADER:
33+
NUM_WORKERS: 16
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

projects/PointRend/point_rend/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ def add_pointrend_config(cfg):
1111
# We retry random cropping until no single category in semantic segmentation GT occupies more
1212
# than `SINGLE_CATEGORY_MAX_AREA` part of the crop.
1313
cfg.INPUT.CROP.SINGLE_CATEGORY_MAX_AREA = 1.0
14+
# Color augmentatition from SSD paper for semantic segmentation model during training.
15+
cfg.INPUT.COLOR_AUG_SSD = False
1416

1517
# Names of the input feature maps to be used by a coarse mask head.
1618
cfg.MODEL.ROI_MASK_HEAD.IN_FEATURES = ("p2",)

projects/PointRend/point_rend/dataset_mapper.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from detectron2.data import detection_utils as utils
1111
from detectron2.data import transforms as T
1212

13+
from .color_augmentation import ColorAugSSD
14+
1315
"""
1416
This file contains the mapping that's applied to "dataset dicts" for semantic segmentation models.
1517
Unlike the default DatasetMapper this mapper uses cropping as the last transformation.
@@ -40,6 +42,12 @@ def __init__(self, cfg, is_train=True):
4042

4143
self.tfm_gens = utils.build_transform_gen(cfg, is_train)
4244

45+
if cfg.INPUT.COLOR_AUG_SSD:
46+
self.tfm_gens.append(ColorAugSSD())
47+
logging.getLogger(__name__).info(
48+
"Color augmnetation used in training: " + str(self.tfm_gens[-1])
49+
)
50+
4351
# fmt: off
4452
self.img_format = cfg.INPUT.FORMAT
4553
self.single_category_max_area = cfg.INPUT.CROP.SINGLE_CATEGORY_MAX_AREA

0 commit comments

Comments
 (0)