今日目標

  • 了解 Denoising Autoencoder
  • 訓練 Denoising Autoencoder
  • 測試不同輸入情形下的 Denoising Autoencoder 表現

Github Ipython Notebook 好讀完整版

Introduction

什麼是 denoising 呢?意思就是把去除雜訊的意思,也就是說這裡的 autoencoder 有把輸入的雜訊去除的功能.例如輸入的圖像不是一個乾淨的圖像而是有許多的白點或破損 (也就是噪音),那這個網路還有辦法辨認出輸入圖像是什麼數字,就被稱為 Denoising Autoencoder

那要如何訓練 denoising autoencoder 呢? 很簡單的只要輸入一個人工加上的噪音影像,然後 loss 為 autoencoder 輸出的影像和原始影像的誤差,並最小化這個誤差,其所輸出的神經網路就可以完成去噪的功能.

以下會用一個 convolutional 的網路結構來完成一個 denoising autoencoder.並用 MNIST 的資料來訓練之.

Implementation

Build helper functions

1
2
3
4
5
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 2, 2, 1], padding = 'SAME')
def deconv2d(x, W, output_shape):
return tf.nn.conv2d_transpose(x, W, output_shape, strides = [1, 2, 2, 1], padding = 'SAME')

Build compute graph

注意到這裡建立了兩個 placeholder,一個是原始影像 x,另一個是雜訊影像 x_noise,而輸入到網路裡面的是 x_noise

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def build_graph():
x_origin = tf.reshape(x, [-1, 28, 28, 1])
x_origin_noise = tf.reshape(x_noise, [-1, 28, 28, 1])
W_e_conv1 = weight_variable([5, 5, 1, 16], "w_e_conv1")
b_e_conv1 = bias_variable([16], "b_e_conv1")
h_e_conv1 = tf.nn.relu(tf.add(conv2d(x_origin_noise, W_e_conv1), b_e_conv1))
W_e_conv2 = weight_variable([5, 5, 16, 32], "w_e_conv2")
b_e_conv2 = bias_variable([32], "b_e_conv2")
h_e_conv2 = tf.nn.relu(tf.add(conv2d(h_e_conv1, W_e_conv2), b_e_conv2))
code_layer = h_e_conv2
print("code layer shape : %s" % h_e_conv2.get_shape())
W_d_conv1 = weight_variable([5, 5, 16, 32], "w_d_conv1")
b_d_conv1 = bias_variable([1], "b_d_conv1")
output_shape_d_conv1 = tf.pack([tf.shape(x)[0], 14, 14, 16])
h_d_conv1 = tf.nn.relu(deconv2d(h_e_conv2, W_d_conv1, output_shape_d_conv1))
W_d_conv2 = weight_variable([5, 5, 1, 16], "w_d_conv2")
b_d_conv2 = bias_variable([16], "b_d_conv2")
output_shape_d_conv2 = tf.pack([tf.shape(x)[0], 28, 28, 1])
h_d_conv2 = tf.nn.relu(deconv2d(h_d_conv1, W_d_conv2, output_shape_d_conv2))
x_reconstruct = h_d_conv2
print("reconstruct layer shape : %s" % x_reconstruct.get_shape())
return x_origin, code_layer, x_reconstruct
tf.reset_default_graph()
x = tf.placeholder(tf.float32, shape = [None, 784])
x_noise = tf.placeholder(tf.float32, shape = [None, 784])
x_origin, code_layer, x_reconstruct = build_graph()

Build cost function

在 cost function 裡面計算 cost 的方式是計算輸出影像和原始影像的 mean square error.

1
2
cost = tf.reduce_mean(tf.pow(x_reconstruct - x_origin, 2))
optimizer = tf.train.AdamOptimizer(0.01).minimize(cost)

Training (Add noise with coefficient 0.3)

在訓練的過程中,輸入的噪音影像 (參數為 0.3),並觀察 mean square error 的下降情形.

在測試的時候,輸入一個原始影像,看重建輸出的影響會和原始影像的 mean square error 是多少.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
sess = tf.InteractiveSession()
batch_size = 50
init_op = tf.global_variables_initializer()
sess.run(init_op)
for epoch in range(10000):
batch = mnist.train.next_batch(batch_size)
batch_raw = batch[0]
batch_noise = batch[0] + 0.3*np.random.randn(batch_size, 784)
if epoch < 1500:
if epoch%100 == 0:
print("step %d, loss %g"%(epoch, cost.eval(feed_dict={x:batch_raw, x_noise: batch_noise})))
else:
if epoch%1000 == 0:
print("step %d, loss %g"%(epoch, cost.eval(feed_dict={x:batch_raw, x_noise: batch_noise})))
optimizer.run(feed_dict={x:batch_raw, x_noise: batch_noise})
print("final loss %g" % cost.eval(feed_dict={x: mnist.test.images, x_noise: mnist.test.images}))
step 0, loss 0.112669
step 100, loss 0.040153
step 200, loss 0.0327908
step 300, loss 0.035064
step 400, loss 0.0333917
step 500, loss 0.0303075
step 600, loss 0.0353892
step 700, loss 0.0350619
step 800, loss 0.0328716
step 900, loss 0.0291624
step 1000, loss 0.034999
step 1100, loss 0.0368471
step 1200, loss 0.0339421
step 1300, loss 0.0329562
step 1400, loss 0.0305635
step 2000, loss 0.0319757
step 3000, loss 0.0340622
step 4000, loss 0.0306117
step 5000, loss 0.0317413
step 6000, loss 0.0297122
step 7000, loss 0.0349187
step 8000, loss 0.00620675
step 9000, loss 0.00623596
final loss 0.0024923

Plot reconstructed images

使用沒有在訓練過程中的測試噪音影像,觀察經過網路去噪之後的結果.

Reconstructed images with coefficient 0.3

結果很不錯

Reconstructed images with coefficient 0.5

結果已經有點變得模糊

Reconstructed images with coefficient 0.7

已經快變認不出來了

Reconstructed images with coefficient 0.9

勉勉強強有一些紋路.

Plot code layer result

觀察中間的 code layer 的結果.

可以看到是部分的 filter 有反應,而反應的 filter 也是模模糊糊的影像,但這樣的輸出經過 decoder 卻可以很漂亮的重建回原來影像.

Trainging (Add noise with coefficient 0.8)

接下來我們想要挑戰比較困難的使用更模糊的影像來訓練神經網路看看它的結果如何.

step 0, loss 0.112311
step 100, loss 0.0289463
step 200, loss 0.0289349
step 300, loss 0.0273639
step 400, loss 0.0275356
step 500, loss 0.0253755
step 600, loss 0.0251334
step 700, loss 0.027199
step 800, loss 0.0272284
step 900, loss 0.0243694
step 1000, loss 0.0256118
step 1100, loss 0.025205
step 1200, loss 0.0246229
step 1300, loss 0.0241241
step 1400, loss 0.0257103
step 2000, loss 0.0247174
step 3000, loss 0.0235407
step 4000, loss 0.026623
step 5000, loss 0.0257211
step 6000, loss 0.0246029
step 7000, loss 0.0241382
step 8000, loss 0.0238624
step 9000, loss 0.0230421
final loss 0.0111788

Reconstructed images with coefficient 0.5

可以看到重建的結果很好.

Reconstructed images with coefficient 0.8

在係數為 0.8 的情形下,重建出來的影像結果比用 0.3 訓練出來的網路優秀,但是已經開始有些模糊.

Reconstructed images with coefficient 0.9

一些數字仍然可以辨認,但有一些變得較糊.

Reconstructed images with coefficient 1.0

只剩下少數的可以辨認.

Plot code layer result

一樣是少許的 filter 會有結果

今日心得

實作了 Denoising Autoencoder,並用不同強度的雜訊來測試,其效果就視覺上看起來還算不錯.

而如果加強了雜訊的強度,期訓練出來的 autoencoder 抗噪的能力也會更好!

問題

  • 用普通的 autoencoder 來實作,其結果和 convolutional autoencoder 的比較.
  • 如果不是輸入噪音影像,而是輸入三分之一,或是四分之一的影像,不知道可不可以還原回來?

學習資源連結

Github Denoising Autoencoder example

原文地址: https://blog.c1mone.com.tw/2017/01/03/tensorflow-note-day-19/

Tensorflow Day19 Denoising Autoencoder相关推荐

  1. TensorFlow 实现深度神经网络 —— Denoising Autoencoder

    完整代码请见 models/DenoisingAutoencoder.py at master · tensorflow/models · GitHub: 1. Denoising Autoencod ...

  2. TensorFlow实现去噪自编码器(Denoising Autoencoder)

    TensorFlow实现去噪自编码器(Denoising Autoencoder) 去噪自编码器(Denoising Autoencoder, DAE) DAE模型架构 DAE实现 数据预处理 模型构 ...

  3. 论文阅读笔记-Gated relational stacked denoising autoencoder with localized author embedding for

    论文题目:Gated relational stacked denoising autoencoder with localized author  embedding for global cita ...

  4. Tensorflow Day17 Sparse Autoencoder

    今日目標 了解 Sparse Autoencoder 了解 KL divergence & L2 loss 實作 Sparse Autoencoder Github Ipython Noteb ...

  5. Tensorflow Day18 Convolutional Autoencoder

    今日目標 了解 Convolutional Autoencoder 實作 Deconvolutional layer 實作 Max Unpooling layer 觀察 code layer 以及 d ...

  6. Denoising Auto-Encoder

    在神经网络模型训练开始前,通过Auto-encoder对模型进行预训练,可以确定编码器的初始参数值.然而,受模型复杂度.训练集数据量以及数据噪声等问题的影响,通过Auto-encoder得到的初始模型 ...

  7. 深度学习教程 TensorFlow and Deep Learning Tutorials

    Google's Deep Learning Tutorials TensorFlow Official Deep Learning Tutorial [中文]. MLP with Dropout T ...

  8. Tensorflow基于minist数据集实现自编码器

    Tensorflow实现自编码器 自编码器 Denoising AutoEncoder(去噪自编码器) 自编码器 特征的稀疏表达:使用少量的基本特征组合拼装得到更高层抽象的特征. 如:图像碎片可由少量 ...

  9. Win10:tensorflow学习笔记(4)

    前言 学以致用,以学促用.输出检验,完整闭环. 经过前段时间的努力,已经在电脑上搭好了深度学习系统,接下来就要开始跑程序了,将AI落地了. 安装win10下tensforlow 可以参照之前的例子:w ...

最新文章

  1. 软件工程--第三周学习进度
  2. python中的函数def和函数的参数
  3. 1021. Deepest Root (25)
  4. GYM 101669F - Binary Transformations
  5. opencv:灰色和彩色图像的像素直方图及直方图均值化的实现与展示
  6. 60秒语音有救了?曝微信测试语音进度条,内部人士一句话打回原形
  7. Spring @Bean @Scope @Qualifier
  8. mysql 独占查询,MySql历史与架构
  9. 请求转发与重定向的区别
  10. 错误与异常_2-7选择题
  11. shell编程四剑客之 grep
  12. html js点赞功能实现,利用浏览器的JS代码实现QQ空间自动点赞功能
  13. 腾讯云docker部署halo个人博客及myql数据库笔记
  14. NPOI 连续插入多张图片的办法,及部分样式的代码
  15. git使用报错:fatal: Couldn‘t find remote ref master的解决方法
  16. ACM:搜索算法专题(1)——24点
  17. 遇上Android客户端打包党,该怎么办?
  18. Failed to compile编译失败
  19. syl——linux学习(一)
  20. 手机linux系统介绍,Ubuntu手机系统介绍及Ubuntu刷机教程分享

热门文章

  1. ZOJ 2675 Little Mammoth(计算几何)
  2. DDoS攻防战(三):ip黑白名单防火墙frdev的原理与实现
  3. (二)在.net中如何使用Memcached
  4. C++找出一个二维数组中的鞍点,即该位置上的元素在该行上最大,在该列上最小(也可能没有鞍点)
  5. DCT C语言 修改了c程序以及matlab的程序
  6. win10: Coursera 视频无法观看问题解决。
  7. CSDN:Markdown extra grammar
  8. copyof java_死磕 java集合之CopyOnWriteArrayList源码分析
  9. 【Python】find()函数居然还能指定搜索的起点和终点?
  10. 【Leetcode】大神总结的所有TopK问题模板(基于快速排序)