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

时间:2019-04-01 15:09:53

标签: python keras neural-network autoencoder

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

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

以下是我的model的相关层:

INPUT_SHAPE=(128,128,3)
input_img = Input(shape=INPUT_SHAPE, name='enc_input')

#1
x = Conv2D(64, (3, 3), padding='same', activation='relu')(input_img)
x = BatchNormalization()(x)

//Many Conv2D, BatchNormalization(), MaxPooling() layers
.
.
.

#Flatten
fc_input = Flatten(name='enc_output')(x)

y = Dropout(DROP_RATE)(fc_input)
y = Dense(128, activation='relu')(y)
y = Dropout(DROP_RATE)(y)
fc_output = Dense(128, activation='linear')(y)   

#Reshape
decoder_input = Reshape((8, 8, 2), name='decoder_input')(fc_output)

#Decoder part

#UnPooling-1
z = UpSampling2D()(decoder_input)
//Many Conv2D, BatchNormalization, UpSampling2D layers
.
.
.
#16
decoder_output = Conv2D(3, (3, 3), padding='same', activation='linear', name='decoder_output')(z)

autoenc_model = Model(input_img, decoder_output)

here是包含整个模型体系结构的笔记本。

为了从训练有素的自动编码器获取解码器网络,我尝试使用:

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

dec_model = Model(autoenc_model.get_layer('decoder_input'), autoenc_model.get_layer('decoder_output'))

似乎都不起作用。

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

我在其他任何地方都找不到满意的答案。构建自动编码器的Keras blog article仅涉及如何为2个分层的自动编码器提取解码器。

解码器的输入/输出形状应为:(128,)和(128,128,3),分别是'decoder_input'的输入形状和'decoder_output'层的输出形状。

2 个答案:

答案 0 :(得分:2)

需要更改:

List<Element>

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='gosuggest_inputSrc']))).sendKeys("M");
List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("Xpath of the dynamic drop down")));
for (WebElement element:myList) {
         if(element.getText().contains("Mumbai"));
         element.click();
    }

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)

您还可以参考以下独立示例: https://github.com/keras-team/keras/blob/master/examples/variational_autoencoder.py

答案 1 :(得分:1)

我的解决方案不是很好,可能还有更好的解决方案,但是由于目前还没有人答复,因此我将其发布(我实际上是希望有人这样做,以便您可以改进自己的实现。 '将在下面看到)。

所以我所做的就是建立了一个网络,该网络可以将辅助输入直接输入到潜在空间。 不幸的是,这两个输入都是强制性的,因此我最终得到一个网络,该网络需要为“不需要的”输入填充全零的伪数组(您将在第二秒看到)。

使用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)

请注意,您不应该编译编码器和解码器。

(某些代码被省略或带有...留给您,以满足您的特定需求)。

最后,要训练您必须提供一个空数组。因此,要训练整个自动编码器:

图片在这种情况下为X

model.fit([images,np.zeros((len(n_latent),...))],images)

然后您可以使用以下功能获得潜在功能:

latent_features = encoder.predict(images)

或将解码器与潜在输入和伪变量一起使用(请注意上面输入的顺序):

decoder.predict([Z_inputs,np.zeros(shape=images.shape)])

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

decoder_layer.set_weights(model_layer.get_weights()) 

应该可以,但是我还没有确认。这样做的缺点是每次训练自动编码器模型时都必须再次复制权重。

总而言之,我知道这里有很多问题,但是我再一次发布,只是因为我没有看到其他人回答,并希望这对您仍然有用。

如果不清楚,请发表评论。