去噪自动编码器

去噪自动编码器降噪自动编码器是一种用于图像去噪无监督的反馈神经网络原理如下图所示训练代码如下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)
上一篇 2022年6月1日 下午3:46
下一篇 2022年6月1日 下午3:46


相关推荐

  • 韦根协议c语言,韦根协议(26位)及其读取算法

    韦根协议c语言,韦根协议(26位)及其读取算法000000000000 除去第一次原始数据的处理 每次与的数据都是向右移 8 位 所有与完了都不要忘记向右移 我来分别说一下 右移的位数 处理原始数据向右移 1 位 HID 向右移 16 位 Pid1 右移 8 位 Pid2 不用移了 接下来放程序 mw shl code c true volatilelong 0 volatileintr

    2026年3月18日
    2
  • flutter 配置文件_怎么配置mysql的环境变量

    flutter 配置文件_怎么配置mysql的环境变量废话不多说,这篇文章仅仅作为自己的备忘。在安装flutter的时候,由于要配置环境变量,在windows上面倒还好,什么都是可视化的操作,自然不会有多大的问题,然而在mac上面,由于对mac不是很熟悉,就环境变量的配置都纠结了好久,每次配好之后重启一下,环境变量就不知所踪,百度了好久,最后才找到解决方案,当然,大神可以略过,这篇文章也只针对mac菜鸟。touch~/.bash_profile…

    2025年6月22日
    3
  • 顺序结构程序设计[通俗易懂]

    顺序结构程序设计[通俗易懂]顺序结构程序设计总结1编程解题一般过程:  ①分析问题  ②设计算法  ③编写调试程序  ④运行程序验证结果2程序的基本结构  ①头文件   是编程必写语句        &…

    2025年6月6日
    4
  • 于Linux-2.6.32内核上编译ipset-6.23的坎坷经历[通俗易懂]

    于Linux-2.6.32内核上编译ipset-6.23的坎坷经历[通俗易懂]新版本的ipset上周在儿童医院给小小看病等待叫号的间隙,收到了Netfilter邮件列表的推送消息,一览了ipset最新的6.23版本的新特性,很多正是我目前所需要的,特别是timeout和skbinfo参数的支持,具体的详情请自行查看manual,如果不想看那么多,我这里简单的贴一下:  timeout      All set types supportstheoptional

    2026年4月16日
    5
  • AI笔记系列(三)—— Agent与多智能体系统

    AI笔记系列(三)—— Agent与多智能体系统

    2026年3月15日
    2
  • atop用法_atop linux 命令 在线中文手册

    atop用法_atop linux 命令 在线中文手册atop 简介本文要介绍的 atop 就是一款用于监控 Linux 系统资源与进程的工具 它以一定的频率记录系统的运行状态 所采集的数据包含系统资源 CPU 内存 磁盘和网络 使用情况和进程运行情况 并能以日志文件的方式保存在磁盘中 服务器出现问题后 我们可获取相应的 atop 日志文件进行分析 atop 是一款开源软件 我们可以从这里获得其源码和 rpm 安装包 一 atop 使用方法在安装 atop 之后 我们在命令行

    2025年11月4日
    8

发表回复

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

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