自动编码器输出和特征向量不正确

autoencodeur output and feature vector are incorrect

我正在使用自动编码器对图像进行特征提取。我的图像是位图 => 像素值 = 0 或 1

我使用以下代码:

X_train_autoencodeur = X_train.reshape(-1, 96*96)
X_valid_autoencodeur=X_valid.reshape(-1,96*96)

input_img = Input(shape=(96*96,))
encoded = Dense(1024, activation='relu')(input_img)
encoded = Dense(512, activation='relu')(encoded)
encoded = Dense(256, activation='relu')(encoded)
encoded = Dense(128, activation='relu')(encoded)



decoded = Dense(256, activation='relu')(encoded)
decoded = Dense(512, activation='relu')(decoded)
decoded = Dense(1024, activation='relu')(decoded)
decoded = Dense(96*96, activation='sigmoid')(decoded)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

autoencoder.fit(X_train_autoencodeur, X_train_autoencodeur,
                epochs=100,
                batch_size=256,
                shuffle=True,
                validation_data=(X_valid_autoencodeur, X_valid_autoencodeur))


然后我用

绘制重建图像
decoded_imgs = autoencoder.predict(X_valid_autoencodeur)
plt.imshow(decoded_imgs[7].reshape(96,96))

经过 3 个 epoch 验证和训练损失值变得非常低并且没有改变

重建后的图像全是黑色,特征向量都一样

我已经训练了 100 个 epoch 的自动编码器,我应该训练更多吗? 我是否在我的代码上犯了错误,这可以解释糟糕的重建?

您的代码似乎是正确的。我相信问题出在数据本身。您是否对图像进行了预处理,使其在 0-1 之间标准化,如下所示:

x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.

根据我的经验,我知道自动编码器通常需要很长时间来训练(更像是 1000 个纪元),即使您使用的是卷积神经网络。

然而,您正在尝试使用完全连接的神经网络(并且是相当大的神经网络),这将花费更长的时间来学习一些东西。

我的建议是:尝试使用 CNN 和更多训练周期。

首先,您的网络(不必要地)非常大并且有很多参数,因此需要大量数据来训练。所以我建议在每个编码器和解码器结构中只尝试使用 2 层。 非常重要的一点是,对图像进行编码,卷积自动编码器肯定会得到更好的结果,所以试试吧。 最后为什么你使用二元交叉熵作为损失函数?试试mse.

祝你好运!