【发布时间】: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