代码之家  ›  专栏  ›  技术社区  ›  KOB

在tf.where()给出的索引处设置张量的值

  •  0
  • KOB  · 技术社区  · 6 年前

    我正在尝试将噪波添加到保存图像灰度像素值的张量中。我想将像素值的随机数设置为255。

    我在想:

    random = tf.random_normal(tf.shape(input))
    mask = tf.where(tf.greater(random, 1))
    

    然后我试图找出如何设置 input 到255 mask .

    1 回复  |  直到 6 年前
        1
  •  0
  •   benjaminplanche    6 年前

    tf.where() 也可以用于此目的,在遮罩元素所在的位置指定噪波值 True ,否则为原件 input 价值观:

    import tensorflow as tf
    
    input = tf.zeros((4, 4))
    noise_value = 255.
    random = tf.random_normal(tf.shape(input))
    mask = tf.greater(random, 1.)
    input_noisy = tf.where(mask, tf.ones_like(input) * noise_value, input)
    
    with tf.Session() as sess:
        print(sess.run(input_noisy))
        # [[   0.  255.    0.    0.]
        #  [   0.    0.    0.    0.]
        #  [   0.    0.    0.  255.]
        #  [   0.  255.  255.  255.]]