Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从Keras中经过训练的自动编码器模型获取解码器_Python_Keras_Neural Network_Autoencoder - Fatal编程技术网

Python 从Keras中经过训练的自动编码器模型获取解码器

Python 从Keras中经过训练的自动编码器模型获取解码器,python,keras,neural-network,autoencoder,Python,Keras,Neural Network,Autoencoder,我正在训练一个深度自动编码器,将人脸映射到128维的潜在空间,然后将其解码回原来的128x128x3格式 我希望在培训了自动编码器之后,我能够以某种方式“切片”自动编码器的后半部分,即负责使用功能性Keras API和autoenc_模型将潜在空间(128,)映射到图像空间(128,128,3)的解码器网络。get_layer() 以下是my的相关层: 是包含整个模型架构的笔记本 为了从经过培训的自动编码器获取解码器网络,我尝试使用: dec_model = Model(inputs=autoe

我正在训练一个深度自动编码器,将人脸映射到128维的潜在空间,然后将其解码回原来的128x128x3格式

我希望在培训了自动编码器之后,我能够以某种方式“切片”自动编码器的后半部分,即负责使用功能性Keras API和
autoenc_模型将潜在空间(128,)映射到图像空间(128,128,3)的解码器网络。get_layer()

以下是my的相关层:

是包含整个模型架构的笔记本

为了从经过培训的自动编码器获取解码器网络,我尝试使用:

dec_model = Model(inputs=autoenc_model.get_layer('decoder_input').input, outputs=autoenc_model.get_layer('decoder_output').output)

两者似乎都不起作用

我需要从自动编码器中提取解码器层,因为我想首先训练整个自动编码器模型,然后独立使用编码器和解码器

我在其他任何地方都找不到满意的答案。内置自动编码器仅介绍如何提取2层自动编码器的解码器


解码器输入/输出形状应该是:(128,)和(128,128,3),这分别是“解码器输入”层的输入形状和“解码器输出”层的输出形状。

我的解决方案不是很优雅,可能还有更好的解决方案,但由于还没有人回复,我将发布它(我实际上希望有人会这样做,这样我就可以改进我自己的实现,如下所示)

所以我所做的是建立一个网络,它可以接受二次输入,直接进入潜在空间。 不幸的是,这两个输入都是必须的,因此我最终得到了一个网络,它需要为“不需要的”输入(您将在稍后看到)使用满零的虚拟数组

使用Keras功能API:

image_input = Input(shape=image_shape)
conv1 = Conv2D(...,activation='relu')(image_input)
...
dense_encoder = Dense(...)(<layer>)
z_input = Input(shape=n_latent)
decoder_entry = Dense(...,activation='relu')(Add()([dense_encoder,z_input]))
...
decoder_output = Conv2DTranspose(...)


model = Model(inputs=[image_input,z_input], outputs=decoder_output)
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

encoder = Model(inputs=image_input,outputs=dense_encoder)
decoder = Model(inputs=[z_input,image_input], outputs=decoder_output)
然后,您可以使用以下方法获取潜在特征:

latent_features = encoder.predict(images)
或者使用带有潜在输入和虚拟变量的解码器(注意上面输入的顺序):

最后,我没有尝试过的另一个解决方案是使用相同的体系结构构建并行模型,一个是自动编码器,第二个是解码器部分,然后使用:

decoder_layer.set_weights(model_layer.get_weights()) 
它应该可以工作,但我还没有确认。它的缺点是每次训练自动编码器模型时都必须再次复制权重

总而言之,我知道这里有很多问题,但我只是发布了这篇文章,因为我没有看到其他人回复,我希望这篇文章对你仍然有用


如果有不清楚的地方,请发表评论。

需要进行几项更改:

z = UpSampling2D()(decoder_input)

现在,您可以使用自动编码器进行训练,并使用解码器进行预测

import numpy as np
autoenc_model.fit(np.ones((5,128,128,3)), np.ones((5,128,128,3)))
dec_model.predict(np.ones((1,8,8,2)))
您还可以参考这个自包含的示例:

谢谢你的回答,但是这一行,
dec\u model=model(直接输入,解码器输出)
,不应该是
dec\u model=model(z,解码器输出)
?谢谢。模型输入需要是一个“输入”层。我们将“输入”层添加到解码器中,以便以后可以将其作为一个独立的模型使用。解码器中名为“z”的上采样层有点误导,因为它通常是为编码器的潜在空间输出保留的。谢谢。与此相关的一个问题是我已经问过了。
decoder.predict([Z_inputs,np.zeros(shape=images.shape)])
decoder_layer.set_weights(model_layer.get_weights()) 
z = UpSampling2D()(decoder_input)
direct_input = Input(shape=(8,8,2), name='d_input')
#UnPooling-1
z = UpSampling2D()(direct_input)
autoenc_model = Model(input_img, decoder_output)
dec_model = Model(direct_input, decoder_output)
autoenc_model = Model(input_img, dec_model(decoder_input))
import numpy as np
autoenc_model.fit(np.ones((5,128,128,3)), np.ones((5,128,128,3)))
dec_model.predict(np.ones((1,8,8,2)))