facebookresearch / detectron2

Detectron2 is a platform for object detection, segmentation and other visual recognition tasks.
https://detectron2.readthedocs.io/en/latest/
Apache License 2.0
30.53k stars 7.48k forks source link

ValueError: Failed to use mask_format=='polygon' from the given annotations! #4548

Closed 15024287710Jackson closed 2 years ago

15024287710Jackson commented 2 years ago

File "/root/miniconda3/envs/detectronTwoEth/lib/python3.8/site-packages/torch/utils/data/_utils/worker.py", line 287, in _worker_loop data = fetcher.fetch(index) File "/root/miniconda3/envs/detectronTwoEth/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 32, in fetch data.append(next(self.dataset_iter)) File "/root/autodl-tmp/code/detectron2/detectron2/data/common.py", line 201, in iter yield self.dataset[idx] File "/root/autodl-tmp/code/detectron2/detectron2/data/common.py", line 90, in getitem data = self._map_func(self._dataset[cur_idx]) File "/root/autodl-tmp/code/detectron2/detectron2/utils/serialize.py", line 26, in call return self._obj(*args, **kwargs) File "/root/autodl-tmp/code/detectron2/detectron2/data/dataset_mapper.py", line 189, in call self._transform_annotations(dataset_dict, transforms, image_shape) File "/root/autodl-tmp/code/detectron2/detectron2/data/dataset_mapper.py", line 131, in _transform_annotations instances = utils.annotations_to_instances( File "/root/autodl-tmp/code/detectron2/detectron2/data/detection_utils.py", line 405, in annotations_to_instances raise ValueError( ValueError: Failed to use mask_format=='polygon' from the given annotations!

When I train with Fashionpedia whose segmentation is birmask, I do not how to solve the problem.

Can someone help me ?

image

github-actions[bot] commented 2 years ago

You've chosen to report an unexpected problem or bug. Unless you already know the root cause of it, please include details about it by filling the issue template. The following information is missing: "Instructions To Reproduce the Issue and Full Logs"; "Your Environment";

owardlaw commented 2 years ago

What are you using to label your images?

15024287710Jackson commented 2 years ago

Dear, I just use the dataset called Fashionpedia, Here is Link : https://github.com/cvdfoundation/fashionpedia image Some annotations of segmentation use polygon format, and the other annotations of segmentation use RLE format. I want know how I can transfer the RLE format to the polygon format.

ppwwyyxx commented 2 years ago

Please show full log as the "Unexpected behaviors" issue template asked for.

owardlaw commented 2 years ago

@15024287710Jackson

I label my data with labelme, 'pip install labelme'.

For example, create a data folder, and in that folder create a test and train folder. Put your COCO JSON annotations in each folder alongside your images. Load the data with the code below. Make sure you specify your class names and change n to the number of classes that you have.

cfg.MODEL.ROI_HEADS.NUM_CLASSES = n


def get_data_dicts(directory, classes):
    dataset_dicts = []
    for filename in [file for file in os.listdir(directory) if file.endswith('.json')]:
        json_file = os.path.join(directory, filename)
        with open(json_file) as f:
            img_anns = json.load(f)

        record = {}

        filename = os.path.join(directory, img_anns["imagePath"])

        record["file_name"] = filename
        annos = img_anns["shapes"]
        record["height"] = img_anns["imageHeight"]
        record["width"] = img_anns["imageWidth"]

        objs = []
        for anno in annos:
            px = [a[0] for a in anno['points']] # x coord
            py = [a[1] for a in anno['points']] # y-coord
            poly = [(x, y) for x, y in zip(px, py)] # poly for segmentation
            poly = [p for x in poly for p in x]

            obj = {
                "bbox": [np.min(px), np.min(py), np.max(px), np.max(py)],
                "bbox_mode": BoxMode.XYXY_ABS,
                "segmentation": [poly],
                "category_id": classes.index(anno['label']),
                "iscrowd": 0
            }

            objs.append(obj)
        record["annotations"] = objs
        dataset_dicts.append(record)
    return dataset_dicts

classes = ["X", "X"]
data_path = "./XXXXX"

for d in ["train", "test"]:
    DatasetCatalog.register(
        "category_" + d, 
        lambda d=d: get_data_dicts(data_path+d, classes)
    )
    MetadataCatalog.get("category_" + d).set(thing_classes=classes)

microcontroller_metadata = MetadataCatalog.get("category_train")
github-actions[bot] commented 2 years ago

Requested information was not provided in 7 days, so we're closing this issue.

Please open new issue if information becomes available. Otherwise, use github discussions for free-form discussions.

15024287710Jackson commented 2 years ago

Dear ppwwyyxx,

I am sorry to reply your help so late.

Here is the full log about error.

[09/17 13:12:22 d2.engine.train_loop]: Starting training from iteration 0 ERROR [09/17 13:12:24 d2.engine.train_loop]: Exception during training: Traceback (most recent call last): File "/root/autodl-tmp/code/detectron2/detectron2/engine/train_loop.py", line 149, in train self.run_step() File "/root/autodl-tmp/code/detectron2/detectron2/engine/defaults.py", line 494, in run_step self._trainer.run_step() File "/root/autodl-tmp/code/detectron2/detectron2/engine/train_loop.py", line 268, in run_step data = next(self._data_loader_iter) File "/root/autodl-tmp/code/detectron2/detectron2/data/common.py", line 234, in iter for d in self.dataset: File "/root/miniconda3/envs/detectronTwoEth/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 521, in next data = self._next_data() File "/root/miniconda3/envs/detectronTwoEth/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1203, in _next_data return self._process_data(data) File "/root/miniconda3/envs/detectronTwoEth/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1229, in _process_data data.reraise() File "/root/miniconda3/envs/detectronTwoEth/lib/python3.8/site-packages/torch/_utils.py", line 434, in reraise raise exception ValueError: Caught ValueError in DataLoader worker process 3. Original Traceback (most recent call last): File "/root/autodl-tmp/code/detectron2/detectron2/data/detection_utils.py", line 403, in annotations_to_instances masks = PolygonMasks(segms) File "/root/autodl-tmp/code/detectron2/detectron2/structures/masks.py", line 309, in init self.polygons: List[List[np.ndarray]] = [ File "/root/autodl-tmp/code/detectron2/detectron2/structures/masks.py", line 310, in process_polygons(polygons_per_instance) for polygons_per_instance in polygons File "/root/autodl-tmp/code/detectron2/detectron2/structures/masks.py", line 298, in process_polygons raise ValueError( ValueError: Cannot create polygons: Expect a list of polygons per instance. Got '<class 'numpy.ndarray'>' instead.

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/root/miniconda3/envs/detectronTwoEth/lib/python3.8/site-packages/torch/utils/data/_utils/worker.py", line 287, in _worker_loop data = fetcher.fetch(index) File "/root/miniconda3/envs/detectronTwoEth/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 32, in fetch data.append(next(self.dataset_iter)) File "/root/autodl-tmp/code/detectron2/detectron2/data/common.py", line 201, in iter yield self.dataset[idx] File "/root/autodl-tmp/code/detectron2/detectron2/data/common.py", line 90, in getitem data = self._map_func(self._dataset[cur_idx]) File "/root/autodl-tmp/code/detectron2/detectron2/utils/serialize.py", line 26, in call return self._obj(*args, **kwargs) File "/root/autodl-tmp/code/detectron2/detectron2/data/dataset_mapper.py", line 189, in call self._transform_annotations(dataset_dict, transforms, image_shape) File "/root/autodl-tmp/code/detectron2/detectron2/data/dataset_mapper.py", line 131, in _transform_annotations instances = utils.annotations_to_instances( File "/root/autodl-tmp/code/detectron2/detectron2/data/detection_utils.py", line 405, in annotations_to_instances raise ValueError( ValueError: Failed to use mask_format=='polygon' from the given annotations!

[09/17 13:12:24 d2.engine.hooks]: Total training time: 0:00:01 (0:00:00 on hooks) [09/17 13:12:24 d2.utils.events]:  iter: 0 lr: N/A max_mem: 375M Traceback (most recent call last): File "train_netPedia.py", line 172, in launch( File "/root/autodl-tmp/code/detectron2/detectron2/engine/launch.py", line 82, in launch main_func(*args) File "train_netPedia.py", line 166, in main return trainer.train() File "/root/autodl-tmp/code/detectron2/detectron2/engine/defaults.py", line 484, in train super().train(self.start_iter, self.max_iter) File "/root/autodl-tmp/code/detectron2/detectron2/engine/train_loop.py", line 149, in train self.run_step() File "/root/autodl-tmp/code/detectron2/detectron2/engine/defaults.py", line 494, in run_step self._trainer.run_step() File "/root/autodl-tmp/code/detectron2/detectron2/engine/train_loop.py", line 268, in run_step data = next(self._data_loader_iter) File "/root/autodl-tmp/code/detectron2/detectron2/data/common.py", line 234, in iter for d in self.dataset: File "/root/miniconda3/envs/detectronTwoEth/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 521, in next data = self._next_data() File "/root/miniconda3/envs/detectronTwoEth/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1203, in _next_data return self._process_data(data) File "/root/miniconda3/envs/detectronTwoEth/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1229, in _process_data data.reraise() File "/root/miniconda3/envs/detectronTwoEth/lib/python3.8/site-packages/torch/_utils.py", line 434, in reraise raise exception ValueError: Caught ValueError in DataLoader worker process 3. Original Traceback (most recent call last): File "/root/autodl-tmp/code/detectron2/detectron2/data/detection_utils.py", line 403, in annotations_to_instances masks = PolygonMasks(segms) File "/root/autodl-tmp/code/detectron2/detectron2/structures/masks.py", line 309, in init self.polygons: List[List[np.ndarray]] = [ File "/root/autodl-tmp/code/detectron2/detectron2/structures/masks.py", line 310, in process_polygons(polygons_per_instance) for polygons_per_instance in polygons File "/root/autodl-tmp/code/detectron2/detectron2/structures/masks.py", line 298, in process_polygons raise ValueError( ValueError: Cannot create polygons: Expect a list of polygons per instance. Got '<class 'numpy.ndarray'>' instead.

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/root/miniconda3/envs/detectronTwoEth/lib/python3.8/site-packages/torch/utils/data/_utils/worker.py", line 287, in _worker_loop data = fetcher.fetch(index) File "/root/miniconda3/envs/detectronTwoEth/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 32, in fetch data.append(next(self.dataset_iter)) File "/root/autodl-tmp/code/detectron2/detectron2/data/common.py", line 201, in iter yield self.dataset[idx] File "/root/autodl-tmp/code/detectron2/detectron2/data/common.py", line 90, in getitem data = self._map_func(self._dataset[cur_idx]) File "/root/autodl-tmp/code/detectron2/detectron2/utils/serialize.py", line 26, in call return self._obj(*args, **kwargs) File "/root/autodl-tmp/code/detectron2/detectron2/data/dataset_mapper.py", line 189, in call self._transform_annotations(dataset_dict, transforms, image_shape) File "/root/autodl-tmp/code/detectron2/detectron2/data/dataset_mapper.py", line 131, in _transform_annotations instances = utils.annotations_to_instances( File "/root/autodl-tmp/code/detectron2/detectron2/data/detection_utils.py", line 405, in annotations_to_instances raise ValueError( ValueError: Failed to use mask_format=='polygon' from the given annotations!

Here is the setting in my train_netPedia which is the copy of train_net.

def setup(args): """ Create configs and perform basic setups. """ cfg = get_cfg() cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.MODEL.ROI_HEADS.NUM_CLASSES = 46 cfg.MODEL.SEM_SEG_HEAD.NUM_CLASSES = 46 cfg.MODEL.FCOS.NUM_CLASSES = 46 cfg.SOLVER.IMS_PER_BATCH = 7 cfg.DATALOADER.NUM_WORKERS = 5 cfg.MODEL.MASKIOU_ON = True ITERS_IN_ONE_EPOCH = int(80000 / 8) cfg.SOLVER.CHECKPOINT_PERIOD = ITERS_IN_ONE_EPOCH cfg.TEST.EVAL_PERIOD = ITERS_IN_ONE_EPOCH cfg.INPUT.MASK_FORMAT='bitmask' cfg.freeze() default_setup(cfg, args) return cfg

15024287710Jackson commented 2 years ago

@ppwwyyxx

15024287710Jackson commented 2 years ago

I copy the wrong setting for you.

I does not add cfg.INPUT.MASK_FORMAT='bitmask' when I train the model.

ppwwyyxx commented 2 years ago

Detectron2 does not automatically turn RLE into polygons because there isn't an accurate implementation that can do this. If you must, https://github.com/facebookresearch/detectron2/blob/7c2c8fb168a2093ce06a531c1208fba48d2984ec/detectron2/utils/visualizer.py#L119-L136 this function can be a reference so you can do the conversion yourself. But it's inaccurate.

If your input contains RLE annotations it's recommended to use MASK_FORMAT='bitmask'.