Exemplo n.º 1
0
    def __call__(self, img_group, is_flow=False):

        image_h = img_group[0].shape[0]
        image_w = img_group[0].shape[1]
        crop_w, crop_h = self.crop_size

        offsets = GroupMultiScaleCrop.fill_fix_offset(
            False, image_w, image_h, crop_w, crop_h)
        oversample_group = list()
        for o_w, o_h in offsets:
            normal_group = list()
            flip_group = list()
            for i, img in enumerate(img_group):
                crop = mmcv.imcrop(img, np.array(
                    [o_w, o_h, o_w + crop_w-1, o_h + crop_h-1]))
                normal_group.append(crop)
                flip_crop = mmcv.imflip(crop)

                if is_flow and i % 2 == 0:
                    flip_group.append(mmcv.iminvert(flip_crop))
                else:
                    flip_group.append(flip_crop)

            oversample_group.extend(normal_group)
            oversample_group.extend(flip_group)
        return oversample_group, None
Exemplo n.º 2
0
    def __call__(self, results):
        img_group = results['img_group']
        img_h, img_w = img_group[0].shape[:2]
        crop_w, crop_h = self.crop_size

        offsets = MultiScaleCrop.fill_fix_offset(False, img_w, img_h, crop_w,
                                                 crop_h)
        oversample_group = list()
        for o_w, o_h in offsets:
            normal_group = list()
            flip_group = list()
            for i, img in enumerate(img_group):
                crop = mmcv.imcrop(
                    img,
                    np.array([o_w, o_h, o_w + crop_w - 1, o_h + crop_h - 1]))
                normal_group.append(crop)
                flip_crop = mmcv.imflip(crop)

                if results['modality'] == 'Flow' and i % 2 == 0:
                    flip_group.append(mmcv.iminvert(flip_crop))
                else:
                    flip_group.append(flip_crop)

            oversample_group.extend(normal_group)
            oversample_group.extend(flip_group)
        results['img_group'] = oversample_group
        results['crop_bbox'] = None
        results['img_shape'] = results['img_group'][0].shape

        return results
Exemplo n.º 3
0
 def __call__(self, results):
     if np.random.rand() > self.prob:
         return results
     for key in results.get('img_fields', ['img']):
         img = results[key]
         img_inverted = mmcv.iminvert(img)
         results[key] = img_inverted.astype(img.dtype)
     return results
Exemplo n.º 4
0
    def __call__(self, img_group, scale, crop_history=None, flip=False,
                 keep_ratio=True, div_255=False, is_flow=False):
        # 1. rescale
        if keep_ratio:
            tuple_list = [mmcv.imrescale(
                img, scale, return_scale=True) for img in img_group]
            img_group, scale_factors = list(zip(*tuple_list))
            scale_factor = scale_factors[0]
        else:
            tuple_list = [mmcv.imresize(
                img, scale, return_scale=True) for img in img_group]
            img_group, w_scales, h_scales = list(zip(*tuple_list))
            scale_factor = np.array([w_scales[0], h_scales[0],
                                     w_scales[0], h_scales[0]],
                                    dtype=np.float32)

        # 2. crop (if necessary)
        if crop_history is not None:
            self.op_crop = GroupCrop(crop_history)
        if self.op_crop is not None:
            img_group, crop_quadruple = self.op_crop(
                img_group, is_flow=is_flow)
        else:
            crop_quadruple = None

        img_shape = img_group[0].shape
        # 3. flip
        if flip:
            img_group = [mmcv.imflip(img) for img in img_group]
        if is_flow:
            for i in range(0, len(img_group), 2):
                img_group[i] = mmcv.iminvert(img_group[i])
        # 4a. div_255
        if div_255:
            img_group = [mmcv.imnormalize(img, 0, 255, False)
                         for img in img_group]
        # 4. normalize
        img_group = [mmcv.imnormalize(
            img, self.mean, self.std, self.to_rgb) for img in img_group]
        # 5. pad
        if self.size_divisor is not None:
            img_group = [mmcv.impad_to_multiple(
                img, self.size_divisor) for img in img_group]
            pad_shape = img_group[0].shape
        else:
            pad_shape = img_shape
        if is_flow:
            assert len(img_group[0].shape) == 2
            img_group = [np.stack((flow_x, flow_y), axis=2)
                         for flow_x, flow_y in zip(
                             img_group[0::2], img_group[1::2])]
        # 6. transpose
        img_group = [img.transpose(2, 0, 1) for img in img_group]

        # Stack into numpy.array
        img_group = np.stack(img_group, axis=0)
        return img_group, img_shape, pad_shape, scale_factor, crop_quadruple
Exemplo n.º 5
0
    def __call__(self, results):
        img_group = results['img_group']
        img_h, img_w = img_group[0].shape[:2]
        crop_w, crop_h = self.crop_size
        # assert crop_h == img_h or crop_w == img_w

        if crop_h == img_h:
            w_step = (img_w - crop_w) // 2
            offsets = [
                (0, 0),  # left
                (2 * w_step, 0),  # right
                (w_step, 0),  # middle
            ]
        elif crop_w == img_w:
            h_step = (img_h - crop_h) // 2
            offsets = [
                (0, 0),  # top
                (0, 2 * h_step),  # down
                (0, h_step),  # middle
            ]
        else:
            w_step = (img_w - crop_w) // 4
            h_step = (img_h - crop_h) // 4

            offsets = list()
            offsets.append((0 * w_step, 2 * h_step))  # left
            offsets.append((4 * w_step, 2 * h_step))  # right
            offsets.append((2 * w_step, 2 * h_step))  # center

        oversample_group = list()
        for o_w, o_h in offsets:
            normal_group = list()
            flip_group = list()
            for i, img in enumerate(img_group):
                crop = mmcv.imcrop(
                    img,
                    np.array([o_w, o_h, o_w + crop_w - 1, o_h + crop_h - 1]))
                normal_group.append(crop)
                flip_crop = mmcv.imflip(crop)

                if results['modality'] == 'Flow' and i % 2 == 0:
                    flip_group.append(mmcv.iminvert(flip_crop))
                else:
                    flip_group.append(flip_crop)

            oversample_group.extend(normal_group)

        results['img_group'] = oversample_group
        results['crop_bbox'] = None
        results['img_shape'] = results['img_group'][0].shape

        return results
Exemplo n.º 6
0
    def __call__(self, results):
        img_group = results['img_group']
        flip = True if np.random.rand() < self.flip_ratio else False
        if flip:
            img_group = [mmcv.imflip(img, self.direction) for img in img_group]
        if results['modality'] == 'Flow':
            for i in range(0, len(img_group), 2):
                img_group[i] = mmcv.iminvert(img_group[i])

        results['flip'] = flip
        results['flip_direction'] = self.direction
        results['img_group'] = img_group

        return results
Exemplo n.º 7
0
    def __call__(self, results):
        """Performs the Flip augmentation.

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        """
        _init_lazy_if_proper(results, self.lazy)
        modality = results['modality']
        if modality == 'Flow':
            assert self.direction == 'horizontal'

        if np.random.rand() < self.flip_ratio:
            flip = True
        else:
            flip = False

        results['flip'] = flip
        results['flip_direction'] = self.direction

        if not self.lazy:
            if flip:
                for i, img in enumerate(results['imgs']):
                    mmcv.imflip_(img, self.direction)
                lt = len(results['imgs'])
                for i in range(0, lt, 2):
                    # flow with even indexes are x_flow, which need to be
                    # inverted when doing horizontal flip
                    if modality == 'Flow':
                        results['imgs'][i] = mmcv.iminvert(results['imgs'][i])

            else:
                results['imgs'] = list(results['imgs'])
        else:
            lazyop = results['lazy']
            if lazyop['flip']:
                raise NotImplementedError('Use one Flip please')
            lazyop['flip'] = flip
            lazyop['flip_direction'] = self.direction

        return results
Exemplo n.º 8
0
    def __call__(self, img_group, is_flow=False):

        image_h = img_group[0].shape[0]
        image_w = img_group[0].shape[1]
        crop_w, crop_h = self.crop_size
        assert crop_h == image_h or crop_w == image_w

        if crop_h == image_h:
            w_step = (image_w - crop_w) // 2
            offsets = list()
            offsets.append((0, 0))  # left
            offsets.append((2 * w_step, 0))  # right
            offsets.append((w_step, 0))  # middle
        elif crop_w == image_w:
            h_step = (image_h - crop_h) // 2
            offsets = list()
            offsets.append((0, 0))  # top
            offsets.append((0, 2 * h_step))  # down
            offsets.append((0, h_step))  # middle

        oversample_group = list()
        for o_w, o_h in offsets:
            normal_group = list()
            flip_group = list()
            for i, img in enumerate(img_group):
                crop = mmcv.imcrop(
                    img,
                    np.array([o_w, o_h, o_w + crop_w - 1, o_h + crop_h - 1]))
                normal_group.append(crop)
                flip_crop = mmcv.imflip(crop)

                if is_flow and i % 2 == 0:
                    flip_group.append(mmcv.iminvert(flip_crop))
                else:
                    flip_group.append(flip_crop)

            oversample_group.extend(normal_group)
            # oversample_group.extend(flip_group)
        return oversample_group, None
Exemplo n.º 9
0
 def test_iminvert(self):
     img = np.array([[0, 128, 255], [1, 127, 254], [2, 129, 253]],
                    dtype=np.uint8)
     img_r = np.array([[255, 127, 0], [254, 128, 1], [253, 126, 2]],
                      dtype=np.uint8)
     assert_array_equal(mmcv.iminvert(img), img_r)
Exemplo n.º 10
0
    def test_flip(self):
        with pytest.raises(ValueError):
            # direction must be in ['horizontal', 'vertical']
            Flip(direction='vertically')

        target_keys = ['imgs', 'flip_direction', 'modality']

        # do not flip imgs.
        imgs = list(np.random.rand(2, 64, 64, 3))
        results = dict(imgs=copy.deepcopy(imgs), modality='RGB')
        flip = Flip(flip_ratio=0, direction='horizontal')
        flip_results = flip(results)
        assert assert_dict_has_keys(flip_results, target_keys)
        assert np.array_equal(imgs, results['imgs'])
        assert id(flip_results['imgs']) == id(results['imgs'])
        assert np.shape(flip_results['imgs']) == np.shape(imgs)

        # always flip imgs horizontally.
        imgs = list(np.random.rand(2, 64, 64, 3))
        results = dict(imgs=copy.deepcopy(imgs), modality='RGB')
        results['gt_bboxes'] = np.array([[0, 0, 60, 60]])
        results['proposals'] = np.array([[0, 0, 60, 60]])
        flip = Flip(flip_ratio=1, direction='horizontal')
        flip_results = flip(results)
        assert assert_dict_has_keys(flip_results, target_keys)
        if flip_results['flip'] is True:
            assert check_flip(imgs, flip_results['imgs'],
                              flip_results['flip_direction'])
        assert id(flip_results['imgs']) == id(results['imgs'])
        assert np.shape(flip_results['imgs']) == np.shape(imgs)

        # flip flow images horizontally
        imgs = [
            np.arange(16).reshape(4, 4).astype(np.float32),
            np.arange(16, 32).reshape(4, 4).astype(np.float32)
        ]
        results = dict(imgs=copy.deepcopy(imgs), modality='Flow')
        flip = Flip(flip_ratio=1, direction='horizontal')
        flip_results = flip(results)
        assert assert_dict_has_keys(flip_results, target_keys)
        imgs = [x.reshape(4, 4, 1) for x in imgs]
        flip_results['imgs'] = [
            x.reshape(4, 4, 1) for x in flip_results['imgs']
        ]
        if flip_results['flip'] is True:
            assert check_flip([imgs[0]],
                              [mmcv.iminvert(flip_results['imgs'][0])],
                              flip_results['flip_direction'])
            assert check_flip([imgs[1]], [flip_results['imgs'][1]],
                              flip_results['flip_direction'])
        assert id(flip_results['imgs']) == id(results['imgs'])
        assert np.shape(flip_results['imgs']) == np.shape(imgs)

        # always flip imgs vertivally.
        imgs = list(np.random.rand(2, 64, 64, 3))
        results = dict(imgs=copy.deepcopy(imgs), modality='RGB')
        flip = Flip(flip_ratio=1, direction='vertical')
        flip_results = flip(results)
        assert assert_dict_has_keys(flip_results, target_keys)
        if flip_results['flip'] is True:
            assert check_flip(imgs, flip_results['imgs'],
                              flip_results['flip_direction'])
        assert id(flip_results['imgs']) == id(results['imgs'])
        assert np.shape(flip_results['imgs']) == np.shape(imgs)

        assert repr(flip) == (f'{flip.__class__.__name__}'
                              f'(flip_ratio={1}, direction=vertical, '
                              f'flip_label_map={None}, lazy={False})')

        # transform label for the flipped image with the specific label.
        _flip_label_map = {4: 6}
        imgs = list(np.random.rand(2, 64, 64, 3))

        # the label should be mapped.
        results = dict(imgs=copy.deepcopy(imgs), modality='RGB', label=4)
        flip = Flip(flip_ratio=1,
                    direction='horizontal',
                    flip_label_map=_flip_label_map)
        flip_results = flip(results)
        assert results['label'] == 6

        # the label should not be mapped.
        results = dict(imgs=copy.deepcopy(imgs), modality='RGB', label=3)
        flip = Flip(flip_ratio=1,
                    direction='horizontal',
                    flip_label_map=_flip_label_map)
        flip_results = flip(results)
        assert results['label'] == 3
Exemplo n.º 11
0
    def __call__(self,
                 img_group,
                 scale,
                 crop_history=None,
                 flip=False,
                 keep_ratio=True,
                 div_255=False,
                 is_flow=False,
                 interpolation='bilinear',
                 normalize=True,
                 more_aug=False):
        # 1. rescale
        if keep_ratio:
            tuple_list = [
                mmcv.imrescale(img,
                               scale,
                               return_scale=True,
                               interpolation=interpolation)
                for img in img_group
            ]
            img_group, scale_factors = list(zip(*tuple_list))
            scale_factor = scale_factors[0]
        else:
            tuple_list = [
                mmcv.imresize(img,
                              scale,
                              return_scale=True,
                              interpolation=interpolation) for img in img_group
            ]
            img_group, w_scales, h_scales = list(zip(*tuple_list))
            scale_factor = np.array(
                [w_scales[0], h_scales[0], w_scales[0], h_scales[0]],
                dtype=np.float32)

        # 2. crop (if necessary)
        if crop_history is not None:
            self.op_crop = GroupCrop(crop_history,
                                     input_size=self.crop_size,
                                     resize=True)
        if self.op_crop is not None and isinstance(
                self.op_crop, (GroupCrop, GroupMultiScaleCrop)):
            img_group, crop_quadruple = self.op_crop(
                img_group, is_flow=is_flow, interpolation=interpolation)
        elif self.op_crop is not None:
            img_group, crop_quadruple = self.op_crop(img_group,
                                                     is_flow=is_flow)
        else:
            crop_quadruple = None

        img_shape = img_group[0].shape

        if more_aug:
            seq = iaa.Sequential([
                iaa.GaussianBlur(sigma=np.random.uniform(0, 5)),
                iaa.AdditiveGaussianNoise(loc=0,
                                          scale=(0.0, 0.05 * 255),
                                          per_channel=0.5),
            ])
            img_group = seq(images=np.array(img_group))

        # 3. flip
        if flip:
            img_group = [mmcv.imflip(img) for img in img_group]
        if is_flow:
            for i in range(0, len(img_group), 2):
                img_group[i] = mmcv.iminvert(img_group[i])
        # 4a. div_255
        if div_255:
            img_group = [
                mmcv.imnormalize(img, 0, 255, False) for img in img_group
            ]
        # 4. normalize
        if normalize:
            img_group = [
                mmcv.imnormalize(img, self.mean, self.std, self.to_rgb)
                for img in img_group
            ]
        # 5. pad
        if self.size_divisor is not None:
            img_group = [
                mmcv.impad_to_multiple(img, self.size_divisor)
                for img in img_group
            ]
            pad_shape = img_group[0].shape
        else:
            pad_shape = img_shape
        if is_flow:
            assert len(img_group[0].shape) == 2
            img_group = [
                np.stack((flow_x, flow_y), axis=2)
                for flow_x, flow_y in zip(img_group[0::2], img_group[1::2])
            ]
        # 6. transpose
        if len(img_shape) == 2:
            img_group = [img[:, :, np.newaxis] for img in img_group]
        img_group = [img.transpose(2, 0, 1) for img in img_group]

        # Stack into numpy.array
        img_group = np.stack(img_group, axis=0)
        return img_group, img_shape, pad_shape, scale_factor, crop_quadruple
Exemplo n.º 12
0
    def __call__(self,
                 img_group,
                 scale,
                 crop_history=None,
                 flip=False,
                 keep_ratio=True,
                 div_255=False,
                 is_flow=False,
                 image_name=None):

        if self.resize_crop or self.rescale_crop:
            if self.afterdetect_resize:
                img_group = [
                    cv2.resize(img, self.afterdetect_resize)
                    for img in img_group
                ]
            img_group, crop_quadruple = self.op_crop(img_group)
            img_shape = img_group[0].shape
            scale_factor = None
        else:
            # 1. rescale
            if keep_ratio:
                tuple_list = [
                    mmcv.imrescale(img, scale, return_scale=True)
                    for img in img_group
                ]
                img_group, scale_factors = list(zip(*tuple_list))
                scale_factor = scale_factors[0]
            else:
                tuple_list = [
                    mmcv.imresize(img, scale, return_scale=True)
                    for img in img_group
                ]
                img_group, w_scales, h_scales = list(zip(*tuple_list))
                scale_factor = np.array(
                    [w_scales[0], h_scales[0], w_scales[0], h_scales[0]],
                    dtype=np.float32)
            if self.pre_mean_volume is not None:
                volume_len = self.pre_mean_volume.shape[0]
                img_group = [
                    img - self.pre_mean_volume[i % volume_len, ...]
                    for i, img in enumerate(img_group)
                ]
            # 2. crop (if necessary)
            if crop_history is not None:
                self.op_crop = GroupCrop(crop_history)
            if self.op_crop is not None:
                img_group, crop_quadruple = self.op_crop(img_group,
                                                         is_flow=is_flow)
            else:
                crop_quadruple = None

            img_shape = img_group[0].shape
        # import matplotlib.pyplot as plt
        # import os
        # for i in range(len(img_group)):
        #     plt.imshow(img_group[i])
        #     save_dir = os.path.join("show_fig",image_name.split("/")[-1])
        #     if not os.path.exists(save_dir):
        #         os.mkdir(save_dir)
        #     plt.savefig(os.path.join(save_dir,"{}".format(i)))
        # 3. flip
        if flip:
            img_group = [mmcv.imflip(img) for img in img_group]
        if is_flow:
            for i in range(0, len(img_group), 2):
                img_group[i] = mmcv.iminvert(img_group[i])
        # 4a. div_255
        if div_255:
            img_group = [
                mmcv.imnormalize(img, 0, 255, False) for img in img_group
            ]
        # 4. normalize
        img_group = [
            mmcv.imnormalize(img, self.mean, self.std, self.to_rgb)
            for img in img_group
        ]
        # 5. pad
        if self.size_divisor is not None:
            img_group = [
                mmcv.impad_to_multiple(img, self.size_divisor)
                for img in img_group
            ]
            pad_shape = img_group[0].shape
        else:
            pad_shape = img_shape
        if is_flow:
            assert len(img_group[0].shape) == 2
            img_group = [
                np.stack((flow_x, flow_y), axis=2)
                for flow_x, flow_y in zip(img_group[0::2], img_group[1::2])
            ]
        # 6. transpose
        img_group = [img.transpose(2, 0, 1) for img in img_group]

        # Stack into numpy.array
        img_group = np.stack(img_group, axis=0)
        return img_group, img_shape, pad_shape, scale_factor, crop_quadruple
Exemplo n.º 13
0
    def test_flip(self):
        with pytest.raises(ValueError):
            # direction must be in ['horizontal', 'vertical']
            Flip(direction='vertically')

        target_keys = ['imgs', 'flip_direction', 'modality']

        # do not flip imgs.
        imgs = list(np.random.rand(2, 64, 64, 3))
        results = dict(imgs=copy.deepcopy(imgs), modality='RGB')
        flip = Flip(flip_ratio=0, direction='horizontal')
        flip_results = flip(results)
        assert self.check_keys_contain(flip_results.keys(), target_keys)
        assert np.array_equal(imgs, results['imgs'])
        assert id(flip_results['imgs']) == id(results['imgs'])
        assert np.shape(flip_results['imgs']) == np.shape(imgs)

        # always flip imgs horizontally.
        imgs = list(np.random.rand(2, 64, 64, 3))
        results = dict(imgs=copy.deepcopy(imgs), modality='RGB')
        flip = Flip(flip_ratio=1, direction='horizontal')
        flip_results = flip(results)
        assert self.check_keys_contain(flip_results.keys(), target_keys)
        if flip_results['flip'] is True:
            assert self.check_flip(imgs, flip_results['imgs'],
                                   flip_results['flip_direction'])
        assert id(flip_results['imgs']) == id(results['imgs'])
        assert np.shape(flip_results['imgs']) == np.shape(imgs)

        # flip flow images horizontally
        imgs = [
            np.arange(16).reshape(4, 4).astype(np.float32),
            np.arange(16, 32).reshape(4, 4).astype(np.float32)
        ]
        results = dict(imgs=copy.deepcopy(imgs), modality='Flow')
        flip = Flip(flip_ratio=1, direction='horizontal')
        flip_results = flip(results)
        assert self.check_keys_contain(flip_results.keys(), target_keys)
        imgs = [x.reshape(4, 4, 1) for x in imgs]
        flip_results['imgs'] = [
            x.reshape(4, 4, 1) for x in flip_results['imgs']
        ]
        if flip_results['flip'] is True:
            assert self.check_flip([imgs[0]],
                                   [mmcv.iminvert(flip_results['imgs'][0])],
                                   flip_results['flip_direction'])
            assert self.check_flip([imgs[1]], [flip_results['imgs'][1]],
                                   flip_results['flip_direction'])
        assert id(flip_results['imgs']) == id(results['imgs'])
        assert np.shape(flip_results['imgs']) == np.shape(imgs)

        # always flip imgs vertivally.
        imgs = list(np.random.rand(2, 64, 64, 3))
        results = dict(imgs=copy.deepcopy(imgs), modality='RGB')
        flip = Flip(flip_ratio=1, direction='vertical')
        flip_results = flip(results)
        assert self.check_keys_contain(flip_results.keys(), target_keys)
        if flip_results['flip'] is True:
            assert self.check_flip(imgs, flip_results['imgs'],
                                   flip_results['flip_direction'])
        assert id(flip_results['imgs']) == id(results['imgs'])
        assert np.shape(flip_results['imgs']) == np.shape(imgs)

        assert repr(flip) == (f'{flip.__class__.__name__}'
                              f'(flip_ratio={1}, direction=vertical, '
                              f'lazy={False})')