/
main.go
181 lines (153 loc) · 4.12 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
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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"sort"
"github.com/tensorflow/tensorflow/tensorflow/go"
"github.com/tensorflow/tensorflow/tensorflow/go/op"
)
const (
graphFile = "/model/tensorflow_inception_graph.pb"
labelsFile = "/model/imagenet_comp_graph_label_strings.txt"
)
// Label type
type Label struct {
Label string `json:"label"`
Probability float32 `json:"probability"`
}
// Labels type
type Labels []Label
func (a Labels) Len() int { return len(a) }
func (a Labels) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a Labels) Less(i, j int) bool { return a[i].Probability > a[j].Probability }
func main() {
os.Setenv("TF_CPP_MIN_LOG_LEVEL", "2")
if len(os.Args) < 2 {
log.Fatalf("usage: imgrecognition <image_url>")
}
fmt.Printf("url: %s\n", os.Args[1])
// Get image from URL
response, e := http.Get(os.Args[1])
if e != nil {
log.Fatalf("unable to get image from url: %v", e)
}
defer response.Body.Close()
modelGraph, labels, err := loadModel()
if err != nil {
log.Fatalf("unable to load model: %v", err)
}
// Get normalized tensor
tensor, err := normalizeImage(response.Body)
if err != nil {
log.Fatalf("unable to make a tensor from image: %v", err)
}
// Create a session for inference over modelGraph
session, err := tensorflow.NewSession(modelGraph, nil)
if err != nil {
log.Fatalf("could not init session: %v", err)
}
output, err := session.Run(
map[tensorflow.Output]*tensorflow.Tensor{
modelGraph.Operation("input").Output(0): tensor,
},
[]tensorflow.Output{
modelGraph.Operation("output").Output(0),
},
nil)
if err != nil {
log.Fatalf("could not run inference: %v", err)
}
res := getTopFiveLabels(labels, output[0].Value().([][]float32)[0])
for _, l := range res {
fmt.Printf("label: %s, probability: %.2f%%\n", l.Label, l.Probability*100)
}
}
func loadModel() (*tensorflow.Graph, []string, error) {
// Load inception model
model, err := ioutil.ReadFile(graphFile)
if err != nil {
return nil, nil, err
}
graph := tensorflow.NewGraph()
if err := graph.Import(model, ""); err != nil {
return nil, nil, err
}
// Load labels
labelsFile, err := os.Open(labelsFile)
if err != nil {
return nil, nil, err
}
defer labelsFile.Close()
scanner := bufio.NewScanner(labelsFile)
var labels []string
for scanner.Scan() {
labels = append(labels, scanner.Text())
}
return graph, labels, scanner.Err()
}
func getTopFiveLabels(labels []string, probabilities []float32) []Label {
var resultLabels []Label
for i, p := range probabilities {
if i >= len(labels) {
break
}
resultLabels = append(resultLabels, Label{Label: labels[i], Probability: p})
}
sort.Sort(Labels(resultLabels))
return resultLabels[:5]
}
func normalizeImage(body io.ReadCloser) (*tensorflow.Tensor, error) {
var buf bytes.Buffer
io.Copy(&buf, body)
tensor, err := tensorflow.NewTensor(buf.String())
if err != nil {
return nil, err
}
graph, input, output, err := getNormalizedGraph()
if err != nil {
return nil, err
}
session, err := tensorflow.NewSession(graph, nil)
if err != nil {
return nil, err
}
normalized, err := session.Run(
map[tensorflow.Output]*tensorflow.Tensor{
input: tensor,
},
[]tensorflow.Output{
output,
},
nil)
if err != nil {
return nil, err
}
return normalized[0], nil
}
// Creates a graph to decode, rezise and normalize an image
func getNormalizedGraph() (graph *tensorflow.Graph, input, output tensorflow.Output, err error) {
s := op.NewScope()
input = op.Placeholder(s, tensorflow.String)
// 3 return RGB image
decode := op.DecodeJpeg(s, input, op.DecodeJpegChannels(3))
// Sub: returns x - y element-wise
output = op.Sub(s,
// make it 224x224: inception specific
op.ResizeBilinear(s,
// inserts a dimension of 1 into a tensor's shape.
op.ExpandDims(s,
// cast image to float type
op.Cast(s, decode, tensorflow.Float),
op.Const(s.SubScope("make_batch"), int32(0))),
op.Const(s.SubScope("size"), []int32{224, 224})),
// mean = 117: inception specific
op.Const(s.SubScope("mean"), float32(117)))
graph, err = s.Finalize()
return graph, input, output, err
}