-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
129 lines (103 loc) · 3.54 KB
/
main.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
package main
import (
"context"
"fmt"
"os"
"strconv"
"github.com/project-flogo/contrib/activity/log"
"github.com/project-flogo/contrib/trigger/rest"
"github.com/project-flogo/core/activity"
"github.com/project-flogo/core/api"
"github.com/project-flogo/core/data/coerce"
"github.com/project-flogo/core/engine"
"github.com/project-flogo/ml/activity/inference"
)
var (
httpport = os.Getenv("HTTPPORT")
)
func main() {
app := myApp()
e, err := api.NewEngine(app)
if err != nil {
fmt.Println("Error:", err)
return
}
engine.RunEngine(e)
}
func myApp() *api.App {
app := api.NewApp()
// Convert the HTTPPort to an integer
port, err := strconv.Atoi(httpport)
if err != nil {
fmt.Println("Error:", err)
return app
}
// Defining the trigger used - in this case the REST Trigger
trg := app.NewTrigger(&rest.Trigger{}, &rest.Settings{Port: port})
h, _ := trg.NewHandler(&rest.HandlerSettings{Method: "POST", Path: "/gaussian"})
h.NewAction(RunActivities)
// making a map of al activities to use, in this case the Inference and log activities
//store in map to avoid activity instance recreation
infAct, _ := api.NewActivity(&inference.Activity{})
logAct, _ := api.NewActivity(&log.Activity{})
activities = map[string]activity.Activity{"inference": infAct,"log":logAct}
return app
}
var activities map[string]activity.Activity
func RunActivities(ctx context.Context, inputs map[string]interface{}) (map[string]interface{}, error) {
// logging
msg:="Making an inference"
_, err := api.EvalActivity(activities["log"], &log.Input{Message: msg})
// Basic inputs for the inference activity
modelPath := "Archive_simpleCNN.zip"
framework := "Tensorflow"
// features is the array that will be used to hold the input feature set that we pass into the inference activity
var features []interface{}
inp:=inputs["content"].(map[string]interface{})
// So I read in the list of gaussin arrays from the POST request, but I need to convert them to the form the CNN model expects
// (for an array of shape [x,10] to a tensor of shape [x,10,1,1] where x is the number of gaussians sent),
// while the inference activity is taking them in as []interfaces{} at each layer
d := inp["Input"].([]interface{})
fmt.Println(d)
var datafeat [][][][]float32
for _, row := range d { // looping over each gaussian sent
row2 := row.([]interface{})
var gaus [][][]float32
for _, item := range row2 { // looping over each item in the gaussian
var it [][]float32 //Adding one more dimension
var barebracket []float32 // and again
barebracket=append(barebracket,float32(item.(float64)))
it=append(it,barebracket)
gaus = append(gaus, it)
}
datafeat = append(datafeat, gaus)
}
// Now append the input feature with the name X to the features array. This will be passed into
// the inference activity.
features = append(features, map[string]interface{}{
"name": "X",
"data": datafeat,
})
// Running Inference Activity with appropriate inputs
out, err := api.EvalActivity(
activities["inference"], &inference.Input{
Model:modelPath,
Features:features,
Framework:framework,
SigDefName:"serving_default",
Tag:"serve",
},
)
if err != nil {
return nil, err
}
mapPred := out["result"].(map[string]interface{})
response := make(map[string]interface{})
response["output"] = mapPred
//logging
str,_:=coerce.ToString(mapPred)
msg2:="Inference finished, output:"+str
_, err = api.EvalActivity(activities["log"], &log.Input{Message: msg2})
reply := &rest.Reply{Code: 200, Data: response}
return reply.ToMap(), nil
}