/
image_classification_predictor.go
149 lines (124 loc) · 3.92 KB
/
image_classification_predictor.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
package predictor
import (
"context"
"strings"
"github.com/k0kubun/pp"
"github.com/pkg/errors"
"github.com/rai-project/caffe"
"github.com/rai-project/config"
"github.com/rai-project/dlframework"
"github.com/rai-project/dlframework/framework/agent"
"github.com/rai-project/dlframework/framework/options"
common "github.com/rai-project/dlframework/framework/predictor"
"github.com/rai-project/tracer"
"github.com/rai-project/tracer/ctimer"
gotensor "gorgonia.org/tensor"
)
type ImageClassificationPredictor struct {
*ImagePredictor
probabilitiesLayerIndex int
probabilities interface{}
}
// New ...
func NewImageClassificationPredictor(model dlframework.ModelManifest, opts ...options.Option) (common.Predictor, error) {
ctx := context.Background()
span, ctx := tracer.StartSpanFromContext(ctx, tracer.APPLICATION_TRACE, "new_predictor")
defer span.Finish()
modelInputs := model.GetInputs()
if len(modelInputs) != 1 {
return nil, errors.New("number of inputs not supported")
}
firstInputType := modelInputs[0].GetType()
if strings.ToLower(firstInputType) != "image" {
return nil, errors.New("input type not supported")
}
predictor := new(ImageClassificationPredictor)
return predictor.Load(ctx, model, opts...)
}
func (self *ImageClassificationPredictor) Load(ctx context.Context, modelManifest dlframework.ModelManifest, opts ...options.Option) (common.Predictor, error) {
pred, err := self.ImagePredictor.Load(ctx, modelManifest, opts...)
if err != nil {
return nil, err
}
p := &ImageClassificationPredictor{
ImagePredictor: pred,
}
p.probabilitiesLayerIndex, err = p.GetOutputLayerIndex("probabilities_layer")
if err != nil {
return nil, errors.Wrap(err, "failed to get the probabilities layer index")
}
return p, nil
}
// Predict ...
func (p *ImageClassificationPredictor) Predict(ctx context.Context, data interface{}, opts ...options.Option) error {
span, ctx := tracer.StartSpanFromContext(ctx, tracer.APPLICATION_TRACE, "predict")
defer span.Finish()
if p.TraceLevel() >= tracer.FRAMEWORK_TRACE {
err := p.predictor.StartProfiling("caffe", "c_predict")
if err != nil {
log.WithError(err).WithField("framework", "caffe").Error("unable to start framework profiling")
} else {
defer func() {
p.predictor.EndProfiling()
profBuffer, err := p.predictor.ReadProfile()
if err != nil {
pp.Println(err)
return
}
t, err := ctimer.New(profBuffer)
if err != nil {
panic(err)
}
t.Publish(ctx, tracer.FRAMEWORK_TRACE)
p.predictor.DisableProfiling()
}()
}
}
if data == nil {
return errors.New("input data nil")
}
input, ok := data.([]*gotensor.Dense)
if !ok {
return errors.New("input data is not slice of dense tensors")
}
err := p.predictor.SetInput(0, input[0].Float32s())
if err != nil {
return errors.Wrapf(err, "failed to set input")
}
err = p.predictor.Predict(ctx)
if err != nil {
return errors.Wrapf(err, "failed to perform Predict")
}
return nil
}
// ReadPredictedFeatures ...
func (p *ImageClassificationPredictor) ReadPredictedFeatures(ctx context.Context) ([]dlframework.Features, error) {
span, ctx := tracer.StartSpanFromContext(ctx, tracer.APPLICATION_TRACE, "read_predicted_features")
defer span.Finish()
output, err := p.predictor.ReadOutputData(ctx, p.probabilitiesLayerIndex)
if err != nil {
return nil, err
}
labels, err := p.GetLabels()
if err != nil {
return nil, errors.Wrap(err, "cannot get the labels")
}
return p.CreateClassificationFeatures(ctx, output, labels)
}
func (p ImageClassificationPredictor) Modality() (dlframework.Modality, error) {
return dlframework.ImageClassificationModality, nil
}
func init() {
config.AfterInit(func() {
framework := caffe.FrameworkManifest
agent.AddPredictor(framework, &ImageClassificationPredictor{
ImagePredictor: &ImagePredictor{
ImagePredictor: common.ImagePredictor{
Base: common.Base{
Framework: framework,
},
},
},
})
})
}