如何在tensorflow中反转tf.image.per_image_standarization()函数?
Tensorflow中的如何在tensorflow中反转tf.image.per_image_standarization()函数?,tensorflow,deep-learning,normalization,Tensorflow,Deep Learning,Normalization,Tensorflow中的tf.image.per\u image\u standarization()以零均值和单位方差转换每个图像。因此,在训练深度学习模型时,这会导致非爆炸性梯度。但是,当我们想要显示图像阵列时,如何在Tensorflow中还原此z分数标准化步骤?通过“显示图像阵列”,我想您的意思是在tensorboard中显示它。如果是这种情况,那么您不需要做任何事情,tensorboard可以处理已经标准化的图像。如果您希望原始值用于任何其他目的,为什么不在标准化变量之前使用该变量,例如
tf.image.per\u image\u standarization()
以零均值和单位方差转换每个图像。因此,在训练深度学习模型时,这会导致非爆炸性梯度。但是,当我们想要显示图像阵列时,如何在Tensorflow中还原此z分数标准化步骤?通过“显示图像阵列”,我想您的意思是在tensorboard中显示它。如果是这种情况,那么您不需要做任何事情,tensorboard可以处理已经标准化的图像。如果您希望原始值用于任何其他目的,为什么不在标准化变量之前使用该变量,例如:
img = tf.placeholder(...)
img_std = tf.image.per_image_standardization(img)
您可以使用img
或img\u std
以任何您认为合适的方式进行操作
如果你有一个用例去规范化上面没有提到的标准化图像,那么你需要自己计算平均值和标准偏差,然后乘以标准偏差并加上平均值。请注意,tf.image.per_image_standarization
使用文档中定义的调整后的stddev
:
adjusted_stddev = max(stddev, 1.0/sqrt(image.NumElements()))
该层将创建一些内部变量,可用于恢复原始数据。请注意,这是未记录的行为,不能保证保持不变。不过,现在,您可以使用下面的代码(已测试)来参考如何获取相关张量和恢复原始数据:
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\u t
和variance\u t
行,以按名称获取对相关张量的引用。(需要重写sess.run()
部分。)您可以在xrange(…(无需重写)中为i取消注释以开头的四行,以打印所有可用创建的张量供您参考。:)
上述代码按原样输出:
[[0.]
[1.]
[2.]]
[[3.]
[4.]
[5.]
[[6.]
[7.]
[8.]]
这正是传送到网络的数据。Hi。是关于规范化表,而不是规范化碰巧在表中的数据。