Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit

Permalink
Apply tf_upgrade_v2 for metrics (#913)
Browse files Browse the repository at this point in the history
  • Loading branch information
ytfksw authored Apr 10, 2020
1 parent 5569e43 commit 14d57ab
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
26 changes: 13 additions & 13 deletions blueoil/metrics/mean_average_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ def _streaming_tp_fp_array(
shape=[], dtype=tf.int64, name="num_gt_boxes_value")

# Update operations.
tp_op = tf.assign(tp_value, tf.concat([tp_value, tp], axis=0), validate_shape=False)
fp_op = tf.assign(fp_value, tf.concat([fp_value, fp], axis=0), validate_shape=False)
scores_op = tf.assign(scores_value, tf.concat([scores_value, scores], axis=0), validate_shape=False)
num_gt_boxes_op = tf.assign_add(num_gt_boxes_value, num_gt_boxes)
tp_op = tf.compat.v1.assign(tp_value, tf.concat([tp_value, tp], axis=0), validate_shape=False)
fp_op = tf.compat.v1.assign(fp_value, tf.concat([fp_value, fp], axis=0), validate_shape=False)
scores_op = tf.compat.v1.assign(scores_value, tf.concat([scores_value, scores], axis=0), validate_shape=False)
num_gt_boxes_op = tf.compat.v1.assign_add(num_gt_boxes_value, num_gt_boxes)

# Value and update ops.
values = (tp_value, fp_value, scores_value, num_gt_boxes_value)
Expand All @@ -136,7 +136,7 @@ def _average_precision(precision, recall, name=None):
The implementation follows Pascal 2012 and ILSVRC guidelines.
See also: https://sanchom.wordpress.com/tag/average-precision/
"""
with tf.name_scope(name, 'average_precision', [precision, recall]):
with tf.compat.v1.name_scope(name, 'average_precision', [precision, recall]):
# Convert to float64 to decrease error on Riemann sums.
precision = tf.cast(precision, dtype=tf.float64)
recall = tf.cast(recall, dtype=tf.float64)
Expand Down Expand Up @@ -164,7 +164,7 @@ def _safe_div_zeros(numerator, denominator, name):
Returns:
0 if `denominator` <= 0, else `numerator` / `denominator`
"""
return tf.where(
return tf.compat.v1.where(
tf.greater(denominator, 0),
tf.divide(numerator, denominator),
tf.zeros_like(numerator),
Expand All @@ -180,7 +180,7 @@ def _safe_div_ones(numerator, denominator, name):
Returns:
1 if `denominator` <= 0, else `numerator` / `denominator`
"""
return tf.where(
return tf.compat.v1.where(
tf.greater(denominator, 0),
tf.divide(numerator, denominator),
tf.ones_like(numerator),
Expand All @@ -200,7 +200,7 @@ def _precision_recall(
"""
default_name = 'precision_recall_{}'.format(class_name)
# Sort by score.
with tf.name_scope(scope, default_name, [num_gt_boxes, tp, fp, scores]):
with tf.compat.v1.name_scope(scope, default_name, [num_gt_boxes, tp, fp, scores]):
num_detections = tf.size(scores)
# Sort detections by score.
scores, idxes = tf.nn.top_k(scores, k=num_detections, sorted=True)
Expand All @@ -215,15 +215,15 @@ def _precision_recall(

scalar_precision = tf.cond(
tf.equal(tf.size(precision), 0),
lambda: tf.constant(0, dtype=dtype),
lambda: precision[-1],
true_fn=lambda: tf.constant(0, dtype=dtype),
false_fn=lambda: precision[-1],
name="scalar_precision"
)

scalar_recall = tf.cond(
tf.equal(tf.size(recall), 0),
lambda: tf.constant(0, dtype=dtype),
lambda: recall[-1],
true_fn=lambda: tf.constant(0, dtype=dtype),
false_fn=lambda: recall[-1],
name="scalar_recall"
)

Expand All @@ -244,7 +244,7 @@ def _cummax(x, reverse=False, name=None):
Returns:
A `Tensor`. Has the same type as `x`.
"""
with tf.name_scope(name, "Cummax", [x]) as name:
with tf.compat.v1.name_scope(name, "Cummax", [x]) as name:
# x = tf.convert_to_tensor(x, name="x")
# Not very optimal: should directly integrate reverse into tf.scan.
if reverse:
Expand Down
8 changes: 4 additions & 4 deletions blueoil/metrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ def tp_tn_fp_fn_for_each(output, labels, threshold=0.5):
"""
predicted = tf.greater_equal(output, threshold)
gt_positive = tf.reduce_sum(tf.cast(labels, tf.int32), 0, keep_dims=True)
gt_negative = tf.reduce_sum(tf.cast(tf.logical_not(labels), tf.int32), 0, keep_dims=True)
gt_positive = tf.reduce_sum(tf.cast(labels, tf.int32), axis=0, keepdims=True)
gt_negative = tf.reduce_sum(tf.cast(tf.logical_not(labels), tf.int32), axis=0, keepdims=True)
true_positive = tf.math.logical_and(predicted, labels)
true_positive = tf.reduce_sum(tf.cast(true_positive, tf.int32), 0, keep_dims=True)
true_positive = tf.reduce_sum(tf.cast(true_positive, tf.int32), axis=0, keepdims=True)

true_negative = tf.math.logical_and(tf.logical_not(predicted), tf.math.logical_not(labels))
true_negative = tf.reduce_sum(tf.cast(true_negative, tf.int32), 0, keep_dims=True)
true_negative = tf.reduce_sum(tf.cast(true_negative, tf.int32), axis=0, keepdims=True)
false_negative = gt_positive - true_positive
false_positive = gt_negative - true_negative

Expand Down

0 comments on commit 14d57ab

Please sign in to comment.