如何在tensorflow(python)中调用opencv函数? [英] how to call opencv functions in tensorflow(python)?

查看:110
本文介绍了如何在tensorflow(python)中调用opencv函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在训练模型时,我已经自定义了损失函数.此函数中损失值的计算需要使用opencv函数.请参见代码,但是我弄错了.我不知道如何解决它,有人可以帮我吗?非常感谢.

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.

#这是我的损失函数def instance_loss_function(predict,label):best_match_label_image = search_MaxPixelAccuracy_permutation(predict_convert_gray_image(predict),标签)Forecast_image =预测loss_sum = 0.0best_match_label_image_contours_number = len(cv2.findContours(best_match_label_image.reshape(513,513),cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)[1])Forecast_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])对于范围(1,counter_min + 1)中的我:ith_instance_IoU = compute_oneClassIoU(predict_image,best_match_label_image,i)如果ith_instance_IoU!= 0:loss_sum =损失总和+ 2 *(1/(1 + ith_instance_IoU)-1/2)elif ith_instance_IoU == 0:loss_sum =损失总和+2如果np.abs(counter_max-counter_min)!= 0:loss_sum =损失总和+ 1 * np.abs(counter_max-counter_min)返回loss_sum然后我这样调用损失函数:

#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)

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

推荐答案

要能够训练您的网络tensorflow,需要创建一个可区分的操作图.如果要使用OpenCV函数,Tensorflow不知道如何为此构建导数.因此,您不能使用来自不同软件包的任意功能,不能将它们组合在一起并希望它能起作用.

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.

这篇关于如何在tensorflow(python)中调用opencv函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆