Reputation: 291
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)
Upvotes: 1
Views: 1415
Reputation: 1214
So you have an output image sized [1000,1000] which is the predicted array/tensor by your model.
One of the first thing you can do is reshape the labels and predictions from [1000,1000] to [1000*1000, ]. This reduces the complexity from being N^2 to N. This should boostup the speed significantly .
And you can also try the IoU from Scikit which maybe a bit faster than your vesion.
You can find an example here: How to find IoU from segmentation masks?
Doc: http://scikit-learn.org/stable/modules/generated/sklearn.metrics.jaccard_similarity_score.html
Upvotes: 1