Keras 2.2.4错误:AttributeError:“ NoneType”对象没有属性“ inbound_nodes”

时间:2019-04-02 06:31:48

标签: python-3.x tensorflow keras keras-layer keras-2

我正在为我的网络构建新的渠道明智的运营方式。 全局平均池化结果将在第一个x(输入)值上乘以(元素方式)。 但是,当我运行train.py文件时,它将发生我无法理解的错误。请帮助!!

错误消息:

Traceback (most recent call last):
  File "E:/githubRemote/train.py", line 49, in <module>
    model = init_model()
  File "E:/githubRemote/train.py", line 37, in init_model
    model = Model(inputs=im_n, outputs=resd)
  File "C:\Users\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\Anaconda3\lib\site-packages\keras\engine\network.py", line 93, in __init__
    self._init_graph_network(*args, **kwargs)
  File "C:\Users\Anaconda3\lib\site-packages\keras\engine\network.py", line 231, in _init_graph_network
    self.inputs, self.outputs)
  File "C:\Users\Anaconda3\lib\site-packages\keras\engine\network.py", line 1366, in _map_graph_network
    tensor_index=tensor_index)
  File "C:\Users\Anaconda3\lib\site-packages\keras\engine\network.py", line 1353, in build_map
    node_index, tensor_index)
  File "C:\Users\Anaconda3\lib\site-packages\keras\engine\network.py", line 1353, in build_map
    node_index, tensor_index)
  File "C:\Users\Anaconda3\lib\site-packages\keras\engine\network.py", line 1353, in build_map
    node_index, tensor_index)
  [Previous line repeated 3 more times]
  File "C:\Users\Anaconda3\lib\site-packages\keras\engine\network.py", line 1325, in build_map
    node = layer._inbound_nodes[node_index]
AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

我的错误代码是乘图层操作。 当我评论net = Multiply()([x, excitation])时 会工作的! 我认为keras模型可能会认为代码行不会构成一层Keras。所以这是一个NoneType -.-

我的代码:

def CAlayer(x, channel, reduction=16):
    # tensorflow implement
    # avg_pool = tflearn.global_avg_pool(inputx)
    # conv_1   = slim.conv2d(avg_pool, channel // reduction, 1)
    # conv_2   = slim.conv2d(conv_1, channel, 1, activation_fn=None)
    # excitation = tf.nn.sigmoid(conv_2)
    # keras implementation
    avg_pool   = GlobalAveragePooling2D()(x)
    avg_pool   = expand_dims(avg_pool, axis=1)
    avg_pool   = expand_dims(avg_pool, axis=1)
    conv_1     = Conv2D(channel//reduction, 1, activation=None, padding='same')(avg_pool)
    conv_1_ac  = Activation('relu')(conv_1)
    conv_2     = Conv2D(channel, 1, activation=None, padding='same')(conv_1_ac)
    excitation = Activation('sigmoid')(conv_2)
--> net = Multiply()([excitation, x])
    # print (net.shape)
    return net

1 个答案:

答案 0 :(得分:0)

在您使用过的代码中:

avg_pool   = expand_dims(avg_pool, axis=1)

这引起了问题,因为 expand_dims 是在keras.backend下定义的函数 将TensorFlow张量作为输出,但所有操作都应封装在Keras层中。

您必须使用其等效的Keras图层功能。 经验法则:所有Keras图层功能均以大写字母开头。