如何在TensorFlow(Python)中调用OpenCV函数



当我训练模型时,我自定义了一个损失函数。此函数中损失值的计算需要 opencv 函数。看到代码,但我弄错了。我不知道怎么解决,有人可以帮我吗?多谢。

#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 然后我像这样调用损失函数:

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)

但它不起作用,在此处输入图像描述

为了能够训练网络张量流,需要创建一个可微操作的图。如果你想使用OpenCV函数,Tensorflow不知道如何为此构建衍生品。因此,您不能使用来自不同软件包的任意函数,将它们组合起来并希望它有效。

最新更新