Dims
Dims

Reputation: 50999

How to do zero padding in keras conv layer?

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?

Upvotes: 11

Views: 28641

Answers (5)

Anurag Gupta
Anurag Gupta

Reputation: 509

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".

Upvotes: 1

Omar Villa
Omar Villa

Reputation: 49

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.

Upvotes: 0

Neal
Neal

Reputation: 101

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)

Upvotes: 10

Sergii Gryshkevych
Sergii Gryshkevych

Reputation: 4159

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

Upvotes: 3

Jonas Adler
Jonas Adler

Reputation: 10759

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

Upvotes: 14

Related Questions