albumentations icon indicating copy to clipboard operation
albumentations copied to clipboard

New aliases for bbox formats (#448)

Open katsura-jp opened this issue 5 years ago • 7 comments

Following #448 additional formats

  • xyxy
  • xywh
  • xywh_center_norm
  • xyxy_norm
  • xywh_norm
  • xywh_center

katsura-jp avatar Oct 30 '19 18:10 katsura-jp

@katsura-jp

Thanks for your PR! Can you please updated your PR such that we use UPPERCASE_NAMES for all bboxes formats and use them everywhere in code. It seems we are having to much repetition of "xyxy", "xywh" etc in the code which is error prone.

E.g declare what boxes we have as follows:

BBOXES_FORMAT_COCO = "coco"
BBOXES_FORMAT_PASCAL_VOC= "coco"
BBOXES_FORMAT_PASCAL_VOC = "yolo"
...

And use these constants elsewhere

BloodAxe avatar Nov 01 '19 09:11 BloodAxe

@BloodAxe

Thank you for your comment. In which file should UPPERCASE_NAMES be written? Is it all right with albumentations/augmentations/bbox_utils.py?

katsura-jp avatar Nov 02 '19 04:11 katsura-jp

If we stop supporting python2, we can use Enum for this PR:

Create Enum Class:

from enum import Enum

class BboxesFormat(Enum):
    COCO = 'coco'
    PASCAL_VOC = 'pascal_voc'
    YOLO = 'yolo'
    XYXY = 'xyxy'
    XYWH = 'xywh'
    XYWH_CENTER_NORM = 'xywh_center_norm'
    XYXY_NORM = 'xyxy_norm'
    XYWH_NORM = 'xywh_norm'
    XYWH_CENTER = 'xywh_center'

And then check:

if BboxesFormat(target_format) in {BboxesFormat.COCO, BboxesFormat.XYXY}: ...

And we don't need check:

if target_format not in {"coco", "pascal_voc", "yolo"}:

Our code raise ValueError in this place: BboxesFormat(target_format).

maruschin avatar Nov 05 '19 05:11 maruschin

@katsura-jp simple solution is:

BBOX_FORMAT_COCO = {'coco", 'xywh'}
BBOX_FORMAT_PASCAL_VOC = {'pascal_voc', 'xyxy'}
BBOX_FORMAT_ALBUMENTATIONS = {'albumentations', 'xyxy_norm'}
BBOX_FORMAT_YOLO = {'yolo', 'xywh_center_norm'}
BBOX_FORMAT_XYWH_NORM = {'xywh_norm'}
BBOX_FORMAT_XYWH_CENTER = {'xywh_center'}


BBOX_FORMATS = (
    BBOX_FORMAT_COCO
    | BBOX_FORMAT_PASCAL_VOC
    | BBOX_FORMAT_ALBUMENTATIONS
    | BBOX_FORMAT_YOLO
    | BBOX_FORMAT_XYWH_NORM
    | BBOX_FORMAT_XYWH_CENTER
)

And then working with constants:

if target_format not in BBOX_FORMATS: ...
if target_format in BBOX_FORMAT_COCO | BBOX_FORMAT_XYWH_NORM: ...

maruschin avatar Nov 05 '19 05:11 maruschin

At this time we support only python 2.7 so we can use Enum class.

@katsura-jp I think, the best solution is to add Enumeration to bbox_utils.py like said @maruschin This changes is similar, but better than what @BloodAxe suggested.

from enum import Enum

class BboxesFormat(Enum):
    COCO = 'coco'
    PASCAL_VOC = 'pascal_voc'
    YOLO = 'yolo'
    XYXY = 'xyxy'
    XYWH = 'xywh'
    XYWH_CENTER_NORM = 'xywh_center_norm'
    XYXY_NORM = 'xyxy_norm'
    XYWH_NORM = 'xywh_norm'
    XYWH_CENTER = 'xywh_center'

    @classmethod
    def has_value(cls, value):
        return value in cls._value2member_map_

Dipet avatar Nov 05 '19 12:11 Dipet

@Dipet, I thought that Enum class is only in python3, I was wrong.

I don't think that we need classmethod like has_value, we can instead:

BboxesFormat.has_value('coco')

Check in another way:

BboxesFormat('coco') in BboxesFormat

In this case, we will have an informative error. If some value is not in the Bbox Enum.

maruschin avatar Nov 05 '19 13:11 maruschin

Please update the PR or it will be closed.

Dipet avatar Jul 29 '22 10:07 Dipet