How to do it...

IoU is calculated as follows (the code file is available as Selective_search.ipynb in GitHub):

  1. Define the IoU extraction function, demonstrated in the following code:
from copy import deepcopyimport numpy as npdef extract_iou(candidate, current_y,img_shape):     boxA = deepcopy(candidate)     boxB = deepcopy(current_y)     img1 = np.zeros(img_shape)     img1[boxA[1]:boxA[3],boxA[0]:boxA[2]]=1     img2 = np.zeros(img_shape)     img2[int(boxB[1]):int(boxB[3]),int(boxB[0]):int(boxB[2])]=1     iou = np.sum(img1*img2)/(np.sum(img1)+np.sum(img2)- np.sum(img1*img2))     return iou

In the preceding function, we take the candidate, actual object region, and image shape as input.

Further, we initialized two zero-value arrays of the same shape for the candidate image ...

Get Neural Networks with Keras Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.