这是采用自编码器实现黑白图像去噪声代码
#==========================================
# This code was written by Sihua Peng, PhD.
#==========================================
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D
# 加载数据
(x_train, _), (x_test, _) = mnist.load_data()
# 归一化数据
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
# 调整输入数据的形状
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))
# 添加噪声
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)
# 剪裁数据使其在[0, 1]范围内
x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy = np.clip(x_test_noisy, 0., 1.)
# Autoencoder 模型
input_img = Input(shape=(28, 28, 1))
# 编码器
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
# 解码器
x = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
# 模型编译
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
# 模型训练
autoencoder.fit(x_train_noisy, x_train,
epochs=100,
batch_size=128,
shuffle=True,
validation_data=(x_test_noisy, x_test))
# 对比原图,带噪声图和去噪后的图
decoded_imgs = autoencoder.predict(x_test_noisy)
n = 10 # 显示10个数字
plt.figure(figsize=(20, 4))
for i in range(1, n + 1):
# 显示原图
ax = plt.subplot(2, n, i)
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(2, n, i + n)
plt.imshow(decoded_imgs[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
这是采用自编码器实现彩色图像去噪声代码
#==========================================
# This code was written by Sihua Peng, PhD.
#==========================================
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D
# 加载数据
(x_train, _), (x_test, _) = cifar10.load_data()
# 归一化数据
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
# 调整输入数据的形状
x_train = np.reshape(x_train, (len(x_train), 32, 32, 3))
x_test = np.reshape(x_test, (len(x_test), 32, 32, 3))
# 添加噪声
noise_factor = 0.1
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)
# 剪裁数据使其在[0, 1]范围内
x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy = np.clip(x_test_noisy, 0., 1.)
# Autoencoder 模型
input_img = Input(shape=(32, 32, 3))
# 编码器
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
# 解码器
x = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)
# 模型编译
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
# 模型训练
autoencoder.fit(x_train_noisy, x_train,
epochs=100,
batch_size=128,
shuffle=True,
validation_data=(x_test_noisy, x_test))
# 对比原图,带噪声图和去噪后的图
decoded_imgs = autoencoder.predict(x_test_noisy)
n = 10 # 显示10个数字
plt.figure(figsize=(20, 6))
for i in range(n):
# 显示原图
ax = plt.subplot(3, n, i + 1)
plt.imshow(x_test_noisy[i])
plt.title("Noisy")
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# 显示重构的图
ax = plt.subplot(3, n, i + 1 + n)
plt.imshow(decoded_imgs[i])
plt.title("Denoised")
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# 显示原图
ax = plt.subplot(3, n, i + 1 + 2*n)
plt.imshow(x_test[i])
plt.title("Original")
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()