albumentations-team / albumentations

Fast and flexible image augmentation library. Paper about the library: https://www.mdpi.com/2078-2489/11/2/125
https://albumentations.ai
MIT License
14.2k stars 1.65k forks source link

When removing small boxes remove masks and key points for the same instance as well #428

Open tatigabru opened 5 years ago

tatigabru commented 5 years ago

📚 Documentation

'A.BboxParams(min_area=min_area...)' removes small boxes. For instance segmentation, it would be handy to remove masks and key points for the same instance as well. In here one may add a list of removed instances: https://github.com/albu/albumentations/blob/master/albumentations/augmentations/bbox_utils.py

removed_instances = []
for num, bbox in enumerate(bboxes):
        transformed_box_area = calculate_bbox_area(bbox, rows, cols)
        bbox[:4] = np.clip(bbox[:4], 0, 1.0)
        clipped_box_area = calculate_bbox_area(bbox, rows, cols)
        if not transformed_box_area or clipped_box_area / transformed_box_area <= min_visibility:
            continue
        else:
            bbox[:4] = np.clip(bbox[:4], 0, 1.0)
        if calculate_bbox_area(bbox, rows, cols) <= min_area:
            removed_instances.append(num) 
            continue
        resulting_boxes.append(bbox)
    return resulting_boxes, removed_instances

And then use it to remove corresponding masks and keypoints. To do it better, it would be nice to have masks also as a set for instance segmentation, to avoid glueing them together on one mask or using additional_targets. Masks, boxes and keypoints are related to the same instances in coco, etc., it would be handy for instance segmentation.

xiongzubiao commented 5 years ago

It would be really nice to have this feature for instance segmentation. I had to set min_area and min_visibility as a negative value to keep all the boxes, since I have no way to figure out which boxes get removed.

xiongzubiao commented 5 years ago

@tatigabru I managed to get the filtered indexes using label_fields:

params = A.BboxParams(format="coco", label_fields=["bbox_ids"]) augumentor = A.Compose(transforms, **params) auged = augumentor(image=image, bboxes=bboxes, bbox_ids=np.arange(len(bboxes)), masks=...)

Then you could use auged["bbox_ids"] to filter masks and keypoints.