Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras, How to define padding in Convolution2D?

In the Convolution2D docs of keras, I found there are no details on definition of padding

keras.layers.convolutional.Convolution2D(nb_filter, 
                                         nb_row, 
                                         nb_col, 
                                         init='glorot_uniform', 
                                         activation=None, 
                                         weights=None, 
                                         border_mode='valid', 
                                         subsample=(1, 1), 
                                         dim_ordering='default', 
                                         W_regularizer=None, 
                                         b_regularizer=None, 
                                         activity_regularizer=None, 
                                         W_constraint=None, 
                                         b_constraint=None, 
                                         bias=True)

the subsample argument is

tuple of length 2. Factor by which to subsample output. Also called strides elsewhere.

and I think it is stride.

And the border_mode argument is

'valid', 'same' or 'full'. ('full' requires the Theano backend.)

valid and same are also arguments in TensorFlow's conv2d function.

How to define the padding, how to set the value of it?

like image 399
GoingMyWay Avatar asked Mar 11 '23 03:03

GoingMyWay


2 Answers

What you want is the ZeroPadding2D layer, just put it before a convolutional layer. This is more flexible than just putting this functionality inside Convolution2D.

like image 157
Dr. Snoopy Avatar answered Apr 02 '23 15:04

Dr. Snoopy


The accepted answer above mentioned the ZeroPadding2D layer in Keras. However, if you want to use some other kinds of padding which are not provided by Keras, such as reflection padding, you should implement them by yourself.

As far as I know, there are two ways to custom padding layer in Keras: 1. Use Lambda layer in Keras, and call the padding function from Keras's backend(typically TensorFlow). 2. Define your own Layer class. See this question for more details.

like image 27
Tab Avatar answered Apr 02 '23 15:04

Tab