去噪自动编码器

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


相关推荐

  • allure 命令行参数说明

    allure 命令行参数说明一、查看allure命令的帮助文档allure-hallure命令的语法格式allure[options][command][commandoptions]options列表Options:–help命令行帮助文档-q,–quiet切换至安静模式Default:false-v,–verbose切换至冗长模式Default:false–ve…

    2022年7月26日
    22
  • 单片机交通信号灯控制系统设计_交通灯控制电路设计图

    单片机交通信号灯控制系统设计_交通灯控制电路设计图交通信号灯控制系统设计 作为一个硬件程序设计民工,最近一直在学习python,写个爬虫,排个序,再画个界面,其实还是挺好玩的。然而这不是我的主业啊!!!-_-|||下学期开学就要找工作了,明天刚好是新的一个月,还是滚回去调我的FPGA吧。今天先更新一个很小很小的例子作为开端,这是前几天xxx给我出的一道很随意的题目,主要是看面对一个项目…

    2022年9月24日
    2
  • Activity工作流:流程创建总结

    Activity工作流:流程创建总结Activiti工作流的应用示例1、新建流程模型模型管理->模型工作区点击“创建”后会立即跳转到“流程在线设计器”页面,请参考下一节2、在线流程设计器模型管理->模型工作区->编辑3、设置流程属性l名称:流程定义名称l描述信息:流程定义描述l流程标识:流程定义KEY,对应procDefKey,用来标识一个流程4、拖拽元素介…

    2022年5月23日
    31
  • 国家标准《电子计算机机房设计规范》(GB50174-93)_计算机机房设计标准GB50174

    国家标准《电子计算机机房设计规范》(GB50174-93)_计算机机房设计标准GB501748电气8.1供配电8.1.1电子信息系统机房用电负荷等级及供电要求应根据机房的等级,按现行国家标准《供配电系统设计规范》GB50052及本规范附录A的要求执行。8.1.2电子信息设备供电电源质量应根据电子信息系统机房的等级,按本规范附录A的要求执行。8.1.3供配电系统应为电子信息系统的可扩展性预留备用容量。8.1.4户外供电线路不宜采用架空方式敷设。当户外供电线路采用具有金属外护套的电缆时,在…

    2022年10月2日
    2
  • 协同过滤算法

    协同过滤算法###1.协同过滤算法协同过滤(CollaborativeFiltering)推荐算法是最经典、最常用的推荐算法。所谓协同过滤,基本思想是**根据用户之前的喜好以及其他兴趣相近的用户的选择

    2022年7月2日
    21
  • 硬件资料和软件资料_电脑硬件检测工具哪个好

    硬件资料和软件资料_电脑硬件检测工具哪个好一些常用的资料_硬件/系统/等标题前数字代表专题所在楼层数2. BIOS报警声意义3. BIOS自检与开机故障相关问题5. 计算机几个常见指标的意义6. 显卡GPU参数7. 显示卡常见故障全面解决8. 集成声卡常见故障及解决9. 显示器经典故障以及处理办法10. AMI主板代码大全(BIOS-ID)12. AWARD主板代码大全(BIOS-ID)16. 黑屏故障17. WindowsX

    2022年10月20日
    2

发表回复

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

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