![](/img/trans.png)
[英]using tf.where() to select 3d tensor by 2d conditions & replacing elements in a 2d indices with keys and values
[英]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.