I have a instances boolean mask which shape is (448, 1000, 1000) for 448 instances, the average pixel of instance is around 100.
Now if I have a prediction matrix which shape is (1000, 1000) and predict instance by integer, i.e. If the matrix predict 500 instance, np.unique(pred) will be 501 (500 class + 1 background).
I need to calculate the IOU (jaccard index) for each pair prediction and mask to find the maximum IOU. I have wrote codes below but it's super slow and inefficient.
c = 0 #intersection count
u = 0 #union count
pred_used = [] #record prediction used
# loop for every ground truth mask
for idx_m in range(len(mask[:,0,0])):
m = mask[idx_m,:,:] #take one mask
intersect_list = []
union_list = []
# loop every prediction
for idx_pred in range(1, int(np.max(pred))+1):
p = (pred==idx_pred) # take one prediction mask
intersect = np.sum(m.ravel() * p.ravel()) #calculate intersect
union = np.sum(m.ravel() + p.ravel() - m.ravel()*p.ravel())
intersect_list.append(intersect)
union_list.append(union_list)
if np.sum(intersect_list) > 0:
idx_max_iou = np.argmax(np.array(intersect_list))
c += intersect_list[idx_max_iou]
u += union_list[idx_max_iou]
pred_used.append(idx_max_iou)