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