Using prediction from keras model as a layer inside another keras model

半世苍凉 提交于 2021-02-10 18:25:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!