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
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])
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