How to preprocess mask for multiclass segmentation?
See original GitHub issueHello. Images and masks in PNG format. 8 Classes. How to preprocess masks for categorical_crossentropy loss right?
I tried to use (128, 128, 1) mask, setting each pixel of the mask with a number from 0 to 7, but got error: Error when checking target: expected softmax to have shape (128, 128, 8) but got array with shape (128, 128, 1)
Code for model creation: `BACKBONE = ‘resnet50’ preprocess_input = get_preprocessing(BACKBONE)
model = Unet(BACKBONE, input_shape=(128,128,3), encoder_weights=‘imagenet’, encoder_freeze=True, classes=8, activation=‘softmax’)
model.compile(‘Adam’, loss=‘categorical_crossentropy’, metrics=[iou_score])`
Thanks. Sorry for bad English.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:11 (1 by maintainers)
Top Results From Across the Web
How to pre-process RGB segmentation mask for multi-class ...
I want to create semantic segmentation masks from the RGB masks, by assigning integer values to the pixels in the range 0-23 (where...
Read more >How to prepare masks for multiclass semantic segmentation?
It's very straightforward for binary semantic segmentation: black color (0s) is responsible for background, whereas white color (1s) is responsible for objects ...
Read more >RGB Mask to Single Channel Mask for Multiclass Segmentation
This video will learn how to process an RGB (Red Green Blue) mask into a single channel mask for multiclass segmentation. Here, we...
Read more >How To Pre-Process Rgb Segmentation Mask For ... - ADocLib
For the sake of convenience, let's subtract 1 from the segmentation mask, resulting in plt.imshow(tf.keras.preprocessing.image.array_to_img( ...
Read more >Multi-class Image Segmentation with Unet | by Kian - Medium
Every RGB image (in jpg format) has an accompanying grayscale and pixel indexed segmentation mask (in png format) where each index value ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I have the answer;
You need indeed to convert your RGB mask to a one-hot encoding image with shape (H,W,Channels) with Channels equals to the number of classes (containing the background).
Imagine you have an image/array (a mask) of shape (128,128,3). First you need to notice the unique elements which are corresponding to a label.
palette is your current label pixels values in your RGB MASK (the unique elements)
Then, you can use a function like this to convert it to one-hot encoding:
Here is for the processing of the masks (in this example you will have a (128,128,5) one-hot encoding mask.
And you can run
define model with output of N classes, where N > 1
model = Unet(‘resnet34’, classes=5, activation=‘softmax’)
for multiclass segmentation choose another loss and metric
model.compile(‘Adam’, loss=‘categorical_crossentropy’, metrics=[‘categorical_accuracy’])
Once your model is trained, the predict function will outputs a (128,128,5) mask with probability inside it.
Then you can use argmax function with numpy to convert it to (128,128,3) true image and visualize results.
Hope it will helps
There is an example in
examples/
dir now