zhixuhao / unet

unet for image segmentation
MIT License
4.58k stars 2k forks source link

predicted image is gray image not binary image #132

Open avinash2222 opened 5 years ago

Yuanzhisheng commented 5 years ago

I have the same problem and hope to ask why it is like that.

Machuntox commented 5 years ago

I have the same problem. My predictions turn out to be grey. Any solutions?

Yuanzhisheng commented 5 years ago

I think it is because some functions in keras is out of date. Some functions are deprecated and below are the warnings when I run the program in the terminal.

WARNING: Logging before flag parsing goes to stderr. W0709 11:53:23.746956 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.

W0709 11:53:23.759259 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

W0709 11:53:23.761847 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:4185: The name tf.truncated_normal is deprecated. Please use tf.random.truncated_normal instead.

W0709 11:53:23.785439 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:3976: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.

W0709 11:53:23.865253 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:133: The name tf.placeholder_with_default is deprecated. Please use tf.compat.v1.placeholder_with_default instead.

I think it is inner problem of Keras, so I may try other GitHub programs.

Machuntox commented 5 years ago

I think it is because some functions in keras is out of date. Some functions are deprecated and below are the warnings when I run the program in the terminal.

WARNING: Logging before flag parsing goes to stderr. W0709 11:53:23.746956 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.

W0709 11:53:23.759259 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

W0709 11:53:23.761847 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:4185: The name tf.truncated_normal is deprecated. Please use tf.random.truncated_normal instead.

W0709 11:53:23.785439 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:3976: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.

W0709 11:53:23.865253 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:133: The name tf.placeholder_with_default is deprecated. Please use tf.compat.v1.placeholder_with_default instead.

I think it is inner problem of Keras, so I may try other GitHub programs.

Thank you for the answer. I found a solution, which works for your images. In the data.py you have to convert every "img = img / 255" to "img = img / 255." This sometimes solves the problem.

avinash2222 commented 5 years ago

this is callled image normalization, I have already written it

amorrissette commented 5 years ago

Predicted images from the model are the output of a sigmoid function which will be values between 0 and 1 like a "gray scale" image. To get a binary image you can just apply a threshold to the model output (setting values below 0.5 to 0 and above to 1).

Here is the last layer of the unet model: conv10 = Conv2D(1, 1, activation = 'sigmoid')(conv9) See here for more details.

So not a bug in keras or the code but rather a feature of the model.

Machuntox commented 5 years ago

Predicted images from the model are the output of a sigmoid function which will be values between 0 and 1 like a "gray scale" image. To get a binary image you can just apply a threshold to the model output (setting values below 0.5 to 0 and above to 1).

Here is the last layer of the unet model: conv10 = Conv2D(1, 1, activation = 'sigmoid')(conv9) See here for more details.

So not a bug in keras or the code but rather a feature of the model.

could you give me a code example to do exactly that? I know what you mean, but how do I change the sigmoid function to do exactly that?

amorrissette commented 5 years ago

Sure assuming you get a predicted image from the model like this: predicted_image_gray = model.predict(input_image) predicted_image_gray should be a numpy array with values(<type 'numpy.float32'>

You can just get a binary (actually a boolean) array with: predicted_image_binary = predicted_image_gray > 0.5 Now the values within this numpy array will be <type 'numpy.bool_'>

Machuntox commented 5 years ago

Sure assuming you get a predicted image from the model like this: predicted_image_gray = model.predict(input_image) predicted_image_gray should be a numpy array with values(<type 'numpy.float32'>

You can just get a binary (actually a boolean) array with: predicted_image_binary = predicted_image_gray > 0.5 Now the values within this numpy array will be <type 'numpy.bool_'>

Thank you, I'll try that tomorrow. Just for my understanding, will this trick turn the generated image into black and white pixels? If so, I think my problem is a different one. The problem is not, that the image has different pixels. The problem is, that every pixel has the same color, which is grey. It is basically just one grey pixel.

See my repo: https://colab.research.google.com/drive/13i18jJbHRswCgCg8Nmz79c_M4M8JQFxG

deaspo commented 5 years ago

Sure assuming you get a predicted image from the model like this: predicted_image_gray = model.predict(input_image) predicted_image_gray should be a numpy array with values(<type 'numpy.float32'> You can just get a binary (actually a boolean) array with: predicted_image_binary = predicted_image_gray > 0.5 Now the values within this numpy array will be <type 'numpy.bool_'>

Thank you, I'll try that tomorrow. Just for my understanding, will this trick turn the generated image into black and white pixels? If so, I think my problem is a different one. The problem is not, that the image has different pixels. The problem is, that every pixel has the same color, which is grey. It is basically just one grey pixel.

See my repo: https://colab.research.google.com/drive/13i18jJbHRswCgCg8Nmz79c_M4M8JQFxG

Go and change the following line 125 in data.py io.imsave(os.path.join(save_path,"%d_predict.png"%i),img_as_ubyte(img)) to io.imsave(os.path.join(save_path,"%d_predict.tif"%i),img_as_float(img))

See if saving in .tif format works for you...

chen-chunling commented 3 years ago

I think it is because some functions in keras is out of date. Some functions are deprecated and below are the warnings when I run the program in the terminal. WARNING: Logging before flag parsing goes to stderr. W0709 11:53:23.746956 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead. W0709 11:53:23.759259 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead. W0709 11:53:23.761847 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:4185: The name tf.truncated_normal is deprecated. Please use tf.random.truncated_normal instead. W0709 11:53:23.785439 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:3976: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead. W0709 11:53:23.865253 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:133: The name tf.placeholder_with_default is deprecated. Please use tf.compat.v1.placeholder_with_default instead. I think it is inner problem of Keras, so I may try other GitHub programs.

Thank you for the answer. I found a solution, which works for your images. In the data.py you have to convert every "img = img / 255" to "img = img / 255." This sometimes solves the problem.

May I ask is there any difference between "img = img / 255" and "img = img / 255"? Why in the data.py I need to convert every "img = img / 255" to "img = img / 255."

deaspo commented 3 years ago

I think it is because some functions in keras is out of date. Some functions are deprecated and below are the warnings when I run the program in the terminal. WARNING: Logging before flag parsing goes to stderr. W0709 11:53:23.746956 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead. W0709 11:53:23.759259 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead. W0709 11:53:23.761847 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:4185: The name tf.truncated_normal is deprecated. Please use tf.random.truncated_normal instead. W0709 11:53:23.785439 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:3976: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead. W0709 11:53:23.865253 4589221312 deprecation_wrapper.py:119] From /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:133: The name tf.placeholder_with_default is deprecated. Please use tf.compat.v1.placeholder_with_default instead. I think it is inner problem of Keras, so I may try other GitHub programs.

Thank you for the answer. I found a solution, which works for your images. In the data.py you have to convert every "img = img / 255" to "img = img / 255." This sometimes solves the problem.

May I ask is there any difference between "img = img / 255" and "img = img / 255"? Why in the data.py I need to convert every "img = img / 255" to "img = img / 255."

Yes, Case for img= img / 255 the resultant img has integer values. The other case the img are float values. Definitely there is loss during conversion to integers, and most numpy functions are of type float32.

chen-chunling commented 3 years ago

May I ask is there any difference between "img = img / 255" and "img = img / 255"? Why in the data.py I need to convert every "img = img / 255" to "img = img / 255."

Yes, Case for img= img / 255 the resultant img has integer values. The other case the img are float values. Definitely there is loss during conversion to integers, and most numpy functions are of type float32.

Thanks for your reply, and I didn't notice the decimal point under the number 255 :) @deaspo