【问题标题】:Cannot add SPP layer in CNN using Keras无法使用 Keras 在 CNN 中添加 SPP 层
【发布时间】:2018-05-29 19:18:07
【问题描述】:

我想在 CNN 的密集层之前应用空间金字塔池化。

我使用 Keras 来实现。

Tensorflow 被用作后端。

但是,我遇到了一个错误。

我的代码有什么问题?谢谢。

Traceback (most recent call last): File "<pyshell#25>", line 1, in <module> model.add(SpatialPyramidPooling(pooling_regions, input_shape=Input(shape = (None,None,None,3)))) File "C:\Program Files\Python36\lib\site-packages\spp\SpatialPyramidPooling.py", line 33, in __init__ super(SpatialPyramidPooling, self).__init__(**kwargs) File "C:\Program Files\Python36\lib\site-packages\keras\engine\topology.py", line 311, in __init__ batch_input_shape = (batch_size,) + tuple(kwargs['input_shape']) File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 439, in __iter__ "Tensor objects are not iterable when eager execution is not " TypeError: Tensor objects are not iterable when eager execution is not enabled. To iterate over this tensor use tf.map_fn.

代码如下:

from keras.engine.topology import Layer
from keras.models import Sequential
import keras.backend as K
import numpy as np

model = Sequential()
model.add(SpatialPyramidPooling((1,2,4), Input(shape=(None, None, None, 3))))

class SpatialPyramidPooling(Layer):
    def __init__(self, pool_list, **kwargs):

        self.dim_ordering = K.image_dim_ordering()
        assert self.dim_ordering in {'tf', 'th'}, 'dim_ordering must be in {tf, th}'

        self.pool_list = pool_list

        self.num_outputs_per_channel = sum([i * i for i in pool_list])

        super(SpatialPyramidPooling, self).__init__(**kwargs)

    def call(self, x, mask=None):

        input_shape = K.shape(x)
        print(input_shape)
        print(K.eval(input_shape))
        outputs = K.variable(value=np.random.random((3,4)))

        return outputs

【问题讨论】:

  • 这是你的全部代码吗?获取NameError: name 'SpatialPyramidPooling' is not defined

标签: python tensorflow keras


【解决方案1】:

我很确定您应该使用 input_shape=(None,None,None,3) 而不是 input_shape=Input(shape = (None,None,None,3))

此外,您不能使用任何要求在call 方法中存在数据的函数。您正在使用K.shapeK.eval,两者都会给您带来编译错误。

如果您需要有关输入形状的信息,则必须在 def build(self, input_shape): 方法中进行。

【讨论】:

  • 返回InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'spatial_pyramid_pooling_7_input' with dtype float and shape [?,?,?,?,3] [[Node: spatial_pyramid_pooling_7_input = Placeholder[dtype=DT_FLOAT, shape=[?,?,?,?,3], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
  • 更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 2021-05-28
  • 2021-07-09
相关资源
最近更新 更多