Detectron2: How to output prediction in pred_masks_rle instead of pred_masks

Created on 19 Nov 2019  路  4Comments  路  Source: facebookresearch/detectron2

I'm running instance segmentation inference on a model trained with custom dataset:

predictor = DefaultPredictor(cfg)
outputs = predictor(im)

I need to save "outputs" dict to filesystem, but it has very big size, because prediction is saved in outputs['instances'].pred_masks
How can I save predictions in in outputs['instances'].pred_masks_rle instead of outputs['instances'].pred_masks, that is more memory efficient?
Thanks

invaliunrelated

Most helpful comment

Ok I solved using pycocotools, I put the code below in case it's useful to someone.

Now I use instances_to_json to convert instances to json, then I save it to disk.
Is there an opposite function converting json to instances?
Thanks

import pycocotools.mask as mask_util

instances = outputs['instances']

instances.pred_masks_rle = [mask_util.encode(np.asfortranarray(mask)) for mask in instances.pred_masks]
for rle in instances.pred_masks_rle:
    rle['counts'] = rle['counts'].decode('utf-8')

instances.remove('pred_masks')

# TO TEST INVERT CONVERSION
instances.pred_masks = np.stack([mask_util.decode(rle) for rle in instances.pred_masks_rle])

All 4 comments

The "RLE" format is defined by COCO api and you can use COCO api to convert masks to RLE. That's unrelated to detectron2.

Ok sorry, anyway if you can suggest any possible way to save efficiently instance prediction to filesystem, I will appreciate.

Using COCO api to convert masks to RLE is an efficient way.

Ok I solved using pycocotools, I put the code below in case it's useful to someone.

Now I use instances_to_json to convert instances to json, then I save it to disk.
Is there an opposite function converting json to instances?
Thanks

import pycocotools.mask as mask_util

instances = outputs['instances']

instances.pred_masks_rle = [mask_util.encode(np.asfortranarray(mask)) for mask in instances.pred_masks]
for rle in instances.pred_masks_rle:
    rle['counts'] = rle['counts'].decode('utf-8')

instances.remove('pred_masks')

# TO TEST INVERT CONVERSION
instances.pred_masks = np.stack([mask_util.decode(rle) for rle in instances.pred_masks_rle])
Was this page helpful?
0 / 5 - 0 ratings

Related issues

invisprints picture invisprints  路  4Comments

kl720 picture kl720  路  3Comments

ItamarSafriel picture ItamarSafriel  路  4Comments

Cold-Winter picture Cold-Winter  路  3Comments

soumik12345 picture soumik12345  路  3Comments