Python 试图获取vgg16模型的输出时,在请求使用keras模型get_output_时抛出错误

Python 试图获取vgg16模型的输出时,在请求使用keras模型get_output_时抛出错误,python,keras,conv-neural-network,vgg-net,Python,Keras,Conv Neural Network,Vgg Net,我已经创建了一个改进的卷积神经网络,其中包括keras vgg16,我想得到最后一个vgg16层的输出(这是我添加的层之前的层),但是当我这样做时,会抛出以下错误: 创建神经网络: def get_vgg16_model(input_shape, y_flat): vgg16model = VGG16(weights=None, include_top=False, input_shape=input_shape) model =

我已经创建了一个改进的卷积神经网络,其中包括
keras vgg16
,我想得到最后一个vgg16层的输出(这是我添加的层之前的层),但是当我这样做时,会抛出以下错误:

创建神经网络:

def get_vgg16_model(input_shape, y_flat):
    vgg16model = VGG16(weights=None, include_top=False,
                       input_shape=input_shape)

    model = Sequential()
    model.add(BatchNormalization(input_shape=input_shape))
    model.add(vgg16model)
    # I want to get the output from above this comment
    model.add(Dense(64, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Flatten())
    model.add(Dense(10, activation='softmax'))

    model.summary()
    opt = SGD(lr=0.001, decay=1e-6, momentum=0.5, nesterov=True)  # was 'adam'

    model.compile(loss='categorical_crossentropy',
                  optimizer=opt, metrics=['acc'])
    return model

...

    # input placeholder
    inp = model.input
    # getting the output to all layers, we are only interested in vgg16's
    outputs = [layer.output for layer in model.layers]
    # evaluation function
    functor = K.function([inp, K.learning_phase()], outputs)
    layer_outs = functor([X, 1]) 

...

    elif config.mode == 'vgg16':
        # output of vgg16
        return layer_outs[1]
当使用适当的输入运行时,将返回:

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
batch_normalization_1 (Batch (None, 999, 32, 3)        12        
_________________________________________________________________
vgg16 (Model)                (None, 31, 1, 512)        14714688  
_________________________________________________________________
dense_1 (Dense)              (None, 31, 1, 64)         32832     
_________________________________________________________________
dropout_1 (Dropout)          (None, 31, 1, 64)         0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 1984)              0         
_________________________________________________________________
dense_2 (Dense)              (None, 10)                19850     
=================================================================
Total params: 14,767,382
Trainable params: 14,767,376
Non-trainable params: 6
_________________________________________________________________

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "F:\aaaa.py", line 210, in solrSearch
    tmp = featureExtractor(X=X_ill_temp, config=config, y=y_ill_temp)
  File "F:\aaaa.py", line 362, in featureExtractor
    outputs = [layer.output for layer in model.layers]
  File "F:\aaaa.py", line 362, in <listcomp>
    outputs = [layer.output for layer in model.layers]
  File "F:\...\venv\lib\site-packages\keras\engine\base_layer.py", line 845, in output
    ' has multiple inbound nodes, '
AttributeError: Layer vgg16 has multiple inbound nodes, hence the notion of "layer output" is ill-defined. Use `get_output_at(node_index)` instead.
模型:“顺序_1”
_________________________________________________________________
层(类型)输出形状参数
=================================================================
批次1(批次(无、999、32、3)12
_________________________________________________________________
vgg16(型号)(无、31、1512)14714688
_________________________________________________________________
稠密的(稠密的)(无,31,1,64)32832
_________________________________________________________________
辍学1(辍学)(无、31、1、64)0
_________________________________________________________________
展平1(展平)(无,1984)0
_________________________________________________________________
致密_2(致密)(无,10)19850
=================================================================
总参数:14767382
可培训参数:14767376
不可培训参数:6
_________________________________________________________________
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
solrSearch中的文件“F:\aaaa.py”,第210行
tmp=featureExtractor(X=X\u ill\u temp,config=config,y=y\u ill\u temp)
featureExtractor中第362行的文件“F:\aaaa.py”
输出=[model.layers中图层的layer.output]
文件“F:\aaaa.py”,第362行,在
输出=[model.layers中图层的layer.output]
文件“F:\…\venv\lib\site packages\keras\engine\base\u layer.py”,第845行,在输出中
'具有多个入站节点,'
AttributeError:Layer vgg16有多个入站节点,因此“Layer output”的概念定义不清。请改用“get_output_at(node_index)`来代替。
我还使用
keras层
实现了一个卷积神经网络,并使用相同的方法获得中间输出,但在这种情况下不会抛出错误

现在,我的问题是,如何通过修改
layer\u out
代码来使用get\u output\u at