COCO dataset from custom semantic segmentation dataset for detectron2

I have several datasets, made of pairs of images (greyscaled, groundtruth) looking like this:

where the groundtruth labels can decomposed into three binary masks.

These datasets (for example) are available as a numpy array of shape (N, width, height, comp), or as pairs of png images also available on github.

The project would be to train different semantic/ instance segmentation models available in Detectron2 on these datasets. I understand that detectron 2 needs a COCO formatted dataset to work on.

I would like to build a minimalist coco dataset from a pair of grey+groundtruth (or masks) images.
Is there a tool available in PyTorch for that purpose?

I know there are two libraries (pycococreator, imantics) for this. I haven’t been successful up to now with pycococreator (A draft colab notebook is available).

For example, the following snippet fails:

from pycococreatortools import pycococreatortools

N = 1
grey = data[N,:,:,0]
labels = data[N,:,:,1]
mask1 = labels == 1
mask2 = labels == 2
mask3 = labels == 3

segmentation_id = "chromosome1_1" 
image_id = "0001"
category_info = "chromosome1"
binary_mask = mask1
image = grey
print(image.size, image.shape)

annotation_info_mask1 = pycococreatortools.create_annotation_info(segmentation_id, image_id, 
                                                              image.size, tolerance=2)

What kind of error are you getting using the provided code snippet?


Here’s what I get in the colab notebook:


TypeError                                 Traceback (most recent call last)

<ipython-input-27-ac798acc8333> in <module>()
      2                                                                   category_info,
      3                                                                   binary_mask,
----> 4                                                                   image.size, tolerance=2)

2 frames

/usr/local/lib/python3.6/dist-packages/PIL/ in resize(self, size, resample, box)
   1866             )
-> 1868         size = tuple(size)
   1870         if box is None:

TypeError: 'int' object is not iterable

By the way, I also tried imantics, but without example, it’s not that easy:

import imantics

fails yielding:


AttributeError                            Traceback (most recent call last)

<ipython-input-28-b97153601dd2> in <module>()
----> 1 imantics.Mask.polygons(mask1)

/usr/local/lib/python3.6/dist-packages/imantics/ in polygons(self)
    798         :rtype: :class:`Polygons`
    799         """
--> 800         if not self._c_polygons:
    802             # Generate polygons from mask

AttributeError: 'numpy.ndarray' object has no attribute '_c_polygons'

Try to pass the desired shape to PIL.Image.resize as a tuple instead of a single int to get rid of the first error.
I’m not familiar with imantics :confused:

Sorry for ultradelayed answer. The discusion is updated. Here I tried to generate a legit coco dataset using only pycocotools.