ZeroPadding2D
,传入的参数如果是一个二维的tuple,((top_pad, bottom_pad), (left_pad, right_pad))
,它表示在上下左右分别补多少层零。
from keras.layers import ZeroPadding2D, Input
from keras import Model
from numpy import *
image = array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
image = image.reshape((1, 3, 3, 1))
inputs = Input(shape=(3, 3, 1))
pad = ZeroPadding2D(((1, 0), (1, 0)))(inputs)
model = Model(inputs, pad)
out = model.predict(image)
print(reshape(out, (4, 4)))
>>>
[[0. 0. 0. 0.]
[0. 1. 2. 3.]
[0. 4. 5. 6.]
[0. 7. 8. 9.]]
将参数改为((2, 0), (2, 0))
,则
pad = ZeroPadding2D(((2, 0), (2, 0)))(inputs)
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 1. 2. 3.]
[0. 0. 4. 5. 6.]
[0. 0. 7. 8. 9.]]
((1, 1), (1, 1))
则在上下左右都补一层零
pad = ZeroPadding2D(((1, 1), (1, 1)))(inputs)
[[0. 0. 0. 0. 0.]
[0. 1. 2. 3. 0.]
[0. 4. 5. 6. 0.]
[0. 7. 8. 9. 0.]
[0. 0. 0. 0. 0.]]