mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

how can i keep the width and height same after Conv2d layer

Open itisianlee opened this issue 9 years ago • 4 comments

I try to keep the width and height same after Conv2d layer. But when my kernel is even as 4, how can I set the padding to get same Width? I got how the new size can be calculated.But I think I should just pad in one side. out_height = floor((height+2padding[0]-dilation[0](kernel_size[0]-1)-1)/stride[0])+1 out_width = floor((width+2padding[1]-dilation[1](kernel_size[1]-1)-1)/stride[1])+1

itisianlee avatar Sep 14 '17 02:09 itisianlee

@apache/mxnet-committers: This issue has been inactive for the past 90 days. It has no label and needs triage.

For general "how-to" questions, our user forum (and Chinese version) is a good place to get help.

szha avatar Dec 22 '17 22:12 szha

proposed labels: "Question", "HowTo" Can you please ask usage related questions at - https://discuss.mxnet.io/ Please feel free to reopen if you encounter any issues.

zhiz21 avatar Mar 20 '18 02:03 zhiz21

Suggested tags: "Question", "HowTo"

b0noI avatar Mar 20 '18 21:03 b0noI

@itisianlee typically stride and dilation are behavioral parameter that you want to use when you try to achieve specific behavior. dilation when you want to enlarge your receptive field. stride when you want to decrease your output resolution. padding is typically used in order to control the output size. Let's first check your formula after fixing stride dilation and kernel.

Using your example: stride = (1,1) dilation = (1,1) kernel = (4,4)

out_height = floor((height+2*padding[0]-1*(4-1)-1)/1)+1
out_width = floor((width+2*padding[1]-1*(4-1)-1)/1)+1

out_height = floor((height+2*padding[0]-3-1))+1
out_width = floor((width+2*padding[1]-3-1))+1

out_height = height+2*padding[0]-3
out_width = width+2*padding[1]-3

padding[0] = 3/2
padding[1] = 3/2

You get indeed a non-integer value for your padding, and unfortunately Conv2D does not support asymetric padding.

Two solutions come to mind to solve that:

  • use nd.pad() that supports asymmetric padding followed by a convolution with (0,0) padding.
  • over-pad, for example here using padding=(2,2) and use the nd.slice() operator to slice one column and one row from your output feature maps.

@szha can you please close this issue? Thanks!

@itisianlee if you would like to follow-up, please create an issue on https://discuss.mxnet.io, thanks!

ThomasDelteil avatar Jul 04 '18 23:07 ThomasDelteil