问题
Suppose we have a model already trained for some task can we use that models prediction as a lambda layer inside another model? I am thinking something in the following format:
pretrained_model=get_Model() #Loaded from a different file
pretrained_model.load_weights('pretrained_model_weights.h5')
base_model = VGG16(weights = 'imagenet',include_top=False,input_shape (240,320,3))
for layer in base_model.layers:
layer.trainable = True
img_input=base_model.input
encoded=base_model.output
pretrained_model_output=Lambda(lambda x: pretrained_model.predict(img_input))
#Then run pretrained_model_output through an architecture that gives same output size as base_model.output and then
concat = Concatenate([img_input,Output_Convolutions_pretrained_model_output],axis=-1)
#then feed this through another block in the model
Is something like this viable in Keras?
回答1:
This is much easier than you think, you just need to do:
pretrained_model_output= pretrained_model(img_input)
来源:https://stackoverflow.com/questions/59777378/using-prediction-from-keras-model-as-a-layer-inside-another-keras-model