Python 尝试输出Keras中上一层到最后一层时出错

Python 尝试输出Keras中上一层到最后一层时出错,python,deep-learning,keras,keras-2,Python,Deep Learning,Keras,Keras 2,我试图在keras模型中输出上一个到最后一个致密层。我首先加载模型架构和权重: base_model = applications.ResNet50(weights = None, include_top = False, input_shape = (image_size[0], image_size[1], nb_c

我试图在keras模型中输出上一个到最后一个致密层。我首先加载模型架构和权重:

base_model = applications.ResNet50(weights = None,
                                            include_top = False, 
                                            input_shape = (image_size[0], image_size[1], nb_channels))
top_model = Sequential()
top_model.add(Flatten(input_shape=base_model.output_shape[1:]))
top_model.add(Dense(1024, init = 'glorot_uniform', activation='relu', name = 'last_layer_1024'))
top_model.add(Dropout(0.5))
top_model.add(Dense(nb_classes, activation = 'softmax', name = 'softmax_layer'))
top_model_tensor = top_model(base_model.output)
model = Model(inputs = base_model.input, outputs = top_model_tensor)
model.load_weights(weights_path)
然后,我通过以下操作移除最后一个致密层:

model.layers[-1].pop()
#model.outputs = [model.layers[-1].layers[-1].output]
#model.layers[-1].layers[-1].outbound_nodes = []
short_top_model = Model(top_model.input, top_model.get_layer('last_layer_1024').output)
如果取消注释注释注释行,则会出现以下错误:
InvalidArgumentError(回溯请参见上文):必须为带有dtype float的占位符张量“展平输入”输入一个值。如果我对它们进行注释,最后一个密集层就不会被有效地移除(我的意思是,当我在
model
上调用
predict
时,我仍然会得到最后一个密集层的输出)。我如何解决这个问题

另外,如果有一种不同的方法让模型输出上一个到最后一个密集层,我也可以把它作为一个答案(而不是试图用这种方法修复)

另一个不起作用的解决方案是,在加载权重后,只需执行以下操作即可剪切长模型:

model.layers[-1].pop()
#model.outputs = [model.layers[-1].layers[-1].output]
#model.layers[-1].layers[-1].outbound_nodes = []
short_top_model = Model(top_model.input, top_model.get_layer('last_layer_1024').output)
您将得到以下错误:


RuntimeError:Graph disconnected:无法获取“展平1\u输入:0”,shape=(?,1,1,2048),dtype=float32,device=/device:GPU:2)层“展平1\u输入”的张量值。访问以下以前的层时没有问题:[]

尝试剪切模型、更改其输入/输出等。听起来不像keras对用户的期望

您只需创建另一个遵循相同路径但提前结束的模型:

#do this "before" creating "top_model_tensor".
short_top = Model(
                  top_model.input,
                  top_model.get_layer('last_layer_1024').output
                 )

top_model_out = top_model(base_model.output)
short_top_out = short_top(base_model.output)

model = Model(base_model.input,top_model_out)
short_model = Model(base_model.input,short_top_out)

根据预期结果选择要使用的选项。培训一个更新另一个。

尝试剪切模型、更改其输入/输出等。听起来不像keras对用户的期望

您只需创建另一个遵循相同路径但提前结束的模型:

#do this "before" creating "top_model_tensor".
short_top = Model(
                  top_model.input,
                  top_model.get_layer('last_layer_1024').output
                 )

top_model_out = top_model(base_model.output)
short_top_out = short_top(base_model.output)

model = Model(base_model.input,top_model_out)
short_model = Model(base_model.input,short_top_out)

根据预期结果选择要使用的选项。培训一个更新另一个。

上述答案的简短版本

#again create connection between two model
feature_vec_model = Model(
                  top_model.input,
                  top_model.get_layer('last_layer_1024').output
                 )
feature_vec_model_output = feature_vec_model(base_model.output)
#Connection created


# Define final connected model & load pretrained weights
complete_feature_vec_model = Model(base_model.input,feature_vec_model_output)
complete_feature_vec_model.load_weights("path_to_model")

以上答案的简短版本

#again create connection between two model
feature_vec_model = Model(
                  top_model.input,
                  top_model.get_layer('last_layer_1024').output
                 )
feature_vec_model_output = feature_vec_model(base_model.output)
#Connection created


# Define final connected model & load pretrained weights
complete_feature_vec_model = Model(base_model.input,feature_vec_model_output)
complete_feature_vec_model.load_weights("path_to_model")

嗨,丹尼尔,我的问题措辞错误。我实际上想要输出的是1024密集层,而不是nb_类密集层。我会很难将权重加载到较短的模型吗?因为重量是在较长的模型中训练的。我现在就试试。如果你这样做,你不需要加载重量。两个模型中的权重是共享的。将它们加载到长模型中。如果你愿意的话,以后从简短的模型中保存它们。嗨,丹尼尔,我的问题措辞错误。我实际上想要输出的是1024密集层,而不是nb_类密集层。我会很难将权重加载到较短的模型吗?因为重量是在较长的模型中训练的。我现在就试试。如果你这样做,你不需要加载重量。两个模型中的权重是共享的。将它们加载到长模型中。如果需要,请稍后从短模型中保存它们。