我希望将自编码器的学习和应用分为两部分,按照https://blog.keras.io/building-autoencoders-in-keras.html所述的步骤,并使用时尚MNIST数据进行测试:
- 加载图像,进行拟合(可能需要数小时或数天),并使用回调函数保存最佳自编码器模型。该过程可能需要数周才能完成第二个步骤。
- 使用此最佳模型(手动选择文件名),并绘制原始图像、由自编码器编码器生成的编码表示以及使用自编码器解码器进行预测的结果。我在从训练和保存的自编码器中提取编码器和解码器层方面遇到了问题(请参见第二步)。
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]
这样的东西,但它不起作用。
因此,我的一般问题是如何提取加载的模型的部分。
encoder = autoencoder.get_layer('dense_5')
和decoder = autoencoder.get_layer('dense_6')
就足够了吗?如果在创建时为它们自定义名称,例如encoder = Model(input_img, encoded, name="decoder")
,那就更好了。 - HUSMEN