forked from rai-project/evaluation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
summary_model_accuracy.go
139 lines (122 loc) · 3.47 KB
/
summary_model_accuracy.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package evaluation
import (
"errors"
"strings"
"github.com/c3sr/evaluation/writer"
"github.com/spf13/cast"
db "upper.io/db.v3"
)
//easyjson:json
type SummaryModelAccuracyInformation struct {
SummaryBase `json:",inline"`
Top1Accuracy float64 `json:"top1_accuracy,omitempty"`
Top5Accuracy float64 `json:"top5_accuracy,omitempty"`
}
type SummaryModelAccuracyInformations []SummaryModelAccuracyInformation
func (SummaryModelAccuracyInformation) Header(opts ...writer.Option) []string {
extra := []string{
"top1_accuracy",
"top5_accuracy",
}
return append(SummaryBase{}.Header(opts...), extra...)
}
func (s SummaryModelAccuracyInformation) Row(opts ...writer.Option) []string {
extra := []string{
cast.ToString(s.Top1Accuracy),
cast.ToString(s.Top5Accuracy),
}
return append(s.SummaryBase.Row(opts...), extra...)
}
func (s SummaryModelAccuracyInformation) key() string {
return strings.Join(
[]string{
s.ModelName,
s.ModelVersion,
s.FrameworkName,
s.FrameworkVersion,
s.HostName,
s.MachineArchitecture,
cast.ToString(s.UsingGPU),
},
",",
)
}
func (SummaryModelAccuracyInformations) Header(opts ...writer.Option) []string {
return SummaryModelAccuracyInformation{}.Header(opts...)
}
func (s SummaryModelAccuracyInformations) Rows(opts ...writer.Option) [][]string {
rows := [][]string{}
for _, e := range s {
rows = append(rows, e.Row(opts...))
}
return rows
}
func (s SummaryModelAccuracyInformations) Group() (SummaryModelAccuracyInformations, error) {
groups := map[string]SummaryModelAccuracyInformations{}
for _, v := range s {
k := v.key()
if _, ok := groups[k]; !ok {
groups[k] = SummaryModelAccuracyInformations{}
}
groups[k] = append(groups[k], v)
}
res := []SummaryModelAccuracyInformation{}
for _, v := range groups {
if len(v) == 0 {
log.Error("expecting more more than one input in SummaryModelAccuracyInformations")
continue
}
for _, vv := range v {
if vv.Top1Accuracy != 0 && vv.Top5Accuracy != 0 {
res = append(res, vv)
break
}
}
}
return res, nil
}
func (a ModelAccuracy) PredictAccuracyInformationSummary(e Evaluation) (*SummaryModelAccuracyInformation, error) {
return &SummaryModelAccuracyInformation{
SummaryBase: e.summaryBase(),
Top1Accuracy: a.Top1,
Top5Accuracy: a.Top5,
}, nil
}
func (as ModelAccuracies) PredictAccuracyInformationSummary(e Evaluation) ([]*SummaryModelAccuracyInformation, error) {
res := []*SummaryModelAccuracyInformation{}
for _, a := range as {
s, err := a.PredictAccuracyInformationSummary(e)
if err != nil {
log.WithError(err).Error("failed to get accuracy information summary")
continue
}
res = append(res, s)
}
return res, nil
}
func (e Evaluation) PredictAccuracyInformationSummary(accCol *ModelAccuracyCollection) (*SummaryModelAccuracyInformation, error) {
accs, err := accCol.Find(db.Cond{"_id": e.ModelAccuracyID})
if err != nil {
return nil, err
}
if len(accs) != 1 {
return nil, errors.New("expecting on model accuracy output")
}
acc := accs[0]
return acc.PredictAccuracyInformationSummary(e)
}
func (es Evaluations) PredictAccuracyInformationSummary(accCol *ModelAccuracyCollection) (SummaryModelAccuracyInformations, error) {
res := []SummaryModelAccuracyInformation{}
for _, e := range es {
s, err := e.PredictAccuracyInformationSummary(accCol)
if err != nil {
log.WithError(err).Error("failed to get accuracy information summary")
continue
}
if s == nil {
continue
}
res = append(res, *s)
}
return res, nil
}