Generating Semantic Segmentation Mask using Labelme
Well, had you heard about Labelme? If you hadn’t hear about it, Labelme is the common annotation tool for creating polygon for segmentation data. While the main format remains a native Labelme format, now they supported conversion to another format such as COCO and PascalVOC.
Here is the main repository of Labelme
Installation
Depending on the operating system, you can see the installation options. Directly, I will use the most pythonic way using pip. In this case, we will create a new virtual environment, but actually it’s very optional. It’s often necessary to install the pyqt5 library.
# python3
conda create --name=labelme python=3
source activate labelme
# conda install -c conda-forge pyside2
# conda install pyqt
# pip install pyqt5 # pyqt5 can be installed via pip on python3
pip install labelme
Annotating the Data
Just imagine this project aims to identify the presence of platelets in a blood smear from various blood clotting cases. We will use as a sample image, the image of red blood cells and platelets. I will only label the platelets in this case.
Click on create a polygon, or with shortcut key W. I will prefer to autosave the annotation using the option in File.
You need to annotate all of platelets (or on your case, the desired) objects so the training shouldn’t confuse them with the background.
After finished, save the annotation.
Convert Annotation to PascalVOC and Draw Segmentation Mask
The complete steps is explained here, but I will briefly explain it in this case.
Now, you will have the JSON annotation file in Labelme format. You need to convert it to segmentation mask.
You need to put the objects into labels.txt file. I will just use nano and add the following objects into it.
After that, you can validate the label.
labelme <FOLDER_NAME> --labels labels.txt --nodata --validatelabel exact --config '{shift_auto_shape_color: -2}'
Then, as you can see in the repository, there is a script to convert and generate the semantic segmentation mask named labelme2voc.py. We will use the code, so it’s better to clone the repository.
I guess it will be impractical. Would create a GUI later.
As you can see, now I have train_voc folder from this command.
The segmentation mask will be inside the SegmentationClassPNG folder.
That’s all. Commonly, the desired format will be like this (at least since I use AWS Sagemaker fine tuning service).
train
├── class_names.txt
├── images
│ └── Platelets2.jpg
├── masks
│ └── Platelets2.png
Good luck trying with your own images!
About the Author
Salman is the Chief Data Officer at Allure AI, an emerging beauty-tech startup in Indonesia. He was graduated from Astronomy and Astrophysics studies at Institut Teknologi Bandung. An avid reader, mountaineer, and developing interests in astronomy and computational neuroscience.
Previously, he had an internship as an AI engineer in Konvergen AI, a software engineer at Chatbiz.id, and had a research assistantship in the astronomy department, as well as assisting various courses in astronomy, computational science, and management department.