forked from rai-project/dlframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jaccard.go
55 lines (50 loc) · 1.54 KB
/
jaccard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package metrics
import (
"github.com/c3sr/config"
"github.com/c3sr/dlframework"
)
/*
Compute the jaccard overlap of two sets of boxes. The jaccard overlap
is simply the intersection over union of two boxes. Here we operate on
ground truth boxes and default boxes.
E.g.:
A ∩ B / A ∪ B = A ∩ B / (area(A) + area(B) - A ∩ B)
Args:
box_a: Predicted bounding boxes
box_b: Ground Truth bounding boxes
Return:
jaccard overlap: Shape: [n_pred, n_gt]
*/
func BoundingBoxJaccard(boxA, boxB *dlframework.BoundingBox) float64 {
intersection := BoundingBoxIntersectionOverUnion(boxA, boxA)
areaA := float64(boxA.Area())
areaB := float64(boxB.Area())
union := areaA + areaB - intersection
return intersection / union
}
func Jaccard(featA, featB *dlframework.Feature) float64 {
boxA, ok := featA.Feature.(*dlframework.Feature_BoundingBox)
if !ok {
panic("unable to convert first feature to boundingbox")
}
boxB, ok := featB.Feature.(*dlframework.Feature_BoundingBox)
if !ok {
panic("unable to convert second feature to boundingbox")
}
return BoundingBoxJaccard(boxA.BoundingBox, boxB.BoundingBox)
}
func init() {
config.AfterInit(func() {
RegisterFeatureCompareFunction("Jaccard",
func(actual *dlframework.Features, expected interface{}) float64 {
if actual == nil || len(*actual) != 1 {
panic("expecting one feature for argument")
}
expectedFeature, ok := expected.(*dlframework.Feature)
if !ok {
panic("expecting a feature for second argument")
}
return Jaccard((*actual)[0], expectedFeature)
})
})
}