Beispiel #1
0
 def __call__(self, img, boxes, labels):
     img = img.astype('float32')
     if random.randint(2):
         delta = random.uniform(-self.brightness_delta, self.brightness_delta)
         img += delta
     mode = random.randint(2)
     if mode == 1:
         if random.randint(2):
             alpha = random.uniform(self.contrast_lower,
                                    self.contrast_upper)
             img *= alpha
     # convert color from BGR to HSV
     img = mmcv.bgr2hsv(img)
     # random saturation
     if random.randint(2):
         img[..., 1] *= random.uniform(self.saturation_lower,
                                       self.saturation_upper)
     # random hue
     if random.randint(2):
         img[..., 0] += random.uniform(-self.hue_delta, self.hue_delta)
         img[..., 0][img[..., 0] > 360] -= 360
         img[..., 0][img[..., 0] < 0] += 360
     # convert color from HSV to BGR
     img = mmcv.hsv2bgr(img)
     # random contrast
     if mode == 0:
         if random.randint(2):
             alpha = random.uniform(self.contrast_lower,
                                    self.contrast_upper)
             img *= alpha
     # randomly swap channels
     if random.randint(2):
         img = img[..., random.permutation(3)]
     return img, boxes, labels
Beispiel #2
0
    def _augm(img, brightness_delta, contrast_mode, contrast_alpha,
              saturation_alpha, hue_delta, noise_sigma, color_scales):
        def _clamp_image(_img):
            _img[_img < 0.0] = 0.0
            _img[_img > 255.0] = 255.0
            return _img

        img = img.astype(np.float32)

        # random brightness
        if brightness_delta is not None:
            img += brightness_delta
            img = _clamp_image(img)

        # random contrast
        # mode == 0 --> do random contrast first
        # mode == 1 --> do random contrast last
        if contrast_mode == 1:
            if contrast_alpha is not None:
                img *= contrast_alpha
                img = _clamp_image(img)

        # convert color from BGR to HSV
        if saturation_alpha is not None or hue_delta is not None:
            img = mmcv.bgr2hsv(img / 255.)

        # random saturation
        if saturation_alpha is not None:
            img[:, :, 1] *= saturation_alpha
            img[:, :, 1][img[:, :, 1] > 1.0] = 1.0
            img[:, :, 1][img[:, :, 1] < 0.0] = 0.0

        # random hue
        if hue_delta is not None:
            img[:, :, 0] += hue_delta
            img[:, :, 0][img[:, :, 0] > 360.0] -= 360.0
            img[:, :, 0][img[:, :, 0] < 0.0] += 360.0

        # convert color from HSV to BGR
        if saturation_alpha is not None or hue_delta is not None:
            img = mmcv.hsv2bgr(img) * 255.

        # random contrast
        if contrast_mode == 0:
            if contrast_alpha is not None:
                img *= contrast_alpha
                img = _clamp_image(img)

        if color_scales is not None:
            img *= color_scales.reshape((1, 1, -1))

        # gaussian noise
        if noise_sigma is not None:
            img += np.random.normal(loc=0.0, scale=noise_sigma, size=img.shape)

        # clamp
        img = _clamp_image(img)

        return img.astype(np.uint8)
Beispiel #3
0
    def __call__(self, results):
        img = results['img']
        # random brightness
        if random.randint(2):
            delta = random.uniform(-self.brightness_delta,
                                   self.brightness_delta)
            # img += delta
            img = np.add(img, delta)

        # mode == 0 --> do random contrast first
        # mode == 1 --> do random contrast last
        mode = random.randint(2)
        if mode == 1:
            if random.randint(2):
                alpha = random.uniform(self.contrast_lower,
                                       self.contrast_upper)
                # img *= alpha
                img = np.multiply(img, alpha)

        # convert color from BGR to HSV
        img = img.astype(np.uint8)
        img = mmcv.bgr2hsv(img)

        # random saturation
        if random.randint(2):
            # img[..., 1] *= random.uniform(self.saturation_lower,
            #                               self.saturation_upper)
            img[..., 1] = np.multiply(
                img[..., 1],
                random.uniform(self.saturation_lower, self.saturation_upper))

            # random hue
        if random.randint(2):
            # img[..., 0] += random.uniform(-self.hue_delta, self.hue_delta)
            img[...,
                0] = np.add(img[..., 0],
                            random.uniform(-self.hue_delta, self.hue_delta))
            img[..., 0][img[..., 0] > 360] -= 360
            img[..., 0][img[..., 0] < 0] += 360

        # convert color from HSV to BGR
        img = mmcv.hsv2bgr(img)

        # random contrast
        if mode == 0:
            if random.randint(2):
                alpha = random.uniform(self.contrast_lower,
                                       self.contrast_upper)
                # img *= alpha
                img = np.multiply(img, alpha)

        # randomly swap channels
        if random.randint(2):
            img = img[..., random.permutation(3)]

        results['img'] = img
        # cv_showimg(**results)
        return results
Beispiel #4
0
    def photo_metric_distortion(self, results, params=None):
        """Call function to perform photometric distortion on images.

        Args:
            results (dict): Result dict from loading pipeline.
            params (dict, optional): Pre-defined parameters. Default to None.

        Returns:
            dict: Result dict with images distorted.
        """
        if params is None:
            params = self.get_params()
        results['img_info']['color_jitter'] = params

        if 'img_fields' in results:
            assert results['img_fields'] == ['img'], \
                'Only single img_fields is allowed'
        img = results['img']
        assert img.dtype == np.float32, \
            'PhotoMetricDistortion needs the input image of dtype np.float32,'\
            ' please set "to_float32=True" in "LoadImageFromFile" pipeline'
        # random brightness
        if params['delta'] is not None:
            img += params['delta']

        # mode == 0 --> do random contrast first
        # mode == 1 --> do random contrast last
        if params['contrast_first']:
            if params['alpha'] is not None:
                img *= params['alpha']

        # convert color from BGR to HSV
        img = mmcv.bgr2hsv(img)

        # random saturation
        if params['saturation'] is not None:
            img[..., 1] *= params['saturation']

        # random hue
        if params['hue'] is not None:
            img[..., 0] += params['hue']
            img[..., 0][img[..., 0] > 360] -= 360
            img[..., 0][img[..., 0] < 0] += 360

        # convert color from HSV to BGR
        img = mmcv.hsv2bgr(img)

        # random contrast
        if not params['contrast_first']:
            if params['alpha'] is not None:
                img *= params['alpha']

        # randomly swap channels
        if params['permutation'] is not None:
            img = img[..., params['permutation']]

        results['img'] = img
        return results
Beispiel #5
0
 def hue(self, img):
     """Hue distortion."""
     if random.randint(2):
         img = mmcv.bgr2hsv(img)
         img[:, :,
             0] = (img[:, :, 0].astype(int) +
                   random.randint(-self.hue_delta, self.hue_delta)) % 180
         img = mmcv.hsv2bgr(img)
     return img
Beispiel #6
0
 def saturation(self, img):
     """Saturation distortion."""
     if random.randint(2):
         img = mmcv.bgr2hsv(img)
         img[:, :, 1] = self.convert(
             img[:, :, 1],
             alpha=random.uniform(self.saturation_lower,
                                  self.saturation_upper))
         img = mmcv.hsv2bgr(img)
     return img
Beispiel #7
0
    def __call__(self, results):
        img = results['img']
        assert img.dtype == np.float32, \
            'PhotoMetricDistortion needs the input image of dtype np.float32,' \
            ' please set "to_float32=True" in "LoadImageFromFile" pipeline'
        # random brightness
        if random.randint(2):
            delta = random.uniform(-self.brightness_delta,
                                   self.brightness_delta)
            img += delta

        # mode == 0 --> do random contrast first
        # mode == 1 --> do random contrast last
        mode = random.randint(2)
        if mode == 1:
            if random.randint(2):
                alpha = random.uniform(self.contrast_lower,
                                       self.contrast_upper)
                img *= alpha

        # convert color from BGR to HSV
        img = mmcv.bgr2hsv(img)

        # random saturation
        if random.randint(2):
            img[..., 1] *= random.uniform(self.saturation_lower,
                                          self.saturation_upper)

        # random exposure
        if random.randint(2):
            img[..., 2] *= random.uniform(self.exposure_lower,
                                          self.exposure_upper)

        # random hue
        if random.randint(2):
            img[..., 0] += random.uniform(-self.hue_delta, self.hue_delta)
            img[..., 0][img[..., 0] > 360] -= 360
            img[..., 0][img[..., 0] < 0] += 360

        # convert color from HSV to BGR
        img = mmcv.hsv2bgr(img)

        # random contrast
        if mode == 0:
            if random.randint(2):
                alpha = random.uniform(self.contrast_lower,
                                       self.contrast_upper)
                img *= alpha

        # randomly swap channels
        if random.randint(2):
            img = img[..., random.permutation(3)]

        results['img'] = img
        return results
    def __call__(self, img, boxes, gt_masks, gt_segs, labels):
        """
        apply transform when calling
        :param img: numpy.ndarray,(h,w,c)
        :param boxes: numpy.ndarray,(n,4), x1,y1,x2,y2
        :param segments: list(list),(n)(x),x is variant
        :param labels: numpy.ndarray,(n)
        :return:
        """

        # random brightness
        if random.randint(2):
            delta = random.uniform(-self.brightness_delta,
                                   self.brightness_delta)
            img += delta

        # mode == 0 --> do random contrast first
        # mode == 1 --> do random contrast last
        mode = random.randint(2)
        if mode == 1:
            if random.randint(2):
                alpha = random.uniform(self.contrast_lower,
                                       self.contrast_upper)
                img *= alpha

        # convert color from BGR to HSV
        img = mmcv.bgr2hsv(img)

        # random saturation
        if random.randint(2):
            img[..., 1] *= random.uniform(self.saturation_lower,
                                          self.saturation_upper)

        # random hue
        if random.randint(2):
            img[..., 0] += random.uniform(-self.hue_delta, self.hue_delta)
            img[..., 0][img[..., 0] > 360] -= 360
            img[..., 0][img[..., 0] < 0] += 360

        # convert color from HSV to BGR
        img = mmcv.hsv2bgr(img)

        # random contrast
        if mode == 0:
            if random.randint(2):
                alpha = random.uniform(self.contrast_lower,
                                       self.contrast_upper)
                img *= alpha

        # randomly swap channels
        if random.randint(2):
            img = img[..., random.permutation(3)]

        return img, boxes, gt_masks, gt_segs, labels
Beispiel #9
0
    def __call__(self, img, boxes, labels):
        print('photo metric distortion, extra_aug.py')
        i += 1
        # random brightness
        if random.randint(2):
            delta = random.uniform(-self.brightness_delta,
                                   self.brightness_delta)
            img += delta

        # mode == 0 --> do random contrast first
        # mode == 1 --> do random contrast last
        mode = random.randint(2)
        if mode == 1:
            if random.randint(2):
                alpha = random.uniform(self.contrast_lower,
                                       self.contrast_upper)
                img *= alpha

        # convert color from BGR to HSV
        img = mmcv.bgr2hsv(img)

        # random saturation
        if random.randint(2):
            img[..., 1] *= random.uniform(self.saturation_lower,
                                          self.saturation_upper)

        # random hue
        if random.randint(2):
            img[..., 0] += random.uniform(-self.hue_delta, self.hue_delta)
            img[..., 0][img[..., 0] > 360] -= 360
            img[..., 0][img[..., 0] < 0] += 360

        # convert color from HSV to BGR
        img = mmcv.hsv2bgr(img)

        # random contrast
        if mode == 0:
            if random.randint(2):
                alpha = random.uniform(self.contrast_lower,
                                       self.contrast_upper)
                img *= alpha

        # randomly swap channels
        if random.randint(2):
            img = img[..., random.permutation(3)]

        return img, boxes, labels
Beispiel #10
0
    def __call__(self, results):
        img = results['img']
        # random brightness
        if random.randint(2):
            delta = random.uniform(-self.brightness_delta,
                                   self.brightness_delta)
            img += delta

        # mode == 0 --> do random contrast first
        # mode == 1 --> do random contrast last
        mode = random.randint(2)
        if mode == 1:
            if random.randint(2):
                alpha = random.uniform(self.contrast_lower,
                                       self.contrast_upper)
                img *= alpha

        # convert color from BGR to HSV
        img = mmcv.bgr2hsv(img)

        # random saturation
        if random.randint(2):
            img[..., 1] *= random.uniform(self.saturation_lower,
                                          self.saturation_upper)

        # random hue
        if random.randint(2):
            img[..., 0] += random.uniform(-self.hue_delta, self.hue_delta)
            img[..., 0][img[..., 0] > 360] -= 360
            img[..., 0][img[..., 0] < 0] += 360

        # convert color from HSV to BGR
        img = mmcv.hsv2bgr(img)

        # random contrast
        if mode == 0:
            if random.randint(2):
                alpha = random.uniform(self.contrast_lower,
                                       self.contrast_upper)
                img *= alpha

        # randomly swap channels
        if random.randint(2):
            img = img[..., random.permutation(3)]

        results['img'] = img
        return results
    def __call__(self, img, boxes, labels):
        # random brightness 随机亮度
        if random.randint(2):
            delta = random.uniform(-self.brightness_delta,
                                   self.brightness_delta)
            img += delta

        # mode == 0 --> do random contrast first
        # mode == 1 --> do random contrast last
        mode = random.randint(2)
        if mode == 1:
            if random.randint(2):
                alpha = random.uniform(self.contrast_lower,
                                       self.contrast_upper)
                img *= alpha

        # convert color from BGR to HSV
        img = mmcv.bgr2hsv(img)

        # random saturation 随机饱和度
        if random.randint(2):
            img[..., 1] *= random.uniform(self.saturation_lower,
                                          self.saturation_upper)

        # random hue 随机色度
        if random.randint(2):
            img[..., 0] += random.uniform(-self.hue_delta, self.hue_delta)
            img[..., 0][img[..., 0] > 360] -= 360
            img[..., 0][img[..., 0] < 0] += 360

        # convert color from HSV to BGR
        img = mmcv.hsv2bgr(img)

        # random contrast 随机对比度
        if mode == 0:
            if random.randint(2):
                alpha = random.uniform(self.contrast_lower,
                                       self.contrast_upper)
                img *= alpha

        # randomly swap channels 随机交换channel
        if random.randint(2):
            img = img[..., random.permutation(3)]

        return img, boxes, labels
Beispiel #12
0
    def __call__(self, results):
        """Call function.

        Args:
            results (dict): A dict containing the necessary information and
                data for augmentation.

        Returns:
            dict: A dict containing the processed data and information.
        """
        fg, alpha = results['fg'], results['alpha']

        # convert to HSV space;
        # convert to float32 image to keep precision during space conversion.
        fg = mmcv.bgr2hsv(fg.astype(np.float32) / 255)
        # Hue noise
        hue_jitter = np.random.randint(self.hue_range[0], self.hue_range[1])
        fg[:, :, 0] = np.remainder(fg[:, :, 0] + hue_jitter, 360)

        # Saturation noise
        sat_mean = fg[:, :, 1][alpha > 0].mean()
        # jitter saturation within range (1.1 - sat_mean) * [-0.1, 0.1]
        sat_jitter = (1.1 - sat_mean) * (np.random.rand() * 0.2 - 0.1)
        sat = fg[:, :, 1]
        sat = np.abs(sat + sat_jitter)
        sat[sat > 1] = 2 - sat[sat > 1]
        fg[:, :, 1] = sat

        # Value noise
        val_mean = fg[:, :, 2][alpha > 0].mean()
        # jitter value within range (1.1 - val_mean) * [-0.1, 0.1]
        val_jitter = (1.1 - val_mean) * (np.random.rand() * 0.2 - 0.1)
        val = fg[:, :, 2]
        val = np.abs(val + val_jitter)
        val[val > 1] = 2 - val[val > 1]
        fg[:, :, 2] = val
        # convert back to BGR space
        fg = mmcv.hsv2bgr(fg)
        results['fg'] = fg * 255

        return results
Beispiel #13
0
    def __call__(self, results):
        """Call function to perform photometric distortion on images.

        Args:
            results (dict): Result dict from loading pipeline.

        Returns:
            dict: Result dict with images distorted.
        """

        img = results['img']
        # random brightness
        img = self.brightness(img)

        # mode == 0 --> do random contrast first
        # mode == 1 --> do random contrast last
        mode = random.randint(2)
        if mode == 1:
            img = self.contrast(img)

        hsv_mode = random.randint(4)
        if hsv_mode:
            # random saturation/hue distortion
            img = mmcv.bgr2hsv(img)
            if hsv_mode == 1 or hsv_mode == 3:
                img = self.saturation(img)
            if hsv_mode == 2 or hsv_mode == 3:
                img = self.hue(img)
            img = mmcv.hsv2bgr(img)

        # random contrast
        if mode == 0:
            img = self.contrast(img)

        # randomly swap channels
        self.swap_channels(img)

        results['img'] = img
        return results
    def __call__(self, results):
        print('start')
        import copy
        img = copy.deepcopy(results['img'])
        print(img.shape)
        img[0,0,0]+=1
        print('loaded')
        # random brightness
        if random.randint(2):
            print('a')
            delta = random.uniform(-self.brightness_delta,
                                   self.brightness_delta)
            print(delta)
            print(img.shape)

            print('success')
            print(delta.shape)
            img += delta
            print('exit')
        print('first')
        # mode == 0 --> do random contrast first
        # mode == 1 --> do random contrast last
        mode = random.randint(2)
        if mode == 1:
            if random.randint(2):
                print('exit1')
                alpha = random.uniform(self.contrast_lower,
                                       self.contrast_upper)
                img *= alpha
                print(img.shape)
        # convert color from BGR to HSV
        print('bgr')
        img = mmcv.bgr2hsv(img)
        print('mid')
        # random saturation
        if random.randint(2):
            
            img[..., 1] *= 2#random.uniform(self.saturation_lower,
                                          #self.saturation_upper)
            print('exit2')
        # random hue
        if random.randint(2):
            img[..., 0] += random.uniform(-self.hue_delta, self.hue_delta)
            img[..., 0][img[..., 0] > 360] -= 360
            img[..., 0][img[..., 0] < 0] += 360
            print(img.shape)
        # convert color from HSV to BGR
        img = mmcv.hsv2bgr(img)

        # random contrast
        if mode == 0:
            if random.randint(2):
                alpha = random.uniform(self.contrast_lower,
                                       self.contrast_upper)
                img *= alpha
                print(img.shape)
        # randomly swap channels
        if random.randint(2):
            img = img[..., random.permutation(3)]

        results['img'] = img
        print('end')
        return results
Beispiel #15
0
    def __call__(self, results):
        """Call function to perform photometric distortion on images.

        Args:
            results (dict): Result dict from loading pipeline.

        Returns:
            dict: Result dict with images distorted.
        """

        if 'img_fields' in results:
            assert results['img_fields'] == ['img'], \
                'Only single img_fields is allowed'
        img = results['img']
        assert img.dtype == np.float32, \
            'PhotoMetricDistortion needs the input image of dtype np.float32,'\
            ' please set "to_float32=True" in "LoadImageFromFile" pipeline'
        # random brightness
        if random.randint(2):
            delta = random.uniform(-self.brightness_delta,
                                   self.brightness_delta)
            img += delta

        # mode == 0 --> do random contrast first
        # mode == 1 --> do random contrast last
        mode = random.randint(2)
        if mode == 1:
            if random.randint(2):
                alpha = random.uniform(self.contrast_lower,
                                       self.contrast_upper)
                img *= alpha

        # convert color from BGR to HSV
        img = mmcv.bgr2hsv(img)

        # random saturation
        if random.randint(2):
            img[..., 1] *= random.uniform(self.saturation_lower,
                                          self.saturation_upper)

        # random hue
        if random.randint(2):
            img[..., 0] += random.uniform(-self.hue_delta, self.hue_delta)
            img[..., 0][img[..., 0] > 360] -= 360
            img[..., 0][img[..., 0] < 0] += 360

        # convert color from HSV to BGR
        img = mmcv.hsv2bgr(img)

        # random contrast
        if mode == 0:
            if random.randint(2):
                alpha = random.uniform(self.contrast_lower,
                                       self.contrast_upper)
                img *= alpha

        # randomly swap channels
        if random.randint(2):
            img = img[..., random.permutation(3)]

        results['img'] = img
        return results
    def __call__(self, results, state=None):
        if state is None:
            delta_randint = random.randint(2)
            delta = random.uniform(-self.brightness_delta,
                                   self.brightness_delta)
            mode = random.randint(2)
            mode_randint = random.randint(2)
            alpha = random.uniform(self.contrast_lower, self.contrast_upper)
            sat_randint = random.randint(2)
            sat_multiplier = random.uniform(self.saturation_lower,
                                            self.saturation_upper)
            hue_randint = random.randint(2)
            hue_multiplier = random.uniform(-self.hue_delta, self.hue_delta)
            contrast_randint = random.randint(2)
            contrast_alpha = random.uniform(self.contrast_lower,
                                            self.contrast_upper)
            perm_randint = random.randint(2)
            perm = random.permutation(3)

            state = dict(delta_randint=delta_randint,
                         delta=delta,
                         mode=mode,
                         mode_randint=mode_randint,
                         alpha=alpha,
                         sat_randint=sat_randint,
                         sat_multiplier=sat_multiplier,
                         hue_randint=hue_randint,
                         hue_multiplier=hue_multiplier,
                         contrast_randint=contrast_randint,
                         contrast_alpha=contrast_alpha,
                         perm_randint=perm_randint,
                         perm=perm)
        else:
            delta_randint = state['delta_randint']
            delta = state['delta']
            mode = state['mode']
            mode_randint = state['mode_randint']
            alpha = state['alpha']
            sat_randint = state['sat_randint']
            sat_multiplier = state['sat_multiplier']
            hue_randint = state['hue_randint']
            hue_multiplier = state['hue_multiplier']
            contrast_randint = state['contrast_randint']
            contrast_alpha = state['contrast_alpha']
            perm_randint = state['perm_randint']
            perm = state['perm']

        img = results['img']
        # random brightness
        if delta_randint:
            delta = delta
            img += delta

        # mode == 0 --> do random contrast first
        # mode == 1 --> do random contrast last
        mode = mode
        if mode == 1:
            if mode_randint:
                alpha = alpha
                img *= alpha

        # convert color from BGR to HSV
        img = mmcv.bgr2hsv(img)

        # random saturation
        if sat_randint:
            img[..., 1] *= sat_multiplier

        # random hue
        if hue_randint:
            img[..., 0] += hue_multiplier
            img[..., 0][img[..., 0] > 360] -= 360
            img[..., 0][img[..., 0] < 0] += 360

        # convert color from HSV to BGR
        img = mmcv.hsv2bgr(img)

        # random contrast
        if mode == 0:
            if contrast_randint:
                alpha = contrast_alpha
                img *= alpha

        # randomly swap channels
        if perm_randint:
            img = img[..., perm]

        results['img'] = img
        return results, state
Beispiel #17
0
    def __call__(self, img, boxes, labels, masks=None):
        if self.color_choose == 0:
            if random.uniform() < self.gray_p:
                gray = mmcv.bgr2gray(img)
                img = mmcv.gray2bgr(gray)
                return img, boxes, labels, masks
            # random brightness
            if random.randint(2):
                delta = random.uniform(-self.brightness_delta,
                                       self.brightness_delta)
                img += delta

            # mode == 0 --> do random contrast first
            # mode == 1 --> do random contrast last
            mode = random.randint(2)
            if mode == 1:
                if random.randint(2):
                    alpha = random.uniform(self.contrast_lower,
                                           self.contrast_upper)
                    img *= alpha

            # convert color from BGR to HSV
            img = mmcv.bgr2hsv(img)

            # random saturation
            if random.randint(2):
                img[..., 1] *= random.uniform(self.saturation_lower,
                                              self.saturation_upper)

            # random hue
            if random.randint(2):
                img[..., 0] += random.uniform(-self.hue_delta, self.hue_delta)
                img[..., 0][img[..., 0] > 360] -= 360
                img[..., 0][img[..., 0] < 0] += 360

            # convert color from HSV to BGR
            img = mmcv.hsv2bgr(img)

            # random contrast
            if mode == 0:
                if random.randint(2):
                    alpha = random.uniform(self.contrast_lower,
                                           self.contrast_upper)
                    img *= alpha

            # randomly swap channels
            if random.randint(2):
                img = img[..., random.permutation(3)]
        else:
            if self.color_choose == 1:
                # random brightness
                if random.randint(2):
                    delta = random.uniform(-self.brightness_delta,
                                           self.brightness_delta)
                    img += delta
            elif self.color_choose == 2:
                # random contrast first
                if random.randint(2):
                    alpha = random.uniform(self.contrast_lower,
                                           self.contrast_upper)
                    img *= alpha
            else:
                # convert color from BGR to HSV
                img = mmcv.bgr2hsv(img)

                if self.color_choose == 3:
                    # random saturation
                    if random.randint(2):
                        img[..., 1] *= random.uniform(self.saturation_lower,
                                                      self.saturation_upper)
                if self.color_choose == 4:
                    # random hue
                    if random.randint(2):
                        img[..., 0] += random.uniform(-self.hue_delta,
                                                      self.hue_delta)
                        img[..., 0][img[..., 0] > 360] -= 360
                        img[..., 0][img[..., 0] < 0] += 360

                # convert color from HSV to BGR
                img = mmcv.hsv2bgr(img)

        return img, boxes, labels, masks
Beispiel #18
0
    def random_photo_metric_distortion(self, img):
        # https://github.com/open-mmlab/mmdetection/blob/a054aef422d650a644459bdc232248eefa8ae8b7/mmdet/datasets/extra_aug.py#L8
        # brightness_delta=32
        # contrast_range=(0.5, 1.5),
        # saturation_range=(0.5, 1.5),
        # hue_delta=18)
        # brightness_delta = photo_metric_distortion['brightness_delta']
        # contrast_lower, contrast_upper = photo_metric_distortion['contrast_range']
        # saturation_lower, saturation_upper = photo_metric_distortion['saturation_range']
        # hue_delta = self.photo_metric_distortion['hue_delta']

        brightness_delta = 32
        contrast_lower, contrast_upper = (0.5, 1.5)
        saturation_lower, saturation_upper = (0.5, 1.5)
        hue_delta = 18

        # change datatype before do photo metric distortion
        img = img.astype(np.float32)

        # random brightness
        if np.random.randint(2):
            delta = np.random.uniform(-brightness_delta, brightness_delta)
            img += delta
            img = img.clip(min=0, max=255)

        # mode == 0 --> do random contrast first
        # mode == 1 --> do random contrast last
        mode = np.random.randint(2)
        if mode == 1:
            if np.random.randint(2):
                alpha = np.random.uniform(contrast_lower, contrast_upper)
                img *= alpha
                img = img.clip(min=0, max=255)

        # convert color from BGR to HSV
        img = mmcv.bgr2hsv(img)

        # random saturation
        if np.random.randint(2):
            img[..., 1] *= np.random.uniform(saturation_lower,
                                             saturation_upper)
            img[..., 1] = img[..., 1].clip(min=0, max=1)

        # random hue
        if np.random.randint(2):
            img[..., 0] += np.random.uniform(-hue_delta, hue_delta)
            # img[..., 0][img[..., 0] > 360] -= 360
            # img[..., 0][img[..., 0] < 0] += 360
            img[..., 0] = img[..., 0].clip(min=0, max=360)

        # convert color from HSV to BGR
        img = mmcv.hsv2bgr(img)

        # random contrast
        if mode == 0:
            if np.random.randint(2):
                alpha = np.random.uniform(contrast_lower, contrast_upper)
                img *= alpha
                img = img.clip(min=0, max=255)

        # randomly swap channels
        # if np.random.randint(2):
        #     img = img[..., np.random.permutation(3)]

        return img.astype(np.uint8)