0

when I train the model,I have customized a loss function.The calculation of the loss value in this function requires the function of opencv.See the code,but I get a wrong.I don't know how to solve it,someone can help me?Thanks a lot.

#this is my loss function def instance_loss_function(predict,label): best_match_label_image=search_MaxPixelAccuracy_permutation(predict_convert_gray_image(predict),label) predict_image=predict loss_sum=0.0 best_match_label_image_contours_number=len(cv2.findContours( best_match_label_image.reshape(513,513), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1]) predict_image_contours_number=len(cv2.findContours( predict_image.reshape(513,513), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1]) counter_max=np.max([best_match_label_image_contours_number,predict_image_contours_number]) counter_min=np.min([best_match_label_image_contours_number,predict_image_contours_number]) for i in range(1,counter_min+1): ith_instance_IoU=compute_oneClassIoU(predict_image,best_match_label_image,i) if ith_instance_IoU!=0: loss_sum=loss_sum+2*(1/(1+ith_instance_IoU)-1/2) elif ith_instance_IoU==0: loss_sum=loss_sum+2 if np.abs(counter_max-counter_min)!=0: loss_sum=loss_sum+1*np.abs(counter_max-counter_min) return loss_sum and then I call the loss function like this:

loss=tf.py_func(instance_loss_function,[valid_logits,valid_labels],tf.float32)
train_op = optimizer.minimize(loss, global_step, var_list=train_var_list)

but it does not work, enter image description here

1 Answer 1

2

To be able to train you network tensorflow needs to create a graph of differentiable operations. If you want to use OpenCV functions, Tensorflow has no idea of how to build derivatives for that. So you can't use arbitrary functions from different software packages, combine them and hope that it works.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you,but I need to use the function of opencv( cv2.findContours() and cv2.drawContours()) when calculating the loss value.Is there something that can replace these two functions?
I am not sure what you are trying to achieve... If you want your model to do a boundary prediction then do a different set up.
I am training an instance segmentation model.In our image dataset, there is only one class in the foreground but multiple instances.We need to split different instances and in order to improve the performance of the model,We have customized the loss function(The calculation of the loss value requires the function of OpenCV).In order to call the OpenCV function in tensorflow, I have tried tf.py_func() and AutoGraph.But all failed.
From my understanding, when you are calling OpenCV functions in python it will call native c++ libs. So I don't think that tensorflow will be able to create a graph for that. I think you can just try to recreate the OpenCV code in numpy or tensorflow itself if it is possible. But again, all operations need to be differentiable and I don't know if that is the case.
I will try it.Thank you!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.