Last active
June 23, 2022 02:39
-
-
Save kipronokoech/508476985969cd7893c075f64ff61127 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
import numpy as np | |
from shapely.geometry import Polygon,Point | |
import matplotlib.pyplot as plt | |
import shapely | |
import cv2 as cv | |
import os | |
import gc | |
def evaluation(ground,pred,iou_value): | |
""" | |
ground= array of ground-truth contours. | |
preds = array of predicted contours. | |
iou_value= iou treshold for TP and otherwise. | |
""" | |
truth=np.squeeze(ground) | |
preds=np.squeeze(pred) | |
#we will use this function to check iou less than threshold | |
def CheckLess(list1,val): | |
return(all(x<=val for x in list1)) | |
# Using predicted output as the reference | |
prob1=[] | |
for i in range(len(preds)): | |
f1=preds[i] | |
# define a Shapely polygone for prediction i | |
f1=shapely.geometry.Polygon(f1) | |
# determine the radius | |
f1_radius=np.sqrt((f1.area)/np.pi) | |
#buffer the polygon fromt the centroid | |
f1_buffered=shapely.geometry.Point(f1.centroid).buffer(f1_radius*500) | |
cont=[] | |
for i in range(len(truth)): | |
ff=shapely.geometry.Polygon(np.squeeze(truth[i])) | |
if f1_buffered.contains(ff)== True: | |
iou=(ff.intersection(f1).area)/(ff.union(f1).area) | |
cont.append((iou)) | |
prob1.append(cont) | |
fp=0 | |
for t in prob1: | |
if CheckLess(t,iou_value)==True: | |
fp=fp+1 | |
prob2=[] | |
#loop through each groun truth instance | |
for i in range(len(truth)): | |
f1=truth[i] | |
f1=shapely.geometry.Polygon(f1) | |
#find radius | |
f1_radius=np.sqrt((f1.area)/np.pi) | |
#buffer the polygon from the centroid | |
f1_buffered=shapely.geometry.Point(f1.centroid).buffer(f1_radius*500) | |
cont=[] | |
# merge up the ground truth instance against prediction | |
# to determine the IoU | |
for i in range(len(preds)): | |
ff=shapely.geometry.Polygon(np.squeeze(preds[i])) | |
if f1_buffered.contains(ff)== True: | |
#calculate IoU | |
iou=(ff.intersection(f1).area)/(ff.union(f1).area) | |
cont.append((iou)) | |
# probability of a given prediction to be contained in a | |
# ground truth instance | |
prob2.append(cont) | |
fn=0 | |
tp=0 | |
for t in prob2: | |
if np.sum(t)==0: | |
fn=fn+1 | |
elif CheckLess(t,iou_value)==False: | |
tp=tp+1 | |
#lets add this section just to print the results | |
print("TP:",tp,"\t FP:",fp,"\t FN:",fn,"\t GT:",truth.shape[0]) | |
precision=round(tp/(tp+fp),3) | |
recall=round(tp/(tp+fn),3) | |
f1= round(2*((precision*recall)/(precision+recall)),3) | |
print("Precall:",precision,"\t Recall:",recall, "\t F1 score:",f1) | |
return tp,fp,fn,precision,recall,f1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment