keras.layers.ZeroPadding2D()

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.]]
发布了83 篇原创文章 · 获赞 4 · 访问量 5359

猜你喜欢

转载自blog.csdn.net/weixin_43486780/article/details/105452991