Tensorflow Day19 Denoising Autoencoder
今日目標
- 了解 Denoising Autoencoder
- 訓練 Denoising Autoencoder
- 測試不同輸入情形下的 Denoising Autoencoder 表現(xiàn)
Github Ipython Notebook 好讀完整版
Introduction
什麼是 denoising 呢?意思就是把去除雜訊的意思,也就是說這裡的 autoencoder 有把輸入的雜訊去除的功能.例如輸入的圖像不是一個乾淨的圖像而是有許多的白點或破損 (也就是噪音),那這個網(wǎng)路還有辦法辨認出輸入圖像是什麼數(shù)字,就被稱為?Denoising Autoencoder.
那要如何訓練 denoising autoencoder 呢? 很簡單的只要輸入一個人工加上的噪音影像,然後 loss 為 autoencoder 輸出的影像和原始影像的誤差,並最小化這個誤差,其所輸出的神經(jīng)網(wǎng)路就可以完成去噪的功能.
以下會用一個 convolutional 的網(wǎng)路結(jié)構(gòu)來完成一個 denoising autoencoder.並用 MNIST 的資料來訓練之.
Implementation
Build helper functions
| 12345 | 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,而輸入到網(wǎng)路裡面的是?x_noise.
| 12345678910111213141516171819202122232425262728293031323334 | 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_conv2print("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_conv2print("reconstruct layer shape : %s" % x_reconstruct.get_shape()) return x_origin, code_layer, x_reconstructtf.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.
| 12 | 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)
在訓練的過程中,輸入的噪音影像 (參數(shù)為?0.3),並觀察 mean square error 的下降情形.
在測試的時候,輸入一個原始影像,看重建輸出的影響會和原始影像的 mean square error 是多少.
| 1234567891011121314151617181920 | sess = tf.InteractiveSession()batch_size = 50init_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})) |
Plot reconstructed images
使用沒有在訓練過程中的測試噪音影像,觀察經(jīng)過網(wǎng)路去噪之後的結(jié)果.
Reconstructed images with coefficient 0.3
結(jié)果很不錯
Reconstructed images with coefficient 0.5
結(jié)果已經(jīng)有點變得模糊
Reconstructed images with coefficient 0.7
已經(jīng)快變認不出來了
Reconstructed images with coefficient 0.9
勉勉強強有一些紋路.
Plot code layer result
觀察中間的 code layer 的結(jié)果.
可以看到是部分的 filter 有反應,而反應的 filter 也是模模糊糊的影像,但這樣的輸出經(jīng)過 decoder 卻可以很漂亮的重建回原來影像.
Trainging (Add noise with coefficient 0.8)
接下來我們想要挑戰(zhàn)比較困難的使用更模糊的影像來訓練神經(jīng)網(wǎng)路看看它的結(jié)果如何.
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.0111788Reconstructed images with coefficient 0.5
可以看到重建的結(jié)果很好.
Reconstructed images with coefficient 0.8
在係數(shù)為 0.8 的情形下,重建出來的影像結(jié)果比用 0.3 訓練出來的網(wǎng)路優(yōu)秀,但是已經(jīng)開始有些模糊.
Reconstructed images with coefficient 0.9
一些數(shù)字仍然可以辨認,但有一些變得較糊.
Reconstructed images with coefficient 1.0
只剩下少數(shù)的可以辨認.
Plot code layer result
一樣是少許的 filter 會有結(jié)果
今日心得
實作了?Denoising Autoencoder,並用不同強度的雜訊來測試,其效果就視覺上看起來還算不錯.
而如果加強了雜訊的強度,期訓練出來的 autoencoder 抗噪的能力也會更好!
問題
- 用普通的 autoencoder 來實作,其結(jié)果和 convolutional autoencoder 的比較.
- 如果不是輸入噪音影像,而是輸入三分之一,或是四分之一的影像,不知道可不可以還原回來?
學習資源連結(jié)
Github Denoising Autoencoder example
原文地址:?https://blog.c1mone.com.tw/2017/01/03/tensorflow-note-day-19/
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Tensorflow Day19 Denoising Autoencoder的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Tensorflow Day16 Aut
- 下一篇: Tensorflow Day18 Con