Commit 0f9476bd authored by Csaba Botos's avatar Csaba Botos Committed by Francisco Massa

Remove Detectron dependency (#457)

* Remove Detectron dependency

I have looked into the boxes.py to swap [these lines](https://github.com/facebookresearch/Detectron/blob/8170b25b425967f8f1c7d715bea3c5b8d9536cd8/detectron/utils/boxes.py#L51L52):
```
import detectron.utils.cython_bbox as cython_bbox
import detectron.utils.cython_nms as cython_nms
```

```
from maskrcnn_benchmark.structures.boxlist_ops import boxlist_iou
from maskrcnn_benchmark.structures.boxlist_ops import boxlist_nms
```
However some functions are missing from the `boxlist_ops` like the [`soft_nms`](https://github.com/facebookresearch/Detectron/blob/master/detectron/utils/cython_nms.pyx#L98L203) .

So I just tried to modify the `maskrcnn-benchmark/tools/cityscapes/convert_cityscapes_to_coco.py` script.
Here we have `polys_to_boxes` function from `segms.py` and I could not find its analogous in the maskrcnn_benchmark lib. It seems to me that the original function in `segms.py` is using pure lists so I just wrote two auxiliary functions reusing the boxList's convert method( https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/maskrcnn_benchmark/structures/bounding_box.py#L67L70 )
and Detectron's polys_to_boxes ( https://github.com/facebookresearch/Detectron/blob/b5dcc0fe1d091cb70f9243939258215dd63e3dfa/detectron/utils/segms.py#L135L140 ):
```
def poly_to_box(poly):
    """Convert a polygon into a tight bounding box."""
    x0 = min(min(p[::2]) for p in poly)
    x1 = max(max(p[::2]) for p in poly)
    y0 = min(min(p[1::2]) for p in poly)
    y1 = max(max(p[1::2]) for p in poly)
    box_from_poly = [x0, y0, x1, y1]

    return box_from_poly

def xyxy_to_xywh(xyxy_box):
    xmin, ymin, xmax, ymax = xyxy_box
    TO_REMOVE = 1
    xywh_box = (xmin, ymin, xmax - xmin + TO_REMOVE, ymax - ymin + TO_REMOVE)
    return xywh_box
```

* removed leftovers

* Update convert_cityscapes_to_coco.py
parent 6d7d80a1
...@@ -31,9 +31,6 @@ import sys ...@@ -31,9 +31,6 @@ import sys
import cityscapesscripts.evaluation.instances2dict_with_polygons as cs import cityscapesscripts.evaluation.instances2dict_with_polygons as cs
import detectron.utils.segms as segms_util
import detectron.utils.boxes as bboxs_util
def parse_args(): def parse_args():
parser = argparse.ArgumentParser(description='Convert dataset') parser = argparse.ArgumentParser(description='Convert dataset')
...@@ -50,6 +47,23 @@ def parse_args(): ...@@ -50,6 +47,23 @@ def parse_args():
return parser.parse_args() return parser.parse_args()
def poly_to_box(poly):
"""Convert a polygon into a tight bounding box."""
x0 = min(min(p[::2]) for p in poly)
x1 = max(max(p[::2]) for p in poly)
y0 = min(min(p[1::2]) for p in poly)
y1 = max(max(p[1::2]) for p in poly)
box_from_poly = [x0, y0, x1, y1]
return box_from_poly
def xyxy_to_xywh(xyxy_box):
xmin, ymin, xmax, ymax = xyxy_box
TO_REMOVE = 1
xywh_box = (xmin, ymin, xmax - xmin + TO_REMOVE, ymax - ymin + TO_REMOVE)
return xywh_box
def convert_coco_stuff_mat(data_dir, out_dir): def convert_coco_stuff_mat(data_dir, out_dir):
"""Convert to png and save json with path. This currently only contains """Convert to png and save json with path. This currently only contains
the segmentation labels for objects+stuff in cocostuff - if we need to the segmentation labels for objects+stuff in cocostuff - if we need to
...@@ -143,6 +157,7 @@ def convert_cityscapes_instance_only( ...@@ -143,6 +157,7 @@ def convert_cityscapes_instance_only(
images = [] images = []
annotations = [] annotations = []
ann_dir = os.path.join(data_dir, ann_dir) ann_dir = os.path.join(data_dir, ann_dir)
for root, _, files in os.walk(ann_dir): for root, _, files in os.walk(ann_dir):
for filename in files: for filename in files:
if filename.endswith(ends_in % data_set.split('_')[0]): if filename.endswith(ends_in % data_set.split('_')[0]):
...@@ -193,9 +208,10 @@ def convert_cityscapes_instance_only( ...@@ -193,9 +208,10 @@ def convert_cityscapes_instance_only(
ann['category_id'] = category_dict[object_cls] ann['category_id'] = category_dict[object_cls]
ann['iscrowd'] = 0 ann['iscrowd'] = 0
ann['area'] = obj['pixelCount'] ann['area'] = obj['pixelCount']
ann['bbox'] = bboxs_util.xyxy_to_xywh(
segms_util.polys_to_boxes( xyxy_box = poly_to_box(ann['segmentation'])
[ann['segmentation']])).tolist()[0] xywh_box = xyxy_to_xywh(xyxy_box)
ann['bbox'] = xywh_box
annotations.append(ann) annotations.append(ann)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment