tensorflow - 如何反转tensorflow中的tf.image.per_image_standardization()函数?
问题描述
tf.image.per_image_standardization()
在 Tensorflow 中,将每个图像转换为零均值和单位方差。所以这会导致在训练深度学习模型时梯度不爆炸。但是当我们想要显示图像数组时,我们如何在 Tensorflow 中恢复这个 z-score 归一化步骤?
解决方案
该tf.image.per_image_standardization()
层将创建一些可用于恢复原始数据的内部变量。请注意,这是未记录的行为,不能保证保持不变。不过,目前,您可以使用下面的代码(经过测试)作为参考如何获取相关张量并恢复原始数据:
import tensorflow as tf
import numpy as np
img_size = 3
a = tf.placeholder( shape = ( img_size, img_size, 1 ), dtype = tf.float32 )
b = tf.image.per_image_standardization( a )
with tf.Session() as sess:
tensors, tensor_names = [], []
for l in sess.graph.get_operations():
tensors.append( sess.graph.get_tensor_by_name( l.name + ":0" ) )
tensor_names.append( l.name )
#mean_t = sess.graph.get_tensor_by_name( "per_image_standardization/Mean:0" )
#variance_t = sess.graph.get_tensor_by_name( "per_image_standardization/Sqrt:0" )
foobar = np.reshape( np.array( range( img_size * img_size ), dtype = np.float32 ), ( img_size, img_size, 1 ) )
res = sess.run( tensors, feed_dict = { a : foobar } )
#for i in xrange( len( res ) ):
# print( i, tensor_names[ i ] + ":" )
# print( res[ i ] )
# print()
mean = res[ 6 ] # "per_image_standardization/Mean:0"
variance = res[ 13 ] # "per_image_standardization/Sqrt:0"
standardized = res[ 18 ] # "per_image_standardization:0"
original = standardized * variance + mean
print( original )
您可以取消注释mean_t
和variance_t
行以按名称获取对相关张量的引用。(需要对该部分进行一些重写。)您可以取消注释以(无需重写)sess.run()
开头的四行,以打印所有可用的创建张量以供您启发。for i in xrange(...
:)
上面的代码按原样输出:
[[[0.]
[1.]
[2.]][[3.]
[4.]
[5.]][[6.]
[7.]
[8.]]]
这正是输入网络的数据。
推荐阅读
- java - 使用 Java 访问 Windows 中的环境变量失败
- javascript - JavaScript 中的 do/while 混淆
- javascript - Next.js API 将 FormData 转发到外部 API
- swift - Swift中大括号后跟类名的语法含义
- sql - 有没有办法在不使用子查询的情况下根据不同的行计算平均值?
- algorithm - 如何通过替换循环来最小化图的顶点?
- python - 为什么不和谐机器人不说话?
- python - 无法使用 Python 中的发布请求获取下一页数据
- python - 无法从 python 扫描仪的列表中删除新行
- r - 通过平均所有点将多个栅格合并为一个最终栅格