-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstorage.go
170 lines (154 loc) · 3.7 KB
/
storage.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package facenet
import (
"archive/zip"
"io"
"os"
"google.golang.org/protobuf/proto"
"github.com/bububa/facenet/classifier"
"github.com/bububa/facenet/core"
)
const (
// PeopleFilename represents people data filename in zip
PeopleFilename = "people.pb"
// ClassifierFilename represents classifier data filename in zip
ClassifierFilename = "classifier.model"
)
// Storage represents db storage
type Storage struct {
people *core.People
classifier classifier.Classifier
}
// NewStorage returns new Storage
func NewStorage(people *core.People, classifier classifier.Classifier) *Storage {
return &Storage{
people: people,
classifier: classifier,
}
}
// Load load storage from file
func (s *Storage) Load(fname string) error {
if s.people == nil {
s.people = new(core.People)
}
zipFn, err := zip.OpenReader(fname)
if err != nil {
if os.IsNotExist(err) {
s.classifier = classifier.NewDefault()
return nil
}
return err
}
defer zipFn.Close()
for _, f := range zipFn.File {
info := f.FileInfo()
if info.IsDir() {
continue
}
switch info.Name() {
case PeopleFilename:
r, err := f.Open()
if err != nil {
return err
}
buf, err := io.ReadAll(r)
if err != nil {
return err
}
if err = proto.Unmarshal(buf, s.people); err != nil {
return err
}
s.people.Setup()
case ClassifierFilename:
r, err := f.Open()
if err != nil {
return err
}
s.classifier = new(classifier.Neural)
s.classifier.Read(r)
}
}
return nil
}
// Save save storage to file
func (s *Storage) Save(fname string) error {
fn, err := os.Create(fname)
if err != nil {
return err
}
defer fn.Close()
// 创建zip
zipWriter := zip.NewWriter(fn)
defer zipWriter.Close()
if s.people != nil {
peopleFn, err := zipWriter.Create(PeopleFilename)
if err != nil {
return err
}
if err := s.people.Save(peopleFn); err != nil {
return err
}
}
if s.classifier != nil {
classifierFn, err := zipWriter.Create(ClassifierFilename)
if err != nil {
return err
}
s.classifier.Write(classifierFn)
}
return nil
}
// SetClassifier set classifier
func (s *Storage) SetClassifier(c classifier.Classifier) {
s.classifier = c
}
// People returns people
func (s *Storage) People() *core.People {
return s.people
}
// Add add person to people
func (s *Storage) Add(items ...*core.Person) {
if s.people == nil {
s.people = new(core.People)
}
s.people.Append(items...)
}
// Delete delete a person by name
func (s *Storage) Delete(name string) bool {
if s.people == nil {
return false
}
return s.people.Delete(name)
}
// Predict returns predictation results
func (s *Storage) Predict(input []float32) ([]*core.Person, []float64, error) {
scores := s.classifier.Predict(input)
if len(scores) == 0 {
return nil, nil, core.NewError(core.NothingMatchErr, "no match results")
}
ret := make([]*core.Person, 0, len(scores))
people := s.people.GetList()
for idx := range scores {
ret = append(ret, people[idx])
}
return ret, scores, nil
}
// Match returns best match result
func (s *Storage) Match(input []float32) (*core.Person, float64, error) {
if s.classifier == nil {
return s.people.Match(input)
}
idx, score := s.classifier.Match(input)
if idx < 0 {
return nil, score, core.NewError(core.NothingMatchErr, "no match results")
}
people := s.People().GetList()
return people[idx], score, nil
}
// Train for trainging classifier
func (s *Storage) Train(split float64, iterations int, verbosity int) {
s.classifier.Train(s.people, split, iterations, verbosity)
}
// BatchTrain for trainging classifier
func (s *Storage) BatchTrain(split float64, iterations int, verbosity int, batch int) {
s.classifier.BatchTrain(s.people, split, iterations, verbosity, batch)
}