我想使用预训练模型的卷积特征图作为主模型的输入特征。
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) # 移除最后的分类层,以便获取底层的卷积嵌入 model.layers.pop() # while "conv2d" not in model.layers[-1].name.lower(): # model.layers.pop() model.layers[0] = new_input_layerreturn 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()
函数。所以,我应该如何正确地连接我的模型?为什么它不起作用?我猜这可能与输入如何被馈送到子模型以及我如何热交换它们的输入层有关。
感谢帮助!
回答:
如果我们这样做,事情就会起作用:
sub_models_outputs = [m(inputs) for m in sub_models]
而不是这样:
sub_models_outputs = [m.layers[-1] for m in sub_models]
简而言之:模型需要作为层来调用。