Python 使用保存的瓶颈值进行迁移学习(使用完整模型进行推理)

Python 使用保存的瓶颈值进行迁移学习(使用完整模型进行推理),python,neural-network,keras,deep-learning,Python,Neural Network,Keras,Deep Learning,我正在使用tensorflow后端的最新Keras 如果我使用模型的较小版本进行瓶颈值培训,我不太确定整合完整模型进行推理的正确方法 # Save bottleneck values from keras.applications.xception import Xception base_model = Xception(weights='imagenet', include_top=False) prediction = base_model.predict(x) ** SAVE bo

我正在使用tensorflow后端的最新Keras

如果我使用模型的较小版本进行瓶颈值培训,我不太确定整合完整模型进行推理的正确方法

# Save  bottleneck values

from keras.applications.xception import Xception
base_model = Xception(weights='imagenet', include_top=False)
prediction =  base_model.predict(x)
** SAVE bottleneck data***
现在让我们假设我的完整模型如下所示:

base_model = Xception(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(classes, activation='softmax')(x)
model = Model(input=base_model.input, output=predictions)
但是为了加速训练,我想通过加载瓶颈值绕过早期的层;因此,我创建了一个较小的模型,仅包括新层。然后我训练并保存模型

bottleneck_input = Input(shape = bottleneck_shape)
x = GlobalAveragePooling2D() (bottleneck_input)
x = Dense(1024, activation='relu')(x)
predictions = Dense(classes, activation='softmax')(x)
model = Model(input= bottleneck_input, output=predictions)
save_full_model() #save model
训练完这个较小的模型后,我想对完整的模型进行推理。所以我需要把基本模型和较小的模型放在一起。不确定什么是最好的方法

base_model = Xception(weights='imagenet', include_top=False)
#x = base_model.output

loaded_model = load_model() # load bottleneck model

#now to combine both models (something like this?)
Model(inputs = base_model.inputs, outputs = loaded_model.outputs)
将模型组合起来进行推理的正确方法是什么?
我不知道是否有办法使用我的完整模型进行训练,而只是从瓶颈层开始训练,从输入层开始推理。请注意,这与冻结层不同,冻结层只是冻结权重,权重不会更新,但仍会计算每个数据点。

每个模型都是一个具有额外属性的层,如损失函数等。因此,您可以将其用作功能API中的层。在您的情况下,它可能看起来像:

input = Input(...)
base_model = Xception(weights='imagenet', include_top=False)
# Apply model to input like layer
base_output = base_model(input)
loaded_model = load_model()
# Now the bottleneck model
out = loaded_model(base_output)
final_model = Model(input, out) # New computation graph