去噪自动编码器

去噪自动编码器降噪自动编码器是一种用于图像去噪无监督的反馈神经网络原理如下图所示训练代码如下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)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • hypervisor kvm_docker vmware

    hypervisor kvm_docker vmwareLinuxhypervisor介绍荣涛2021年10月14日1.引言管理程序对操作系统所做的事情就像操作系统对进程所做的大致相同。它们为执行提供隔离的虚拟硬件平台,从而提供对底层机器的完全访问的错觉。但并非所有的虚拟机管理程序都是一样的,这是一件好事,因为Linux是关于灵活性和选择的。本文首先简要介绍虚拟化和管理程序,然后探讨几个基于Linux的管理程序。2.平台设备模拟平台虚拟化是关于在两个或多个操作系统之间共享一个平台,以便更有效地利用资源。但平台不仅仅意味着处理

    2022年9月22日
  • 电商后台管理系统(一)

    电商后台管理系统(一)后台管理系统git地址:https://gitee.com/kk23851一.项目大体架构二.用户登录用户登录页面思路:用Element表单验证完成以后,把数据存储到本地用户登录代码位置如图:三.用户管理用户列表页面绘制用户列表基本结构,请求用户列表数据,将用户列表数据展示,实现用户列表分页,实现搜索功能,实现添加用户,修改用户信息,删除用户,分配权限用户管理代码位置如图:四.权限管理权限管理有俩个板块分别是角色列表和权限列表,用到的技术无非就是element-ui,所

    2022年6月10日
  • 两万字博文教你python爬虫requests库【详解篇】[通俗易懂]

    两万字博文教你python爬虫requests库【详解篇】[通俗易懂] ????上一篇博文一篇万字博文带你入坑爬虫这条不归路(你还在犹豫什么&抓紧上车)【❤️熬夜整理&建议收藏❤️】被众多爬虫爱好者/想要学习爬虫的小伙伴们阅读之后,很多小伙伴私信我说——大佬搞爬虫都是用的socket套接字嘛????? ????(苦笑)“那肯定不是啊!python为我们封装了那么多伟大而又简单实用的爬虫库,”不过我想说的是,“学啥技术都是从底层抓起,万丈高楼平地起,它也是基于地基稳!所以在入坑文中简单地介绍使用了下底层爬虫库——socket!”???? ????而本文

    2022年5月4日
  • stm32编程入门书籍_STM32开发板

    stm32编程入门书籍_STM32开发板1.STM32需要了解的基础知识STM32系列专为要求高性能、低成本、低功耗的嵌入式应用设计的ARMCortex®-M0,M0+,M3,M4和M7内核,按内核架构分为不同产品:主流产品(STM32F0、STM32F1、STM32F3)、超低功耗产品(STM32L0、STM32L1、STM32L4、STM32L4+)、高性能产品(STM32F2、STM32F4、STM32F7、STM32H7)。内核是ARM公司统一设计的于ARMv7架构的Cortex系列由ARM公司在2006年推出,Co

    2022年9月7日
  • Σ求和符号_西格玛符号怎么打

    Σ求和符号_西格玛符号怎么打转自:https://zh.wikipedia.org/wiki/%E6%B1%82%E5%92%8C%E7%AC%A6%E5%8F%B7求和符号(Σ,sigma),是欧拉于1755年首先使用的。这个符号是源于希腊文σογμαρω(增加)的字头,Σ正是σ的大写。求和的结果是给定的数值相加后的总值,又称加总。举例而言,若有4个数值:1、3、5、7,则这4个数值的总和为:

    2022年10月9日
  • ventricular septal defect_three identical strangers

    ventricular septal defect_three identical strangers转一个BLOG,是美国一同行写的关于eXtremeDB的,但作者似乎是个中国人。这是BLOG原文地址:http://www.weiqigao.com/blog/2006/04/25/extremedb_exposed.html…

    2022年10月14日

发表回复

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

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