-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathclassifier.go
207 lines (154 loc) · 4.78 KB
/
classifier.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package elasticthought
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/couchbaselabs/logg"
"github.com/golang/protobuf/proto"
"github.com/tleyden/elastic-thought/caffe"
"github.com/tleyden/go-couch"
)
// A classifier uses a trained model to classify new incoming data points
type Classifier struct {
ElasticThoughtDoc
SpecificationUrl string `json:"specification-url" binding:"required"`
TrainingJobID string `json:"training-job-id" binding:"required"`
Scale string `json:"scale" binding:"required"`
ImageWidth string `json:"image-width" binding:"required"`
ImageHeight string `json:"image-height" binding:"required"`
Color bool `json:"color"`
Gpu bool `json:"gpu"`
// had to make exported, due to https://github.com/gin-gonic/gin/pull/123
// waiting for this to get merged into master branch, since go get
// pulls from master branch.
Configuration Configuration
}
// Create a new classifier. If you don't use this, you must set the
// embedded ElasticThoughtDoc Type field.
func NewClassifier(c Configuration) *Classifier {
return &Classifier{
ElasticThoughtDoc: ElasticThoughtDoc{Type: DOC_TYPE_CLASSIFIER},
Configuration: c,
}
}
// Insert into database (only call this if you know it doesn't arleady exist,
// or else you'll end up w/ unwanted dupes)
func (c *Classifier) Insert() error {
db := c.Configuration.DbConnection()
id, rev, err := db.Insert(c)
if err != nil {
err := fmt.Errorf("Error inserting classifier: %v. Err: %v", c, err)
return err
}
c.Id = id
c.Revision = rev
return nil
}
func (c *Classifier) SetSpecificationUrl(specUrlCbfs string) error {
updater := func(classifier *Classifier) {
classifier.SpecificationUrl = specUrlCbfs
}
doneMetric := func(classifier Classifier) bool {
return classifier.SpecificationUrl == specUrlCbfs
}
if _, err := c.casUpdate(updater, doneMetric); err != nil {
return err
}
return nil
}
func (c *Classifier) casUpdate(updater func(*Classifier), doneMetric func(Classifier) bool) (bool, error) {
db := c.Configuration.DbConnection()
genUpdater := func(classifierPtr interface{}) {
cjp := classifierPtr.(*Classifier)
updater(cjp)
}
genDoneMetric := func(classifierPtr interface{}) bool {
cjp := classifierPtr.(*Classifier)
return doneMetric(*cjp)
}
refresh := func(classifierPtr interface{}) error {
cjp := classifierPtr.(*Classifier)
return cjp.RefreshFromDB(db)
}
return casUpdate(db, c, genUpdater, genDoneMetric, refresh)
}
func (c *Classifier) RefreshFromDB(db couch.Database) error {
classifier := Classifier{}
err := db.Retrieve(c.Id, &classifier)
if err != nil {
logg.LogTo("CLASSIFIER", "Error getting latest: %v", err)
return err
}
*c = classifier
return nil
}
// Find a classifier in the db with the given id,
// or return an error if not found
func (c *Classifier) Find(id string) error {
db := c.Configuration.DbConnection()
c.Id = id
if err := c.RefreshFromDB(db); err != nil {
return err
}
return nil
}
func (c Classifier) Validate() error {
logg.LogTo("CLASSIFIER", "Validate training job")
if err := c.validateTrainingJob(); err != nil {
return err
}
logg.LogTo("CLASSIFIER", "Validate classifier net")
if err := c.validateClassifierNet(); err != nil {
return err
}
logg.LogTo("CLASSIFIER", "Validation passed")
return nil
}
func (c Classifier) validateTrainingJob() error {
trainingJob := NewTrainingJob(c.Configuration)
err := trainingJob.Find(c.TrainingJobID)
if err != nil {
return err
}
return nil
}
// make sure the specification url points to a valid prototxt file
func (c Classifier) validateClassifierNet() error {
_, err := c.classifierNet()
if err != nil {
return err
}
return nil
}
// read the classifier prototxt and create protobuf struct and return
func (c Classifier) classifierNet() (*caffe.NetParameter, error) {
specContents, err := c.getClassifierPrototxt()
if err != nil {
return nil, fmt.Errorf("Error getting classifier prototxt content. Err: %v", err)
}
// read into object with protobuf (must have already generated go protobuf code)
netParam := &caffe.NetParameter{}
if err := proto.UnmarshalText(string(specContents), netParam); err != nil {
logg.LogTo("CLASSIFIER", "Error unmarshalling %v", string(specContents))
return nil, err
}
return netParam, nil
}
// read raw classifier prototxt from url and return bytes
func (c Classifier) getClassifierPrototxt() ([]byte, error) {
resp, err := http.Get(c.SpecificationUrl)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return bytes, nil
}
func (c Classifier) getTrainingJob() (*TrainingJob, error) {
trainingJob := NewTrainingJob(c.Configuration)
err := trainingJob.Find(c.TrainingJobID)
return trainingJob, err
}