Refactor metrics
This commit is contained in:
parent
65e9735bd9
commit
a97f8def7c
@ -1,8 +1,9 @@
|
|||||||
|
from functools import reduce
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
from typing import Iterable
|
from typing import Iterable
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from funcy import lmap, lpluck
|
from funcy import lmap, lpluck, first
|
||||||
|
|
||||||
from cv_analysis.utils import lift
|
from cv_analysis.utils import lift
|
||||||
from cv_analysis.utils.rectangle import Rectangle
|
from cv_analysis.utils.rectangle import Rectangle
|
||||||
@ -30,25 +31,26 @@ def compute_document_score(result_dict, ground_truth_dicts):
|
|||||||
|
|
||||||
def rectangle_from_dict(d):
|
def rectangle_from_dict(d):
|
||||||
x1, y1, w, h = itemgetter("x", "y", "width", "height")(d)
|
x1, y1, w, h = itemgetter("x", "y", "width", "height")(d)
|
||||||
x2 = x1 + w
|
return Rectangle(x1, y1, x1 + w, y1 + h)
|
||||||
y2 = y1 + h
|
|
||||||
return Rectangle(x1, y1, x2, y2)
|
|
||||||
|
|
||||||
|
|
||||||
def compute_page_iou(results_boxes: Iterable[Rectangle], ground_truth_boxes: Iterable[Rectangle]):
|
def compute_page_iou(predicted_rectangles: Iterable[Rectangle], true_rectangles: Iterable[Rectangle]):
|
||||||
results = list(results_boxes)
|
def find_best_iou(sum_so_far_and_candidate_rectangles, true_rectangle):
|
||||||
truth = list(ground_truth_boxes)
|
sum_so_far, predicted_rectangles = sum_so_far_and_candidate_rectangles
|
||||||
|
best_match, best_iou = find_max_overlap(true_rectangle, predicted_rectangles)
|
||||||
|
return sum_so_far + best_iou, predicted_rectangles - {best_match}
|
||||||
|
|
||||||
def find_best_iou(gt_box):
|
predicted_rectangles = set(predicted_rectangles)
|
||||||
best_match, best_iou = find_max_overlap(gt_box, results)
|
true_rectangles = set(true_rectangles)
|
||||||
results.remove(best_match)
|
|
||||||
return best_iou
|
iou_sum = first(reduce(find_best_iou, true_rectangles, (0, predicted_rectangles)))
|
||||||
|
normalizing_factor = 1 / max(len(predicted_rectangles), len(true_rectangles))
|
||||||
|
score = normalizing_factor * iou_sum
|
||||||
|
|
||||||
score = sum(map(find_best_iou, truth)) / max(len(results), len(truth))
|
|
||||||
return score
|
return score
|
||||||
|
|
||||||
|
|
||||||
def find_max_overlap(box: Rectangle, box_list: Iterable[Rectangle]):
|
def find_max_overlap(rectangle: Rectangle, candidate_rectangles: Iterable[Rectangle]):
|
||||||
best_candidate = max(box_list, key=lambda x: box.iou(x))
|
best_candidate_rectangle = max(candidate_rectangles, key=rectangle.iou)
|
||||||
iou = box.iou(best_candidate)
|
iou = rectangle.iou(best_candidate_rectangle)
|
||||||
return best_candidate, iou
|
return best_candidate_rectangle, iou
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user