keras.layers.Conv2D中的padding解析

same方式:

求最小整数2p,使得(img_size + 2p - kernel_size) / stride为正整数,然后以右、下优先的方式进行零填充。

例:img_size=4, kernel_size=3, stride=2, (4 + 2p - 3) / 2为整数,解得2p=1,则图片的右下角填充1,左上角无填充

valid方式:

从左向右,右边多余的会被舍弃

 

验证代码:

from keras.models import Sequential, Model
from keras.layers import Conv2D, ZeroPadding2D
import numpy as np
from keras import backend as K

input_size = 5

model = Sequential()
model.add(ZeroPadding2D(1, input_shape=(input_size, input_size, 1)))
model.add(Conv2D(1, (3, 3), padding='valid', strides=2, activation='relu'))

model.summary()

weights = [np.array([[[[1.]], [[1.]], [[1.]]],
                     [[[1.]], [[1.]], [[1.]]],
                     [[[1.]], [[1.]], [[1.]]]]), 
           np.array([0.])]
model.layers[1].set_weights(weights)

x = np.arange(input_size * input_size)
x = x.reshape((1, input_size, input_size, 1))

iterate = Model([model.input], [model.layers[1].output])

y = iterate(x)

print(y)

 

你可能感兴趣的:(tensorflow学习)