1

I currently have a keras-model that classifies each pixel as either belonging to object or background using a fully convolutional network. As I have only 2 classes (object or background) I am using a sigmoid activation in the output layer and binary_crossentropy loss.

However, for several images I am unsure of what class some of the pixels belong to, therefore I would like to label these pixels as void such that the networks weights are not updated based on the prediction for these pixels. Is there a method for dealing with void labels in Keras?

One way of doing this in general would be to specify loss weights for each pixel, and set the weight for void pixels to 0. (E.g. like this for tensorflow). I investigated using sample_weights in Keras, but I could not get that to work as it does not seem to be made for weighting pixels.

mclearner
  • 21
  • 4
  • > that functionality does not seem to be intended for this purpose. If it works, it works. I would use that, but make sure you comment it clearly. – collector Nov 28 '17 at 15:57
  • Sorry. I was not clear enough. I was not able to use the sample_weights functionality for image segmentation. And I don't think it is possible. I have edited the text. – mclearner Nov 28 '17 at 16:45

1 Answers1

0

So it turns out that sample_weight can be used after all. As the sample weight can only have 2 dimensions (batch_size, no of predictions), the trick was to reshape the output from the model to be flat.

The last 2 lines of my model definition:

model.add(Reshape((-1, 1)))
model.compile(loss='binary_crossentropy', optimizer='adam', sample_weight_mode='temporal', metrics=['binary_accuracy'])

I use Reshape((-1,1)) because my input images have variable dimensions. When I train the model I then also have to use flattened labels and sample_weight.

I found the answer here.

mclearner
  • 21
  • 4