forked from rai-project/dlframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
298 lines (268 loc) · 7.58 KB
/
model.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package dlframework
import (
"os"
"path/filepath"
"sort"
"strings"
"github.com/Masterminds/semver"
"github.com/unknwon/com"
"github.com/pkg/errors"
"golang.org/x/sync/syncmap"
"github.com/c3sr/config"
yaml "gopkg.in/yaml.v2"
)
var modelRegistry = syncmap.Map{}
var DefaultModelElementType string = "float32"
func (model ModelManifest) GetElementType() string {
modelInputs := model.GetInputs()
typeParameters := modelInputs[0].GetParameters()
if typeParameters == nil {
return DefaultModelElementType
}
pet, ok := typeParameters["element_type"]
if !ok {
return DefaultModelElementType
}
petVal := pet.Value
if petVal == "" {
return DefaultModelElementType
}
var val string
if err := yaml.Unmarshal([]byte(petVal), &val); err != nil {
log.Errorf("unable to get element type %v as a string", petVal)
return DefaultModelElementType
}
return strings.ToLower(val)
}
func (model ModelManifest) GetInputDimensions() ([]int, error) {
modelInputs := model.GetInputs()
typeParameters := modelInputs[0].GetParameters()
if typeParameters == nil {
return nil, errors.New("invalid type parameters")
}
pdims, ok := typeParameters["dimensions"]
if !ok {
log.Debug("arbitrary input dimensions")
return nil, nil
}
pdimsVal := pdims.GetValue()
if pdimsVal == "" {
return nil, errors.New("invalid input dimensions")
}
var dims []int
if err := yaml.Unmarshal([]byte(pdimsVal), &dims); err != nil {
return nil, errors.Errorf("unable to get input dimensions %v as an integer slice", pdimsVal)
}
return dims, nil
}
func (m *ModelManifest) Modality() (Modality, error) {
inputs := m.GetInputs()
output := m.GetOutput()
if len(inputs) == 0 {
return "", errors.New("no input is specified")
}
input := inputs[0]
switch strings.ToLower(input.Type) {
case "image":
switch strings.ToLower(output.Type) {
case "image":
return ImageEnhancementModality, nil
case "classification":
return ImageClassificationModality, nil
case "boundingbox":
return ImageObjectDetectionModality, nil
case "semanticsegment":
return ImageSemanticSegmentationModality, nil
case "instancesegment":
return ImageInstanceSegmentationModality, nil
}
case "raw":
return RawModality, nil
}
panic("unhandled modality")
return UnknownModality, nil
}
func (m *ModelManifest) Validate() error {
name := m.GetName()
if name == "" {
return errors.New("model name cannot be empty")
}
version := m.GetVersion()
if version == "" {
version = "latest"
}
if version != "latest" {
if _, err := semver.NewVersion(version); err != nil {
return errors.Wrapf(err,
"the version %s for the model %s is not in semantic version format",
m.GetVersion(),
m.GetName(),
)
}
}
return nil
}
func (m ModelManifest) MustCanonicalName() string {
s, err := m.CanonicalName()
if err != nil {
log.WithField("model_name", m.GetName()).Fatal("unable to get model canonical name")
return ""
}
return s
}
func (m ModelManifest) CanonicalName() (string, error) {
if m.GetFramework() == nil {
return "", errors.Errorf("the model %s does not have a valid framework", m.GetName())
}
framework, err := m.ResolveFramework()
if err != nil {
return "", err
}
frameworkName, err := framework.CanonicalName()
if err != nil {
return "", errors.Wrapf(err, "cannot get canonical name for the framework %s and model %s in the registry", m.GetFramework().GetName(), m.GetName())
}
fm, ok := frameworkRegistry.Load(frameworkName)
if !ok {
return "", errors.Wrapf(err, "cannot get framework %s for model %s in the registry", frameworkName, m.GetName())
}
if _, ok := fm.(FrameworkManifest); !ok {
return "", errors.Errorf("invalid framework %s registered for model %s in the registry", frameworkName, m.GetName())
}
modelName := CleanString(m.GetName())
if modelName == "" {
return "", errors.New("model name must not be empty")
}
modelVersion := CleanString(m.GetVersion())
if modelVersion == "" {
modelVersion = "latest"
}
return frameworkName + "/" + modelName + ":" + modelVersion, nil
}
func (m ModelManifest) FrameworkConstraint() (*semver.Constraints, error) {
return semver.NewConstraint(m.GetFramework().GetVersion())
}
func (m ModelManifest) MustResolveFramework() FrameworkManifest {
f, err := m.ResolveFramework()
if err != nil {
panic(err)
}
return f
}
func (m ModelManifest) ResolveFramework() (FrameworkManifest, error) {
frameworks, err := Frameworks()
if err != nil {
return FrameworkManifest{}, err
}
if len(frameworks) == 0 {
return FrameworkManifest{}, errors.New("framework not found")
}
if m.GetFramework().GetVersion() == "lastest" {
sort.Slice(frameworks, func(ii, jj int) bool {
f1, _ := semver.NewVersion(frameworks[ii].GetVersion())
f2, _ := semver.NewVersion(frameworks[jj].GetVersion())
return f1.LessThan(f2)
})
return frameworks[len(frameworks)-1], nil
}
modelFrameworkConstraint, err := m.FrameworkConstraint()
if err != nil {
return FrameworkManifest{},
errors.Wrapf(err, "cannot get framework constraints for model %v", m.GetName())
}
filtered := []FrameworkManifest{}
for _, framework := range frameworks {
frameworkVersion, err := semver.NewVersion(framework.GetVersion())
if err != nil {
continue
}
if !modelFrameworkConstraint.Check(frameworkVersion) {
continue
}
filtered = append(filtered, framework)
}
if len(frameworks) == 0 {
return FrameworkManifest{}, errors.New("framework not found")
}
sort.Slice(filtered, func(ii, jj int) bool {
f1, _ := semver.NewVersion(filtered[ii].GetVersion())
f2, _ := semver.NewVersion(filtered[jj].GetVersion())
return f1.LessThan(f2)
})
return filtered[len(filtered)-1], nil
}
func (m ModelManifest) Register() error {
n, err := m.CanonicalName()
if err != nil {
return err
}
return m.RegisterNamed(n)
}
func (m ModelManifest) RegisterNamed(s string) error {
name := strings.ToLower(s)
if _, ok := modelRegistry.Load(name); ok {
return errors.Errorf("the %s model has already been registered", s)
}
modelRegistry.Store(s, m)
return nil
}
func RegisteredModelNames() []string {
return syncMapKeys(&modelRegistry)
}
func Models() ([]ModelManifest, error) {
names := RegisteredModelNames()
models := make([]ModelManifest, len(names))
for ii, name := range names {
m, ok := modelRegistry.Load(name)
if !ok {
return nil, errors.Errorf("model %s was not found", name)
}
model, ok := m.(ModelManifest)
if !ok {
return nil, errors.Errorf("model %s was found but not of type ModelManifest", name)
}
models[ii] = model
}
sort.Slice(models, func(ii, jj int) bool {
return models[ii].GetName() < models[jj].GetName()
})
return models, nil
}
func FindModel(name string) (*ModelManifest, error) {
var model *ModelManifest
modelRegistry.Range(func(key0 interface{}, value interface{}) bool {
key, ok := key0.(string)
if !ok {
return true
}
if key != name {
return true
}
m, ok := value.(ModelManifest)
if !ok {
return true
}
model = &m
return false
})
if model == nil {
return nil, errors.Errorf("model %s not found in registry", name)
}
return model, nil
}
func (m *ModelManifest) WorkDir() (string, error) {
cannonicalName, err := m.CanonicalName()
if err != nil {
return "", err
}
cannonicalName = strings.Replace(cannonicalName, ":", "_", -1)
cannonicalName = strings.Replace(cannonicalName, " ", "_", -1)
cannonicalName = strings.Replace(cannonicalName, "-", "_", -1)
workDir := filepath.Join(config.App.TempDir, "dlframework", cannonicalName)
if !com.IsDir(workDir) {
if err := os.MkdirAll(workDir, 0700); err != nil {
return "", errors.Wrapf(err, "failed to create model work directory %v", workDir)
}
}
return workDir, nil
}