-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.go
346 lines (297 loc) · 8.49 KB
/
utils.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package utils
// #include <stdlib.h>
// #cgo LDFLAGS: -ltensorflow
// #cgo CFLAGS: -I${SRCDIR}/../../tensorflow/tensorflow
// #include "tensorflow/c/c_api.h"
import "C"
import (
"bufio"
"bytes"
"fmt"
"image"
"image/color"
"image/png"
"io/ioutil"
"log"
"os"
"reflect"
"unsafe"
"github.com/disintegration/imaging"
imagetypes "github.com/rai-project/image/types"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
"github.com/tensorflow/tensorflow/tensorflow/go/op"
"golang.org/x/image/colornames"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
)
// DRAWING UTILITY FUNCTIONS
// HLine draws a horizontal line
func HLine(img *image.RGBA, x1, y, x2 int, col color.Color) {
for ; x1 <= x2; x1++ {
img.Set(x1, y, col)
}
}
// VLine draws a veritcal line
func VLine(img *image.RGBA, x, y1, y2 int, col color.Color) {
for ; y1 <= y2; y1++ {
img.Set(x, y1, col)
}
}
// Rect draws a rectangle utilizing HLine() and VLine()
func Rect(img *image.RGBA, x1, y1, x2, y2, width int, col color.Color) {
for i := 0; i < width; i++ {
HLine(img, x1, y1+i, x2, col)
HLine(img, x1, y2+i, x2, col)
VLine(img, x1+i, y1, y2, col)
VLine(img, x2+i, y1, y2, col)
}
}
// Segment draws a rectangle utilizing HLine() and VLine()
func Segment(img *image.RGBA, mask [][]float32, col color.Color, x1, y1, x2, y2 float32) *image.RGBA {
height := len(mask)
width := len(mask[0])
seg := image.NewRGBA(image.Rect(0, 0, width, height))
for ii := 0; ii < height; ii++ {
for jj := 0; jj < width; jj++ {
if mask[ii][jj] > 0.2 {
seg.Set(jj, ii, col)
}
}
}
segScaled := imaging.Resize(seg, int(x2)-int(x1), int(y2)-int(y1), imaging.NearestNeighbor)
out, _ := os.Create("/tmp/test.png")
defer out.Close()
err := png.Encode(out, segScaled)
if err != nil {
log.Println(err)
}
overlay := imaging.Overlay(img, segScaled, image.Pt(int(x1), int(y1)), 0.5)
rgba := &image.RGBA{
Pix: overlay.Pix,
Stride: overlay.Stride,
Rect: overlay.Rect,
}
return rgba
}
func ToPng(filePath string, imgByte []byte, bounds image.Rectangle) {
img := imagetypes.NewRGBImage(bounds)
copy(img.Pix, imgByte)
out, _ := os.Create(filePath)
defer out.Close()
err := png.Encode(out, img.ToRGBAImage())
if err != nil {
log.Println(err)
}
}
// LABEL UTILITY FUNCTIONS
func LoadLabels(labelsFile string) []string {
var labels []string
file, err := os.Open(labelsFile)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
labels = append(labels, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Printf("ERROR: failed to read %s: %v", labelsFile, err)
}
return labels
}
func GetLabel(idx int, probabilities []float32, classes []float32, labels []string) string {
index := int(classes[idx])
label := fmt.Sprintf("%s (%2.0f%%)", labels[index], probabilities[idx]*100.0)
return label
}
func AddLabel(img *image.RGBA, x, y, class int, label string) {
col := colornames.Map[colornames.Names[class]]
point := fixed.Point26_6{fixed.Int26_6(x * 64), fixed.Int26_6(y * 64)}
d := &font.Drawer{
Dst: img,
Src: image.NewUniform(colornames.Black),
Face: basicfont.Face7x13,
Dot: point,
}
Rect(img, x, y-13, (x + len(label)*7), y-6, 7, col)
d.DrawString(label)
}
// TENSOR UTILITY FUNCTIONS
func DecodeJpegGraph() (graph *tf.Graph, input, output tf.Output, err error) {
s := op.NewScope()
input = op.Placeholder(s, tf.String)
dctMethod := op.DecodeJpegDctMethod("INTEGER_ACCURATE")
output =
op.ExpandDims(s,
op.DecodeJpeg(s, input,
op.DecodeJpegChannels(3), dctMethod),
op.Const(s.SubScope("make_batch"), int32(0)))
graph, err = s.Finalize()
return graph, input, output, err
}
func DecodeJpegNormalizeGraph(height int32, width int32) (graph *tf.Graph, input, output tf.Output, err error) {
s := op.NewScope()
input = op.Placeholder(s, tf.String)
dctMethod := op.DecodeJpegDctMethod("INTEGER_ACCURATE")
output =
op.Cast(s,
op.ResizeBilinear(s,
op.ExpandDims(s,
op.DecodeJpeg(s, input, op.DecodeJpegChannels(3), dctMethod),
op.Const(s.SubScope("make_batch"), int32(0))),
op.Const(s.SubScope("size"), []int32{height, width})),
tf.Uint8)
graph, err = s.Finalize()
return graph, input, output, err
}
func MakeTensorFromImage(filename string) (*tf.Tensor, image.Image, error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, nil, err
}
r := bytes.NewReader(b)
img, _, err := image.Decode(r)
if err != nil {
return nil, nil, err
}
// DecodeJpeg uses a scalar String-valued tensor as input.
tensor, err := tf.NewTensor(string(b))
if err != nil {
return nil, nil, err
}
// Creates a tensorflow graph to decode the jpeg image
graph, input, output, err := DecodeJpegGraph()
if err != nil {
return nil, nil, err
}
// Execute that graph to decode this one image
session, err := tf.NewSession(graph, nil)
if err != nil {
return nil, nil, err
}
defer session.Close()
normalized, err := session.Run(
map[tf.Output]*tf.Tensor{input: tensor},
[]tf.Output{output},
nil)
if err != nil {
return nil, nil, err
}
return normalized[0], img, nil
}
func max(x, y int) int {
if x < y {
return y
}
return x
}
func MakeTensorFromResizedImage(filename string, inputSize int32) (*tf.Tensor, image.Image, int, int, error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, nil, 0, 0, err
}
r := bytes.NewReader(b)
img, _, err := image.Decode(r)
width := img.Bounds().Max.X
height := img.Bounds().Max.Y
resizeRatio := float32(inputSize) / float32(max(width, height))
targetWidth := int32(resizeRatio * float32(width))
targetHeight := int32(resizeRatio * float32(height))
// DecodeJpeg uses a scalar String-valued tensor as input.
tensor, err := tf.NewTensor(string(b))
if err != nil {
return nil, nil, 0, 0, err
}
// Creates a tensorflow graph to decode the jpeg image
graph, input, output, err := DecodeJpegNormalizeGraph(targetHeight, targetWidth)
if err != nil {
return nil, nil, 0, 0, err
}
// Execute that graph to decode this one image
session, err := tf.NewSession(graph, nil)
if err != nil {
return nil, nil, 0, 0, err
}
defer session.Close()
normalized, err := session.Run(
map[tf.Output]*tf.Tensor{input: tensor},
[]tf.Output{output},
nil)
if err != nil {
return nil, nil, 0, 0, err
}
return normalized[0], img, int(targetWidth), int(targetHeight), nil
}
func ReshapeTensorFloats(data [][]float32, shape []int64) (*tf.Tensor, error) {
N, H, W, C := shape[0], shape[1], shape[2], shape[3]
tensor := make([][][][]float32, N)
for n := int64(0); n < N; n++ {
ndata := data[n]
tn := make([][][]float32, H)
for h := int64(0); h < H; h++ {
th := make([][]float32, W)
for w := int64(0); w < W; w++ {
offset := C * (W*h + w)
tw := ndata[offset : offset+C]
th[w] = tw
}
tn[h] = th
}
tensor[n] = tn
}
return tf.NewTensor(tensor)
}
func TensorPtrC(t *tf.Tensor) *C.TF_Tensor {
fld := reflect.Indirect(reflect.ValueOf(t)).FieldByName("c")
if fld.CanInterface() {
return fld.Interface().(*C.TF_Tensor)
}
ptr := unsafe.Pointer(fld.UnsafeAddr())
e := (**C.TF_Tensor)(ptr)
return *e
}
func TensorData(c *C.TF_Tensor) []byte {
// See: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
cbytes := C.TF_TensorData(c)
if cbytes == nil {
return nil
}
length := int(C.TF_TensorByteSize(c))
slice := (*[1 << 30]byte)(unsafe.Pointer(cbytes))[:length:length]
return slice
}
// IMAGE PREPROCESSING UTILITY FUNCTIONS
func NormalizeImageHWC(in *image.NRGBA, mean []float32, scale float32) ([]float32, error) {
height := in.Bounds().Dy()
width := in.Bounds().Dx()
out := make([]float32, 3*height*width)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
outOffset := 3 * (y*width + x)
nrgba := in.NRGBAAt(x, y)
r, g, b := nrgba.R, nrgba.G, nrgba.B
out[outOffset+0] = (float32(r) - mean[0]) / scale
out[outOffset+1] = (float32(g) - mean[1]) / scale
out[outOffset+2] = (float32(b) - mean[2]) / scale
}
}
return out, nil
}
// SORTING UTILITY FUNCTIONS
type Predictions struct {
Indexes []int
Probabilities []float32
}
// Implement sort.Interface Len
func (s Predictions) Len() int { return len(s.Indexes) }
// Implement sort.Interface Less
func (s Predictions) Less(i, j int) bool { return s.Probabilities[i] > s.Probabilities[j] }
// Implment sort.Interface Swap
func (s Predictions) Swap(i, j int) {
// swap value
s.Probabilities[i], s.Probabilities[j] = s.Probabilities[j], s.Probabilities[i]
// swap index
s.Indexes[i], s.Indexes[j] = s.Indexes[j], s.Indexes[i]
}