连接Keras模型/替换输入但保留图层 [英] Connecting Keras models / replacing input but keeping layers

查看:93
本文介绍了连接Keras模型/替换输入但保留图层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题类似于用keras替换输入层



我有一个分类器网络和一个自动编码器网络,我想使用自动编码器的输出(即编码+解码,作为预处理步骤)作为分类器-但在已经对常规数据进行分类器训练之后。



分类网络是使用这样的功能性API构建的(基于此示例):

  clf_input =输入(形状=(28,28,1))
clf_layer = Conv2D(...)(clf_input)
clf_layer = MaxPooling2D(... )(clf_layer)
...
clf_output =密集(num_classes,激活='softmax')(clf_layer)
model = Model(clf_input,clf_output)
model.compile( ...)
model.fit(...)

这样的自动编码器(基于此示例):

  ae_input =输入(shape =(28,28,1))
x = Conv2D(...)(ae_input)
x = MaxPooling2D(...)(x)
...
已编码= MaxPooling2D(...)(x)
x = Conv2d(...)(已编码)
x = UpSampling2D(...)(x)
...
解码= Conv2D(...)(x)
自动编码器=模型(ae_input,已解码)
autoencoder.compile(...)
自动编码器。 fit(...)

我可以像这样连接两个模型(我仍然需要原始模型,因此进行复制):

  model_copy = keras.models.clone_model(model)
model_copy.set_weights(model。 get_weights())
#删除原始输入层
model_copy.layers.pop(0)
#设置新输入
new_clf_output = model_copy(已解码)
#获取
stacked_model = Model(ae_input,new_clf_output)
stacked_model.compile(...)

当我要做的只是将模型应用于新的测试数据时,这非常有用,但是在诸如是:

 用于stacked_model中的图层。图层:
打印layer.get_config()

到达自动编码器的末尾,但随后在分类器模型获取其输入时出现KeyError失败。另外,当使用 keras.utils.plot_model 绘制模型时,我得到以下信息:





在这里您可以看到自动编码器层,但是最后,不是一个分类器模型中的各个层,而是一个块中只有完整的模型。 / p>

有没有办法连接两个模型,例如新的堆叠模型实际上是由所有单独的层组成的?

解决方案

好吧,我能想到的是真正地手动遍历模型的每一层,并再次将它们重新连接,就像这样:

  l = model.layers [1](已解码)#层0是输入层,我们将用range(2, len(model.layers)):
l = model.layers [i](l)
stackedmodel = Model(ae_input,l)
stacked_model.compile(...)

这可以正常工作并产生正确的绘图且没有错误,但这似乎不是最优雅的解决方案...



(顺便说一句,实际上复制模型似乎是不必要的,因为我没有再训练任何东西。)


This questions is similar to Keras replacing input layer.

I have a classifier network and an autoencoder network and I want to use the output of the autoencoder (i.e. encoding + decoding, as a preprocessing step) as the input to the classifier - but after the classifier was already trained on the regular data.

The classification network was built with the functional API like this (based on this example):

clf_input = Input(shape=(28,28,1))
clf_layer = Conv2D(...)(clf_input)
clf_layer = MaxPooling2D(...)(clf_layer)
...
clf_output = Dense(num_classes, activation='softmax')(clf_layer)
model = Model(clf_input, clf_output)
model.compile(...)
model.fit(...)

And the autoencoder like this (based on this example):

ae_input = Input(shape=(28,28,1))
x = Conv2D(...)(ae_input)
x = MaxPooling2D(...)(x)
...
encoded = MaxPooling2D(...)(x)
x = Conv2d(...)(encoded)
x = UpSampling2D(...)(x)
...
decoded = Conv2D(...)(x)
autoencoder = Model(ae_input, decoded)
autoencoder.compile(...)
autoencoder.fit(...)

I can concatenate the two models like this (I still need the original models, hence the copying):

model_copy = keras.models.clone_model(model)
model_copy.set_weights(model.get_weights())
# remove original input layer
model_copy.layers.pop(0)
# set the new input
new_clf_output = model_copy(decoded)
# get the stacked model
stacked_model = Model(ae_input, new_clf_output)
stacked_model.compile(...)

And this works great when all I want to do is apply the model to new test data, but it gives an error on something like this:

for layer in stacked_model.layers:
    print layer.get_config()

where it gets to the end of the autoencoder but then fails with a KeyError at the point where the classifier model gets its input. Also when plotting the model with keras.utils.plot_model I get this:

where you can see the autoencoder layers but then at the end, instead of the individual layers from the classifier model, there is only the complete model in one block.

Is there a way to connect two models such the new stacked model is actually made up of all the individual layers?

解决方案

Ok, what I could come up with is to really manually go through each layer of the model and reconnect them one by one again like this:

l = model.layers[1](decoded)  # layer 0 is the input layer, which we're replacing
for i in range(2, len(model.layers)):
    l = model.layers[i](l)
stacked_model = Model(ae_input, l)
stacked_model.compile(...)

while this works and produces the correct plot and no errors, this does not seem like the most elegant solution...

(btw, the copying of the model actually seems to be unnecessary as I'm not retraining anything.)

这篇关于连接Keras模型/替换输入但保留图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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