首页 > 解决方案 > 如何反转tensorflow中的tf.image.per_image_standardization()函数?

问题描述

tf.image.per_image_standardization()在 Tensorflow 中,将每个图像转换为零均值和单位方差。所以这会导致在训练深度学习模型时梯度不爆炸。但是当我们想要显示图像数组时,我们如何在 Tensorflow 中恢复这个 z-score 归一化步骤?

标签: tensorflowdeep-learningnormalization

解决方案


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_tvariance_t行以按名称获取对相关张量的引用。(需要对该部分进行一些重写。)您可以取消注释以(无需重写)sess.run()开头的四行,以打印所有可用的创建张量以供您启发。for i in xrange(...:)

上面的代码按原样输出:

[[[0.]
[1.]
[2.]]

[[3.]
[4.]
[5.]]

[[6.]
[7.]
[8.]]]

这正是输入网络的数据。


推荐阅读