detectron2: Training Mask R-CNN using RLE bitmasks error
I am following the Mask R-CNN tutorial and changed the dataset_dict to support segmentation maps in bitmap format using RLE instead of polygons. I confirmed the data is processed properly using detectron2 visualization tool. When trying to train the network, I’m getting an error regarding polygons.
File "/home/ubuntu/detectron2/detectron2/data/detection_utils.py", line 149, in transform_instance_annotations polygons = [np.asarray(p).reshape(-1, 2) for p in annotation["segmentation"]] File "/home/ubuntu/detectron2/detectron2/data/detection_utils.py", line 149, in <listcomp> polygons = [np.asarray(p).reshape(-1, 2) for p in annotation["segmentation"]] ValueError: cannot reshape array of size 1 into shape (2)
It seems there is no support for RLE format during training although the visualization works. Is there any way to train using bitmasks?
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 18 (8 by maintainers)
Commits related to this issue
- support RLE format in dataset dicts (#354) Summary: Fix https://github.com/facebookresearch/detectron2/issues/200 Pull Request resolved: https://github.com/fairinternal/detectron2/pull/354 Test Pla... — committed to facebookresearch/detectron2 by ppwwyyxx 5 years ago
- make Benchmark master (#3) * update docs Summary: Pull Request resolved: https://github.com/fairinternal/detectron2/pull/352 Differential Revision: D19229170 Pulled By: ppwwyyxx fbshipit-... — committed to Oneflow-Inc/detectron2 by jackalcooper 4 years ago
- support RLE in convert_to_coco_dict Summary: fix https://github.com/facebookresearch/detectron2/issues/200 Pull Request resolved: https://github.com/fairinternal/detectron2/pull/391 Reviewed By: rbg... — committed to facebookresearch/detectron2 by ppwwyyxx 4 years ago
- Fix RLE segmentation to json for coco annotation. Summary: I got `TypeError: Object of type 'uint32' is not JSON serializable` for `area`, and `TypeError: Object of type 'bytes' is not JSON serializa... — committed to facebookresearch/detectron2 by siyuanfeng-tri 4 years ago
- support RLE in convert_to_coco_dict Summary: fix https://github.com/facebookresearch/detectron2/issues/200 Pull Request resolved: https://github.com/fairinternal/detectron2/pull/391 Reviewed By: rbg... — committed to SironaMedical/nines-detectron2 by ppwwyyxx 4 years ago
- Fix RLE segmentation to json for coco annotation. Summary: I got `TypeError: Object of type 'uint32' is not JSON serializable` for `area`, and `TypeError: Object of type 'bytes' is not JSON serializa... — committed to SironaMedical/nines-detectron2 by siyuanfeng-tri 4 years ago
- +0.5 for keypoints in COCO (#200) Summary: Discussed in https://github.com/fairinternal/detectron2/issues/195#issuecomment-506832332 Two runs of s1x gave 65.1 & 65.3 respectively Pull Request resolve... — committed to facebookresearch/detectron2 by ppwwyyxx 5 years ago
I think I need to do a bit more to get this actually working.
int
explicitly, otherwise json dump complains aboutTypeError: Object of type 'uint32' is not JSON serializable
https://github.com/facebookresearch/detectron2/blob/master/detectron2/data/datasets/coco.py#L339segmentation["counts"]
into an ascii string otherwise json dump complains about not being able to serialize bytes.TypeError: Object of type 'bytes' is not JSON serializable
Now the default dataloader can work with RLE formats inside your dataset. All you need is:
INPUT.MASK_FORMAT='bitmask'
.Did you find the solution? when I try CenterMask.I also got the same error ‘BitMasks’ object has no attribute ‘polygons’ like you.
Here is my process: Follow detectron2 document: I convert a bitmask dataset to RLE format using pycocotools.mask.encode(np.asarray(mask, order=“F”)) . Then I set cfg.INPUT.MASK_FORMAT=‘bitmask’
`
Thanks for the reply! I am not that familiar with different flavors of coco TBH. The only thing I really cared about here is to preserve the RLE annotation (which comes from pycocotools.mask.encode()) through json conversion and back through detectron2’s wrappers, since I have a custom dataset, where everything is an image.
So I looked briefly around https://github.com/facebookresearch/detectron2/blob/master/detectron2/data/datasets/coco.py#L160 and looked up where
imgToAnns
comes from in https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocotools/coco.py I think my workaround is probably fine for what I care about? As long as json is serializing and deserializing that bytes array the same way I should be fine. I did something likesegmentation["counts"] = segmentation["counts"].decode(''ascii")
before calling json dump.