【问题标题】:concatenate() missing 1 required positional argument: 'inputs'concatenate() 缺少 1 个必需的位置参数:“输入”
【发布时间】:2021-10-12 22:25:10
【问题描述】:

我想使用三个导入的网络创建一个多输入 CNN。但是我不完全理解下面的错误。 keras 函数 api 不应该识别导入网络中的输入层吗?

import tensorflow.keras
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers, models, Sequential
from tensorflow.keras.applications import ResNet152, InceptionV3, InceptionResNetV2

导入 1 个模型:

resnet152 = ResNet152(
weights='imagenet',
include_top=False,
input_shape=((224, 224, 3))
)

for layer in resnet152.layers:
    layer.trainable = True

合并 3 个模型的输入和输出。

from tensorflow.keras import Model

resnet152_copy = Model(inputs=resnet152.input, outputs=resnet152.output)
inceptionV3_copy = Model(inputs=inceptionV3.input, outputs=inceptionV3.output)
inception_resnet152_copy = Model(inputs=inceptionV3_resnet152.input, outputs=inceptionV3_resnet152.output)


concat_feature_layer = layers.concatenate(axis=4)([resnet152_copy, inceptionV3_copy, inception_resnet152_copy])
fully_connected_dense_big = layers.Dense(1024, activation='relu')(concat_feature_layer)
dropout_one = layers.Dropout(0.5)(fully_connected_dense_big)
flatten_layer = layers.Flatten()(dropout_one)
fully_connected_dense_small = layers.Dense(512, activation='relu')(flatten_layer)
dropout_two = layers.Dropout(0.5)(fully_connected_dense_small)
fully_connected_dense_class = layers.Dense(4, activation='softmax')

model = Model(
    inputs=[resnet152.input, inceptionV3.input, inceptionV3_resnet152.input],
    outputs=fully_connected_dense_class
)

我在 concat 层中不断收到以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-7cef36b1c56e> in <module>
      6 
      7 
----> 8 concat_feature_layer = layers.concatenate(axis=4)([resnet152_copy, inceptionV3_copy, inception_resnet152_copy])
      9 fully_connected_dense_big = layers.Dense(1024, activation='relu')(concat_feature_layer)
     10 dropout_one = layers.Dropout(0.5)(fully_connected_dense_big)

TypeError: concatenate() missing 1 required positional argument: 'inputs'

【问题讨论】:

  • 请发布完整的错误信息。我猜它出现在第 8 行,但我不确定。并请发布一个最小示例的完整代码。在您的代码 sn-p 中,名称 layers 未定义。
  • layers.concatenate(axis=4)([resnet152_copy, inceptionV3_copy, inception_resnet152_copy]) 中,你关闭括号然后重新打开它们——如果layers.concatenate(axis=4) 返回一个你会调用的函数,你会这样做,但这不是你正在做的——你可能只是想要继续传递参数并且应该添加以逗号分隔的其他参数?
  • @Grismar。不,他是按照文档所说的方式来称呼它的。
  • 嗨 grismar,你是对的,也是错的......我的文档是这样说的,但是我不断收到同样的错误。我试图指定它是 concat 层中的 model.outputs 。如果我只通过没有 .outputs 的网络,则表示 nonetype 对象不可下标...
  • 连接和连接不是一回事。您可能混淆了这两者。

标签: python keras deep-learning


【解决方案1】:

tf.keras中有两种形式的连接层:

concat_feature_layer = layers.concatenate([resnet152_copy, inceptionV3_copy, inception_resnet152_copy], axis=4)
concat_feature_layer = layers.Concatenate(axis=4)([resnet152_copy, inceptionV3_copy, inception_resnet152_copy])

【讨论】:

  • 我认为我的问题是 resnet 输出形状是 (None, 7, 7, 2048) 而 incep 网络有 (None, 8, 8, 1536)。自从我使用功能 api 以来,我已经尝试了第一个解决方案。上面的代码给出 = TypeError: 'NoneType' object is not subscriptable.
  • @NiklasMunkholmHjort 如果这些是形状,那么任何连接都无法解决它......您首先需要决定如何制作相同大小的其他尺寸。
  • 大家好,感谢您的回答。最后一点,这是通过仅针对 resnet 更改输出张量的形状来解决此问题的正确方法,还是会导致过滤不良? resnet152 = ResNet152( weights='imagenet', include_top=False, input_shape=((224, 224, 3)) ) 用于 resnet152.layers 中的层: layer.trainable = True reshaping = layers.Reshape((8, 8, 1568 ))(resnet152.output) re_config_resnet152 = tensorflow.keras.Model(inputs=resnet152.input, outputs=reshaping)
  • 我没有直接使用“ResNet”模型,所以无法评论调用。我可以告诉你的是,这不是重塑的问题。在这种情况下,值的数量恰好匹配(令我惊讶),但第二和第三张量维度具有空间信息,所以我不会混合它。您应该填充 resnet 的输出或修剪 inception 网络的输出,以便它们匹配。
猜你喜欢
  • 1970-01-01
  • 2014-09-13
  • 1970-01-01
  • 1970-01-01
  • 2019-10-10
  • 2017-03-27
  • 2019-11-12
  • 2019-12-23
相关资源
最近更新 更多