-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Instance Segmentation Mask/Bbox Relation #1784
Comments
A workarround i've find : def iris2_training(segmentation_item:dict):
transform = A.Compose([
A.LongestMaxSize(max_size=MAX_SIZE),
A.PadIfNeeded(
min_height=MIN_IMG_HEIGHT,
min_width=MIN_IMG_WIDTH,
border_mode=cv2.BORDER_CONSTANT,
value=0,
always_apply=True),
A.HorizontalFlip(),
A.RandomCrop(MIN_IMG_HEIGHT,MIN_IMG_WIDTH),
A.ToFloat(max_value=255),
ToTensorV2()
],
bbox_params=A.BboxParams(format='pascal_voc',label_fields=['labels','ids'],min_visibility=BBOX_MIN_VISIBILITY),
is_check_shapes=False
)
output = transform(
image=segmentation_item['image'],
masks=segmentation_item['masks'],
bboxes=segmentation_item['boxes'],
labels=segmentation_item['labels'],
ids=range(len(segmentation_item['labels']))
)
return dict(
image=output['image'],
boxes=output['bboxes'],
labels=output['labels'],
masks=[output['masks'][i] for i in output['ids']],
name=segmentation_item['name']
) But it should be working without it. |
Thanks for the proposed solution! Yep, we do have this issue that masks, boxes and keypoints and not binded on the instance level. Your approach is the best that I have seen so far for this problem. |
Also came here with the same issue. Thanks for the workaround people :) Although should be expected from such a library to have that by default or at least being able to add it |
@simonebonato how much would you be willing to donate to help to make this happen? |
I can maybe try to solve it myself if I have time. |
Just to keep you aware.... Now, i just apply augmentation to masks and then recompute the bbox coordinates. For me it makes more sense because this way the bbox will match the final augmented mask. To do this, you can use pycocotools. import pycocotools.mask as mask_utils
import numpy as np
def mask_to_bbox(mask:np.ndarray)->np.ndarray:
"""Convert a mask to a bbox.
it is usefull when we apply augmentation on a mask and we would like a precised bbox corresponding to
the transformed mask.
Args:
mask (np.ndarray): _description_
"""
mask_rle = mask_utils.encode(np.asfortranarray(mask>0))
bbox_xywh = mask_utils.toBbox(mask_rle)
# We convert it to a bbox xyxy
bbox_xyxy = (bbox_xywh+np.array([0,0,bbox_xywh[0],bbox_xywh[1]]))
return bbox_xyxy
...
transform_output = transform(
image=segmentation_item['image'],
masks=segmentation_item['masks']
)
transform_output['boxes']=[mask_to_bbox(mask) for mask in masks] |
Yep, how to rotate bounding boxes so that boxes stay tight is an open question. Recomputing masks at the end was always the way to go. We do have function for it
def bbox_from_mask(mask: np.ndarray) -> tuple[int, int, int, int]:
"""Create bounding box from binary mask (fast version)
Args:
mask (numpy.ndarray): binary mask.
Returns:
tuple: A bounding box tuple `(x_min, y_min, x_max, y_max)`.
"""
rows = np.any(mask, axis=1)
if not rows.any():
return -1, -1, -1, -1
cols = np.any(mask, axis=0)
y_min, y_max = np.where(rows)[0][[0, -1]]
x_min, x_max = np.where(cols)[0][[0, -1]]
return x_min, y_min, x_max + 1, y_max + 1 |
Describe the bug
I work on a usecase of instance segmentation with torchvision. In this case, i have :
I'have created an augmentation set with albumentation, something like that :
I would expect that the parameter related to the visibility is applyable at the instance level.
I mean if the bbox is not enough visible, it should remove the corresponding mask.
I tried adding "masks" in the bbox_params :
but in that case it does not apply augmentation on the masks.
To Reproduce
In order to reproduce, you can use the code bellow :
Expected behavior
I would expect that the parameter related to the visibility is applyable at the instance level.
I mean if the bbox is not enough visible, it should remove the corresponding mask.
Actual behavior
If i do not add masks to label_fields, i have inconsistency.
If i do add masks to label_fields, augmentations are not applyed to it.
The text was updated successfully, but these errors were encountered: