Skip to content

Commit 7b25b83

Browse files
committed
fix: optimize memory
1 parent 0b0be08 commit 7b25b83

File tree

1 file changed

+53
-50
lines changed

1 file changed

+53
-50
lines changed

example/gorgonia/wasm/onnx201904.go

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ package main
44

55
import (
66
"bytes"
7+
"errors"
78
"image"
8-
"log"
99
"math"
10+
"runtime"
1011
"syscall/js"
1112
"time"
1213

@@ -25,19 +26,20 @@ var (
2526
model *onnx.Model
2627
)
2728

28-
func main() {
29+
func logInfo(s interface{}) {
2930
js.Global().Get("document").
3031
Call("getElementById", "info").
31-
Set("innerHTML", "Starting the WASM program")
32+
Set("innerHTML", s)
33+
}
34+
35+
func loadFile() error {
3236
graph = gorgonnx.NewGraph()
3337
model = onnx.NewModel(graph)
3438
files := js.Global().Get("document").Call("getElementById", "knowledgeFile").Get("files")
3539
//fmt.Println("file", files)
3640
//fmt.Println("Length", files.Length())
3741
if files.Length() == 1 {
38-
js.Global().Get("document").
39-
Call("getElementById", "info").
40-
Set("innerHTML", "Reading the model from the memory of the browser")
42+
logInfo("Reading the model from the memory of the browser")
4143
//fmt.Println("Reading from uploaded file")
4244
reader := js.Global().Get("FileReader").New()
4345
reader.Call("readAsDataURL", files.Index(0))
@@ -48,61 +50,48 @@ func main() {
4850
content := reader.Get("result").String()
4951
dataURL, err := dataurl.DecodeString(content)
5052
if err != nil {
51-
js.Global().Get("document").
52-
Call("getElementById", "info").
53-
Set("innerHTML", err)
54-
return
53+
logInfo(err.Error())
54+
return err
5555
}
5656
err = model.Unmarshal(dataURL.Data)
5757
if err != nil {
58-
js.Global().Get("document").
59-
Call("getElementById", "info").
60-
Set("innerHTML", "Fatal... cannot decode model, please reload the page")
58+
logInfo("Fatal... cannot decode model, please reload the page")
59+
return err
6160
}
62-
6361
// modelonnx = dataURL.Data
6462
}
65-
js.Global().Get("document").
66-
Call("getElementById", "info").
67-
Set("innerHTML", "Ready to process!")
63+
return nil
64+
}
65+
66+
func main() {
67+
logInfo("Starting the WASM program")
68+
err := loadFile()
69+
if err != nil {
70+
logInfo(err.Error())
71+
return
72+
}
73+
runtime.GC()
74+
logInfo("Ready to process!")
6875
// Declare callback
6976
cb := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
7077
// handle event
7178
// Get the picture
72-
js.Global().Get("document").
73-
Call("getElementById", "info").
74-
Set("innerHTML", "processing element")
75-
pic := js.Global().Get("document").Call("getElementById", "canvasBox").Call("toDataURL")
76-
dataURL, err := dataurl.DecodeString(pic.String())
79+
logInfo("processing element")
80+
t, err := processImgToArray()
81+
runtime.GC()
7782
if err != nil {
78-
js.Global().Get("document").
79-
Call("getElementById", "info").
80-
Set("innerHTML", "error")
83+
logInfo(err.Error())
8184
return nil
85+
8286
}
83-
var output int
84-
if dataURL.ContentType() == "image/png" {
85-
m, _, err := image.Decode(bytes.NewReader(dataURL.Data))
86-
if err != nil {
87-
js.Global().Get("document").
88-
Call("getElementById", "info").
89-
Set("innerHTML", "error")
90-
return nil
91-
}
92-
output, err = analyze(m)
93-
if err != nil {
94-
/*js.Global().Get("document").
95-
Call("getElementById", "info").
96-
Set("innerHTML", "error")
97-
*/
98-
return nil
99-
}
87+
output, err := analyze(t)
88+
runtime.GC()
89+
if err != nil {
90+
logInfo(err.Error())
91+
return nil
10092
}
10193

102-
log.Println(output)
103-
js.Global().Get("document").
104-
Call("getElementById", "info").
105-
Set("innerHTML", output)
94+
logInfo(output)
10695
return nil
10796
})
10897
// Hook it up with a DOM event
@@ -113,13 +102,29 @@ func main() {
113102
<-c
114103
}
115104

116-
func analyze(m image.Image) (int, error) {
117-
// resize the image
105+
func processImgToArray() ([]float32, error) {
106+
pic := js.Global().Get("document").Call("getElementById", "canvasBox").Call("toDataURL")
107+
dataURL, err := dataurl.DecodeString(pic.String())
108+
if err != nil {
109+
return nil, err
110+
}
111+
if dataURL.ContentType() != "image/png" {
112+
return nil, errors.New("not a png image")
113+
}
114+
m, _, err := image.Decode(bytes.NewReader(dataURL.Data))
115+
if err != nil {
116+
return nil, err
117+
}
118+
118119
img := imaging.Resize(m, size, 0, imaging.Lanczos)
119120
t := make([]float32, size*size)
120121
for i := 0; i < size*size*4; i += 4 {
121122
t[i/4] = float32(img.Pix[i])
122123
}
124+
return t, nil
125+
}
126+
127+
func analyze(t []float32) (int, error) {
123128
T := tensor.New(tensor.WithBacking(t), tensor.WithShape(1, 1, size, size))
124129
err := gorgonnx.Let(graph.Node(model.Input[0]).(node.Node), T)
125130
if err != nil {
@@ -141,7 +146,5 @@ func analyze(m image.Image) (int, error) {
141146
val = v
142147
}
143148
}
144-
log.Println("RES", res)
145-
146149
return res, nil
147150
}

0 commit comments

Comments
 (0)