繁体   English   中英

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

[英]Setting values of a tensor at the indices given by tf.where()

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

我在考虑以下方面:

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

然后尝试找出如何为mask每个索引将input的像素值设置为255。

tf.where()也可以用于此,在mask元素为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.]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM