将来自keras模型的预测用作另一个keras模型中的一层 [英] Using prediction from keras model as a layer inside another keras model

查看:44
本文介绍了将来自keras模型的预测用作另一个keras模型中的一层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们已经为某个任务训练了一个模型,是否可以将该模型预测用作另一个模型中的lambda层?我在考虑以下格式的内容:

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

像这样在Keras中可行吗?

Is something like this viable in Keras?

推荐答案

这比您想象的要容易得多,您只需要这样做:

This is much easier than you think, you just need to do:

pretrained_model_output= pretrained_model(img_input)

这篇关于将来自keras模型的预测用作另一个keras模型中的一层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆