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

fix: update to export RLE mask with the shape of (width, height) order #197

Closed
wants to merge 1 commit into from

Conversation

ktro2828
Copy link
Contributor

@ktro2828 ktro2828 commented Jan 30, 2025

Description

This PR fixes exported RLE mask shape from (height, width) order to (width, height) order.

How to review

How to test

test data

deepen

Polygon

Dataset: https://drive.google.com/file/d/117MTcixJsEpnTg2DGXSC_X_y4SB7li4Z/view?usp=drive_link

  • Before
{
        "token": "a2db6431a515d5c7e8463070cdd2db35",
        "sample_data_token": "a1d3257e11ec9d4a587ea7053b33f1c1",
        "instance_token": "e36688fcefc953126aa80ea19f690d7b",
        "category_token": "3852e92a0d398ff0a9bf5bbc28570a79",
        "attribute_tokens": [],
        "bbox": [
            0,
            389.66209596195347,
            1920,
            675.4739482400669
        ],
        "mask": {
            "size": [
                1280,
                1920
            ],
            "counts": "UGQwUzFtVjEwMDAwMDAwTzEwMDBPMTAwMDAwMDAwME8xME8xMDAwMDAwMDAwMDBPMTBPMTAwMDAwMDAwME8xME8xMDAwMDAwMDAwMDBPMTBPMTAwMDAwMDAwMDAwTzAxMDAwMDAwMDAwMDAwME8wMTAwMDAwMDAwMDAwTzAxMDAwMDAwMDAwMDAwME8wMTAwMDAwMDAwMDAwME8wMTAwMDAwMDAwMDAwME8wMTAwMDAwMDAwMDAwME8wMTAwMDAwMDAwMDAwMDBPMDEwMDAwMDAwMDAwME8wMTAwMDAwMDAwMDAwMDBPMDEwMDAwMDAwMDAwME8xME8xMDAwMDAwMDAwMDBPMTBPMTAwMDAwMDAwME8xME8xMDAwMDAwMDAwMDBPMTBPMTAwMDAwMDAwME8xMDAwTzEwMDAwMDAwMDBPMTBPMTAwMDAwMDAwME8xMDAwTzEwMDAwMDAwMDBPMTAwME8xMDAwMDAwME8xMDAwTzEwMDAwMDAwMDBPMTA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MDAwMDBPMTAwMDAwMDBPMTAwME8xMDAwMDAwMDAwTzEwTzEwMDAwMDAwMDAwME8wMTAwMDAwMDAwMDAwME8wMTAwMDAwMDAwMDAwTzEwTzEwMDAwMDAwTzEwMDAwME8xMDAwMDBPMTAwMDAwMDBPMTBPMTAwMDAwMDAwMDAwTzAxMDAwMDAwMDAwME8xME8xMDAwMDAwMDAwTzEwMDBPMTAwMDAwTzEwMDAwMDAwTzEwMDBPMTAwMDAwMDAwME8xME8xMDAwMDAwMDAwME8wMTAwMDAwMDAwMDAwTzEwTzEwMDAwMDAwTzEwMDAwMDAwTzEwMDBPMTAwMDAwMDAwME8wMTAwMDAwMDAwMDAwME8wMTAwMDAwMDAwME8xMDAwTzEwMDAwMDAwTzEwMDBqQw=="
        },
        "automatic_annotation": false
    },
  • After
{
        "token": "f129c5f4392e1bc4be12681b35632559",
        "sample_data_token": "a1d3257e11ec9d4a587ea7053b33f1c1",
        "instance_token": "0e37be3615ab4966d8dc7c8fcad4940b",
        "category_token": "64ec257a489eab27fb403c6b908f2fdf",
        "attribute_tokens": [],
        "bbox": [
            0,
            389.66209596195347,
            1920,
            675.4739482400669
        ],
        "mask": {
            "size": [
                1920,
                1280
            ],
            "counts": "aVNtZjA3YWsxOEg4SDhHOUg4SDhIOEg4SDhIOEg4SDhINUsxTzJOMU8yTjFPMk4xTzJOMU8xTzJOMU8yTTJPMk4xTzJOMU8xTzFPMU8xTzFPMU8xTzFPMk4xTzFPMU8xTzFPMU8xTzFPMU8xMDAwMU8wMDBPMTAwMDAwME8xMDAwMDAwMDFOMTAwMDAwMDAwTzEwMDAwMDAwME8xMDFPMDAwMDBPMTAwMDAwME8xMDAwMDAwMDFOMTAwMDAwMDAwTzEwMDAwMDAwME8yTzAwMDAwMDBPMTAwMDBPMU8xMDBPMU8yTjEwME8xTzEwME8xTzFPMk8wTzFPMTAwTzJOMTAwTzFPMU8xMDFOMU8xMDBPMU8yTzBPMU8xTzEwME8yTjEwME8xTzFPMk8wTzFPMTAwTzJOMTAwTzFPMU8xMDFOMU8xMDBPMU8yTzBPMU8xTzEwME8yTjEwME8xTzFPMk8wTzFPMTAwTzJOMTAwTzFPMU8xMDFOMU8xMDBPMU8yTzBPMU8xTzEwME8yTjEwME8xTzFPMTBPMDFPMDEwTzAwMTBPMDFPMDAxME8wMU8wMTBPMDAxME8wMU8wMDEwTzAxTzAxME8wMDFPMDEwTzAwMTBPMDFPMDEwTzAwMU8wMTBPMDAxME8wMU8wMU8wMU8wMDEwTzAxTzAzTjdIOEk3SDhJN0g4STdIOEk3SDhJN0g4STdIOEk3SDhJN0g4STdIOEk3SDhJN0g4STdIOEk3SDhJN0g4STdIOEk3SDhJV1xeUzE="
        },
        "automatic_annotation": false
    },

Painting

Dataset: https://drive.google.com/file/d/1G6N4J6aohRytI2v9Q72LapKZwsKdyqDl/view?usp=drive_link

  • Before
{
        "token": "d8c4ba67e59f5f8155ca191cfe72b21b",
        "sample_data_token": "02382f82a702f04cdafa69d9eac0deed",
        "instance_token": "f64b1c8f34abfae0cf8cf4e803675b0c",
        "category_token": "f2c199501add240250f801b5f651f6be",
        "attribute_tokens": [],
        "bbox": [
            996,
            1454,
            1002,
            1493
        ],
        "mask": {
            "size": [
                1860,
                2880
            ],
            "counts": "bVBiYjIzUGoxM04xTzAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDBPMk5UYmReMg=="
        },
        "automatic_annotation": false
    },
  • After
{
        "token": "3dbe75ec855f9db4f35a1a457d88c7a0",
        "sample_data_token": "02382f82a702f04cdafa69d9eac0deed",
        "instance_token": "6ad49cbec66e7221dc2a3b6495bc7f9e",
        "category_token": "ff345f4048d27554367c25f35488510e",
        "attribute_tokens": [],
        "bbox": [
            1542,
            1815,
            1542,
            1852
        ],
        "mask": {
            "size": [
                2880,
                1860
            ],
            "counts": "VlZibzQxb2kyMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMGpVYjA="
        },
        "automatic_annotation": false
    },

test command

Reference

Notes for reviewer

@ktro2828 ktro2828 force-pushed the fix/segmentation-shape branch from ab2ff9c to d16ea5f Compare January 31, 2025 09:21
@ktro2828
Copy link
Contributor Author

Script to overwrite object_ann.json and surface_ann.json after transposing mask shape:

"""This file performs to convert segmentation mask shape from (height, width) order to (width, height) order.

USAGE:
    python3 convert_mask.py <DATA_ROOT>

NOTE:
`DATA_ROOT` is expected as follows:

```shell
DATA_ROOT/
├── <T4 Dataset2>
│   ├── annotations
│   ├── data
│   ...
├── <T4 Dataset2>
│   ├── annotations
│   ├── data
│   ...

"""

from future import annotations

import argparse
import base64
from glob import glob
import json
import os.path as osp
from typing import Any

from nuimages import NuImages
import numpy as np
import pycocotools.mask as cocomask

def convert(filepath: str) -> None:
"""Convert mask shape from (height, width) order to (width, height) order in a single dataset.

After convering mask shape, the original `object_ann.json` and `surface_ann.json` will be overwritten by modified files.

Args:
    filepath (str): Root path to the single T4 dataset.
"""
nuim = NuImages("annotation", filepath)

new_object_ann_list: list[dict[str, Any]] = []
for object_ann in nuim.object_ann:
    new_object_ann = object_ann.copy()

    mask = cocomask.decode(new_object_ann["mask"])
    rle = cocomask.encode(np.asfortranarray(mask.T))
    rle["counts"] = base64.b64encode(rle["counts"]).decode("ascii")
    new_object_ann["mask"] = rle

    new_object_ann_list.append(new_object_ann)

new_surface_ann_list: list[dict[str, Any]] = []
for surface_ann in nuim.surface_ann:
    new_surface_ann = surface_ann.copy()

    mask = cocomask.decode(new_surface_ann["mask"])
    rle = cocomask.encode(np.asfortranarray(mask.T))
    rle["counts"] = base64.b64encode(rle["counts"]).decode("ascii")
    new_surface_ann["mask"] = rle

    new_surface_ann_list.append(new_surface_ann)

with open(osp.join(filepath, "annotation/object_ann.json"), "w") as f:
    json.dump(new_object_ann_list, f, indent=4)

with open(osp.join(filepath, "annotation/surface_ann.json"), "w") as f:
    json.dump(new_surface_ann_list, f, indent=4)

def main() -> None:
"""Run process."""
parser = argparse.ArgumentParser()
parser.add_argument(
"data_root",
type=str,
help="Root direcotpy path storing target T4 datasets.",
)
args = parser.parse_args()

filepaths = glob(osp.join(args.data_root, "*"))
for filepath in filepaths:
    convert(filepath)

if name == "main":
main()

@ktro2828 ktro2828 closed this Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant