去噪自动编码器

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


相关推荐

  • MATLAB 8.1 R2013a license.lic 问题

    MATLAB 8.1 R2013a license.lic 问题MATLAB8.1R2013alicense.lic问题…\MATLAB8.1R2013a\Matlab801\serial\license.lic1.修改系统时间修改系统时间至之前license.lic时间。2.修改license.liclicense.lic内容修改为:INCREMENTAerospace_BlocksetMLM99permanentuncounted\A05070F00D1EB1F923

    2022年7月26日
    13
  • mysql workbench怎么导入数据库sql文件_workbench怎么创建数据库

    mysql workbench怎么导入数据库sql文件_workbench怎么创建数据库把Excel表格通过MySqlWorkbench导入数据库表中的使用总结今天接到一个任务,把excel表中的数据导入到mysql数据库中,通过半个多小时的鼓捣,基本上摸清了里面的门道。首先,准备所要导入的ecxel文件,需要把excel另存为*.csv类型的文件,然后通过notepad转换编码格式为utf-8类型,注意要把表头删去,否则会出问题。其次,导入所转换的文件

    2022年9月21日
    3
  • 系统环境变量与用户环境变量区别_windows7建立用户变量

    系统环境变量与用户环境变量区别_windows7建立用户变量区别:  环境变量分为系统环境变量和用户环境变量。环境变量是指系统环境变量,对所有用户起作用,而用户环境变量只对当前用户起作用。  例如你要用opencv,那么你把opencv的bin目录加入到path变量下面,那么它就是系统环境变量,所用用户登陆,在命令行输入opencv都会有opencv的帮助信息出来。而如果你在某个用户的变量下面新建一个变量,那么它就只对这个用户有用,当你以其他用户登陆时这

    2022年9月16日
    2
  • 灵动标签的使用方法 ecms通过运行sql获取须要的记录

    灵动标签的使用方法 ecms通过运行sql获取须要的记录

    2021年12月9日
    42
  • Win10与Ubuntu 18.04双系统安装。(Win10引导Linux)[通俗易懂]

    Win10与Ubuntu 18.04双系统安装。(Win10引导Linux)[通俗易懂]作为菜鸟,为了满足我自己的求知欲,特别照着几篇大神教程装了一遍,给大家分享一下流程。1、win10安装(已安装请略过)1)系统U盘制作(参照微信公众号“软件安装管家”):http://mp.weixin.qq.com/s?__biz=MzIwMjE1MjMyMw==&mid=2650199025&idx=1&sn=49b0d9b6d9f02b68223f7a9f913cde…

    2022年7月24日
    21
  • 【SpringBoot】1、创建第一个SpringBoot项目

    【SpringBoot】1、创建第一个SpringBoot项目创建SpringBoot项目可以通过两种方式,1、通过访问:https://start.spring.io/,SpringBoot的官方网站进行创建SpringBoot项目;2、通过工具(例如:Idea)创建SpringBoot项目。本次使用开发工具:Idea创建我的第一个SpringBoot项目。首先,打开我们的Idea开发工具选择CreateNewPoject(创建一个新的项目)…

    2022年10月13日
    2

发表回复

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

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