Replies: 4 comments 3 replies
-
@siwalex : I am also trying to do the same using binary mask (from A2D2 instance segmentation annotations) directly. Tried converting binary mask to RLE but seems to be a tidy process since I have more instances from different classes in a single binary mask. Let me know if you find any workarounds |
Beta Was this translation helpful? Give feedback.
-
@abdulazizm here goes a simple script to generate labels in coco format from pgn binary segmentation masks (multiple instances) for instance segmentation.
import datetime
import json
import os
import re
import fnmatch
from PIL import Image
import numpy as np
from pycococreator.pycococreatortools import pycococreatortools
ROOT_DIR = '/train/'
IMAGE_DIR =
ANNOTATION_DIR =
INFO = {
"description": "datasetX",
"url": "",
"version": "1.0",
"year": 2021,
"contributor": "AKM22",
"date_created": datetime.datetime.utcnow().isoformat(' ')
}
LICENSES = [
{
"id": 1,
"name": "Attribution-NonCommercial-ShareAlike License",
"url": "http://creativecommons.org/licenses/by-nc-sa/2.0/"
}
]
CATEGORIES = [
{
'id': 1,
'name': 'XYZ',
'supercategory': 'omega',
}]
def filter_for_jpeg(root, files):
file_types = ['*.png']
file_types = r'|'.join([fnmatch.translate(x) for x in file_types])
files = [os.path.join(root, f) for f in files]
files = [f for f in files if re.match(file_types, f)]
return files
def filter_for_annotations(root, files, image_filename):
file_types = ['*.png']
file_types = r'|'.join([fnmatch.translate(x) for x in file_types])
basename_no_extension = os.path.splitext(os.path.basename(image_filename))[0]
file_name_prefix = basename_no_extension + '.*'
files = [os.path.join(root, f) for f in files]
files = [f for f in files if re.match(file_types, f)]
files = [f for f in files if re.match(file_name_prefix, os.path.splitext(os.path.basename(f))[0])]
return files
def main():
coco_output = {
"info": INFO,
"licenses": LICENSES,
"categories": CATEGORIES,
"images": [],
"annotations": []
}
image_id = 1
segmentation_id = 1
# filter for jpeg images
for root, _, files in os.walk(IMAGE_DIR):
image_files = filter_for_jpeg(root, files)
# go through each image
for image_filename in sorted(image_files):
image = Image.open(image_filename)
image_info = pycococreatortools.create_image_info(
image_id, os.path.basename(image_filename), image.size)
coco_output["images"].append(image_info)
# filter for associated png annotations
for root, _, files in os.walk(ANNOTATION_DIR):
annotation_files = filter_for_annotations(root, files, image_filename)
# go through each associated annotation
for annotation_filename in sorted(annotation_files):
print(annotation_filename)
class_id = [x['id'] for x in CATEGORIES if x['name'] in annotation_filename][0]
category_info = {'id': class_id, 'is_crowd': 'crowd' in image_filename}
binary_mask = np.asarray(Image.open(annotation_filename)
.convert('1')).astype(np.uint8)
annotation_info = pycococreatortools.create_annotation_info(
segmentation_id, image_id, category_info, binary_mask,
image.size, tolerance=2)
if annotation_info is not None:
coco_output["annotations"].append(annotation_info)
segmentation_id = segmentation_id + 1
image_id = image_id + 1
with open('{}/instances_images.json'.format(ROOT_DIR), 'w') as output_json_file:
json.dump(coco_output, output_json_file)
if __name__ == "__main__":
main() |
Beta Was this translation helpful? Give feedback.
-
Training on detectron2 for instance segmentation. Encoding of bitmasks is using RLE instead of polygons. I read in some articles that when encoding instance masks (that has holes) via polygon annotations, they need to be treated with extra steps to account for the hole contours. I am not unsure that I won't break anything while modifying detectron's backend codes to achieve this so I chose the encode the masks using RLE. The masks can be encoded and decoded properly while preserving the holes. Modifying this section from the balloon example from detectron's colab notebook with the dataset structure like this: └─── your_dataset └─── train └─── [image_id]_color.jpg └─── [image_id]_instances.png └─── [image_id]_class_labels.png └─── val └─── [image_id]_color.jpg └─── [image_id]_instances.png └─── [image_id]_class_labels.png
Followed up by registering dataset
Also add this line to use bitmask Overall config should look something like this
|
Beta Was this translation helpful? Give feedback.
-
If you are using binary masks, I used this repo to convert my binary segmentation masks into coco json files: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I have the same problem than this guys : #742
i use this colab (official) : https://colab.research.google.com/drive/16jcaJoc6bCFAQ96jDe2HwtXj7BMD_-m5
and i want to train a dataset from masks. not labeling.
i'm sorry but i don't understand the issue200 : #200
that is suppose to resolve my problem.
Can you explain to me how i'm suppose to do/change in the colab to train with masks?
Thanks.
Alexandre
Beta Was this translation helpful? Give feedback.
All reactions