matterport / Mask_RCNN

Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow
Other
24.54k stars 11.68k forks source link

Computing precision,recall,tp,fp,fn values for ENTIRE dataset #2549

Open sohinimallick opened 3 years ago

sohinimallick commented 3 years ago

I found a Github implementation to compute confusion matrix and tp/fp/fn values from here : https://github.com/Altimis/Confusion-matrix-for-Mask-R-CNN. I implemented the same as follows-

def confusion_matrix(dataset, model, cfg): gt_tot = np.array([]) predtot = np.array([]) mAP = [] for image_id in dataset.image_ids:

load image, bounding boxes and masks for the image id

image, image_meta, gt_class_id, gt_bbox, gt_mask = modellib.load_image_gt(dataset, cfg, image_id)
# convert pixel values (e.g. center)
scaled_image = modellib.mold_image(image, cfg)
# convert image into one sample
sample = np.expand_dims(scaled_image, 0)
# make prediction
yhat = model.detect([image], verbose=0, mask_threshold=0.5)
# extract results for first sample
r = yhat[0]

# compute gt_tot and pred_tot
gt, pred = utils_confusion.gt_pred_lists(gt_class_id, gt_bbox, r['class_ids'], r['rois'])
gt_tot = np.append(gt_tot, gt)
pred_tot = np.append(pred_tot, pred)

AP_, precision_, recall_, overlap_ = mrcnn.utils.compute_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"],
                                                    r["scores"], r['masks'])
mAP_.append(AP_)

tp, fp, fn = utils_confusion.plot_confusion_matrix_from_data(gt_tot, pred_tot, fz=18, figsize=(20, 20), lw=0.5)
del tp[0]
del fp[0]
del fn[0]
print("tp for each class :", tp)
print("fp for each class :", fp)
print("fn for each class :", fn)

confusion_matrix(val_set,model_inference,Icfg)

This gives the individual matrix+tp/fp/fn values for each image. How can I modify it so that I get the values/matrix of the combined dataset i.e. total number of fp,fn,tp.

Also is there a way to compute precision/recall from the entire dataset from this?

nataliameira commented 3 years ago

@sohinimallick Hi. Did you manage to implement this? Did it work on your model?

parthkvv commented 1 year ago

Did you find any solution @sohinimallick?