TensorFlow per_image_standardization

实验环境:windows 10,Python 3.5.6,tensorflow 1.11.0

函数介绍:

标准化处理可以使得不同的特征具有相同的尺度(Scale)。这样,在使用梯度下降法学习参数的时候,不同特征对参数的影响程度就一样了。

 def per_image_standardization(image):
 
  This op computes `(x - mean) / adjusted_stddev`, where `mean` is the average
  of all values in image, and
  `adjusted_stddev = max(stddev, 1.0/sqrt(image.NumElements()))`.

  `stddev` is the standard deviation of all values in `image`. It is capped
  away from zero to protect against division by 0 when handling uniform images.

  Args:
    image: 3-D tensor of shape `[height, width, channels]`.

  Returns:
    The standardized image with same shape as `image`.

此函数的运算过程是将整幅图片标准化(不是归一化),加速神经网络的训练。主要有如下操作:
x − m e a n a d j u s t e d _ s t d d e v \frac{x - mean}{adjusted\_stddev} adjusted_stddevxmean
a d j u s t e d _ s t d d e v = max ⁡ ( s t d d e v , 1.0 i m a g e . N u m E l e m e n t s ( ) ) adjusted\_stddev = \max \left(stddev, \frac{1.0}{\sqrt{image.NumElements()}}\right) adjusted_stddev=max(stddev,image.NumElements() 1.0)

  • 其中x为图片的RGB三通道像素值,
  • mean分别为三通道像素的均值,
  • adjusted_stddev = max(stddev,1.0/sqrt(image.NumElements()))。stddev为三通道像素的标准差,image.NumElements()计算的是三通道各自的像素个数。

标准化和归一化有什么区别呢?
简而言之就是归一化是对样本xi里面的每个维度的特征分别除于不同的数,会改变数据分布,而标准化则是对样本xi的全部维度的特征做相同的标准化处理,即(xi - mean_i) / adjusted_stddev_i,不会改变数据分布。这里分布的意思就是看数据之间的离散程度。
详见http://blog.csdn.net/pipisorry/article/details/52247379

实验代码:

import tensorflow as tf
import matplotlib.image as img
import matplotlib.pyplot as plt
import numpy as np

sess = tf.InteractiveSession()
image = img.imread('./test_img.jpg')
shape = tf.shape(image).eval()
h,w = shape[0],shape[1]
standardization_image = tf.image.per_image_standardization(image)#标准化

# plt.ion() # 打开交互模式。使用plt.ion()这个函数,使matplotlib的显示模式转换为交互(interactive)模式。即使在脚本中遇到plt.show(),代码还是会继续执行。

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.ioff() # 显示前关掉交互模式。在plt.show()之前一定不要忘了加plt.ioff(),如果不加,界面会一闪而过,并不会停留。
plt.show()

实验结果:

两幅hist图分别是原图和标准化后的RGB的像素值分布图,可以看到只是将图片的像素值大小限定到一个范围,但是像素值的分布未改变。
在这里插入图片描述
在这里插入图片描述

reference:https://blog.csdn.net/sinat_21585785/article/details/74251563#commentBox

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值