-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
54 lines (46 loc) · 914 Bytes
/
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
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/sjkaliski/infer"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
)
func main() {
// Load model.
model, err := ioutil.ReadFile(os.Getenv("MODEL"))
if err != nil {
panic(err)
}
// Generate new graph.
graph := tf.NewGraph()
err = graph.Import(model, "")
if err != nil {
panic(err)
}
// Create new inference model.
m, _ := infer.New(&infer.Model{
Graph: graph,
Input: &infer.Input{
Key: "conv2d_1_input",
Dimensions: []int32{28, 28},
},
Output: &infer.Output{
Key: "output_node0",
},
})
img, err := os.Open("sample.png")
if err != nil {
panic(err)
}
defer img.Close()
opts := &infer.ImageOptions{
IsGray: true,
}
predictions, err := m.FromImage(img, opts)
if err != nil {
panic(err)
}
fmt.Println("Prediction:", predictions[0].Class)
fmt.Println("Confidence:", predictions[0].Score)
}