去噪自动编码器

去噪自动编码器降噪自动编码器是一种用于图像去噪无监督的反馈神经网络原理如下图所示训练代码如下fromkeras.layersimportInput,Conv2D,MaxPooling2D,UpSampling2D,ZeroPadding2Dfromkeras.modelsimportModelfromkeras.callbacksimportTensorBoardfromkeras.datasetsimportmnistimportnumpyasnp(x_trai

大家好,又见面了,我是你们的朋友全栈君。

降噪自动编码器是一种用于图像去噪无监督的反馈神经网络

原理如下图所示

在这里插入图片描述

训练代码如下
from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, ZeroPadding2D
from keras.models import Model
from keras.callbacks import TensorBoard
from keras.datasets import mnist
import numpy as np

(x_train, y_train), (x_test, y_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))  # adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))  # adapt this if using `channels_first` image data format


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.)


def train_model():
    input_img = Input(shape=(28, 28, 1))  # adapt this if using `channels_first` image data format
    x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
    x = MaxPooling2D((2, 2), padding='same')(x)
    x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
    x = MaxPooling2D((2, 2), padding='same')(x)
    x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
    encoded = MaxPooling2D((2, 2), padding='same', name='encoder')(x)

    # at this point the representation is (4, 4, 8) i.e. 128-dimensional

    x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
    x = UpSampling2D((2, 2))(x)
    x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
    x = UpSampling2D((2, 2))(x)
    x = Conv2D(16, (3, 3), activation='relu')(x)
    x = UpSampling2D((2, 2))(x)
    decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

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

    autoencoder.fit(x_train_noisy, x_train,
                    epochs=20,
                    batch_size=128,
                    shuffle=True,
                    validation_data=(x_test_noisy, x_test),
                    callbacks=[TensorBoard(log_dir='/tmp/tb', histogram_freq=0, write_graph=False)])

    autoencoder.save('autoencoder.h5')

train_model()
测试代码如下
import numpy as np
from keras.models import Model
from keras.datasets import mnist
import cv2
from keras.models import load_model
from sklearn.metrics import label_ranking_average_precision_score
import time

print('Loading mnist dataset')
t0 = time.time()
(x_train, y_train), (x_test, y_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))  # adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))  # adapt this if using `channels_first` image data format

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.)
t1 = time.time()
print('mnist dataset loaded in: ', t1-t0)

print('Loading model :')
t0 = time.time()
# Load previously trained autoencoder
autoencoder = load_model('autoencoder.h5')
t1 = time.time()
print('Model loaded in: ', t1-t0)


def plot_denoised_images():
    denoised_images = autoencoder.predict(x_test_noisy.reshape(x_test_noisy.shape[0], x_test_noisy.shape[1], x_test_noisy.shape[2], 1))
    test_img = x_test_noisy[0]
    resized_test_img = cv2.resize(test_img, (280, 280))
    cv2.imshow('input', resized_test_img)
    cv2.waitKey(0)
    output = denoised_images[0]
    resized_output = cv2.resize(output, (280, 280))
    cv2.imshow('output', resized_output)
    cv2.waitKey(0)
打赏

如果对您有帮助,就打赏一下吧O(∩_∩)O
去噪自动编码器
去噪自动编码器

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/137703.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 解决Win10系统图标变白[通俗易懂]

    解决Win10系统图标变白[通俗易懂]解决因为安装路径文件名更改导致图标变白

    2022年10月19日
    6
  • dmesg 命令

    dmesg 命令dmesg 这个命令 Linux 下用的还是比较多的 现在来详细看一下 Linuxdmesg 命令用于显示开机信息 kernel 会将开机信息存储在 ringbuffer 中 您若是开机时来不及查看信息 可利用 dmesg 来查看 开机信息亦保存在 var log 目录中 名称为 dmesg 的文件里 一 语法 dmesg cn s lt 缓冲区大小 gt 二 选项 c 显示信息后

    2025年7月28日
    2
  • 数据库置疑什么原因_sql2008数据库置疑

    数据库置疑什么原因_sql2008数据库置疑在MSSQLSERVER中一直有这样的问题,SQLSERVER的状态”置疑”,我们先来分析一下SQLSERVER数据库”置疑”的原因:1.错误的删除日志;2.硬件(HD)损坏,造成日志和数据文件写错误;3.硬盘的空间不够,比如日志文件过大;解决办法:这是最简单的办法是有数据库的全备份,然后恢复即可.步骤:1.删除原始的数据库:USEMASTERGODROPDATABASEDB_SUEPE…

    2022年8月20日
    7
  • css background之设置图片为背景技巧

    css background之设置图片为背景技巧

    2021年9月20日
    52
  • linux 查看cpu核数、内存总容量、硬盘总容量

    linux 查看cpu核数、内存总容量、硬盘总容量一 查看 cpu 核数 cat proc cpuinfo grep physicalid sort uniq wc lcat proc cpuinfo grep process sort uniq wc l 二 查看内存总容量 cat proc meminfo 三 查看硬盘总容量 df hl 显示 文件系统容量已用可用已用 挂载点 FilesystemSi

    2025年7月14日
    3
  • python的dropna函数_Pandas dropna()函数不工作「建议收藏」

    python的dropna函数_Pandas dropna()函数不工作「建议收藏」我试图从pandas数据框中删除NA值。我使用了dropna()(它应该从数据帧中删除所有NA行)。然而,它不起作用。代码如下:importpandasaspdimportnumpyasnpprison_data=pd.read_csv(‘https://andrewshinsuke.me/docs/compas-scores-two-years.csv’)这就是获取数据帧的方法。…

    2025年11月19日
    10

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号