How to do zero padding in keras conv layer?

北慕城南 提交于 2021-02-06 23:59:45

问题


I am trying to implement model from scientific article, which says they are using zero padding. Is it possible to configure this padding in keras Conv2D?

Only possible values for padding I see are

padding: one of "valid" or "same" (case-insensitive).

Is it possible to pad with zeros or other constant values?


回答1:


"same" means zero padding. It is currently not possible to pad with other constants in an efficient way.




回答2:


When you use padding='valid', there's no padding.

When you use padding='same' with strides=1, the input are zero-padded so that width and height of output is the same as the input. As described in the document, "same" is slightly inconsistent across backends with strides != 1.

If you want to manually set the padding value, maybe the simplest way is to add a ZeroPadding2D layer before Conv2D.

For example, ZeroPadding2D(padding=((1,2),(3,4))) will add 1 dimension on the left, 2 on the right, 3 on the top and 4 on the bottom. ZeroPadding2D(5) will add 5 dimension on all 4 borders.

(btw, It's a wrap layer of backend function spatial_2d_padding)




回答3:


Take a look at spatial_2d_padding function. It pads a tensor with zeroes.




回答4:


https://keras.io/layers/convolutional/

ZeroPadding2D=>

keras.layers.ZeroPadding2D(padding=(1, 1), data_format=None) Zero-padding layer for 2D input (e.g. picture).

This layer can add rows and columns of zeros at the top, bottom, left and right side of an image tensor.

Arguments

padding: int, or tuple of 2 ints, or tuple of 2 tuples of 2 ints. If int: the same symmetric padding is applied to height and width. If tuple of 2 ints: interpreted as two different symmetric padding values for height and width: (symmetric_height_pad, symmetric_width_pad). If tuple of 2 tuples of 2 ints: interpreted as ((top_pad, bottom_pad), (left_pad, right_pad))

data_format: A string, one of "channels_last" or "channels_first". The ordering of the dimensions in the inputs. "channels_last" corresponds to inputs with shape (batch, height, width, channels) while "channels_first" corresponds to inputs with shape (batch, channels, height, width). It defaults to the image_data_format value found in your Keras config file at ~/.keras/keras.json. If you never set it, then it will be "channels_last".




回答5:


I think padding='same' means that in case we are in a hidden layer the empty cells that the stride find will be filled with the values of the previous layer not with zeros, only if is the input layer will be filled with zeros.



来源:https://stackoverflow.com/questions/45013060/how-to-do-zero-padding-in-keras-conv-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!