【自动编码器2 图像去噪 全python代码】Autoencoder Image Denoising

1. 自动编码器简介

自动编码器是一种特殊类型的神经网络,经过训练后可将其输入复制到输出。例如,给定一张手写数字图像,自动编码器首先将该图像编码为较低维度的潜在表示,然后将潜在表示解码回图像。自动编码器学习压缩数据,同时最小化重构误差。
书接上回 【自动编码器1 重建图像 全python代码】,自动编码器还可以通过训练来去除图像中的噪声。您将通过对每幅图像应用随机噪声来创建 Fashion MNIST 数据集的噪声版本。然后,您将使用噪声图像作为输入,并使用原始图像作为目标来训练自动编码器。

2. Python 实现

让我们重新导入数据集。

2.1 导入 TensorFlow 和其他库

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf

from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.model_selection import train_test_split
from tensorflow.keras import layers, losses
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.models import Model

2.2 加载数据集

导入 Fashion MNIST 数据集训练基本自动编码器。此数据集中的每幅图像均为 28x28 像素。

(x_train, _), (x_test, _) = fashion_mnist.load_data()

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

x_train = x_train[..., tf.newaxis]
x_test = x_test[..., tf.newaxis]

print(x_train.shape)

2.3 自动编码器:图像去噪

先通过对每幅图像应用随机噪声来创建 Fashion MNIST 数据集的噪声版本。然后,您将使用噪声图像作为输入,并使用原始图像作为目标来训练自动编码器。

  1. 向图像添加随机噪声
noise_factor = 0.2
x_train_noisy = x_train + noise_factor * tf.random.normal(shape=x_train.shape)
x_test_noisy = x_test + noise_factor * tf.random.normal(shape=x_test.shape)

x_train_noisy = tf.clip_by_value(x_train_noisy, clip_value_min=0., clip_value_max=1.)
x_test_noisy = tf.clip_by_value(x_test_noisy, clip_value_min=0., clip_value_max=1.)
  1. 绘制噪声图像
n = 10
plt.figure(figsize=(20, 2))
for i in range(n):
    ax = plt.subplot(1, n, i + 1)
    plt.title("original + noise")
    plt.imshow(tf.squeeze(x_test_noisy[i]))
    plt.gray()
plt.show()

在这里插入图片描述
3. 定义卷积自动编码器
您将使用 中的Conv2D层encoder和中的Conv2DTranspose层训练卷积自动编码器 decoder。

class Denoise(Model):
  def __init__(self):
    super(Denoise, self).__init__()
    self.encoder = tf.keras.Sequential([
      layers.Input(shape=(28, 28, 1)),
      layers.Conv2D(16, (3, 3), activation='relu', padding='same', strides=2),
      layers.Conv2D(8, (3, 3), activation='relu', padding='same', strides=2)])

    self.decoder = tf.keras.Sequential([
      layers.Conv2DTranspose(8, kernel_size=3, strides=2, activation='relu', padding='same'),
      layers.Conv2DTranspose(16, kernel_size=3, strides=2, activation='relu', padding='same'),
      layers.Conv2D(1, kernel_size=(3, 3), activation='sigmoid', padding='same')])

  def call(self, x):
    encoded = self.encoder(x)
    decoded = self.decoder(encoded)
    return decoded

autoencoder = Denoise()
autoencoder.compile(optimizer='adam', loss=losses.MeanSquaredError())
autoencoder.fit(x_train_noisy, x_train,
                epochs=10,
                shuffle=True,
                validation_data=(x_test_noisy, x_test))

查看编码器的摘要。注意图像从 28x28 降采样到 7x7。

autoencoder.encoder.summary()

在这里插入图片描述
同样解码器将图像从 7x7 上采样到 28x28。

autoencoder.decoder.summary()

在这里插入图片描述
4. 绘制自动编码器产生的噪声图像和去噪图像

encoded_imgs = autoencoder.encoder(x_test_noisy).numpy()
decoded_imgs = autoencoder.decoder(encoded_imgs).numpy()
n = 10
plt.figure(figsize=(20, 4))
for i in range(n):

    # display original + noise
    ax = plt.subplot(2, n, i + 1)
    plt.title("original + noise")
    plt.imshow(tf.squeeze(x_test_noisy[i]))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    # display reconstruction
    bx = plt.subplot(2, n, i + n + 1)
    plt.title("reconstructed")
    plt.imshow(tf.squeeze(decoded_imgs[i]))
    plt.gray()
    bx.get_xaxis().set_visible(False)
    bx.get_yaxis().set_visible(False)
plt.show()

在这里插入图片描述
上图的第二行即为自动编码器图像去噪的效果。

3. Reference

[1] https://www.tensorflow.org/tutorials/generative/autoencoder
[2] https://www.deeplearningbook.org

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值