Python 如何在TfLearn或TensorFlow中创建自定义度量?

Python 如何在TfLearn或TensorFlow中创建自定义度量?,python,numpy,tensorflow,tflearn,metric,Python,Numpy,Tensorflow,Tflearn,Metric,TensorFlow提供了许多常用的评估指标,但我不知道如何创建自己的指标。 我正在构建一个基于AlexNet的CNN模型,用于抓取检测,我希望在评估数据时使用矩形度量(如本文中:)。矩形度量表示满足以下两个条件: - The grasp angle is within 30 degree of the ground truth grasp. - The Jaccard index of the predicted grasp and the ground truth is greater th

TensorFlow提供了许多常用的评估指标,但我不知道如何创建自己的指标。 我正在构建一个基于AlexNet的CNN模型,用于抓取检测,我希望在评估数据时使用矩形度量(如本文中:)。矩形度量表示满足以下两个条件:

- The grasp angle is within 30 degree of the ground truth grasp.
- The Jaccard index of the predicted grasp and the ground truth is greater than 25 percent.
因此,我的第一次尝试是使用TFLearn()上提供的AlexNet模型,并创建一个文件,用于使用numpy计算度量。下面是包含不完整代码的度量文件(因为我不允许共享),但其主要部分如下:

def grasp_error(grasps,targets,max_angle = 30,min_overlap=0.25):
        return np.mean([np.max([grasp_classification(grasps[i],targets,max_angle,min_overlap) for i in range(grasps.shape[0])])]) #for target in targets[i]])

#compute the error of the test set
def grasp_classification(grasp,target,max_angle = 30,min_overlap = 0.25):
    ...
    if abs(np.arctan2(grasp[sinpos],grasp[cospos]) - np.arctan2(target[sinpos],target[cospos]))< (max_angle * 2./180.)*np.pi:
        if jaccard_index(grasp,target) > min_overlap:
            return 1
    return 0

# computes Jaccard index of two grasping rectangeles
def jaccard_index(grasp,target):
    ...
    return intersect/overall
我得到了这个错误:

 File "gnet.py", line 57, in <module>
    learning_rate=0.0005)   
  File "/usr/local/lib/python2.7/dist-packages/tflearn/layers/estimator.py", line 159, in regression
    metric.build(incoming, placeholder, inputs)
  File "/usr/local/lib/python2.7/dist-packages/tflearn/metrics.py", line 119, in build
    prediction = predictions.eval()
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 569, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 3741, in _eval_using_default_session
    return session.run(tensors, feed_dict)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 778, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 982, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1032, in _do_run
    target_list, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1052, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value is_training
     [[Node: is_training/read = Identity[T=DT_BOOL, _class=["loc:@is_training"], _device="/job:localhost/replica:0/task:0/cpu:0"](is_training)]]
现在,错误不会显示,但培训将停止,但出现以下异常:

Reminder: Custom metric function arguments must be defined as: custom_metric(y_pred, y_true, x).
 File "gnet.py", line 57, in <module>
    learning_rate=0.0005)   
  File "/usr/local/lib/python2.7/dist-packages/tflearn/layers/estimator.py", line 159, in regression
    metric.build(incoming, placeholder, inputs)
  File "/usr/local/lib/python2.7/dist-packages/tflearn/metrics.py", line 119, in build
    prediction = predictions.eval()
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 569, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 3741, in _eval_using_default_session
    return session.run(tensors, feed_dict)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 778, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 982, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1032, in _do_run
    target_list, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1052, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value is_training
     [[Node: is_training/read = Identity[T=DT_BOOL, _class=["loc:@is_training"], _device="/job:localhost/replica:0/task:0/cpu:0"](is_training)]]
def rect_metric(prediction, target, inputs):
    x = []
    sess = tf.InteractiveSession()
    with sess as default:
        pred = prediction.eval(session=sess)
        tar =  target.eval(session=sess)
        x = tf.reduce_sum(grasp_error(pred,tar))
    return x
Reminder: Custom metric function arguments must be defined as: custom_metric(y_pred, y_true, x).