Tiz
Tiz

Reputation: 51

Binary semantic Segmentation with Deeplabv3+ keras (designed for multiclass semantic segmentation)

I am new to Keras so sorry if the question is silly.

I found here https://keras.io/examples/vision/deeplabv3_plus/ the deeplabv3+ model to perform multiclass semantic segmentation. I need to adapt this code to another purpose, because I need to perform binary semantic segmentation on medical images. Is it correct to change from

NUM_CLASSES = 20

to NUM_CLASSES = 1?

If I put NUM_CLASSES = 2, I get an error about mismatch between logits and labels.

About the loss function, the code line is loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True)

I thought to change it to

loss = keras.losses.BinaryCrossentropy(from_logits=True)

but loss becomes negative. Should I add something else?

Thank you!

Edit: the deeplabv3+ for multiclass semantic segmentation uses keras.activations.linear(x) in the last layer. For my purpose, should I use softmax instead of keras.activations.linear(x) with BinaryCrossEntropy and put from_logits=False?

Upvotes: 1

Views: 1665

Answers (2)

Leila Cml
Leila Cml

Reputation: 1

And do you know how to change the end of the code to visualize the masks generated by the model ? (https://keras.io/examples/vision/deeplabv3_plus/)

Upvotes: 0

Tiz
Tiz

Reputation: 51

I solved the problem, if anyone needs the answer: "... for binary segmentation, it's preferable to keep NUM_CLASS = 1 since you're trying to predict a binary mask that represents a single class against the background. If you wish to predict a one-hot-encoded segmentation mask (in the current context, setting NUM_CLASS = 2), only then use softmax activation along with sparse categorical cross-entropy loss otherwise use sigmoid activation along with binary cross-entropy."

https://github.com/keras-team/keras-io/issues/648

Upvotes: 1

Related Questions