David Biggs
David Biggs

Reputation: 41

COCO annotations to masks - For instance segmentation model training

I am trying to convert my COCO annotations to a mask. Each instance in the COCO annotations need to be represented as an unique instance in the mask. I found this question which does convert the annotations to a mask but there are only two unique instances of objects within the mask.

Here is an image of a mask. One can see that there are only two distinct instances (colors). I need every object to be a unique instance (color)

Is there another technique I can use to preserve ALL individual instances?

Many thanks.

Upvotes: 4

Views: 4573

Answers (1)

ylpjört
ylpjört

Reputation: 138

Referring to the question you linked, you should be able to achieve the desired result by simply avoiding the following loop where the individual masks are combined:

mask = coco.annToMask(anns[0])
for i in range(len(anns)):
    mask += coco.annToMask(anns[i])  

For example, the following code creates subfolders by appropriate annotation categories and saves black and white masks in the corresponding folders with the name of the images to which the masks belong:

from pycocotools.coco import COCO
import os
from matplotlib import image
from pathlib import Path

img_dir = "./your_coco_dataset_folder/images"
annFile = "./your_coco_dataset_folder/annotations/instances.json"

coco=COCO(annFile)

# Get category IDs and annotation IDs
catIds = coco.getCatIds()
annsIds = coco.getAnnIds()

# Create folders named after annotation categories
for cat in catIds:
    Path(os.path.join("./your_output_folder",coco.loadCats(cat)[0]['name'])).mkdir(parents=True, exist_ok=True)

for ann in annsIds:
    # Get individual masks
    mask = coco.annToMask(coco.loadAnns(ann)[0])
    # Save masks to BW images
    file_path = os.path.join("./your_output_folder",coco.loadCats(coco.loadAnns(ann)[0]['category_id'])[0]['name'],coco.loadImgs(coco.loadAnns(ann)[0]['image_id'])[0]['file_name'])
    image.imsave(file_path, mask, cmap="gray")

If I understand correctly, you have multiple masks of one category for individual images. In this case, the masks would have to be assigned to lists, for instance, and processed accordingly. Or in the above example, the individual image files would have to be uniquely named for saving (see, e.g, here). Creating different colors is then just a matter of further processing, e.g., with numpy.

All this under the condition that the desired object masks are really defined as individual instances in your COCO data set.

Upvotes: 3

Related Questions