COCO dataset from custom semantic segmentation dataset for detectron2

username: dip4fish
cooked: <p>Hello,<br>
I have several datasets, made of pairs of images (greyscaled, groundtruth) looking like this:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/7/67d567810b226be677cc3901196d9e3b02b14d5f.png" alt="yZvLx" data-base62-sha1="eOyoyyNPLfCUUbu86aREBjyDYHZ" width="431" height="85"><br>
where the groundtruth labels can decomposed into three binary masks.</p>
<p>These datasets (<a href="https://github.com/jeanpat/DeepFISH/blob/master/dataset/Cleaned_LowRes_13434_overlapping_pairs.tar.xz" rel="noopener nofollow ugc">for example</a>) are available as a numpy array of shape (N, width, height, comp), or as pairs of png images also <a href="https://github.com/jeanpat/DeepFISH/tree/png-dataset/dataset/FullRes" rel="noopener nofollow ugc">available on github</a>.</p>
<p>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.</p>
<p>I would like to build a minimalist coco dataset from a pair of grey+groundtruth (or masks) images.<br>
Is there a tool available in PyTorch for that purpose?</p>
<p>I know there are two libraries (<a href="https://patrickwasp.com/create-your-own-coco-style-dataset/" rel="noopener nofollow ugc">pycococreator</a>, <a href="https://imantics.readthedocs.io/en/latest/index.html" rel="noopener nofollow ugc">imantics</a>) for this. I haven’t been successful up to now with pycococreator (<a href="https://colab.research.google.com/drive/1sC-oqYqv60VrL5yrQqCymkooXEWv-_QD" rel="noopener nofollow ugc">A draft colab notebook is available</a>).</p>
<p>For example, the following snippet fails:</p>
<pre><code class="lang-auto">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, 
                                                              category_info,
                                                              binary_mask,
                                                              image.size, tolerance=2)
</code></pre>

username: ptrblck
cooked: <p>What kind of error are you getting using the provided code snippet?</p>

username: dip4fish
cooked: <p>Hello,</p>
<p>Here’s <a href="https://colab.research.google.com/drive/1sC-oqYqv60VrL5yrQqCymkooXEWv-_QD" rel="nofollow noopener">what I get</a> in the colab notebook:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

&lt;ipython-input-27-ac798acc8333&gt; in &lt;module&gt;()
      2                                                                   category_info,
      3                                                                   binary_mask,
----&gt; 4                                                                   image.size, tolerance=2)

2 frames

/usr/local/lib/python3.6/dist-packages/PIL/Image.py in resize(self, size, resample, box)
   1866             )
   1867 
-&gt; 1868         size = tuple(size)
   1869 
   1870         if box is None:

TypeError: 'int' object is not iterable
</code></pre>
<p>By the way, I also tried imantics, but without example, it’s not that easy:</p>
<pre><code class="lang-auto">import imantics
imantics.Mask.polygons(mask1)
</code></pre>
<p>fails yielding:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

&lt;ipython-input-28-b97153601dd2&gt; in &lt;module&gt;()
----&gt; 1 imantics.Mask.polygons(mask1)

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

AttributeError: 'numpy.ndarray' object has no attribute '_c_polygons'
</code></pre>

username: ptrblck
cooked: <p>Try to pass the desired shape to <code>PIL.Image.resize</code> as a tuple instead of a single int to get rid of the first error.<br>
I’m not familiar with <code>imantics</code> <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=9" title=":confused:" class="emoji" alt=":confused:"></p>

username: dip4fish
cooked: <p>hello,<br>
Sorry for ultradelayed answer. The <a href="https://discuss.pytorch.org/t/my-json-file-doesnt-seem-to-be-a-legit-coco-dataset/91953">discusion is updated</a>. Here I tried to generate a legit coco dataset using only pycocotools.</p>