神经网络学习小记录(keras)-去噪自编码器(全连接、卷积)

神经网络学习小记录(keras)-去噪自编码器(全连接、卷积)

概念

**
在深度学习中,自编码器是非常有用的一种无监督学习模型。两个核心部分是编码器(encoder)和解码器(decoder),前者将原始表示编码成隐层表示,后者将隐层表示解码成原始表示(相似),训练目标为最小化重构误差,而且一般而言,隐层的特征维度低于原始特征维度。
去噪自编码器(denoising autoencoder,DAE)是一类接受损坏数据作为输入,并训练来预测原始未被损坏数据作为输入的自编码器。-----来源百度百科
encoder和decoder可以由多种深度学习模型构成,例如全连接层、卷积层或LSTM等,以下使用Keras来实现用于图像去噪的全连接卷积自编码器

理论推导

Denoising Autoencoders (dA)

作用

1.对图像去噪
2.对数据进行压缩降维(省略);

Keras代码部分-------encoder和decoder使用dense来实现。

#- Denoising Autoencoder example
import numpy as np
np.random.seed(1337)  # for reproducibility

from keras.datasets import mnist
from keras.models import Model
from keras.utils import np_utils
from keras.layers.convolutional import Conv2D, MaxPooling2D, UpSampling2D, ZeroPadding2D
from keras.layers import Dense, Input
import matplotlib.pyplot as plt

# download the mnist to the path '~/.keras/datasets/' if it is the first time to be called
# X shape (60,000 28x28), y shape (10,000, )
(X_train,y_train), (X_test, y_test) = mnist.load_data()

# 1.dense  data pre-processing
X_train = X_train.reshape(X_train.shape[0], -1)
X_test = X_test.reshape(X_test.shape[0], -1)

X_train = X_train.astype("float32")/255.
X_test = X_test.astype("float32")/255.
print(X_train.shape)
print(X_test.shape)

##加噪
noise_factor = 0.5
X_train_noisy = X_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=X_train.shape)
X_test_noisy = X_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=X_test.shape)

X_train_noisy = np.clip(X_train_noisy, 0., 1.)
X_test_noisy = np.clip(X_test_noisy, 0., 1.)

##1.去噪自编码器全连接网络建模
input_img = Input(shape=(784,))

# encoder layers
encoded = Dense(128, activation='relu')(input_img)
encoded = Dense(64, activation='relu')(encoded)
encoded = Dense(10, activation='relu')(encoded)
encoder_output = Dense(2)(encoded)

# decoder layers
decoded = Dense(10, activation='relu')(encoder_output)
decoded = Dense(64, activation='relu')(decoded)
decoded = Dense(128, activation='relu')(decoded)
decoded = Dense(784, activation='tanh')(decoded)

# construct the autoencoder model
autoencoder = Model(input=input_img, output=decoded)



autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

epochs = 10
batch_size = 128

history = autoencoder.fit(X_train_noisy, X_train,
                          batch_size=batch_size,
                          epochs=epochs, verbose=1,
                          validation_data=(X_test_noisy, X_test))
## 查看编码效果
decoded_imgs = autoencoder.predict(X_test_noisy)

n = 10
plt.figure(figsize=(20, 6))
for i in range(n):
    # 原图
    ax = plt.subplot(3, n, i + 1)
    plt.imshow(X_test_noisy[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    # 解码效果图
    ax = plt.subplot(3, n, i + n + 1)
    plt.imshow(decoded_imgs[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

plt.show()
autoencoder.summary()
## 训练过程可视化
print(history.history.keys())

plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper right')
plt.show()

网络结构:
在这里插入图片描述

当epoch=10时:
训练输入与输出:在这里插入图片描述
损失值:
在这里插入图片描述

Keras代码部分-------encoder和decoder使用CNN来实现。

#- Denoising Autoencoder example
import numpy as np
np.random.seed(1337)  # for reproducibility

from keras.datasets import mnist
from keras.models import Model
from keras.utils import np_utils
from keras.layers.convolutional import Conv2D, MaxPooling2D, UpSampling2D, ZeroPadding2D
from keras.layers import Dense, Input
import matplotlib.pyplot as plt

# download the mnist to the path '~/.keras/datasets/' if it is the first time to be called
# X shape (60,000 28x28), y shape (10,000, )
(X_train,y_train), (X_test, y_test) = mnist.load_data()

# 2.CNN data pre-processing
# X_train = X_train.reshape(X_train.shape[0], 28,28,1)
# X_test = X_test.reshape(X_test.shape[0], 28,28,1)

X_train = X_train.astype("float32")/255.
X_test = X_test.astype("float32")/255.
print(X_train.shape)
print(X_test.shape)

##加噪
noise_factor = 0.5
X_train_noisy = X_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=X_train.shape)
X_test_noisy = X_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=X_test.shape)

X_train_noisy = np.clip(X_train_noisy, 0., 1.)
X_test_noisy = np.clip(X_test_noisy, 0., 1.)

## 2.去噪自编码器卷积网络建模
# x = Input(shape=(28, 28, 1))
#
# # 编码器
# conv1_1 = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
# pool1 = MaxPooling2D((2, 2), padding='same')(conv1_1)
# conv1_2 = Conv2D(32, (3, 3), activation='relu', padding='same')(pool1)
# h = MaxPooling2D((2, 2), padding='same')(conv1_2)
#
#
# # 解码器
# conv2_1 = Conv2D(32, (3, 3), activation='relu', padding='same')(h)
# up1 = UpSampling2D((2, 2))(conv2_1)
# conv2_2 = Conv2D(32, (3, 3), activation='relu', padding='same')(up1)
# up2 = UpSampling2D((2, 2))(conv2_2)
# r = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(up2)
#
# autoencoder = Model(inputs=x, outputs=r)

autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

epochs = 10
batch_size = 128

history = autoencoder.fit(X_train_noisy, X_train,
                          batch_size=batch_size,
                          epochs=epochs, verbose=1,
                          validation_data=(X_test_noisy, X_test))
## 查看编码效果
decoded_imgs = autoencoder.predict(X_test_noisy)

n = 10
plt.figure(figsize=(20, 6))
for i in range(n):
    # 原图
    ax = plt.subplot(3, n, i + 1)
    plt.imshow(X_test_noisy[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    # 解码效果图
    ax = plt.subplot(3, n, i + n + 1)
    plt.imshow(decoded_imgs[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

plt.show()
autoencoder.summary()
## 训练过程可视化
print(history.history.keys())

plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper right')
plt.show()

网络结构:
在这里插入图片描述

训练输入与输出:
在这里插入图片描述

损失值:
在这里插入图片描述
----------未完待续

  • 4
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个使用深度收缩残差网络中的去噪自适应调节阈值模块的卷积自编码器去噪的示例代码: ```python import tensorflow as tf from tensorflow.keras.layers import Conv1D, Conv1DTranspose, Input, Lambda, GlobalAveragePooling1D, Dense, Reshape, Multiply, Add from tensorflow.keras import backend as K # 定义软阈值函数 def soft_threshold(x, threshold): return K.sign(x) * K.maximum(K.abs(x) - threshold, 0.0) # 定义SENet模块 def se_block(x): squeeze = GlobalAveragePooling1D()(x) excitation = Dense(K.int_shape(x)[-1] // 16, activation='relu')(squeeze) excitation = Dense(K.int_shape(x)[-1], activation='sigmoid')(excitation) excitation = Reshape((1, K.int_shape(x)[-1]))(excitation) scale = Multiply()([x, excitation]) return scale # 定义深度收缩残差网络中的去噪自适应调节阈值模块 def denoising_threshold_module(x): threshold = Lambda(lambda x: K.mean(K.abs(x), axis=[1,2], keepdims=True))(x) threshold = Lambda(lambda x: soft_threshold(x[0], x[1]))([x, threshold]) return threshold # 定义带有深度收缩残差网络中的去噪自适应调节阈值模块的卷积自编码器 def denoising_autoencoder(input_shape): # 编码器 inputs = Input(shape=input_shape) encoded = Conv1D(32, 3, activation='relu', padding='same')(inputs) encoded = Conv1D(16, 3, activation='relu', padding='same')(encoded) # 添加深度收缩残差网络中的去噪自适应调节阈值模块 thresholded_encoded = denoising_threshold_module(encoded) # 解码器 decoded = Conv1DTranspose(16, 3, activation='relu', padding='same')(thresholded_encoded) decoded = Conv1DTranspose(32, 3, activation='relu', padding='same')(decoded) decoded = Conv1D(1, 3, activation='sigmoid', padding='same')(decoded) # 构建自编码器模型 autoencoder = tf.keras.Model(inputs, decoded) return autoencoder # 创建卷积自编码器模型 input_shape = (256, 1) # 输入形状 model = denoising_autoencoder(input_shape) model.summary() ``` 在上述代码中,我们定义了一个带有深度收缩残差网络中的去噪自适应调节阈值模块的卷积自编码器模型。在编码器部分的最后添加了一个去噪自适应调节阈值模块,该模块通过计算编码器输出的平均绝对值来动态调节软阈值函数的阈值。然后,我们使用去噪自适应调节阈值模块输出的编码器输出进行解码。在解码器部分,我们保持与之前相同的结构。 最后,通过构建自编码器模型,并打印模型摘要,可以查看模型的结构和参数数量。请根据你的需求进行适当的修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值