Click here to Skip to main content
15,885,829 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using tf1.14 to make a loss function, which project the predicted pose on a 2D image. And then I use the projected point to get the distance transform as loss, I have finished the loss function, but when start training the optimizer gives a error massage. I posted my code here and error, please give some advise...

def transform_pt(v, q, t, cam, Ds_gt):#placeholder_get_pointsds # v: 3d point # q: quarternion (4d vector) = w + xi+ yj + zk
q = tf.convert_to_tensor(q)
t = tf.convert_to_tensor(t)

loss = None
Ds = None
w, x, y, z = tf.split(tf.gather(q, 0), num_or_size_splits=4, axis=-1)
t_0, t_1, t_2 = tf.split(tf.gather(t, 0), num_or_size_splits=3, axis=-1)


for i in range(100):
    # v_0, v_1, v_2 = tf.split(v[i], num_or_size_splits=3, axis=-1)
    # w, x, y, z = tf.split(q[i], num_or_size_splits=4, axis=-1)
    # t_0, t_1, t_2 = tf.split(t[i], num_or_size_splits=3, axis=-1)

    v_0, v_1, v_2 = tf.split(tf.gather(v, i), num_or_size_splits=3, axis=-1)
    # w, x, y, z = tf.split(tf.gather(q, 0), num_or_size_splits=4, axis=-1)
    # t_0, t_1, t_2 = tf.split(tf.gather(t, 0), num_or_size_splits=3, axis=-1)

    a1 = tf.multiply(tf.multiply(w, w), v_0)
    a2 = tf.multiply(tf.multiply(tf.multiply(2.0,y),w),v_2)

    a3 = tf.multiply(tf.multiply(tf.multiply(2.0,z),w),v_1)
    a4 = tf.multiply(tf.multiply(x,x),v_0)
    a5 = tf.multiply(tf.multiply(tf.multiply(2.0,y),x),v_1)
    a6 = tf.multiply(tf.multiply(tf.multiply(2.0,z),x),v_2)
    a7 = tf.multiply(tf.multiply(z,z),v_0)
    a8 = tf.multiply(tf.multiply(y,y),v_0)
    a = tf.subtract(tf.subtract(tf.add(tf.add(tf.add(tf.subtract(tf.add(a1,a2),a3),a4),a5),a6),a7),a8)
    #a = w*w*v[0] + 2*y*w*v[2] - 2*z*w*v[1] + x*x*v[0] + 2*y*x*v[1] + 2*z*x*v[2] - z*z*v[0] - y*y*v[0]


    b1 = tf.multiply(tf.multiply(tf.multiply(2.0,x),y),v_0)
    b2 = tf.multiply(tf.multiply(y,y),v_1)
    b3 = tf.multiply(tf.multiply(tf.multiply(2.0,x),y),v_2)
    b4 = tf.multiply(tf.multiply(tf.multiply(2.0, w), z), v_0)
    b5 = tf.multiply(tf.multiply(z,z),v_1)
    b6 = tf.multiply(tf.multiply(w,w),v_1)
    b7 = tf.multiply(tf.multiply(tf.multiply(2.0, x), w), v_2)
    b8 = tf.multiply(tf.multiply(x,x),v_1)
    #b = tf.add(b1,b2)
    b = tf.subtract(tf.subtract(tf.add(tf.subtract(tf.add(tf.add(tf.add(b1,b2),b3),b4),b5),b6),b7),b8)

    c1 = tf.multiply(tf.multiply(tf.multiply(2.0,x),z),v_0)
    c2 = tf.multiply(tf.multiply(tf.multiply(2.0,y),z),v_1)
    c3 = tf.multiply(tf.multiply(z, z), v_2)
    c4 = tf.multiply(tf.multiply(tf.multiply(2.0,w),y),v_0)
    c5 = tf.multiply(tf.multiply(y,y),v_2)
    c6 = tf.multiply(tf.multiply(tf.multiply(2.0,w),x),v_1)
    c7 = tf.multiply(tf.multiply(x,x),v_2)
    c8 = tf.multiply(tf.multiply(w,w),v_2)
    c = tf.add(tf.subtract(tf.add(tf.subtract(tf.subtract(tf.add(tf.add(c1,c2),c3),c4),c5),c6),c7),c8)

    at = tf.add(a,t_0)
    bt = tf.add(b,t_1)
    ct = tf.add(c,t_2)

    k = tf.concat([at, bt, ct], axis=-1)
    l = tf.expand_dims(k,0)
    


    #res = project2(l, cam)
    res = proj(l, cam)
    out = tf.gather(res, 0)
    k = tf.gather(out, 0)
    m = tf.gather(out, 1)

    k= tf.cast(k, dtype=tf.int32)
    m = tf.cast(m, dtype = tf.int32)
    res_ds = tf.gather(Ds_gt, k)
    res_ds = tf.gather(res_ds, m)
    #res = tf.reduce_sum(tf.gather(res, tf.cast(y, dtype=tf.int32)))

    if Ds is None:
        Ds = res_ds
        #print("guo")
    else:
        Ds += res_ds
Ds = tf.reduce_sum(Ds)
print("Ds_shape",tf.shape(Ds))
try:
    if loss is None:
        loss = Ds
    else:
        loss += Ds
        #loss += Ds_gt[x, y]
    

except:
    pass

return loss

Error message: Traceback (most recent call last):

ValueError: No gradients provided for any variable, check your graph for ops that do not support gradients, between variables ["<tf.variable 'stem="" basic_conv2d="" conv2d="" kernel:0'="" shape="(3," 3,="" 32)="" dtype="float32">", "<tf.variable 'stem="" basic_conv2d="" conv2d="" bias:0'="" shape="(32,)" dtype="float32">", "<tf.variable 'stem="" basic_conv2d="" batch_normalization="" gamma:0'="" shape="(32,)" dtype="float32">", .

Process finished with exit code 1

What I have tried:

I want to find the where the problem is, it seems because i get a loss without the result of network. If i multiply or subtract the result of network, the error is dissapear.like
Quote:
Ds = tf.reduce_sum(Ds * (q * t))
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900