注意的地方: keras中每层的输入输出的tensor是张量, 比如Tensor shape是(N, H, W, C), 对于tf后台, channels_last
Define a slice layer using Lamda layer
def slice(x, h1, h2, w1, w2):
""" Define a tensor slice function
"""
return x[:, h1:h2, w1:w2, :]
定义完slice function之后,利用lambda layer添加到定义的网络中去
# Add slice layer
slice_1 = Lambda(slice, arguments={'h1': 0, 'h2': 6, 'w1': 0, 'w2': 6})(sliced)
# As for tensorfow backend, Lambda doesn't need output shape argument
slice_2 = Lambda(slice, arguments={'h1': 0, 'h2': 6, 'w1': 6, 'w2': 12})(sliced)