Evaluating detection (Intersection Over Union)

Before we move further, we need to know how to measure whether our model detected an object correctly or not; for this, we calculate the Intersection Over Union (IoU) that will return a number, telling us how good our detection was based on some reference (Ground Truth). IoU is calculated by dividing the area where a detection and ground-truth box overlap with each over by the total area covered by the detection and ground-truth box:

Here is an example of a poor, good, and excellent IoU:

By convention, if the IoU is bigger than 0.5, we consider that both boxes match and in this case, a detection is a true positive.

An IoU of zero means that the boxes don't intersect, and an IoU of one means a perfect match.

On our detector, if a cell has more than one  anchor boxes, IoU  helps to choose which one is responsible for object .We choose the anchor which has  highest IoU with the ground truth.

Here's the Python code for IoU:

def iou_non_vectorized(box1, box2): 
   # If one of the rects are empty return 0 (No intersect) 
   if box1 == [] or box2 == []: 
       return 0 
 
   # size of intersect divided by size of union of 2 rects 
   # Get rectangle areas format (left,top,right,bottom) 
   box_1_area = (box1[2] - box1[0] + 1) * (box1[3] - box1[1] + 1) 
   box_2_area = (box2[2] - box2[0] + 1) * (box2[3] - box2[1] + 1) 
 
   # Get the intersection coordinates (x1,y1,x2,y2) 
   intersect_x1 = max(box1[0], box2[0]) 
   intersect_y1 = max(box1[1], box2[1]) 
   intersect_x2 = min(box1[2], box2[2]) 
   intersect_y2 = min(box1[3], box2[3]) 
 
   # Calculate intersection area 
   intersect_area = (intersect_x2 - intersect_x1 + 1) * (intersect_y2 - intersect_y1   
+ 1) return intersect_area / float(box_1_area + box_2_area - intersect_area) We can also change this to a vectorized form on Tensorflow def tf_iou_vectorized(self, box_vec_1, box_vec_2): def run(tb1, tb2): # Break the boxes rects vector in sub-vectors b1_x1, b1_y1, b1_x2, b1_y2 = tf.split(box_vec_1, 4, axis=1) b2_x1, b2_y1, b2_x2, b2_y2 = tf.split(box_vec_2, 4, axis=1) # Get rectangle areas format (left,top,right,bottom) box_vec_1_area = (b1_x2 - b1_x1 + 1) * (b1_y2 - b1_y1 + 1) box_vec_2_area = (b2_x2 - b2_x1 + 1) * (b2_y2 - b2_y1 + 1) xA = tf.maximum(b1_x1, tf.transpose(b2_x1)) yA = tf.maximum(b1_y1, tf.transpose(b2_y1)) xB = tf.minimum(b1_x2, tf.transpose(b2_x2)) yB = tf.minimum(b1_y2, tf.transpose(b2_y2)) interArea = tf.maximum((xB - xA + 1), 0) * tf.maximum((yB - yA + 1), 0) iou = interArea / (box_vec_1_area + tf.transpose(box_vec_2_area) - interArea) return iou op = run(self.tf_bboxes1, self. tf_bboxes2) self.sess.run(op, feed_dict={self.tf_bboxes1: box_vec_1, self.tf_bboxes2: box_vec_2}) tic = time() self.sess.run(op, feed_dict={self.tf_bboxes1: box_vec_1, self.tf_bboxes2: box_vec_2}) toc = time() return toc - tic

We can also change this to a vectorized form on TensorFlow, as shown:

def tf_iou_vectorized(self, box_vec_1, box_vec_2):
def run(tb1, tb2):
# Break the boxes rects vector in sub-vectors
b1_x1, b1_y1, b1_x2, b1_y2 = tf.split(box_vec_1, 4, axis=1)
b2_x1, b2_y1, b2_x2, b2_y2 = tf.split(box_vec_2, 4, axis=1)
# Get rectangle areas format (left,top,right,bottom)
box_vec_1_area = (b1_x2 - b1_x1 + 1) * (b1_y2 - b1_y1 + 1)
box_vec_2_area = (b2_x2 - b2_x1 + 1) * (b2_y2 - b2_y1 + 1)
xA = tf.maximum(b1_x1, tf.transpose(b2_x1))
yA = tf.maximum(b1_y1, tf.transpose(b2_y1))
xB = tf.minimum(b1_x2, tf.transpose(b2_x2))
yB = tf.minimum(b1_y2, tf.transpose(b2_y2))
interArea = tf.maximum((xB - xA + 1), 0) * tf.maximum((yB - yA + 1), 0)
iou = interArea / (box_vec_1_area + tf.transpose(box_vec_2_area) - interArea)
return iou
op = run(self.tf_bboxes1, self. tf_bboxes2)
self.sess.run(op, feed_dict={self.tf_bboxes1: box_vec_1, self.tf_bboxes2: box_vec_2})
tic = time()
self.sess.run(op, feed_dict={self.tf_bboxes1: box_vec_1, self.tf_bboxes2: box_vec_2})
toc = time()
return toc - tic
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.93.20