Skip to content

Commit

Permalink
[Golang][Doc] improve the samples and doc (#4385)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
cchung100m authored and srkreddy1238 committed Nov 22, 2019
1 parent 030a163 commit 70017ef
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 5 deletions.
8 changes: 5 additions & 3 deletions golang/README.md
Expand Up @@ -68,6 +68,8 @@ To Demonstrates sample TVM module compilation using python and deploy via golang

To deploy a realtime module with lib, graph and param.
```bash
python3 gen_mobilenet_lib.py

./complex
```

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

```bash
pack_func_handle_arg
./pack_func_handle_arg
```

To register go function with runtime as a global function.

```bash
pack_func_register
./pack_func_register
```

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

```bash
export PATH="/usr/lib/go-1.10/bin:$PATH"```
export PATH="/usr/lib/go-1.10/bin:$PATH"
```
2 changes: 1 addition & 1 deletion golang/sample/Makefile
Expand Up @@ -31,4 +31,4 @@ all: $(EXECUTABLE)
@go tool compile -pack -o $@ $<

clean:
@rm -f $(EXECUTABLE) *.so *.o *.a
@rm -f $(EXECUTABLE) *.so *.o *.a *.json *.params
2 changes: 1 addition & 1 deletion golang/sample/complex.go
Expand Up @@ -89,7 +89,7 @@ func main() {

// Array allocation attributes
tshapeIn := []int64{1, 224, 224, 3}
tshapeOut := []int64{1, 1001}
tshapeOut := []int64{1, 1000}

// Allocate input Array
inX, err := gotvm.Empty(tshapeIn, "float32", gotvm.CPU(0))
Expand Down
91 changes: 91 additions & 0 deletions golang/sample/gen_mobilenet_lib.py
@@ -0,0 +1,91 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import os
from tvm import relay
from tvm.contrib.download import download_testdata
import tflite.Model


################################################
# Utils for downloading and extracting zip files
# ----------------------------------------------
def extract(path):
import tarfile
if path.endswith("tgz") or path.endswith("gz"):
dir_path = os.path.dirname(path)
tar = tarfile.open(path)
tar.extractall(path=dir_path)
tar.close()
else:
raise RuntimeError('Could not decompress the file: ' + path)


###################################
# Download TFLite pre-trained model
# ---------------------------------

model_url = "https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.4_224.tgz"
model_path = download_testdata(model_url, "mobilenet_v2_1.4_224.tgz", module=['tf', 'official'])
model_dir = os.path.dirname(model_path)
extract(model_path)

# now we have mobilenet_v2_1.4_224.tflite on disk
model_file = os.path.join(model_dir, "mobilenet_v2_1.4_224.tflite")

# get TFLite model from buffer
tflite_model_buf = open(model_file, "rb").read()
tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)


##############################
# Load Neural Network in Relay
# ----------------------------

# TFLite input tensor name, shape and type
input_tensor = "input"
input_shape = (1, 224, 224, 3)
input_dtype = "float32"

# parse TFLite model and convert into Relay computation graph
mod, params = relay.frontend.from_tflite(tflite_model,
shape_dict={input_tensor: input_shape},
dtype_dict={input_tensor: input_dtype})

#############
# Compilation
# -----------

target = 'llvm'

# Build with Relay
with relay.build_config(opt_level=3):
graph, lib, params = relay.build_module.build(
mod, target, params=params)

###############################################
# Save the graph, lib and parameters into files
# ---------------------------------------------

lib.export_library("./mobilenet.so")
print('lib export succeefully')

with open("./mobilenet.json", "w") as fo:
fo.write(graph)

with open("./mobilenet.params", "wb") as fo:
fo.write(relay.save_param_dict(params))

0 comments on commit 70017ef

Please sign in to comment.