我需要在张量流中实现sRGB伽玛曲线。但是我无法计算张量流中的元素明智条件。
在sRGB曲线中,如果该值小于或等于0.0031308,则它是线性运算:x * 12.95 如果该值大于0.0031308,则为伽玛校正: 1.055 * x ^(1 / 2.4)-0.055
我尝试使用tf.cond(image> 0.0031308,function1,function2),但返回错误。如果有人可以帮助我,我将非常感激!!!
答案 0 :(得分:1)
也许您需要tf.where()
和tf.greater()
。例如:
import tensorflow as tf
import numpy as np
image = np.random.random_sample(size=(2,3,3,1))/100
print(image)
image_tf = tf.placeholder(shape=(None,3,3,1),dtype=tf.float32)
new_image = tf.where(tf.greater(image_tf,0.0031308)
,1.055*tf.pow(image_tf,1/2.4) - 0.055
,image_tf*12.95)
with tf.Session() as sess:
print(sess.run(new_image,feed_dict={image_tf:image}))
# image
[[[[0.0048715 ]
[0.00472688]
[0.00138391]]
[[0.00848472]
[0.00055767]
[0.00835372]]
[[0.00872362]
[0.00390934]
[0.00795842]]]
[[[0.00756143]
[0.00494474]
[0.00201968]]
[[0.00350234]
[0.0056558 ]
[0.00602147]]
[[0.00692543]
[0.0045199 ]
[0.00012196]]]]
# new image
[[[[0.05975685]
[0.05832487]
[0.01792167]]
[[0.08960549]
[0.0072218 ]
[0.08867098]]
[[0.09128823]
[0.04970375]
[0.08579818]]]
[[[0.08282798]
[0.06047264]
[0.02615486]]
[[0.04501574]
[0.06712133]
[0.07035124]]
[[0.07787357]
[0.05623017]
[0.00157937]]]]