Skip to content
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

Training Mask R-CNN using RLE bitmasks error #200

Closed
alono88 opened this issue Oct 30, 2019 · 18 comments
Closed

Training Mask R-CNN using RLE bitmasks error #200

alono88 opened this issue Oct 30, 2019 · 18 comments
Labels
enhancement Improvements or good new features

Comments

@alono88
Copy link

alono88 commented Oct 30, 2019

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?

@alono88 alono88 changed the title Mask R-CNN cannot train using RLE bitmasks Training Mask R-CNN using RLE bitmasks error Oct 30, 2019
@ppwwyyxx
Copy link
Contributor

ppwwyyxx commented Oct 31, 2019

The model does support training with bitmasks, if instances.gt_masks = BitMasks(..). See the BitMasks class at https://detectron2.readthedocs.io/modules/structures.html#detectron2.structures.BitMasks

But the data loader does not recognize RLE, and therefore does not convert RLE to bitmasks.

@ppwwyyxx ppwwyyxx added the enhancement Improvements or good new features label Oct 31, 2019
@ppwwyyxx
Copy link
Contributor

You can use a different dataloader following https://detectron2.readthedocs.io/tutorials/data_loading.html

@alono88
Copy link
Author

alono88 commented Oct 31, 2019

Thanks!

@adriaciurana
Copy link

adriaciurana commented Nov 13, 2019

I'm with the same problem. To solve it I have edited the following files.

In structures/masks.py BitMasks class add the following method:

@staticmethod
    def from_rle(
        rles
    ) -> "BitMasks":
        """
        Args:
            rles
            height, width (int)
        """
        masks = [mask_utils.decode(rle).astype(np.bool) for rle in rles]
        return BitMasks(torch.stack([torch.from_numpy(x) for x in masks]))

In data/detection_utils.py change:

if len(annos) and "segmentation" in annos[0]:
        polygons = [obj["segmentation"] for obj in annos]
        if mask_format == "polygon":
            masks = PolygonMasks(polygons)
        else:
            assert mask_format == "bitmask", mask_format
            masks = BitMasks.from_polygon_masks(polygons, *image_size)
        target.gt_masks = masks

by:

if len(annos) and "segmentation" in annos[0]:
        polygons = [obj["segmentation"] for obj in annos]
        if mask_format == "polygon":
            masks = PolygonMasks(polygons)
        elif mask_format == "bitmask":
            masks = BitMasks.from_polygon_masks(polygons, *image_size)
        elif mask_format == "rle":
            masks = BitMasks.from_rle(polygons)

        else:
            assert mask_format
        target.gt_masks = masks

In also dataset_mapper.py change:

# USER: Implement additional transformations if you have other types of data
            annos = [
                utils.transform_instance_annotations(
                    obj, transforms, image_shape, keypoint_hflip_indices=self.keypoint_hflip_indices
                )
                for obj in dataset_dict.pop("annotations")
                if obj.get("iscrowd", 0) == 0
            ]

by:

annos = [
                obj
                for obj in dataset_dict.pop("annotations")
                if obj.get("iscrowd", 0) == 0
            ]

In your cfg change cfg.INPUT.MASK_FORMAT = 'rle'

I think I have not had to apply more changes.

facebook-github-bot pushed a commit that referenced this issue Dec 26, 2019
Summary:
Fix #200

Pull Request resolved: fairinternal/detectron2#354

Test Plan: sandcastle

Reviewed By: rbgirshick

Differential Revision: D19231242

Pulled By: ppwwyyxx

fbshipit-source-id: 5da86672a38f2770e7187556eb128058971e9ba0
@ppwwyyxx
Copy link
Contributor

ppwwyyxx commented Dec 26, 2019

Now the default dataloader can work with RLE formats inside your dataset.
All you need is:

  1. use RLE format (documented at https://detectron2.readthedocs.io/tutorials/datasets.html#standard-dataset-dicts) in your dataset
  2. set INPUT.MASK_FORMAT='bitmask'.

jackalcooper added a commit to Oneflow-Inc/detectron2 that referenced this issue Jan 15, 2020
* update docs

Summary: Pull Request resolved: https://github.com/fairinternal/detectron2/pull/352

Differential Revision: D19229170

Pulled By: ppwwyyxx

fbshipit-source-id: 976adb38ea1939ce15070b68b046bb711bfe5690

* fix GPU CI; fix ETA computation in inference; support nightly build

Summary: Pull Request resolved: https://github.com/fairinternal/detectron2/pull/353

Differential Revision: D19229623

Pulled By: ppwwyyxx

fbshipit-source-id: c370076e7dcbb5d58eed4e580e3603640875c69b

* support RLE format in dataset dicts (facebookresearch#354)

Summary:
Fix facebookresearch#200

Pull Request resolved: https://github.com/fairinternal/detectron2/pull/354

Test Plan: sandcastle

Reviewed By: rbgirshick

Differential Revision: D19231242

Pulled By: ppwwyyxx

fbshipit-source-id: 5da86672a38f2770e7187556eb128058971e9ba0

* Fix bug in aspect ratio grouping (facebookresearch#355)

Summary:
In D19185925, the IterableDataset is parallelized by `torch.utils.data.DataLoader`, however the given  `IterableDataset` does not contain randomness (it uses a deterministic sequence of indices are coordinated with all the workers). As a result, all workers produce the same sequence of data and lead to bad data distribution.

This is fixed by parallelizing the old-style dataset, and then perform aspect ratio grouping.
Pull Request resolved: https://github.com/fairinternal/detectron2/pull/355

Test Plan: f159025687 should match old curves

Reviewed By: rbgirshick

Differential Revision: D19232477

Pulled By: ppwwyyxx

fbshipit-source-id: 78f9d2b3ad3531fd059751ead772351b6e8dc531

* update docs

Summary: Pull Request resolved: https://github.com/fairinternal/detectron2/pull/356

Differential Revision: D19234250

Pulled By: ppwwyyxx

fbshipit-source-id: 2526e91cfc0b4a9979bb1a0e247cbc36d357a844

* Enable XYWHA->XYXY conversion so that horizontal model can be trained on rotated dataset

Summary: Enable XYWHA->XYXY conversion (with bounding horizontal rectangle) so that horizontal model can be trained on rotated dataset for comparison.

Reviewed By: ppwwyyxx

Differential Revision: D18814514

fbshipit-source-id: 90ee67af746e6ea3e52d597923c1217cdb979c1a

* Ensure consistent dtype after BoxMode.convert

Summary: Pull Request resolved: https://github.com/fairinternal/detectron2/pull/357

Differential Revision: D19237617

Pulled By: ppwwyyxx

fbshipit-source-id: e39eeddfc418892c5d2c44caf6f754392192994e

* Add model_zoo.get_checkpoint_url (facebookresearch#358)

Summary:
Because `ModelZooUrls.get` is inconsistent with the style of the other functions (`get` and `get_config_file`), we should not expose `ModelZooUrls.get` as an API (in D19043540).

Pull Request resolved: https://github.com/fairinternal/detectron2/pull/358

Reviewed By: rbgirshick

Differential Revision: D19240191

Pulled By: ppwwyyxx

fbshipit-source-id: 1f4d91636b9654256949a82ec8cad8b6daeefd90

* Call setup_logger in DefaultTrainer if logger is not enabled (facebookresearch/issues/591)

Summary: Pull Request resolved: https://github.com/fairinternal/detectron2/pull/359

Differential Revision: D19249015

Pulled By: ppwwyyxx

fbshipit-source-id: 838a27e9ceb7b009edc5f238239972834cde096e

* fix the assertion in RLE conversion (facebookresearch#360)

Summary:
address facebookresearch@bfda431#r36610044
Pull Request resolved: https://github.com/fairinternal/detectron2/pull/360

Differential Revision: D19252823

Pulled By: ppwwyyxx

fbshipit-source-id: 680ddd1f40d78b471dfd3fc3ee461c5c4d0a2a39

* Do not handle empty inputs in wrappers.Conv2d; it's already supported. (facebookresearch#362)

Summary:
```python
import torch
x = torch.rand(0, 3, 5, 5)

x.requires_grad = True

l = torch.nn.Conv2d(3, 3, 3)

o = l(x)

print(o.shape)

o.sum().backward()

print(x.grad)
print(l.bias.grad)
```
prints expected output.
Pull Request resolved: https://github.com/fairinternal/detectron2/pull/362

Differential Revision: D19255791

Pulled By: ppwwyyxx

fbshipit-source-id: b32697ac444977076f8bc121bb1d0422dafbedea

* add `evaluator.process` time to ETA (facebookresearch#589)

Summary:
The time needed for `evaluator.process()` is not negligible, and it often leads to wrong ETA calculation

```
[12/27 20:06:29 d2.evaluation.evaluator]: Inference done 11/3944. 0.1170 s / img. ETA=0:07:39
[12/27 20:06:35 d2.evaluation.evaluator]: Inference done 29/3944. 0.1192 s / img. ETA=0:07:42
[12/27 20:06:40 d2.evaluation.evaluator]: Inference done 46/3944. 0.1206 s / img. ETA=0:07:42
....
....
[12/27 20:14:33 d2.evaluation.evaluator]: Inference done 1777/3944. 0.1248 s / img. ETA=0:00:06
[12/27 20:14:38 d2.evaluation.evaluator]: Inference done 1816/3944. 0.1245 s / img. ETA=0:00:00
[12/27 20:14:43 d2.evaluation.evaluator]: Inference done 1840/3944. 0.1245 s / img. ETA=-1 day, 23:59:55
[12/27 20:14:49 d2.evaluation.evaluator]: Inference done 1860/3944. 0.1246 s / img. ETA=-1 day, 23:59:50
[12/27 20:14:54 d2.evaluation.evaluator]: Inference done 1879/3944. 0.1246 s / img. ETA=-1 day, 23:59:45
[12/27 20:14:59 d2.evaluation.evaluator]: Inference done 1918/3944. 0.1244 s / img. ETA=-1 day, 23:59:39
```
Pull Request resolved: facebookresearch#589

Differential Revision: D19260964

Pulled By: ppwwyyxx

fbshipit-source-id: c47e1789f1bfde9f4c1898aec3d3274569a9a45e

* move c2 conversion utils to OSS

Reviewed By: ppwwyyxx

Differential Revision: D18802510

fbshipit-source-id: cc18d52e6961ce026399626b04e1ee2d01e4ef11

* move protobuf_model to caffe2_inference

Reviewed By: ppwwyyxx

Differential Revision: D19041105

fbshipit-source-id: ecebcaed7d67887e3574ea7b61cfd45cd7671b36

* support exporting PanopticFPN

Summary: Define what is the Caffe2 conterpoart for PanopticFPN, using `Caffe2PanopticFPN` class. Because there's no C2 op doing the combine (at least now), thus caffe2 model just outputs the raw instance and sementic segmentations, and doing the combine in `PanopticFPNAssembler` if we need to run evaluation.

Reviewed By: ppwwyyxx

Differential Revision: D19041602

fbshipit-source-id: ab3ae355588bdf8308aa397bc3f4a62c6e6bdc9d

* support exporting RetinaNet

Summary:
Define what's the caffe2 conterpart for RetinaNet in `Caffe2RetinaNet`. Because there's no operator doing the "inference" (topk and nms), caffe2 model outputs box_cls, box_delta and features, then `RetinaNetAssembler` handles the inference and post-processing.

The tricky part is dealing with anchors, I simply serialize the entire anchor_generator object and save it as model information and load it for evaluation. There're two other alternatives 1): saving every config key inside cfg.MODEL.ANCHOR_GENERATOR and backbone strides separatly, I feel this involves too many parameters. 2): saving cell anchor as tensor(s), this is what we did for RCNN models, but we don't have operator like GenerateProposals that uses cell anchors directly, and in D2 there's no direct API to create anchor_generator from cell anchors, so I didn't use this method.

In future we should have a dedicated operator (or a set of ops) for the postprocessing. Then we can get rid of storing those inference parameters.

Reviewed By: ppwwyyxx

Differential Revision: D19042349

fbshipit-source-id: 56f19c6fb537d26d9d36a09a76dee5fa955dac81

* reorganize caffe2_export

Summary:
LegacyInstancesAssembler -> GeneralizedRCNNAssembler

ProtobufGeneralizedRCNN -> ProtobufDetectionModel

Move meta-arch related code from `caffe2_export.py` to `caffe2_modeling.py`

Maybe we can merge `caffe2_inference.py` into `caffe2_modeling.py`, but for export-only purpose, the inference part is not needed.

Reviewed By: ppwwyyxx

Differential Revision: D19168799

fbshipit-source-id: 0e217695f2cc5feb6e0043fa9ca14e7339a548e7

* log for slow inference before hittimg warmup count

Summary:
Log for slow models for the first num_warmup * 2 iterations.

This is the change from D19041105 after rebasing to D19260964

Reviewed By: ppwwyyxx

Differential Revision: D19280797

fbshipit-source-id: 1b4640fbe8e3eedba9af3b8a4044c81a2e2ad713

* run c2 conversion on baseline models

Summary: A launch script to check if caffe2 conversion can match the accuracy for all models in D2 model zoo.

Reviewed By: ppwwyyxx

Differential Revision: D19043540

fbshipit-source-id: 56a67a4db5341f919e74199060babdab25c7f8e6

* export API & tests

Reviewed By: wat3rBro

Differential Revision: D19255042

fbshipit-source-id: 6b12bba55c1a86fa65868d235aead7f393133c19

* lock Pillow version

Summary: Pull Request resolved: https://github.com/fairinternal/detectron2/pull/364

Differential Revision: D19283466

Pulled By: ppwwyyxx

fbshipit-source-id: 092f1c56fe1cbf650663af565a35e0c789a5951c

* update docs

Summary: Pull Request resolved: https://github.com/fairinternal/detectron2/pull/365

Differential Revision: D19292963

Pulled By: ppwwyyxx

fbshipit-source-id: b209bdedfeb81f8aacf8239a0a31b99fc26fd2c5

* remove deprecated methods from backbone

Reviewed By: rbgirshick

Differential Revision: D19258472

fbshipit-source-id: a26a92c6eeb8b9193990e69e7c59e7e4e016d20d

* avoid modifying the inputs in ImageList.from_tensors

Summary: in-place operations are evil

Reviewed By: rbgirshick

Differential Revision: D19254747

fbshipit-source-id: 04aad3c5a54d59cc6cebfb51952000cdc203d331

* Fix the name resolution for exception (facebookresearch#640)

Summary:
Thanks for your contribution!

If you're sending a large PR (e.g., >50 lines),
please open an issue first about the feature / bug, and indicate how you want to contribute.
See more at https://detectron2.readthedocs.io/notes/contributing.html#pull-requests
about how we handle PRs.

Before submitting a PR, please run `dev/linter.sh` to lint the code.

Just fixed minor `name`  resolution
Pull Request resolved: facebookresearch#640

Differential Revision: D19297720

Pulled By: ppwwyyxx

fbshipit-source-id: a4a4c82efdcd13e60b5c34e31cfb164e6b83565a

* Use explicit versions in builtin configs (facebookresearch#363)

Summary:
Currently loading any official config will contain a warning of missing VERSION.
Pull Request resolved: https://github.com/fairinternal/detectron2/pull/363

Reviewed By: rbgirshick

Differential Revision: D19260690

Pulled By: ppwwyyxx

fbshipit-source-id: 2b17d6ed34aa3118c8bb89d40952e9d00054758d

* update linter

Reviewed By: rajprateek, takatosp1

Differential Revision: D19318380

fbshipit-source-id: b9afc6c018fd3c2d80c0a205f8ca8888ac8accb0

* run evaluation in the end of trainer, even if no training iteration happens

Reviewed By: rbgirshick

Differential Revision: D19319879

fbshipit-source-id: d84b9c3e957b0544979bc6706716222f31ebdd27

* add model arguments to densepose apply_net.py (facebookresearch#650)

Summary:
Added the ability to provide additional model arguments to densepose's `apply_net.py`, principally in order to be able to run it on CPU. Small copy-paste from `demo/demo.py`
Pull Request resolved: facebookresearch#650

Differential Revision: D19323992

Pulled By: ppwwyyxx

fbshipit-source-id: c193ceb31b40a73cc8329114117c61e5067f50f3

* update docs

Summary: Pull Request resolved: https://github.com/fairinternal/detectron2/pull/366

Differential Revision: D19325757

Pulled By: ppwwyyxx

fbshipit-source-id: d7b0b947d69aec41a2cfc637ca56d13df1f1ea3b

* use densepose files from densepose's public namespace

Reviewed By: vkhalidov

Differential Revision: D19322765

fbshipit-source-id: 1f3dbc974a03d97e3972ac3378e277d2c76bfb17

* Introduced vertical flipping in RandomFlip. (facebookresearch#643)

Summary:
Fixes facebookresearch#507.
Pull Request resolved: facebookresearch#643

Differential Revision: D19314245

Pulled By: ppwwyyxx

fbshipit-source-id: 57f4665ebf1532a77197f75174332d34383733bb

* Use a base Caffe2MetaArch to unify caffe2_modeling

Reviewed By: wat3rBro

Differential Revision: D19278414

fbshipit-source-id: 6ee4d7ce43168ab2694fbe46ea68683552ee86ce

* fix OSS onnx export for unbundled onnx

Summary: Pull Request resolved: https://github.com/fairinternal/detectron2/pull/367

Differential Revision: D19341509

Pulled By: ppwwyyxx

fbshipit-source-id: 24dfa3cdfe652b1a8c3db4e3f8bc8aab409a8d88

* replace "assembler" by outputs conversion function defined with the metaarch

Reviewed By: wat3rBro

Differential Revision: D19324328

fbshipit-source-id: a950d62516768cd878a2fd9a78d1ac5b257a872c

* corrected pretrained weight paths

Summary: Replaced "catalog://" path prefix with "detectron2://"

Reviewed By: ppwwyyxx

Differential Revision: D19327926

fbshipit-source-id: 340e4163f01f778fda31c5b90addb3d9b45e1efb

* corrected UV coordinates flip for torso

Summary:
Fixes flip transform for body parts that do not change labels under flip:
- old behavior: UV coordinates unchanged
- new behavior: UV coordinates flipped

Reviewed By: MarcSzafraniec

Differential Revision: D19327508

fbshipit-source-id: 7747e56b2746984452d13db9a7f6536034eba114

* Add Deeplab + config files

Summary: Adding Deeplab head to Densepose + config files

Reviewed By: vkhalidov

Differential Revision: D19345446

fbshipit-source-id: 3dbcfb38ad4a598a4f7377909d086acaf4a6972d

* Refactor test_boxes, test_model_zoo, test_rpn (facebookresearch#672)

Summary:
I've noticed that there is some inconsistency between different tests, i.e. some use python's builtin `assert` while others use `unittest.TestCase` asserts (a clear preference is for the latter). I went through `test_boxes.py`, `test_model_zoo.py`, and `test_rpn.py` and refactored it. Also some asserting did not work so I fixed that as well + did some minor refactoring in general.
Pull Request resolved: facebookresearch#672

Differential Revision: D19352228

Pulled By: ppwwyyxx

fbshipit-source-id: 0504324b919584d6dfd9f140ed402bb3e33adc02

* image.shape in mapper doc

Summary: Pull Request resolved: facebookresearch#677

Differential Revision: D19361092

Pulled By: ppwwyyxx

fbshipit-source-id: 4536779d2864da4f40792d39b55110af9952ce4b

* update lint

Summary: Pull Request resolved: https://github.com/fairinternal/detectron2/pull/368

Differential Revision: D19361311

Pulled By: ppwwyyxx

fbshipit-source-id: 6f209cbe19ffc4bae9b7493b2ea673c79079f512

* respect both tuple and list for scale_factor

Reviewed By: ppwwyyxx

Differential Revision: D19368592

fbshipit-source-id: 6ee3b00be078a5cb7b925a6149e6f2c66a7fad9f

* binary segmentations learning

Summary: Added the parameter NUM_COARSE_SEGM_CHANNELS that allows to select binary mask segmentation or 15-way corse body segmentation (default) in Densepose OSS

Reviewed By: vkhalidov

Differential Revision: D19345666

fbshipit-source-id: 8292ad9fb9d3a59be98baf3ee038630aeef7336d

* Ensure consistent device after BoxMode.convert

Summary: If passed a cuda tensor, `BoxMode.convert` throws an exception. This fixes this and adds a unit test.

Reviewed By: ppwwyyxx

Differential Revision: D19370051

fbshipit-source-id: 8f525e72974701c9f0301a867183567589e17415

* Add docs about export

Reviewed By: rbgirshick

Differential Revision: D19277161

fbshipit-source-id: a1c5069cb7ff96ed63b53146f30688ee0fe835ce

* fix lint; add assertion in gen_crop_transform (facebookresearch#687)

Summary: Pull Request resolved: https://github.com/fairinternal/detectron2/pull/369

Differential Revision: D19384242

Pulled By: ppwwyyxx

fbshipit-source-id: 6395f1452368b776f2d4b07bde35f06c7a045497

* check in docker scripts

* Add csv metric (#2)

* add change data dir & comment Eval

* add PDWriter to print csv info

* * add run maskrcnn shell scripts

* fomat

* fix log frequency

* fix fmt

* fix format

* update

Co-authored-by: QiaoJing <mir-of@outlook.com>
Co-authored-by: scxfjiang <sc.xfjiang@gmail.com>

* add todo

* copy cfg

* check in changes on cfg

* fix dataset dir

* fix check point path

* fix docker launch

* change path

* fix iter and check in files

Co-authored-by: Yuxin Wu <ppwwyyxxc@gmail.com>
Co-authored-by: Jing Huang <hjfzszthss@gmail.com>
Co-authored-by: Ouail <ouail.bendidi@gmail.com>
Co-authored-by: wat3rBro <wangyanghan6@gmail.com>
Co-authored-by: Suneeta Mall <suneeta.mall@nearmap.com>
Co-authored-by: Roee Shenberg <shenberg@gmail.com>
Co-authored-by: Max Frei <36265931+maxfrei750@users.noreply.github.com>
Co-authored-by: vkhalidov <vkhalidov@gmail.com>
Co-authored-by: MarcSzafraniec <mszafraniec@fb.com>
Co-authored-by: Milos <34348494+kondela@users.noreply.github.com>
Co-authored-by: Grigory Arutyunov <ArutyunovG@yandex.ru>
Co-authored-by: Sam Pepose <sampepose@gmail.com>
Co-authored-by: QiaoJing <mir-of@outlook.com>
Co-authored-by: scxfjiang <sc.xfjiang@gmail.com>
@pvti
Copy link

pvti commented Mar 4, 2020

Hi @ppwwyyxx
I'm trying to train a instance segmentaion model with 1 class X using detectron2's tutorial, but with a binary mask dataset.
With your instructions, I use RLE format (documented at https://detectron2.readthedocs.io/tutorials/datasets.html#standard-dataset-dicts).
In each "annotations", my "segmention" is a dict "represents the per-pixel segmentation mask in COCO’s RLE format. The dict should have keys “size” and “counts”. You can convert a uint8 segmentation mask of 0s and 1s into RLE format by pycocotools.mask.encode(np.asarray(mask, order="F"))." (I verify that bit-mask is successfully converted to RLE format).
So in this case, can you explain some of my questions, please:

  1. What exactly is the "segmentation"?
    For example:
    image
    left: original binary mask of 0s and 1s
    center: binary mask of the 1st instance
    right: binary mask of the 2nd instance
    => "segmentation" = pycocotools.mask.encode(np.asarray(left, order="F")) OR if I'm not wrong, in each instance "segmentation" = pycocotools.mask.encode(np.asarray(center/right , order="F")), right?
  2. I trained the model with the boilerplate code in your getting started collab notebook, every thing is ok. But when I try to evaluate the model's performance using AP metric implemented in COCO API as in your example, I got the error:
    WARNING [03/04 10:32:52 d2.evaluation.coco_evaluation]: json_file was not found in MetaDataCatalog for 'hand10_val'. Trying to convert it to COCO format ... [03/04 10:32:52 d2.data.datasets.coco]: Converting dataset annotations in 'hand10_val' to COCO format ...) [03/04 10:32:52 d2.data.datasets.coco]: Converting dataset dicts into COCO format Traceback (most recent call last): File "/home/n/detectron2/datasets/EgteaGaze+/hand10/test.py", line 132, in <module> evaluator = COCOEvaluator("hand10_val", cfg, False, output_dir="./output/") File "/home/n/detectron2/detectron2/evaluation/coco_evaluation.py", line 71, in __init__ convert_to_coco_json(dataset_name, cache_path) File "/home/n/detectron2/detectron2/data/datasets/coco.py", line 416, in convert_to_coco_json coco_dict = convert_to_coco_dict(dataset_name) File "/home/n/detectron2/detectron2/data/datasets/coco.py", line 334, in convert_to_coco_dict polygons = PolygonMasks([segmentation]) File "/home/n/detectron2/detectron2/structures/masks.py", line 271, in __init__ process_polygons(polygons_per_instance) for polygons_per_instance in polygons File "/home/n/detectron2/detectron2/structures/masks.py", line 271, in <listcomp> process_polygons(polygons_per_instance) for polygons_per_instance in polygons File "/home/n/detectron2/detectron2/structures/masks.py", line 262, in process_polygons "Got '{}' instead.".format(type(polygons_per_instance)) AssertionError: Cannot create polygons: Expect a list of polygons per instance. Got '<class 'dict'>' instead.

I guess that it must have to set INPUT.MASK_FORMAT='bitmask' (this is in the case of training config), so in the case of evaluating, what need to be set?
I also tried to print("cfg.dict= ", cfg.dict) but only got
type(cfg)= <class 'detectron2.config.config.CfgNode'> cfg.__dict__= {'__immutable__': False, '__deprecated_keys__': set(), '__renamed_keys__': {}, '__new_allowed__': False}
How can I see all the atribute of the cfg?
3. Can you note all the things relate in case of training with binary mask, is there anything need to be noticed/different with the polygon-training.
Many thanks for your great framework and your enthusiastic supports!

@ppwwyyxx
Copy link
Contributor

ppwwyyxx commented Mar 4, 2020

Evaluation of a generic (not COCO format) dataset with RLE is not yet supported. There is a TODO in

# TODO: check segmentation type: RLE, BinaryMask or Polygon
polygons = PolygonMasks([segmentation])
area = polygons.area()[0].item()
(cc @botcs )

facebook-github-bot pushed a commit that referenced this issue Mar 5, 2020
Summary:
fix #200
Pull Request resolved: fairinternal/detectron2#391

Reviewed By: rbgirshick

Differential Revision: D20260189

Pulled By: ppwwyyxx

fbshipit-source-id: 690b497e45dda9a3dc9696dc991aac7bf482de0b
@ppwwyyxx
Copy link
Contributor

ppwwyyxx commented Mar 5, 2020

The above issue about evaluation is hopefully fixed in 6901cc7

@siyuanfeng-tri
Copy link
Contributor

The above issue about evaluation is hopefully fixed in 6901cc7

I think I need to do a bit more to get this actually working.

  1. I had to cast this into int explicitly, otherwise json dump complains about TypeError: Object of type 'uint32' is not JSON serializable
    https://github.com/facebookresearch/detectron2/blob/master/detectron2/data/datasets/coco.py#L339
  2. I had to decode segmentation["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

@ppwwyyxx
Copy link
Contributor

There are a few different RLE formats cocoapi recognizes and the 'counts' in coco's original json is actually a list of int.
Should figure out how to convert the bytes to that format.

@siyuanfeng-tri
Copy link
Contributor

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 like segmentation["counts"] = segmentation["counts"].decode(''ascii") before calling json dump.

facebook-github-bot pushed a commit that referenced this issue Apr 27, 2020
Summary:
I got `TypeError: Object of type 'uint32' is not JSON serializable` for `area`, and `TypeError: Object of type 'bytes' is not JSON serializable` for `segmentation["counts"]`during json dump.

Relates to #200
Pull Request resolved: #1275

Reviewed By: rbgirshick

Differential Revision: D21207121

Pulled By: ppwwyyxx

fbshipit-source-id: 1c0b554ff9f980827c9134d6cb2b18a5cf9c78cc
nanddalal pushed a commit to SironaMedical/nines-detectron2 that referenced this issue Jun 5, 2020
Summary:
fix facebookresearch#200
Pull Request resolved: https://github.com/fairinternal/detectron2/pull/391

Reviewed By: rbgirshick

Differential Revision: D20260189

Pulled By: ppwwyyxx

fbshipit-source-id: 690b497e45dda9a3dc9696dc991aac7bf482de0b
nanddalal pushed a commit to SironaMedical/nines-detectron2 that referenced this issue Jun 5, 2020
Summary:
I got `TypeError: Object of type 'uint32' is not JSON serializable` for `area`, and `TypeError: Object of type 'bytes' is not JSON serializable` for `segmentation["counts"]`during json dump.

Relates to facebookresearch#200
Pull Request resolved: facebookresearch#1275

Reviewed By: rbgirshick

Differential Revision: D21207121

Pulled By: ppwwyyxx

fbshipit-source-id: 1c0b554ff9f980827c9134d6cb2b18a5cf9c78cc
@kharyal
Copy link

kharyal commented Jul 10, 2020

Now the default dataloader can work with RLE formats inside your dataset.
All you need is:

  1. use RLE format (documented at https://detectron2.readthedocs.io/tutorials/datasets.html#standard-dataset-dicts) in your dataset
  2. set INPUT.MASK_FORMAT='bitmask'.

Still not working for me. I am not getting any error but the network is not learning anything. It just runs for 500 and doesn't segment anything. The Epoch error is also dropping to 0.

@Paragjain10
Copy link

There are a few different RLE formats cocoapi recognizes and the 'counts' in coco's original json is actually a list of int.
Should figure out how to convert the bytes to that format.

Hello @ppwwyyxx,

I am trying to implement CenterMask2. My annotations are in coco rle format, I have set cfg.INPUT.MASK_FORMAT='bitmask'
But unfortunately, this error is being raised:

/home/student2/anaconda3/envs/Parag_Centremask2/lib/python3.8/site-packages/detectron2/data/detection_utils.py:414: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at /opt/conda/conda-bld/pytorch_1607370172916/work/torch/csrc/utils/tensor_numpy.cpp:141.)
torch.stack([torch.from_numpy(np.ascontiguousarray(x)) for x in masks])
/home/student2/Desktop/Parag_Centremask2/centermask2/centermask/modeling/fcos/fcos_outputs.py:402: UserWarning: This overload of nonzero is deprecated:
nonzero()
Consider using one of the following signatures instead:
nonzero(*, bool as_tuple) (Triggered internally at /opt/conda/conda-bld/pytorch_1607370172916/work/torch/csrc/utils/python_arg_parser.cpp:882.)
per_candidate_nonzeros = per_candidate_inds.nonzero()
Traceback (most recent call last):
File "/home/student2/Desktop/Parag_Centremask2/centermask2/train_net.py", line 294, in
launch(
File "/home/student2/anaconda3/envs/Parag_Centremask2/lib/python3.8/site-packages/detectron2/engine/launch.py", line 62, in launch
main_func(*args)
File "/home/student2/Desktop/Parag_Centremask2/centermask2/train_net.py", line 288, in main
return trainer.train()
File "/home/student2/Desktop/Parag_Centremask2/centermask2/train_net.py", line 165, in train
self.train_loop(self.start_iter, self.max_iter)
File "/home/student2/Desktop/Parag_Centremask2/centermask2/train_net.py", line 154, in train_loop
self.run_step()
File "/home/student2/anaconda3/envs/Parag_Centremask2/lib/python3.8/site-packages/detectron2/engine/defaults.py", line 423, in run_step
self._trainer.run_step()
File "/home/student2/anaconda3/envs/Parag_Centremask2/lib/python3.8/site-packages/detectron2/engine/train_loop.py", line 228, in run_step
loss_dict = self.model(data)
File "/home/student2/anaconda3/envs/Parag_Centremask2/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "/home/student2/anaconda3/envs/Parag_Centremask2/lib/python3.8/site-packages/detectron2/modeling/meta_arch/rcnn.py", line 166, in forward
_, detector_losses = self.roi_heads(images, features, proposals, gt_instances)
File "/home/student2/anaconda3/envs/Parag_Centremask2/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "/home/student2/Desktop/Parag_Centremask2/centermask2/centermask/modeling/centermask/center_heads.py", line 401, in forward
losses, mask_features, selected_mask, labels, maskiou_targets = self._forward_mask(features, proposals)
File "/home/student2/Desktop/Parag_Centremask2/centermask2/centermask/modeling/centermask/center_heads.py", line 476, in _forward_mask
loss, selected_mask, labels, maskiou_targets = mask_rcnn_loss(mask_logits, proposals, self.maskiou_on)
File "/home/student2/Desktop/Parag_Centremask2/centermask2/centermask/modeling/centermask/mask_head.py", line 80, in mask_rcnn_loss
cropped_mask = crop(instances_per_image.gt_masks.polygons, instances_per_image.proposal_boxes.tensor)
AttributeError: 'BitMasks' object has no attribute 'polygons'

Process finished with exit code 1

@DavidNguyen95
Copy link

DavidNguyen95 commented Jun 7, 2021

w to convert the bytes to that

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'

`

@sipie800
Copy link

sipie800 commented Dec 17, 2021

cfg.INPUT.MASK_FORMAT = "bitmask" doesn't work. I do use compressed rle in json which other api like torch or mmdetection can read.

ppwwyyxx added a commit that referenced this issue Jan 2, 2022
Summary:
Discussed in fairinternal/detectron2#195 (comment)
Two runs of s1x gave 65.1 & 65.3 respectively
Pull Request resolved: fairinternal/detectron2#200

Reviewed By: rbgirshick

Differential Revision: D16066515

Pulled By: ppwwyyxx

fbshipit-source-id: 773f918771f31e12e6a26bd92d48d46b023502e4
@koreandrum97
Copy link

koreandrum97 commented Jun 2, 2022

cfg.INPUT.MASK_FORMAT = "bitmask" doesn't work. I do use compressed rle in json which other api like torch or mmdetection can read.

Is it working now? I'd like to semantic segmentation task with detectron2. But I wonder which types of annotation(RLE or Polygons) Please reply!! @ppwwyyxx

@leiluoray1
Copy link

leiluoray1 commented Jun 23, 2022

check the model output here, say if you want to save the first instance segmentation mask, which is pred_masks[0]. you can

mask0 = outputs["instances"].pred_masks[0].cpu().numpy()*255.0
mask0 = mask0.astype(int)
cv2_imshow(mask0)

@hamzagorgulu
Copy link

@koreandrum97 have you found a solution? I train the model with rle masks and assuming the task is converted to semantic segmentation, but I only get masks for instance segmentation. In the [docs](url), it says the output dict should contain "sem_seg" key but I dont get the prediction probabilities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvements or good new features
Projects
None yet
Development

No branches or pull requests