jakeret / tf_unet

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

Can you give a hint on changing padding from 'VALID' to 'SAME' #211

Open foury27 opened 6 years ago

foury27 commented 6 years ago

Hi, I found the output result is too small and want to get a bigger prediction mask. If I changed the conv layer in layers.py there will be an error reporting such like this ''' InvalidArgumentError (see above for traceback): logits and labels must be broadcastable: logits_size=[16384,2] labels_size=[0,2] [[Node: cost/softmax_cross_entropy_with_logits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](cost/softmax_cross_entropy_with_logits/Reshape, cost/softmax_cross_entropy_with_logits/Reshape_1)]] '''

Can you give an hint of where should I change if I wanted to set padding to 'SAME'?

vi1729 commented 6 years ago

Hi, I also tried 'SAME' padding. I also commented the lines in unet.py at lines: 102, 138. Both have same code: size -=4

I could train my image sizes are nx = 520, ny = 696. And I could then train for layers = 5 and features = 32.

Again when I tried with layers = 4. I got the above error (same as you got). Please let me know if you could fix it.

Thank you, Vijaya

DoraUniApp commented 5 years ago

@vijayakakumani did you manage to change the padding to same? I'm trying to do the same and have a similar issue.

DoraUniApp commented 5 years ago

@jakeret do you have any suggestions for this? I realised there are a number of similar questions here but nobody seemed to have a solution apart from mirroring or padding the image to compensate for the reduction in the output. I tried to find where in unet cropping of labels happen and changed that but it just runs into more errors, later on in the code.

vi1729 commented 5 years ago

Hi, I was facing the issue with 'SAME' padding intermittently. I didn't make any changes to unet.py (uncommented size-=4).

I couldn't find the root cause for the issue. But now it's working for me. I doubt I got this issue when I force stoped kernel during training. May be some binaries got corrupted. I tried in different ways, sometimes I had deleted the egg files and build files. I ran setup again. Now I'm not facing this issue for my images with above hyper parameters.

DoraUniApp commented 5 years ago

One thing I know that might cause problems, is the relation between the depth of network and your input image dimensions. Basically, I think your IMGx and IMGy should be divisible by 2^UnetDepth. Was this the case for you? One other thing I'm trying now is changing the padding to SAME and commenting any line that was cropping the training labels in unet.py. It is running now but I'm not yet sure of the output. Did you have to do the same?

erickvinsan commented 5 years ago

I have found the bug. When we use padding equals to "SAME" the convolution return the same Width x Height as the image input. Then the variable 'size' became equals to in_size and the 'self.offset' equals to 0. Debuggind the code I found the problem on the function 'crop_to_shape' in the module 'util'. All Images that pass through this function became WxH = 0 x 0. Finally, the solution is:

Ps: The image size has to be multiple of 2 because when we go until the deepest layer we go dividing (max_pool with stride 2) by 2 and when we go to the surface again we multiply by 2 in each layer (deconv).

Ps 2: The images generated by the epochs on the dir 'prediction' has unet output or black or white but don't worry. Try to predict some other images after training the net and show the images using OpenCV (cv2.imshow) because the net could not reach the 0s and 1s yet and the classification could be spread through (0,1). To see the values use some histogram function over the images to see this behavior.

Thank you Jakeret for the incredibly organized code!

jakeret commented 5 years ago

Sweet! Thanks very much for digging into this!

vi1729 commented 5 years ago

Thank you :-)

mas-dse-greina commented 5 years ago

Is there a way to add this as a parameter in the UNet constructor? I'm wondering if Erick would consider making a PR to allow the user to switch between "valid" and "same" topologies easily.

Thanks.