I am about to start a project on semantic segmentation with a grayscale mask. The thing is, we have to detect for each pixel of the image if its an object or the background (binary class problem). I struggle to relate this pixel binary classification task with a mask labelling, since each pixel will be in a range [0,255]. I have started implementing an U-net with Keras according to this methodology (being fairly new with keras).

  • What kind of loss would you use? - I was thinking of binary_crossentropy
  • What kind of labelling would you use? And therefore, what would be the output shape of the CNN if I do this binary classification pixelwise?

Sorry if I do not use the proper technical terms.


1 Answer 1


let me see if I can help.

(1) I would definitely recommend binary crossentropy for your loss function.

(2) Your labels should be "masks", which are images (the same size as your input images) where your "0-class" pixels are 0's and your "1-class" pixels are 1's. This is basically a black and white image where black and white represent the 2 different classes. The output of the UNET network will be a single channel image (the same dimensions as the input) where your "0-class" is colored black and your "1-class" is colored white.

Here is an example: enter image description here

The output mask in this image is what your label ("mask") should look like. If you train your network right, it should also be the output of UNET. Train for about 40 epochs and you will start to see results.

  • 1
    $\begingroup$ Thank you for your answer. My question is now more about the output of the CNN. Since it is a two class classification, what would my output look like? For now it is a two layers output layer, but I guess it should retrieve two layers where each layer is the probability to belong to each class? $\endgroup$ Nov 14, 2019 at 12:44
  • 1
    $\begingroup$ The output of the CNN is a single channel image (for instance 512x512) where the pixel values range between [0-1]. Values closer to 1 mean the network is more confident that the particular pixel belongs to the "1-class". Values closer to 0 mean the network is more confident the particular pixel belongs to the "0-class". $\endgroup$ Nov 14, 2019 at 17:06
  • 1
    $\begingroup$ Take a look at this implementation too: github.com/zhixuhao/unet $\endgroup$ Nov 14, 2019 at 17:08
  • $\begingroup$ Did this solve your issue? Please upvote and hit the check button if it did. Thank you! $\endgroup$ Nov 19, 2019 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.