-
Notifications
You must be signed in to change notification settings - Fork 1
/
topk.go
80 lines (68 loc) · 2.11 KB
/
topk.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package metrics
import (
"sort"
"github.com/rai-project/config"
"github.com/rai-project/dlframework"
"github.com/spf13/cast"
)
// ClassificationTop1 ...
func ClassificationTop1(features *dlframework.Features, expectedLabelIndex int) bool {
for _, feature := range []*dlframework.Feature(*features) {
_, ok := feature.Feature.(*dlframework.Feature_Classification)
if !ok {
panic("unable to convert first feature to boundingbox")
}
}
sort.Sort(features)
actualLabelIndex := int([]*dlframework.Feature(*features)[0].Feature.(*dlframework.Feature_Classification).Classification.Index)
return actualLabelIndex == expectedLabelIndex
}
func Top1(features *dlframework.Features, expectedLabelIndex int) bool {
return ClassificationTop1(features, expectedLabelIndex)
}
// ClassificationTop5 ...
func ClassificationTop5(features *dlframework.Features, expectedLabelIndex int) bool {
for _, feature := range []*dlframework.Feature(*features) {
_, ok := feature.Feature.(*dlframework.Feature_Classification)
if !ok {
panic("unable to convert first feature to boundingbox")
}
}
sort.Sort(features)
for _, feature := range []*dlframework.Feature(*features)[:5] {
if int(feature.Feature.(*dlframework.Feature_Classification).Classification.Index) == expectedLabelIndex {
return true
}
}
return false
}
// Top5 ...
func Top5(features *dlframework.Features, expectedLabelIndex int) bool {
return ClassificationTop5(features, expectedLabelIndex)
}
func init() {
config.AfterInit(func() {
RegisterFeatureCompareFunction("Top1",
func(actual *dlframework.Features, expected interface{}) float64 {
expectedLabel, err := cast.ToIntE(expected)
if err != nil {
panic("expecting an int for second argument")
}
if Top1(actual, expectedLabel) {
return 1.0
}
return 0.0
})
RegisterFeatureCompareFunction("Top5",
func(actual *dlframework.Features, expected interface{}) float64 {
expectedLabel, err := cast.ToIntE(expected)
if err != nil {
panic("expecting an int for second argument")
}
if Top5(actual, expectedLabel) {
return 1.0
}
return 0.0
})
})
}