Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: logits and labels must have the same shape #37

Open
dpoiesz opened this issue Oct 8, 2020 · 4 comments
Open

ValueError: logits and labels must have the same shape #37

dpoiesz opened this issue Oct 8, 2020 · 4 comments
Labels
question Further information is requested

Comments

@dpoiesz
Copy link

dpoiesz commented Oct 8, 2020

I receive the above error when attempting to train the vanilla_unet. The pipeline works for a unet I wrote that uses "same" padding but was hoping to try the vanilla_unet for it's "valid" padding. I'm not sure what else in my code could have caused this error and would have expected these conditions to have been handled inside the vanilla_unet. Any Recommendations?

initial stack trace below:

Traceback (most recent call last):
File "C:\Users\dpoiesz\Repos\bespin\venv\bespin\lib\site-packages\tensorflow_core\python\framework\tensor_shape.py", line 926, in merge_with
new_dims.append(dim.merge_with(other[i]))
File "C:\Users\dpoiesz\Repos\bespin\venv\bespin\lib\site-packages\tensorflow_core\python\framework\tensor_shape.py", line 309, in merge_with
self.assert_is_compatible_with(other)
File "C:\Users\dpoiesz\Repos\bespin\venv\bespin\lib\site-packages\tensorflow_core\python\framework\tensor_shape.py", line 276, in assert_is_compatible_with
(self, other))
ValueError: Dimensions 256 and 68 are not compatible

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\dpoiesz\Repos\bespin\venv\bespin\lib\site-packages\tensorflow_core\python\ops\nn_impl.py", line 167, in sigmoid_cross_entropy_with_logits
labels.get_shape().merge_with(logits.get_shape())
File "C:\Users\dpoiesz\Repos\bespin\venv\bespin\lib\site-packages\tensorflow_core\python\framework\tensor_shape.py", line 929, in merge_with
raise ValueError("Shapes %s and %s are not compatible" % (self, other))
ValueError: Shapes (None, 256, 256, 1) and (None, 68, 68, 1) are not compatible

@karolzak karolzak added the question Further information is requested label Oct 11, 2020
@karolzak
Copy link
Owner

Hi @dpoiesz !
The answer to your problem is in this single line:

ValueError: Shapes (None, 256, 256, 1) and (None, 68, 68, 1) are not compatible

I assume that your input images and masks are of size 256x256?
Since vanilla_unet is using valid padding (as described in the original U-Net research paper the actual output mask size is smaller than the input size (should be 68x68 in your case).
I covered more specifically why this is happening and how to handle that in this issue: #40
Good luck!

@dpoiesz
Copy link
Author

dpoiesz commented Dec 3, 2020

Thank you very much for responding, I'll try larger chips and let you know how it works out!

@dpoiesz
Copy link
Author

dpoiesz commented Dec 7, 2020

Unfortunately, increasing the input size did not help. A similar error is still received. As I stated before this is very confusing since all I did was swap out my function for yours. The code works fine for the code that uses "same" padding.

Any ideas on what I should look at next?

1/1052 [..............................] - ETA: 4:14:15Traceback (most recent call last):
File "/home/jordan/anaconda3/envs/bespin/lib/python3.7/site-packages/tensorflow_core/python/framework/tensor_shape.py", line 926, in merge_with
new_dims.append(dim.merge_with(other[i]))
File "/home/jordan/anaconda3/envs/bespin/lib/python3.7/site-packages/tensorflow_core/python/framework/tensor_shape.py", line 309, in merge_with
self.assert_is_compatible_with(other)
File "/home/jordan/anaconda3/envs/bespin/lib/python3.7/site-packages/tensorflow_core/python/framework/tensor_shape.py", line 276, in assert_is_compatible_with
(self, other))
ValueError: Dimensions 512 and 324 are not compatible

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/jordan/anaconda3/envs/bespin/lib/python3.7/site-packages/tensorflow_core/python/ops/nn_impl.py", line 167, in sigmoid_cross_entropy_with_logits
labels.get_shape().merge_with(logits.get_shape())
File "/home/jordan/anaconda3/envs/bespin/lib/python3.7/site-packages/tensorflow_core/python/framework/tensor_shape.py", line 929, in merge_with
raise ValueError("Shapes %s and %s are not compatible" % (self, other))
ValueError: Shapes (None, 512, 512, 1) and (None, 324, 324, 1) are not compatible

@karolzak
Copy link
Owner

karolzak commented Dec 8, 2020

512x512 is now both your input size and output mask size, right? That's incorrect for vanilla unet. valid padding is losing border pixels information on each consecutive conv layer so your input size of 512x512 outputs a 324x324 output mask. In the original research paper behind U-Net they used edge mirroring technic as a preprocessing step to turn original image from size 512x512 to 572x572 (to address border pixels loss) and it resulted with 388x388 output mask.
It all makes sense - read the paper! 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants