【问题标题】:How to slice a tensor in keras with variable indices?如何在具有可变索引的 keras 中对张量进行切片?
【发布时间】:2020-12-18 15:20:08
【问题描述】:

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

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], []

如何获得补丁?

【问题讨论】:

    标签: tensorflow keras tf.keras


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-02
      • 2019-04-13
      • 2020-02-16
      • 2018-12-28
      • 2018-11-22
      • 1970-01-01
      相关资源
      最近更新 更多