如何在具有可变索引的keras中切片张量



我有一个100x120x3大小的图像。在对原始图像应用卷积之后,我想在训练期间从图像中获得大小为11x11x3的子集/补丁。补丁将以索引"0"为中心;index1";以及";index2";。我正在尝试的代码写在下面。CCD_ 1的尺寸与图像的尺寸相同。

x = Input(shape=(100,120,3), name='inputA')
index1 = Input(shape=(1,), name='ind1', dtype = 'int32')
index2 = Input(shape=(1,), name='ind2', dtype = 'int32')
feats_new = encoder(x)
patch = feats_new[:,index1-5:index1+6, index2-5:index2+6,:]

我在执行时遇到以下错误:

TypeError: Cannot convert a symbolic Keras input/output to a numpy
array. This error may indicate that you are trying to pass a
symbolic value to a NumPy call, which is not supported. Or, you may
be trying to pass Keras symbolic inputs/outputs to a TF API that
does not register dispatching, preventing Keras from automatically
converting the API call to a lambda layer in the Functional Model.

ValueError: Shapes must be equal rank, but are 2 and 0
From merging shape 2 with other shapes. 
For '{{nodetf.__operators__.getitem_11/strided_slice/stack_2}} =
Pack[N=4,   T=DT_INT32, axis=0 
(tf.__operators__.getitem_11/strided_slice/stack_2/values_0,
Placeholder_1, Placeholder_3,
tf.__operators__.getitem_11/strided_slice/stack_2/values_3)' with
input shapes: [], [?,1], [?,1], []

我该如何获得补丁?

若要将物料批处理在一起,所有项目必须具有相同的形状。如果他们不这样做,你就会犯错误
通常的方法是在批处理之前将输入填充到相同的最大长度