Skip to content

Commit 70017ef

Browse files
cchung100msrkreddy1238
authored andcommitted
[Golang][Doc] improve the samples and doc (apache#4385)
* [Golang][Doc] improve the samples and doc * [Golang][Doc] add asf header * [Golang][Doc] Improve the end to end example * [Golang][Doc] Improve the end to end example
1 parent 030a163 commit 70017ef

File tree

4 files changed

+98
-5
lines changed

4 files changed

+98
-5
lines changed

golang/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ To Demonstrates sample TVM module compilation using python and deploy via golang
6868

6969
To deploy a realtime module with lib, graph and param.
7070
```bash
71+
python3 gen_mobilenet_lib.py
72+
7173
./complex
7274
```
7375

@@ -80,13 +82,13 @@ To demonstrate go function closure conversion to packed function handle.
8082
To demonstrate a packed function handle given as an argument.
8183

8284
```bash
83-
pack_func_handle_arg
85+
./pack_func_handle_arg
8486
```
8587

8688
To register go function with runtime as a global function.
8789

8890
```bash
89-
pack_func_register
91+
./pack_func_register
9092
```
9193

9294
To demonstrate function closure passed as argument to a function call.
@@ -120,5 +122,5 @@ Please refer ```docker/install/ubuntu_install_golang.sh``` for the packages depe
120122
go compiler 1.10 on ubuntu doesn't install on standard path, hence an explicit export may be needed as shown below.
121123

122124
```bash
123-
export PATH="/usr/lib/go-1.10/bin:$PATH"```
125+
export PATH="/usr/lib/go-1.10/bin:$PATH"
124126
```

golang/sample/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ all: $(EXECUTABLE)
3131
@go tool compile -pack -o $@ $<
3232

3333
clean:
34-
@rm -f $(EXECUTABLE) *.so *.o *.a
34+
@rm -f $(EXECUTABLE) *.so *.o *.a *.json *.params

golang/sample/complex.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func main() {
8989

9090
// Array allocation attributes
9191
tshapeIn := []int64{1, 224, 224, 3}
92-
tshapeOut := []int64{1, 1001}
92+
tshapeOut := []int64{1, 1000}
9393

9494
// Allocate input Array
9595
inX, err := gotvm.Empty(tshapeIn, "float32", gotvm.CPU(0))

golang/sample/gen_mobilenet_lib.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
import os
19+
from tvm import relay
20+
from tvm.contrib.download import download_testdata
21+
import tflite.Model
22+
23+
24+
################################################
25+
# Utils for downloading and extracting zip files
26+
# ----------------------------------------------
27+
def extract(path):
28+
import tarfile
29+
if path.endswith("tgz") or path.endswith("gz"):
30+
dir_path = os.path.dirname(path)
31+
tar = tarfile.open(path)
32+
tar.extractall(path=dir_path)
33+
tar.close()
34+
else:
35+
raise RuntimeError('Could not decompress the file: ' + path)
36+
37+
38+
###################################
39+
# Download TFLite pre-trained model
40+
# ---------------------------------
41+
42+
model_url = "https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.4_224.tgz"
43+
model_path = download_testdata(model_url, "mobilenet_v2_1.4_224.tgz", module=['tf', 'official'])
44+
model_dir = os.path.dirname(model_path)
45+
extract(model_path)
46+
47+
# now we have mobilenet_v2_1.4_224.tflite on disk
48+
model_file = os.path.join(model_dir, "mobilenet_v2_1.4_224.tflite")
49+
50+
# get TFLite model from buffer
51+
tflite_model_buf = open(model_file, "rb").read()
52+
tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)
53+
54+
55+
##############################
56+
# Load Neural Network in Relay
57+
# ----------------------------
58+
59+
# TFLite input tensor name, shape and type
60+
input_tensor = "input"
61+
input_shape = (1, 224, 224, 3)
62+
input_dtype = "float32"
63+
64+
# parse TFLite model and convert into Relay computation graph
65+
mod, params = relay.frontend.from_tflite(tflite_model,
66+
shape_dict={input_tensor: input_shape},
67+
dtype_dict={input_tensor: input_dtype})
68+
69+
#############
70+
# Compilation
71+
# -----------
72+
73+
target = 'llvm'
74+
75+
# Build with Relay
76+
with relay.build_config(opt_level=3):
77+
graph, lib, params = relay.build_module.build(
78+
mod, target, params=params)
79+
80+
###############################################
81+
# Save the graph, lib and parameters into files
82+
# ---------------------------------------------
83+
84+
lib.export_library("./mobilenet.so")
85+
print('lib export succeefully')
86+
87+
with open("./mobilenet.json", "w") as fo:
88+
fo.write(graph)
89+
90+
with open("./mobilenet.params", "wb") as fo:
91+
fo.write(relay.save_param_dict(params))

0 commit comments

Comments
 (0)