forked from taherfattahi/dnn-distance-line-protection-zone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·143 lines (120 loc) · 3.17 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
package main
/*
#cgo CFLAGS: -I ./include
//points to the right platform version of tflite libs
#cgo arm LDFLAGS: -L arm
#cgo darwin LDFLAGS: -L macosx
#cgo x86_64 LDFLAGS: -L x86_64
#cgo LDFLAGS: -ltensorflowlite_c
//Raspberry Pi needs to include libatomic when linking w/ tflite
#cgo arm LDFLAGS: -latomic
#include "tensorflow/lite/c/c_api.h"
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"sync"
"unsafe"
)
type TFGan struct {
modelName *C.char
model *C.TfLiteModel
options *C.TfLiteInterpreterOptions
runner *C.TfLiteInterpreter
input *C.TfLiteTensor
inputBuffer []float32
output *C.TfLiteTensor
mutex sync.Mutex
}
func makeTFGan(modelName string) *TFGan {
version := C.TfLiteVersion()
fmt.Printf("Tensorflow Version: %v\n", C.GoString(version))
name := C.CString(modelName)
model := C.TfLiteModelCreateFromFile(name)
if model == nil {
fmt.Printf("failed to create model from - %v\n", C.GoString(name))
return nil
}
options := C.TfLiteInterpreterOptionsCreate()
if options == nil {
fmt.Printf("failed to create options for %v\n", modelName)
return nil
}
C.TfLiteInterpreterOptionsSetNumThreads(options, C.int32_t(4))
runner := C.TfLiteInterpreterCreate(model, options)
if runner == nil {
fmt.Printf("failed to create interperter for %v\n", modelName)
return nil
}
C.TfLiteInterpreterAllocateTensors(runner)
input := C.TfLiteInterpreterGetInputTensor(runner, 0)
if input == nil {
fmt.Printf("input tensor is empty\n")
return nil
}
output := C.TfLiteInterpreterGetOutputTensor(runner, 0)
if output == nil {
fmt.Printf("putput tensor is empty\n")
return nil
}
return &TFGan{
modelName: name,
model: model,
options: options,
runner: runner,
input: input,
output: output,
inputBuffer: []float32{},
}
}
func (gan *TFGan) free() {
C.TfLiteInterpreterDelete(gan.runner)
C.TfLiteModelDelete(gan.model)
C.free(unsafe.Pointer(gan.modelName))
}
func main() {
gan := makeTFGan("model/tfliteModel.tflite")
if gan == nil {
fmt.Printf("failed \n")
return
}
// input your data
// x_sample = scaler.transform([[-12, -1]])
// x_sample = scaler.transform([[14, -1]])
// x_sample = scaler.transform([[10, -5]])
// x_sample = scaler.transform([[6, -2.5]])
// # result nearest to 1 is our array index
// 0 => 0
// 1.03 => 1
// 0.23 => 2
// 0.43 => 3
// 0.03 => 4
// inputData := []float32{ -0.70504665, -0.47355217 }
// inputData := []float32{ 0.74449456, -0.47355217 }
// inputData := []float32{ 0.52148825, -0.8003357 }
inputData := []float32{ 0.2984819, -0.596096 }
ptr1 := C.TfLiteTensorData(gan.input)
if ptr1 == nil {
fmt.Errorf("bad tensor")
}
n1 := uint(C.TfLiteTensorByteSize(gan.input)) / 4
to := (*((*[1<<29 - 1]float32)(ptr1)))[:n1]
copy(to, inputData)
if C.TfLiteInterpreterInvoke(gan.runner) != C.kTfLiteOk {
fmt.Printf("failed to run\n")
}
ptr := C.TfLiteTensorData(gan.output)
if ptr == nil {
fmt.Errorf("bad tensor")
}
n := uint(C.TfLiteTensorByteSize(gan.output)) / 4
result := (*((*[1<<29 - 1]float32)(ptr)))[:n]
fmt.Println(result)
// # result nearest to 1 is our array index
// 0 => 0
// 1.03 => 1
// 0.23 => 2
// 0.43 => 3
// 0.03 => 4
}