【问题标题】:Tensorflow, Keras: Tensor normalization by axisTensorflow,Keras:按轴进行张量归一化
【发布时间】:2018-08-06 11:36:36
【问题描述】:

假设我们有图像张量A,形状为(None, 200, 200, 1)。其中None 是批量大小,(200, 200, 1) 是图像大小。

如何对每张图片进行归一化(0到1)(不使用for迭代)?

即:

A[0] = (A[0] - A[0].min()) / (A[0].max() - A[0].min())
A[1] = (A[1] - A[1].min()) / (A[1].max() - A[1].min())
...
A[n] = (A[n] - A[n].min()) / (A[n].max() - A[n].min())

如果我直接使用A = (A - A.min()) / (A.max() - A.min()),它将通过全局maxmin 标准化所有图像。我希望用自己的maxmin 标准化每个图像。

换句话说,如何实现maxmin 操作,导致形状为:(None, 1, 1, 1),其中每个(1, 1, 1) 包含每个图像的最大值或最小值。

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    您可以使用tf.reduce_maxtf.reduce_min

    import tensorflow as tf
    
    A = tf.random_normal(shape=(-1, 200, 200, 1))
    B = tf.reduce_max(A, axis=(1, 2, 3))
    C = tf.reduce_min(A, axis=(1, 2, 3))
    
    print(B.shape)
    print(C.shape)
    

    输出:

    (?,)
    (?,)
    

    此外,在您的情况下,输出的形状必须为 (None, 1, 1, 1),而不是 (None, 1, 1),因为您已包含最后一个通道维度。

    B = tf.reshape(B, (-1, 1, 1, 1))
    C = tf.reshape(C, (-1, 1, 1, 1))
    
    print(B.shape)
    print(C.shape)
    

    以上代码给出以下输出:

    (?, 1, 1, 1)
    (?, 1, 1, 1)
    

    最后,你为什么要从每个像素中减去max,你的意思是要为normalizing each image between 0-1 减去min

    D = (A - C) / (B - C)
    print(D.shape)
    

    给予

    (?, 200, 200, 1)
    

    正如预期的那样。

    【讨论】:

      猜你喜欢
      • 2018-01-27
      • 1970-01-01
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 2017-08-16
      相关资源
      最近更新 更多