В настоящее время функция преобразования HSV в RGB не имеет регистрированного градиента, Вы можете рассмотреть это как новую проблему. Однако, глядя на реализацию ядра, вполне возможно воспроизвести вычисления, используя базовые операции TensorFlow с определенными градиентами. Ниже приведен пример кода, который вы можете использовать с TF 2.0. Он опирается на API совместимости, доступный как tensorflow.compat.v1, и требует отключения поведения v2. tf.compat позволяет писать код, который работает как в TensorFlow 1.x, так и в 2.x.
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution() # would work as well
(.env) boris@boris-All-Series:~/HSVRGB$ cat ChangeRGB1.py
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
import numpy as np
def newconv_hsv_to_rgb(tensor):
h = tensor[..., 0]
print("h = ",h)
s = tensor[..., 1]
print("s = ",s)
v = tensor[..., 2]
print("v = ",v)
c = s * v;
m = v - c;
dh = h * 6
h_category = tf.cast(dh, tf.int32)
fmodu = tf.mod(dh, 2)
x = c * (1 - tf.abs(fmodu - 1))
component_shape = tf.shape(tensor)[:-1]
dtype = tensor.dtype
rr = tf.zeros(component_shape, dtype=dtype)
gg = tf.zeros(component_shape, dtype=dtype)
bb = tf.zeros(component_shape, dtype=dtype)
h0 = tf.equal(h_category, 0)
rr = tf.where(h0, c, rr)
gg = tf.where(h0, x, gg)
h1 = tf.equal(h_category, 1)
rr = tf.where(h1, x, rr)
gg = tf.where(h1, c, gg)
h2 = tf.equal(h_category, 2)
gg = tf.where(h2, c, gg)
bb = tf.where(h2, x, bb)
h3 = tf.equal(h_category, 3)
gg = tf.where(h3, x, gg)
bb = tf.where(h3, c, bb)
h4 = tf.equal(h_category, 4)
rr = tf.where(h4, x, rr)
bb = tf.where(h4, c, bb)
h5 = tf.equal(h_category, 5)
rr = tf.where(h5, c, rr)
bb = tf.where(h5, x, bb)
r = rr + m
g = gg + m
b = bb + m
return tf.stack([r, g, b], axis=-1)
img = tf.placeholder(tf.float32, (None, None, 3))
# Compute builtin conversion to check that our conversion is correct
tf_conversion = tf.image.hsv_to_rgb(img)
new_conversion = newconv_hsv_to_rgb(img)
# Difference between the builtin conversion and ours
error = tf.losses.mean_squared_error(tf_conversion, new_conversion)
# Take gradients of the conversion
new_conversion_grad = tf.gradients(new_conversion, img)[0]
# Test it
with tf.Session() as sess:
np.random.seed(100)
random_img = np.random.rand(10, 10, 3)
error_val, grad_val = sess.run([error, new_conversion_grad],
feed_dict={img: random_img})
print("ErrorValue : ",error_val)
print("GradientValue : ","\n",grad_val)
(.env) boris@boris-All-Series:~/HSVRGB$ python3 ChangeRGB1.py
h = Tensor("strided_slice:0", shape=(None, None), dtype=float32)
s = Tensor("strided_slice_1:0", shape=(None, None), dtype=float32)
v = Tensor("strided_slice_2:0", shape=(None, None), dtype=float32)
ErrorValue : 1.914486e-16
GradientValue :
[[[-7.0903623e-01 -5.3507453e-01 2.6491349e+00]
[-3.4420034e-03 -1.2991568e-01 2.9949572e+00]
[ 6.7739707e-01 -2.7006465e-01 1.3685231e+00]
[-1.1187987e+00 -3.0346024e-01 1.7070839e+00]
[-1.4286079e-01 -2.4429685e-01 2.8794882e+00]
[-8.3736974e-01 -3.2182935e-01 1.4807379e+00]
[ 7.0991272e-01 -4.7601932e-01 2.6977921e+00]
[-1.6489303e+00 -5.5128366e-01 1.6589090e+00]
[-1.2725148e-02 -5.9869420e-03 2.6076081e+00]
[-7.2826855e-02 -2.3104541e-02 1.7949229e+00]]
[[-3.8093188e-01 -1.6750662e-01 2.0381029e+00]
[ 1.9487220e-01 -1.5211252e+00 2.9376864e+00]
[-3.2028657e-01 -1.6791469e+00 2.8869779e+00]
[-2.8073926e+00 -9.2095655e-01 1.9149334e+00]
[-2.5756566e-02 -3.1316078e-01 2.9695241e+00]
[-1.1568810e+00 -3.1790894e-01 2.0246780e+00]
[-4.9865446e+00 -1.6725011e+00 1.5378234e+00]
[-1.2917670e+00 -7.8397804e-01 2.5293612e+00]
[ 3.6348844e-01 -3.3322984e-01 2.3634272e+00]
[-1.3604884e-01 -7.2082871e-01 2.9360189e+00]]
[[ 2.2405977e+00 -1.0976645e+00 1.9670453e+00]
[ 5.3026118e+00 -1.0830353e+00 1.9313138e+00]
[-8.4499228e-01 -5.8610553e-01 2.3742306e+00]
[-4.0939510e-01 -3.0227065e-01 2.6606662e+00]
[-5.5503459e+00 -9.9687028e-01 1.9931329e+00]
[-1.4935439e+00 -5.4047805e-01 1.8388097e+00]
[ 1.4146855e+00 -7.3642629e-01 2.3290095e+00]
[ 3.1433666e+00 -1.4577173e+00 2.2241900e+00]
[ 1.6218381e+00 -1.3127109e+00 2.4530501e+00]
[ 1.1909736e+00 -9.3451047e-01 2.5385695e+00]]
[[-1.9728693e-01 -1.3764592e-01 2.6312554e+00]
[-1.8067312e+00 -1.2360674e+00 2.1427794e+00]
[-7.6979697e-01 -3.0529758e-01 2.0219712e+00]
[-3.6405182e+00 -1.5169027e+00 1.4856447e+00]
[-1.2936020e+00 -1.1596136e+00 2.4864511e+00]
[-3.6859379e+00 -1.1373434e+00 2.2760563e+00]
[-2.3181956e-02 -4.3088183e-02 2.6901155e+00]
[ 5.2836018e+00 -9.8743206e-01 2.0436888e+00]
[-1.1883819e+00 -6.9543797e-01 2.5436218e+00]
[-1.9239188e+00 -1.1354291e+00 2.2487655e+00]]
[[-2.2597308e+00 -5.2707899e-01 2.2823999e+00]
[ 1.1655506e+00 -9.8020262e-01 2.2139125e+00]
[ 1.0642009e+00 -7.9233128e-01 2.4392457e+00]
[ 1.4843433e-01 -3.6354873e-01 2.8798203e+00]
[-6.3705049e-03 -6.6432320e-02 2.9559131e+00]
[-3.4622440e+00 -1.6833705e+00 2.0106893e+00]
[-6.5574551e-01 -2.1523841e-01 1.0576079e+00]
[-5.4436964e-01 -3.4402964e-01 1.9588964e+00]
[-1.4496111e+00 -1.6559914e+00 2.5990536e+00]
[-4.2540383e-01 -5.7878375e-01 2.7257848e+00]]
[[-1.4656628e+00 -6.7677039e-01 2.5325260e+00]
[ 9.0441978e-01 -5.2645230e-01 2.1720490e+00]
[-9.5323575e-01 -5.2680188e-01 2.3530533e+00]
[-5.6373185e-01 -8.7002283e-01 2.5985782e+00]
[ 3.9345045e+00 -7.6505721e-01 2.0569205e+00]
[ 7.7431995e-01 -4.7417539e-01 2.4820962e+00]
[-4.8437718e-01 -2.1915148e-01 1.6275465e+00]
[ 4.3707579e-01 -1.9197546e-01 2.4519196e+00]
[-2.6141686e+00 -9.2060727e-01 1.5175444e+00]
[-7.9256952e-01 -1.2822233e+00 2.8233058e+00]]
[[ 1.9977541e+00 -6.8054849e-01 1.4883873e+00]
[-1.5395225e+00 -1.2896314e+00 2.5493445e+00]
[ 3.2065277e+00 -9.3633401e-01 2.2292085e+00]
[-3.4984970e-01 -1.1122450e+00 2.8792169e+00]
[-2.8070419e+00 -8.6739111e-01 1.3133061e+00]
[-3.9687700e+00 -1.6181577e+00 1.6856283e+00]
[ 1.0157360e+00 -4.2815140e-01 2.5833552e+00]
[ 2.1874046e+00 -1.5673059e+00 2.2240362e+00]
[-1.2709652e-01 -3.2637388e-01 2.9240730e+00]
[-1.0525483e-01 -9.2458630e-01 2.9331017e+00]]
[[-1.4314592e+00 -5.0455922e-01 1.6104649e+00]
[-1.5325723e+00 -9.3444854e-01 2.3198595e+00]
[-1.6383508e+00 -6.7153680e-01 2.2451391e+00]
[-8.5690892e-01 -1.0388050e+00 2.4954906e+00]
[ 3.0373459e+00 -1.1997150e+00 2.0424688e+00]
[-2.0057840e+00 -8.4400749e-01 1.8166372e+00]
[ 1.2258511e+00 -2.2984383e-01 1.9487077e+00]
[ 1.6870941e-01 -4.3473303e-02 1.4857016e+00]
[ 4.8462566e-02 -5.7503313e-01 2.9548914e+00]
[ 4.0697552e-02 -1.7366436e-01 2.9050908e+00]]
[[-1.6510398e+00 -4.4523790e-01 2.3625135e+00]
[ 2.7343893e-01 -1.9282317e-01 2.7567122e+00]
[ 2.8790088e+00 -9.6638530e-01 1.5163493e+00]
[ 2.0303345e+00 -9.9175930e-01 1.9268475e+00]
[-4.6885853e+00 -9.8755383e-01 1.9309087e+00]
[-2.2911053e+00 -6.6435641e-01 1.6601489e+00]
[ 4.7248042e-01 -3.0973339e-01 2.3302701e+00]
[ 1.2291396e+00 -1.5016364e+00 2.5755520e+00]
[ 1.2174644e+00 -4.0773886e-01 2.4456985e+00]
[-6.6693759e-01 -1.2490079e+00 2.7874868e+00]]
[[-1.1197950e+00 -4.2959565e-01 1.6003017e+00]
[-3.0877006e-01 -7.6644197e-02 2.0273833e+00]
[-4.8644429e-01 -1.9074525e-01 2.1818881e+00]
[ 1.7011230e+00 -5.5131721e-01 1.0857112e+00]
[ 9.2831716e-02 -4.7703058e-01 2.9332557e+00]
[-2.2985213e+00 -1.0034721e+00 1.9387246e+00]
[-1.9428633e-02 -2.7966501e-02 2.7149253e+00]
[-1.5525330e+00 -5.6545180e-01 2.4048400e+00]
[ 1.0981328e+00 -2.5491965e-01 1.9712092e+00]
[ 1.3841882e-01 -7.5312279e-02 2.4196911e+00]]]
No comments:
Post a Comment