Пример #1
0
    def __init__(self, crop_size, keys=['imgs'], cat_max_ratio=1.0):
        self.keys = keys
        if len(keys) < 1:
            raise ValueError(f'please assign keys that you want to apply to.')
        elif len(keys) == 1:
            if is_list_of(crop_size, int):
                self.crop_size = [crop_size]
            elif is_list_of(crop_size, list) and len(crop_size) == 1:
                self.crop_size = crop_size
            else:
                raise TypeError(
                    f'parameter "crop_size" must be type of "list of int" or '
                    f'"list of one list" when there is only one key, please check it.'
                )
        else:
            if is_list_of(crop_size, list) and len(crop_size) == len(keys):
                self.crop_size = crop_size
            elif is_list_of(crop_size, int) and len(crop_size) == 1:
                self.crop_size = [crop_size for _ in range(len(keys))]
            else:
                raise TypeError(
                    f'parameter "crop_size" must be type of "list of list" and '
                    f'have the same length as "keys".')

        self.cat_max_ratio = cat_max_ratio
Пример #2
0
    def __init__(self, keys, order):
        if not isinstance(keys, list):
            raise TypeError(
                f'parameter "keys" should be type of list, '
                f'but got {type(keys)}.')
        else:
            self.keys = keys

        if is_list_of(order, list):
            self.order = order
        elif is_list_of(order, int):
            self.order = [order for _ in range(len(keys))]
        else:
            raise TypeError(
                f'parameter "order" should be type of list, '
                f'but got {type(order)}.')
Пример #3
0
 def __init__(self,
              keys=['imgs'],
              img_scale=[],
              border_color=[127.5, 127.5, 127.5],
              interpolation='area',
              with_box=False,
              box_key=None):
     self.keys = keys
     if len(keys) < 1:
         raise ValueError(f'please assign keys that you want to apply to.')
     elif len(keys) == 1:
         if is_list_of(img_scale, int):
             self.img_scale = [img_scale]
         elif is_list_of(img_scale, list) and len(img_scale) == 1:
             self.img_scale = img_scale
         else:
             raise TypeError(
                 f'paramater "img_scale" must be type of "list of list" or '
                 f'"list of int" when there is only one key.')
         if isinstance(interpolation, str):
             self.interpolation = [interpolation]
         elif is_list_of(interpolation, str) and \
             len(interpolation) == len(keys):
             self.interpolation = interpolation
         else:
             raise TypeError(
                 f'parameter "interpolation" must be type of "list of str" '
                 f'or "str" when there is only one key.')
     else:
         if is_list_of(img_scale, list) and len(img_scale) == len(keys):
             self.img_scale = img_scale
         else:
             raise TypeError(
                 f'parameter "img_scale" must be type of "list of list" '
                 f'with same length as "keys" when there are more '
                 f'than one keys.')
         if is_list_of(interpolation, str) and \
             len(interpolation) == len(keys):
             self.interpolation = interpolation
         else:
             raise TypeError(
                 f'parameter "interpolation" must be type of "list of str" '
                 f'with some length as "keys" when there are more '
                 f'than one keys.')
     self.border_color = border_color
     self.with_box = with_box
     self.box_key = box_key
Пример #4
0
 def __call__(self, results):
     # assert results.get(self.out_keys) is None
     assert len(self.zip_keys) > 0 and is_list_of(self.zip_keys, str)
     data = {}
     for key in self.zip_keys:
         assert results.get(key) is not None
         data[key] = results[key]
     results[self.out_keys] = data
     return results
Пример #5
0
    def random_sample(img_scales):

        assert is_list_of(img_scales, list) and len(img_scales) == 2
        img_scale_long = [max(s) for s in img_scales]
        img_scale_short = [min(s) for s in img_scales]
        long_edge = np.random.randint(min(img_scale_long),
                                      max(img_scale_long) + 1)
        short_edge = np.random.randint(min(img_scale_short),
                                       max(img_scale_short) + 1)
        img_scale = (long_edge, short_edge)
        return img_scale, None
Пример #6
0
    def __init__(self,
                 keys=['imgs'],
                 flip_ratio=0.5,
                 direction='horizontal',
                 with_box=False,
                 box_key=None,
                 restore_size=False,
                 begin_idx=0):
        self.keys = keys
        if len(keys) < 1:
            raise ValueError(f'please assign keys that you want to apply to.')
        elif len(keys) == 1:
            if isinstance(direction, str):
                self.direction = [direction]
            elif is_list_of(direction, str) and len(direction) == 1:
                self.direction = direction
            else:
                raise TypeError(
                    f'parameter "direction" must be type of "list of str" or '
                    f'"str" when there is only one key, please check for it.')
        else:
            if is_list_of(direction, str) and len(direction) == len(keys):
                self.direction = direction
            else:
                raise TypeError(
                    f'parameter "direction" must be type of "list of str" and '
                    f'have the same length as "keys".')

        for item in self.direction:
            assert item in ['horizontal', 'vertical']
        self.with_box = with_box
        self.box_key = box_key
        self.begin_idx = begin_idx
        self.restore_size = restore_size

        if np.random.rand() < flip_ratio:
            self.flip = True
        else:
            self.flip = False
Пример #7
0
    def __init__(self,
                 key='imgs',
                 img_scale=None,
                 multiscale_mode='range',
                 ratio_range=None,
                 interpolation='bilinear',
                 keep_ratio=True,
                 rescale_edge='max',
                 with_box=False,
                 output_stride=None,
                 box_key='gt_bbox',
                 begin_idx=0):
        if img_scale is None:
            self.img_scale = None
        else:
            if is_list_of(img_scale, list):
                self.img_scale = img_scale
            else:
                self.img_scale = [img_scale]
            assert is_list_of(self.img_scale, list)

        if ratio_range is not None:
            # mode 1: given a scale and a range of image ratio
            assert len(self.img_scale) == 1
        else:
            # mode 2: given multiple scales or a range of scales
            assert multiscale_mode in ['value', 'range']

        self.multiscale_mode = multiscale_mode
        self.ratio_range = ratio_range
        self.keep_ratio = keep_ratio
        self.rescale_edge = rescale_edge
        self.interpolation = interpolation
        self.key = key
        self.with_box = with_box
        self.output_stride = output_stride
        self.box_key = box_key
        self.begin_idx = begin_idx
Пример #8
0
def build_module_from_registers(cfg, module_name=None, sub_cfg=None):
    if cfg is None:
        return None
    if module_name is None:
        key = list(cfg.keys())
        assert len(key) == 1
        module_name = key[0]
        module_cfg = copy.deepcopy(cfg[module_name])
    else:
        module_cfg = copy.deepcopy(cfg)
    if not hasattr(Registers, module_name):
        # TODO: the module_name must be inside the Registers!!!
        if module_name == 'train_cfg' or module_name == 'test_cfg':
            return module_cfg
        else:
            raise ValueError(f'No module {module_name} found in Registers.')
    if is_list_of(module_cfg, dict):
        module = nn.ModuleList()
        for item in module_cfg:
            register = getattr(Registers, module_name)
            if item.get('type') is not None:
                module_type = item.pop('type')
            else:
                raise ValueError(f'{item} should include key "type".')
            if sub_cfg is not None:
                item = merge_subcfg(item, sub_cfg)
            module.append(register[module_type](**item))
    elif isinstance(module_cfg, dict):
        register = getattr(Registers, module_name)
        if module_cfg.get('type') is not None:
            module_type = module_cfg.pop('type')
        else:
            raise ValueError(f'{module_cfg} should include key "type".')
        if sub_cfg is not None:
            module_cfg = merge_subcfg(module_cfg, sub_cfg)
        module = register[module_type](**module_cfg)
    else:
        raise ValueError(
            f'"module_cfg" must be type of "list of dict" or "dict"')

    return module
Пример #9
0
    def __init__(self, keys=['imgs'], size=None, pad_val=0):

        self.keys = keys
        if len(keys) < 1:
            raise ValueError(f'please assign keys that you want to apply to.')
        elif len(keys) == 1:
            if size is None or is_list_of(size, int):
                self.size = [size]
            elif is_list_of_list_or_nontype(size) and len(size) == 1:
                self.size = size
            else:
                raise TypeError(
                    f'parameter "size" must be type of "list of one int", "None", '
                    f'"list of one list" or "list of None" when '
                    f'there is only one key, please check for it.')

            if isinstance(pad_val, int):
                self.pad_val = [pad_val]
            elif is_list_of(pad_val, int) and len(pad_val) != 1:
                self.pad_val = [pad_val]
            elif is_list_of(pad_val, list) and len(pad_val) == 1:
                self.pad_val = pad_val
            elif is_list_of(pad_val, int) and len(pad_val) == 1:
                self.pad_val = pad_val
            else:
                raise TypeError(
                    f'parameter "pad_val" must be type of "list of one int", '
                    f'"list of list", "list of one list" or "int" when '
                    f'there is only one key, please check for it.')
        else:
            if is_list_of(size, list) and len(size) == len(keys):
                self.size = size
            else:
                raise TypeError(
                    f'parameter "size" must be type of "list of list" and '
                    f'have the same length as "keys".')
            if is_list_of(pad_val, list) and len(pad_val) == len(keys):
                self.pad_val = [
                    item[0] if len(item) == 1 else item for item in pad_val
                ]
            else:
                raise TypeError(
                    f'parameter "pad_val" must be type of "list of list" and '
                    f'have the same length as "keys".')
Пример #10
0
    def __init__(self, mean, std, keys=['imgs'], to_rgb=True):
        self.keys = keys
        if len(keys) < 1:
            raise ValueError(f'please assign keys that you want to apply to.')
        elif len(keys) == 1:
            if isinstance(mean,
                          (float, int)) or is_list_of(mean, (float, int)):
                self.mean = [mean]
            elif is_list_of(mean, list) and len(mean) == 1:
                self.mean = mean
            else:
                raise TypeError(
                    f'parameter "mean" must be type of "list of int(float)", '
                    f'"float", "int" or "list of one list" when '
                    f'there is only one key, please check for it.')
            if isinstance(std, (float, int)) or is_list_of(std, (float, int)):
                self.std = [std]
            elif is_list_of(std, list) and len(std) == 1:
                self.std = std
            else:
                raise TypeError(
                    f'parameter "std" must be type of "list of int(float)", '
                    f'"float", "int" or "list of one list" when '
                    f'there is only one key, please check for it.')
        else:
            if is_list_of(mean, list) and len(mean) == len(keys):
                self.mean = mean
            else:
                raise TypeError(
                    f'parameter "mean" must be type of "list of list" and '
                    f'have the same length as "keys".')
            if is_list_of(std, list) and len(std) == len(keys):
                self.std = std
            else:
                raise TypeError(
                    f'parameter "std" must be type of "list of list" and '
                    f'have the same length as "keys".')

        self.mean = np.array(self.mean, dtype=np.float32)
        self.std = np.array(self.std, dtype=np.float32)
        self.to_rgb = to_rgb
Пример #11
0
    def __init__(self,
                 keys=['imgs'],
                 degrees=[-10, 10],
                 translate=[0.1, 0.1],
                 scale=[0.9, 1.1],
                 shear=[-2, 2],
                 border_value=[127.5, 127.5, 127.5],
                 with_box=False,
                 box_key=None,
                 to_xywh=False):
        self.keys = keys
        if len(keys) < 1:
            raise TypeError(f'please assign keys that you want to apply to.')
        elif len(keys) == 1:
            if is_list_of(degrees, int) or is_list_of(degrees, float):
                self.degrees = [degrees]
            elif is_list_of(degrees, list) and len(degrees) == len(keys):
                self.degrees = degrees
            else:
                raise TypeError(
                    f'parameter "degrees" must be type of "list of int(float)", '
                    f'"list of one list" when there is only one key, '
                    f'please check for it.')
            if is_list_of(translate, int) or is_list_of(translate, float):
                self.translate = [translate]
            elif is_list_of(translate, list) and len(translate) == len(keys):
                self.translate = translate
            else:
                raise TypeError(
                    f'parameter "translate" must be type of "list of int(float)", '
                    f'"list of one list" when there is only one key, '
                    f'please check for it.')
            if is_list_of(scale, int) or is_list_of(scale, float):
                self.scale = [scale]
            elif is_list_of(scale, list) and len(scale) == len(keys):
                self.scale = scale
            else:
                raise TypeError(
                    f'parameter "scale" must be type of "list of int(float)", '
                    f'"list of one list" when there is only one key, '
                    f'please check for it.')
            if is_list_of(shear, int) or is_list_of(shear, float):
                self.shear = [shear]
            elif is_list_of(shear, list) and len(shear) == len(keys):
                self.shear = shear
            else:
                raise TypeError(
                    f'parameter "shear" must be type of "list of int(float)", '
                    f'"list of one list" when there is only one key, '
                    f'please check for it.')
        else:
            if is_list_of(degrees, list) and len(degrees) == len(keys):
                self.degress = degrees
            else:
                raise TypeError(
                    f'parameter "degrees" must be type of "list of list" and '
                    f'have the same length as "keys".')
            if is_list_of(scale, list) and len(scale) == len(scale):
                self.scale = scale
            else:
                raise TypeError(
                    f'parameter "scale" must be type of "list of list" and '
                    f'have the same length as "keys".')
            if is_list_of(translate, list) and len(translate) == len(keys):
                self.translate = translate
            else:
                raise TypeError(
                    f'parameter "translate" must be type of "list of list" and '
                    f'have the same length as "keys".')
            if is_list_of(shear, list) and len(shear) == len(shear):
                self.shear = shear
            else:
                raise TypeError(
                    f'parameter "shear must be type of "list of list" and '
                    f'have the same length as "keys".')

        self.border_value = border_value
        self.with_box = with_box
        self.box_key = box_key
        self.to_xywh = to_xywh
Пример #12
0
    def random_select(img_scales):

        assert is_list_of(img_scales, list)
        scale_idx = np.random.randint(len(img_scales))
        img_scale = img_scales[scale_idx]
        return img_scale, scale_idx