去噪自动编码器

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


相关推荐

  • 大盘进入涨势的条件

    大盘进入涨势的条件 春节以来,上证指数120分钟,所有的拐点都在这里了,除了本次,春节以来所有“顶、底”信号全部成功,皆为120分钟的拐点,所以,从周四开始,我就开始提示:不追买《120分钟顶信号》。即便是120分钟不能撼动日线的底部,那么你买在120分钟的顶位置,很显然,也是不对的。 120分钟顶,不追买是首要任务,其次呢,我们重点分析120分钟顶失败的意义。双龙之翼的顶底指标,是震荡类指标,即专门狙杀震荡拐点的…

    2022年6月29日
    26
  • Latex 左右引号

    Latex 左右引号参考:LaTeX技巧218:LaTeX如何正确输入引号:双引号“”单引号‘’Latex左右引号在latex中加引号时,使用""的输出为两个同向的引号;正确的做法为:“Firewall”2018.2…

    2022年6月25日
    42
  • 原备案在腾讯云 操作新增网站备案教程

    原备案在腾讯云 操作新增网站备案教程什么叫新增网站备案 备案主体已在腾讯云办理过备案 现要新增网站 则需进行新增网站 原备案在腾讯云 操作 备案准备 为了节约备案时间和顺利通过备案 建议提前了解备案流程 进行备案准备 因各地管局要求不同 需准备的材料也有所不同 建议提前了解各省 自治区 直辖市管局的备案要求 以及相关备案限制 备案要求 1 进行网站

    2026年3月19日
    3
  • eidos云矿机_空投挖矿

    eidos云矿机_空投挖矿eidos空投矿机虽然已经有几款网页版的了,甚至还有收费的,但并不适合长期运行,特别是需要挂在VPS里,所以就出现了这款控制台运行的,如果觉得对你有帮助就给个star吧。项目地址https://github.com/donjan-deng/eidos-miner环境需求nodejs>=10scatter>=11(网页版可在低版本运行)使用方法也有个简易的网页版…

    2022年9月30日
    4
  • 深入JAVA注解(Annotation):自定义注解

    深入JAVA注解(Annotation):自定义注解一、基础知识:元注解要深入学习注解,我们就必须能定义自己的注解,并使用注解,在定义自己的注解之前,我们就必须要了解Java为我们提供的元注解和相关定义注解的语法。元注解:  元注解的作用就是负责注解其他注解。Java5.0定义了4个标准的meta-annotation类型,它们被用来提供对其它annotation类型作说明。Java5.0定义的元注解:    1.@Targe…

    2022年7月13日
    21
  • mt4交易系统源码_如何将源码加载到mt4里面

    mt4交易系统源码_如何将源码加载到mt4里面1、打开编辑器:第二步,新建一个指标或者eaqml4文件.第三步创建一个ea文件:点击下一步:命名,aaa,点击下一步:全部不打勾,点击下一步:全部不打勾,点击完成:然后全部选中,删除代码:然后选中源码,复制到aaa里面,然后点击编写:就可以在ea里面找到你复制的ea了。指标的源码跟ea的一样,只需要建立一个指标文件,然后复制进去就可以了。如果觉得文章对你有帮助,可以关注公众号,谢谢您…

    2022年5月30日
    94

发表回复

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

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