[英]Setting values of a tensor at the indices given by tf.where()
I am attempting to add noise to a tensor that holds the greyscale pixel values of an image. 我正在尝试将噪声添加到容纳图像灰度像素值的张量中。 I want to set a random number of the pixels values to 255.
我想将像素值的随机数设置为255。
I was thinking something along the lines of: 我在考虑以下方面:
random = tf.random_normal(tf.shape(input))
mask = tf.where(tf.greater(random, 1))
and am then trying to figure out how to set the pixel values of input
to 255 for every index of mask
. 然后尝试找出如何为
mask
每个索引将input
的像素值设置为255。
tf.where()
can be used for this too, assigning the noise value where mask elements are True
, else the original input
value: 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.