Label-studio: RLE -> Mask for semantic segmentation

Created on 31 Jul 2020  路  6Comments  路  Source: heartexlabs/label-studio

Hello. I have a problem with loading the mask from RLE. So, I got the json file from labelstudio that had the rle format. I tried decoding it with all the open source codes and nothing works. I also tried using this :https://github.com/thi-ng/umbrella , but unfortunately it didnt work.

After decoding and exporting it, I tried loading it in python. The decoded result is of length 22251060, while my image is 2313 x 2405. The shapes do not match, so I cannot make it as a mask.

What is the correct way of changing the RLE to mask?

Thanks a lot

bug

Most helpful comment

I'm not a project developer, but here is a Python function which can be used to decode RLEs:

import numpy as np

def rle_decode(rle_str, shape, fill_value=1, dtype=int, relative=False):
    """
    Args:
        rle_str (str): rle string
        shape (Tuple[int, int]): shape of the output mask
        relative: if True, rle_str is relative encoded string
    """
    s = rle_str.strip().split(" ")
    starts, lengths = np.array([np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])])
    mask = np.zeros(np.prod(shape), dtype=dtype)
    if relative:
        start = 0
        for index, length in zip(starts, lengths):
            start = start + index
            end = start + length
            mask[start:end] = fill_value
            start = end
        return mask.reshape(shape[::-1]).T
    else:
        starts -= 1
        ends = starts + lengths
        for lo, hi in zip(starts, ends):
            mask[lo:hi] = fill_value
        return mask.reshape(shape[::-1]).T

All 6 comments

I used brush for semantic segmentation btw.

I'm not a project developer, but here is a Python function which can be used to decode RLEs:

import numpy as np

def rle_decode(rle_str, shape, fill_value=1, dtype=int, relative=False):
    """
    Args:
        rle_str (str): rle string
        shape (Tuple[int, int]): shape of the output mask
        relative: if True, rle_str is relative encoded string
    """
    s = rle_str.strip().split(" ")
    starts, lengths = np.array([np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])])
    mask = np.zeros(np.prod(shape), dtype=dtype)
    if relative:
        start = 0
        for index, length in zip(starts, lengths):
            start = start + index
            end = start + length
            mask[start:end] = fill_value
            start = end
        return mask.reshape(shape[::-1]).T
    else:
        starts -= 1
        ends = starts + lengths
        for lo, hi in zip(starts, ends):
            mask[lo:hi] = fill_value
        return mask.reshape(shape[::-1]).T

Dear @zakajd,

Thanks for replying. This function does not work for odd number length of RLE.
Even if the length of RLE is an even number, although it runs, it does not give the correct output. I have tried using the JavaScript library that they provide for encoding-decoding, however even when i decode that RLE it seems extremely wrong..wrong size of dimensions. Seems that label studio uses a custom rle encoding.

Sincerely,
Veljko KOVAC

@vkovac2 I've just released the brush converter to numpy & png formats.
Please, check this PR from label-studio-converter: https://github.com/heartexlabs/label-studio-converter/pull/9

1 Clone this repo
[email protected]:heartexlabs/label-studio-converter.git

2 cd label-studio-converter

3 activate PR branch:
git checkout brush-converter

3 activate your virtualenv if you use it
source <env>/bin/activate

4 install PR as package
pip install -e .

5 start your label-studio
label-studio start <my_project>

6 go to Export page in browser and check BRUSH_TO_NUMPY and BRUSH_TO_PNG options.

If you need more customized export then dive into brush.py:
https://github.com/heartexlabs/label-studio-converter/pull/9/commits/8eb02c4bc2c4cf99c84c308868dc186ab61490b2#diff-8c13b5aae0f94c3f59fe7a98aca8501e

Worked for me. Added a comment on the code that i need to put in order to work. Thanks for adding this function.

Now you can do

pip uninstall label-studio
pip install label-studio==0.7.4

It contains fixed brushes with correct resize behavior, correct multiple brush results in one completion fix and export to PNG/Numpy.
It will be appreciate if you check this release candidate version.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mimre25 picture mimre25  路  6Comments

atakanokan picture atakanokan  路  3Comments

Rishabh-hyperverge picture Rishabh-hyperverge  路  6Comments

fakyras picture fakyras  路  3Comments

andreaferretti picture andreaferretti  路  5Comments