0

我想使用预训练模型的卷积特征图作为主模型的输入特征。

inputs = layers.Input(shape=(100, 100, 12))
sub_models = get_model_ensemble(inputs)
sub_models_outputs = [m.layers[-1] for m in sub_models]
inputs_augmented = layers.concatenate([inputs] + sub_models_outputs, axis=-1)

这是我所做的关键部分get_model_ensemble()

for i in range(len(models)):
    model = models[i]
    for lay in model.layers:
        lay.name = lay.name + "_" + str(i)
    # Remove the last classification layer to rather get the underlying convolutional embeddings
    model.layers.pop()
    # while "conv2d" not in model.layers[-1].name.lower():
    #     model.layers.pop()
    model.layers[0] = new_input_layer
return models

这一切都给出了:

Traceback (most recent call last):
  File "model_ensemble.py", line 151, in <module>
    model = get_mini_ensemble_net()
  File "model_ensemble.py", line 116, in get_mini_ensemble_net
    inputs_augmented = layers.concatenate([inputs] + sub_models_outputs, axis=-1)
  File "/usr/local/lib/python3.4/dist-packages/keras/layers/merge.py", line 508, in concatenate
    return Concatenate(axis=axis, **kwargs)(inputs)
  File "/usr/local/lib/python3.4/dist-packages/keras/engine/topology.py", line 549, in __call__
    input_shapes.append(K.int_shape(x_elem))
  File "/usr/local/lib/python3.4/dist-packages/keras/backend/tensorflow_backend.py", line 451, in int_shape
    shape = x.get_shape()
AttributeError: 'BatchNormalization' object has no attribute 'get_shape'

这是类型信息:

print(type(inputs))
print(type(sub_models[0]))
print(type(sub_models_outputs[0]))

<class 'tensorflow.python.framework.ops.Tensor'>
<class 'keras.engine.training.Model'>
<class 'keras.layers.normalization.BatchNormalization'>

注意:我从中获得的模型已经调用get_model_ensemble()了它们的函数。compile()那么,我应该如何正确连接我的模型?为什么它不起作用?我想这可能与如何将输入馈送到子模型以及我如何热交换它们的输入层有关。

谢谢您的帮助!

4

1 回答 1

0

如果我们这样做,事情就会奏效:

sub_models_outputs = [m(inputs) for m in sub_models]

而不是:

sub_models_outputs = [m.layers[-1] for m in sub_models]

TLDR:模型需要被称为一个层。

于 2017-06-09T17:54:49.060 回答