jakeret / tf_unet

Generic U-Net Tensorflow implementation for image segmentation
GNU General Public License v3.0
1.9k stars 748 forks source link

Wrong Mask for Binary Classes #264

Closed siavashk closed 5 years ago

siavashk commented 5 years ago

If I understand correctly, given a binary mask for an image, this line creates its compliment for the negative class:

https://github.com/jakeret/tf_unet/blob/master/tf_unet/image_util.py#L64

In a binary classification setting, if the background is 0 and the object of interest is 1, this line is supposed to create another mask where the background is 1 and the object of interest is 0.

The issue is that this does not do what you think it does. Assume that the mask is np.array([0, 1, 0]). If you perform a logical not operation on this array, you will get array([-1, -2, -1]). You can verify this in the interactive shell.

jakeret commented 5 years ago

This is indeed the intention of the code. When I run it on py36 I get

>>> import numpy as np
>>> label = np.array([0,1,0], dtype=np.bool)
>>> label
array([False,  True, False])
>>> ~label
array([ True, False,  True])

Wondering why you get a different result

siavashk commented 5 years ago

I am on the bus, so I cannot verify this right now. I didn’t specify the datatype when defining the mask. What if you did the same thing without dtype=np.bool?

jakeret commented 5 years ago

Ok then I'll get the same result as you. However in the code I explicitly specify the data type to be bool

siavashk commented 5 years ago

I inherited from BaseDataProvider to supply my own data and I must have missed the label's datatype. Maybe it should be the responsibility of the child class to make sure that the datatype is np.bool. However, I think that this is a common mistake, maybe one should do a check before performing bitwise logical not? Something like this around line 64:

if label.dtype != 'bool':
    label = label.astype(np.bool)
jakeret commented 5 years ago

Ok I see. Yes I think you're right

siavashk commented 5 years ago

Closing due to inactivity