def precision_recall(num_gbboxes, tp, fp, scope=None): """Compute precision and recall from true positives and false positives booleans arrays """ # Sort by score. with tf.name_scope(scope, 'precision_recall'): # Computer recall and precision. tp = tf.reduce_sum(tf.cast(tp, tf.float32), axis=0) fp = tf.reduce_sum(tf.cast(fp, tf.float32), axis=0) recall = tfe_math.safe_divide(tp, tf.cast(num_gbboxes, tf.float32), 'recall') precision = tfe_math.safe_divide(tp, tp + fp, 'precision') return tf.tuple([precision, recall])
def bboxes_intersection(bbox_ref, bboxes, name=None): """Compute relative intersection between a reference box and a collection of bounding boxes. Namely, compute the quotient between intersection area and box area. Args: bbox_ref: (N, 4) or (4,) Tensor with reference bounding box(es). bboxes: (N, 4) Tensor, collection of bounding boxes. Return: (N,) Tensor with relative intersection. """ with tf.name_scope(name, 'bboxes_intersection'): # Should be more efficient to first transpose. bboxes = tf.transpose(bboxes) bbox_ref = tf.transpose(bbox_ref) # Intersection bbox and volume. int_ymin = tf.maximum(bboxes[0], bbox_ref[0]) int_xmin = tf.maximum(bboxes[1], bbox_ref[1]) int_ymax = tf.minimum(bboxes[2], bbox_ref[2]) int_xmax = tf.minimum(bboxes[3], bbox_ref[3]) h = tf.maximum(int_ymax - int_ymin, 0.) w = tf.maximum(int_xmax - int_xmin, 0.) # Volumes. inter_vol = h * w bboxes_vol = (bboxes[2] - bboxes[0]) * (bboxes[3] - bboxes[1]) scores = tfe_math.safe_divide(inter_vol, bboxes_vol, 'intersection') return scores
def bboxes_jaccard(bbox_ref, bboxes, name=None): """Compute jaccard score between a reference box and a collection of bounding boxes. Args: bbox_ref: (N, 4) or (4,) Tensor with reference bounding box(es). bboxes: (N, 4) Tensor, collection of bounding boxes. Return: (N,) Tensor with Jaccard scores. """ with tf.name_scope(name, 'bboxes_jaccard'): # Should be more efficient to first transpose. bboxes = tf.transpose(bboxes) bbox_ref = tf.transpose(bbox_ref) # Intersection bbox and volume. int_ymin = tf.maximum(bboxes[0], bbox_ref[0]) int_xmin = tf.maximum(bboxes[1], bbox_ref[1]) int_ymax = tf.minimum(bboxes[2], bbox_ref[2]) int_xmax = tf.minimum(bboxes[3], bbox_ref[3]) h = tf.maximum(int_ymax - int_ymin, 0.) w = tf.maximum(int_xmax - int_xmin, 0.) # Volumes. inter_vol = h * w union_vol = -inter_vol \ + (bboxes[2] - bboxes[0]) * (bboxes[3] - bboxes[1]) \ + (bbox_ref[2] - bbox_ref[0]) * (bbox_ref[3] - bbox_ref[1]) jaccard = tfe_math.safe_divide(inter_vol, union_vol, 'jaccard') return jaccard
def bboxes_intersection(bbox_ref, bboxes, name=None): """Compute relative intersection between a reference box and a collection of bounding boxes. Namely, compute the quotient between intersection area and box area. Args: bbox_ref: (N, 4) or (4,) Tensor with reference bounding box(es). bboxes: (N, 4) Tensor, collection of bounding boxes. Return: (N,) Tensor with relative intersection. """ with tf.name_scope(name, 'bboxes_intersection'): # Should be more efficient to first transpose. bboxes = tf.transpose(bboxes) bbox_ref = tf.transpose(bbox_ref) # Intersection bbox and volume. int_ymin = tf.maximum(bboxes[0], bbox_ref[0]) int_xmin = tf.maximum(bboxes[1], bbox_ref[1]) int_ymax = tf.minimum(bboxes[2], bbox_ref[2]) int_xmax = tf.minimum(bboxes[3], bbox_ref[3]) h = tf.maximum(int_ymax - int_ymin, 0.) w = tf.maximum(int_xmax - int_xmin, 0.) # Volumes. inter_vol = h * w # 各个框在[0,0,1,1]内的面积 bboxes_vol = (bboxes[2] - bboxes[0]) * (bboxes[3] - bboxes[1]) # 各个框面积 scores = tfe_math.safe_divide(inter_vol, bboxes_vol, 'intersection') # from tensorflow.python.ops import math_ops # 大于0的位置返回面积比,小于0的位置返回0 # tf.where(math_ops.greater(bboxes_vol, 0), # 返回bool表是否大于0 # math_ops.divide(inter_vol, bboxes_vol), # tf.zeros_like(inter_vol), name=name) return scores
def has_bboxes(bbox_ref=bbox_ref, bboxes=bboxes): bboxes = tf.transpose(bboxes) bbox_ref = tf.transpose(bbox_ref) # Intersection bbox and volume. int_ymin = tf.maximum(bboxes[0], bbox_ref[0]) int_xmin = tf.maximum(bboxes[1], bbox_ref[1]) int_ymax = tf.minimum(bboxes[2], bbox_ref[2]) int_xmax = tf.minimum(bboxes[3], bbox_ref[3]) h = tf.maximum(int_ymax - int_ymin, 0.) w = tf.maximum(int_xmax - int_xmin, 0.) # Volumes. inter_vol = h * w union_vol = -inter_vol \ + (bboxes[2] - bboxes[0]) * (bboxes[3] - bboxes[1]) \ + (bbox_ref[2] - bbox_ref[0]) * (bbox_ref[3] - bbox_ref[1]) jaccard = tfe_math.safe_divide(inter_vol, union_vol, 'jaccard') return jaccard