Created
November 4, 2019 04:23
-
-
Save randcode-generator/6a3de9e7bd7af79e558f2fdf3f44de09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
anchor = \ | |
[ | |
[0.0, 0.0, 100.0, 100.0], | |
[100.0, 100.0, 160.0, 200.0], | |
[100.0, 0.0, 200.0, 100.0] | |
] | |
ground_truth = \ | |
[ | |
[0.0, 0.0, 100.0, 100.0], | |
[100.0, 100.0, 180.0, 180.0] | |
] | |
with tf.Session() as sess: | |
left1, top1, right1, bottom1 = tf.split( | |
value=anchor, num_or_size_splits=4, axis=1) | |
left2, top2, right2, bottom2 = tf.split( | |
value=ground_truth, num_or_size_splits=4, axis=1) | |
# Find the max values of top between ground truth and anchor boxes | |
max_top = tf.maximum(top2, tf.transpose(top1)) | |
# Find the min values of bottom between ground truth and anchor boxes | |
min_bottom = tf.minimum(bottom2, tf.transpose(bottom1)) | |
intersect_heights = tf.maximum(0.0, min_bottom - max_top) | |
# Find the height of the intersection between ground truth and anchor boxes | |
max_left = tf.maximum(left2, tf.transpose(left1)) | |
# Find the max values of left between ground truth and anchor boxes | |
min_right = tf.minimum(right2, tf.transpose(right1)) | |
intersect_widths = tf.maximum(0.0, min_right - max_left) | |
area_of_intersect = intersect_heights * intersect_widths | |
#AREA | |
area1 = (bottom1 - top1) * (right1 - left1) | |
area1 = tf.squeeze(area1, 1) | |
area2 = (bottom2 - top2) * (right2 - left2) | |
area2 = tf.squeeze(area2, 1) | |
area1 = tf.expand_dims(area1, 0) | |
area2 = tf.expand_dims(area2, 1) | |
union = area1 + area2 - area_of_intersect | |
isEqualZero = tf.equal(area_of_intersect, 0.0) | |
iou = tf.where( | |
isEqualZero, | |
tf.zeros_like(area_of_intersect), | |
tf.truediv(area_of_intersect, union) | |
) | |
print(iou.eval()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment