實驗環境:windows 7,anaconda 3(Python 3.5),tensorflow(gpu/cpu)
函數介紹:標準化處理可以使得不同的特徵具有相同的尺度(Scale)。這樣,在使用梯度下降法學習參數的時候,不同特徵對參數的影響程度就一樣了。tf.image.per_image_standardization(image),此函數的運算過程是將整幅圖片標準化(不是歸一化),加速神經網絡的訓練。主要有如下操作,(x - mean) / adjusted_stddev,其中x爲圖片的RGB三通道像素值,mean分別爲三通道像素的均值,adjusted_stddev = max(stddev, 1.0/sqrt(image.NumElements()))。stddev爲三通道像素的標準差,image.NumElements()計算的是三通道各自的像素個數。
實驗代碼:
import tensorflow as tf
import matplotlib.image as img
import matplotlib.pyplot as plt
import numpy as np
sess = tf.InteractiveSession()
image = img.imread('D:/Documents/Pictures/logo7.jpg')
shape = tf.shape(image).eval()
h,w = shape[0],shape[1]
standardization_image = tf.image.per_image_standardization(image)#標準化
fig = plt.figure()
fig1 = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('orginal image')
ax.imshow(image)
ax1 = fig1.add_subplot(311)
ax1.set_title('original hist')
ax1.hist(sess.run(tf.reshape(image,[h*w,-1])))
ax1 = fig1.add_subplot(313)
ax1.set_title('standardization hist')
ax1.hist(sess.run(tf.reshape(standardization_image,[h*w,-1])))
plt.ion()
plt.show()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
實驗結果:
兩幅hist圖分別是原圖和標準化後的RGB的像素值分佈圖,可以看到只是將圖片的像素值大小限定到一個範圍,但是像素值的分佈爲改變。