10

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?

Dims
  • 47,675
  • 117
  • 331
  • 600

5 Answers5

11

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

Jonas Adler
  • 10,365
  • 5
  • 46
  • 73
  • 2
    How do you know? More probably "same" means padding with the same value, doesn't it? – Dims Jul 10 '17 at 13:22
  • 5
    The word "Same" refers to the *shape* of the result, not its values. That is, the returned tensor has the same shape as the input. – Jonas Adler Jul 10 '17 at 13:25
  • It appears that "same" does not work the same way as when using "valid" with a preceding ZeroPadding2D layer. Please check my experiment here: https://stackoverflow.com/questions/60323897/tensorflow-keras-conv2d-layers-with-padding-same-behave-strangely – SomethingSomething Feb 20 '20 at 16:54
8

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)

Neal
  • 81
  • 1
  • 3
2

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

Sergii Gryshkevych
  • 4,029
  • 1
  • 25
  • 42
  • 1
    Indeed. This is ideal for cases where you want a non-standard padding. A convolutional layer has the `padding='same'` option that will automatically pad with zeros to keep the same shape as the input. But if you want something that is not provided by the convolutional layer itself, or in other cases, this is the right function to go. – Daniel Möller Jul 10 '17 at 13:59
  • 2
    Example of usage? – mrgloom Dec 01 '17 at 17:19
1

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

Anurag Gupta
  • 485
  • 5
  • 5
0

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.

Omar Villa
  • 49
  • 2