提取编码器和解码器从训练的自动编码器

问题描述 投票:2回答:1

我要划分自动编码学习和应用分成两个部分以下https://blog.keras.io/building-autoencoders-in-keras.html和使用用于测试目的的时尚MNIST数据:

  1. 加载图像,这样做可能需要几个小时或几天,并使用一个回调来保存最好的自动编码模型拟合。这个过程可以在下面的部分是前几个星期。
  2. 使用这个最佳模型(由文件名手动选择)并画出原始图像,由自动编码器的编码器和使用该自动编码器的解码器中的预测进行的编码表示。我有问题(见第二步骤)提取从所述训练和保存的自动编码器的编码器和译码器层。

在第一步中我有一个非常简单的网络如下:

input_img = Input(shape=(784,))
# encoded representation
encoded = Dense(encoding_dim, activation='relu')(input_img)
# lossy reconstruction
decoded = Dense(784, activation='sigmoid')(encoded)

# full AE model: map an input to its reconstruction
autoencoder = Model(input_img, decoded)

# encoder: map an input to its encoded representation
encoder = Model(input_img, encoded)
# placeholder for an encoded input
encoded_input = Input(shape=(encoding_dim,))
# last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# decoder
decoder = Model(encoded_input, decoder_layer(encoded_input))

该网络是:

autoencoder.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_5 (InputLayer)         (None, 784)               0         
_________________________________________________________________
dense_5 (Dense)              (None, 32)                25120     
_________________________________________________________________
dense_6 (Dense)              (None, 784)               25872     
=================================================================

encoder.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_5 (InputLayer)         (None, 784)               0         
_________________________________________________________________
dense_5 (Dense)              (None, 32)                25120     
=================================================================

所以我训练模型,并通过autoencoder.save('fashion-autoencoder.hdf5')保存。在我的真实的例子我保存它的回调,从而通过保存编码器和解码器似乎并没有真正的解决办法一种解决方法。后来我加载图像(未显示)和不喜欢预测

# encode and decode some images from test set
encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)
# test the shape
print(encoded_imgs[0].shape)

并获得(32,0)的形状。

所以让我们走两步2在那里我有我的问题。我使用加载模型

encoder= K.models.load_model('fashion-autoencoder.hdf5')
# delete the last layers to get the encoder
encoder.layers.pop()
encoder.summary() # show model data

和编码器看起来相同的步骤一个原始什么使我想到了提取运作良好:

Layer (type)                 Output Shape              Param #   
=================================================================
input_5 (InputLayer)         (None, 784)               0         
_________________________________________________________________
dense_5 (Dense)              (None, 32)                25120     
=================================================================
Total params: 50,992
Trainable params: 50,992
Non-trainable params: 0

但我也得到警告

training.py:478: UserWarning: Discrepancy between trainable weights and collected trainable weights, did you set `model.trainable` without calling `model.compile` after ?
'Discrepancy between trainable weights and collected trainable'

我在一种方式理解,但不知道它是多么的重要。然后我再次(未示出)加载图像,并使用所述编码器

encoded_imgs = encoder.predict(x_test)

# test the shape
print(encoded_imgs[0].shape)

但形状不正确的用(784,)

所以,我的编码器提取没有工作,因为尺寸是不正确的。我甚至有那么成功提取解码器(形成保存自动编码),因为我不能使用push()和尝试的东西一样decoder = decoder.layers[-1:-2],但没有奏效。

所以,我一般的问题是如何提取装车型的零部件。

python tensorflow keras keras-layer autoencoder
1个回答
5
投票

由于您使用的功能性API来创建自动编码,最好的办法,以重建编码器和解码器再次使用该功能的API和Model类:

autoencoder= K.models.load_model('fashion-autoencoder.hdf5')

encoder = Model(autoencoder.input, autoencoder.layers[-2].output)

decoder_input = Input(shape=(encoding_dim,))
decoder = Model(decoder_input, autoencoder.layers[-1](decoder_input))

encoder.summary()
decoder.summary()

该车型总结:

Layer (type)                 Output Shape              Param #   
=================================================================
input_4 (InputLayer)         (None, 784)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 32)                25120     
=================================================================
Total params: 25,120
Trainable params: 25,120
Non-trainable params: 0
_________________________________________________________________


Layer (type)                 Output Shape              Param #   
=================================================================
input_6 (InputLayer)         (None, 32)                0         
_________________________________________________________________
dense_4 (Dense)              (None, 784)               25872     
=================================================================
Total params: 25,872
Trainable params: 25,872
Non-trainable params: 0
_________________________________________________________________

pop()涉及layers解决方案属性does not work since you need to update some of the internal attributes of the model。虽然,对于连续机型内置pop()法已经实施。

© www.soinside.com 2019 - 2024. All rights reserved.