Keras将模型输出合并/连接为新层

时间:2017-06-02 23:07:10

标签: python machine-learning keras keras-layer keras-2

我想使用预训练模型'卷积特征映射作为主模型的输入要素。

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()函数。那么,我应该如何正确地连接我的模型?为什么不工作?我想这可能与输入如何输入到子模型以及如何热交换输入层有关。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

如果我们这样做,那就有用了:

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

而不是:

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

TLDR:需要将模型称为图层。