Keras/Theano VGG16 AttributeError: ‘Model’ 对象没有属性 ‘ndim’

我试图构建一个使用Theano后端的Keras模型,将一个时间分布的VGG16网络连接到LSTM层,最后连接到一系列的全连接层。然而,我遇到了以下错误:

Traceback (most recent call last):  File "osr.py", line 341, in <module>    osr.generate_osr_model()  File "osr.py", line 145, in generate_osr_model    cnn_out = GlobalAveragePooling2D()(cnn_base)  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 529, in __call__    self.assert_input_compatibility(x)  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 464, in assert_input_compatibility    if K.ndim(x) != spec.ndim:  File "/usr/local/lib/python2.7/dist-packages/keras/backend/theano_backend.py", line 142, in ndim    return x.ndimAttributeError: 'Model' object has no attribute 'ndim'

这是我用来构建模型的代码部分:

        video = Input(shape=(self.frames_per_sequence,                             3,                             self.rows,                             self.columns))        cnn_base = VGG16(input_shape=(3,                                      self.rows,                                       self.columns),                         weights="imagenet",                         include_top=False)        cnn_out = GlobalAveragePooling2D()(cnn_base)        cnn = Model(input=cnn_base.input, output=cnn_out)        cnn.trainable = False        encoded_frames = TimeDistributed(cnn)(video)        encoded_vid = LSTM(256)(encoded_frames)        hidden_layer = Dense(output_dim=1024, activation="relu")(encoded_vid)        outputs = Dense(output_dim=class_count, activation="softmax")(hidden_layer)        osr = Model(, outputs)        optimizer = Nadam(lr=0.002,                          beta_1=0.9,                          beta_2=0.999,                          epsilon=1e-08,                          schedule_decay=0.004)        osr.compile(loss="categorical_crossentropy",                    optimizer=optimizer,                    metrics=["categorical_accuracy"])

回答:

解决方案是将cnn_base.output作为GlobalAveragePooling2D层的输入:

cnn_out = GlobalAveragePooling2D()(cnn_base.output)

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注