question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to preprocess mask for multiclass segmentation?

See original GitHub issue

Hello. 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:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

26reactions
garellicommented, Jul 15, 2019

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.

img.shape = (128,128,3)
list= []
for i in range(128):
    for j in range(128):
        list.append(img[i,j,:].tolist())

def uniq(lst):
    last = object()
    for item in lst:
        if item == last:
            continue
        yield item
        last = item

def sort_and_deduplicate(l):
    return list(uniq(sorted(l, reverse=True)))

palette = sort_and_deduplicate(list)
palette
[[128.0, 128.0, 0.0],
 [128.0, 0.0, 0.0],
 [0.0, 128.0, 0.0],
 [0.0, 0.0, 128.0],
 [0.0, 0.0, 0.0]]

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:

one_hot_map = []
for colour in palette:
    class_map = tf.reduce_all(tf.equal(img2, colour), axis=-1)
    one_hot_map.append(class_map)
one_hot_map = tf.stack(one_hot_map, axis=-1)
one_hot_map = tf.cast(one_hot_map, tf.float32)

with tf.Session() as sess:
    print (sess.run(one_hot_map))

[[[0. 0. 0. 0. 1.]
  [0. 0. 0. 0. 1.]
  [0. 0. 0. 0. 1.]
  ...
  [0. 0. 0. 0. 1.]
  [0. 0. 0. 0. 1.]
  [0. 0. 0. 0. 1.]]

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

3reactions
qubvelcommented, Aug 9, 2019

There is an example in examples/ dir now

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found