假设我们要应用一个
3x3
的
custom filter
onto an
6x6
形象
.
Dummy example input 形象
(it is
1
channel 形象. So dimension will be
6x6x1
. Here, pixel values are random integer. Generally pixel values should be
0 to 255
or
0.0 to 1.0
.)
input_mat = np.array([
[ [4], [9], [2], [5], [8], [3] ],
[ [3], [6], [2], [4], [0], [3] ],
[ [2], [4], [5], [4], [5], [2] ],
[ [5], [6], [5], [4], [7], [8] ],
[ [5], [7], [7], [9], [2], [1] ],
[ [5], [8], [5], [3], [8], [4] ]
# we need to give the batch size.
# here we will just add a dimension at the beginning which makes batch size=1
input_mat = input_mat.reshape((1, 6, 6, 1))
Dummy conv model where we will use our custom filter
def build_model():
input_tensor = Input(shape=(6,6,1))
x = layers.Conv2D(filters=1,
kernel_size = 3,
kernel_initializer=my_filter,
strides=2,
padding='valid') (input_tensor)
model = Model(inputs=input_tensor, outputs=x)
return model
Testing
model = build_model()
out = model.predict(input_mat)
print(out)
Output
[[[[ 0.]
[-4.]]
[[-5.]
[ 3.]]]]