Commit aaad5f98 by Yuwei Hu Committed by Tianqi Chen

[Tutorial][Frontend] move from_keras tutorial to frontend (#2479)

* [Tutorial][Frontend] move from_keras tutorial to frontend

* remove tutorial/nnvm/from_keras.py
parent cc2b6760
...@@ -3,7 +3,7 @@ Compile Keras Models ...@@ -3,7 +3,7 @@ Compile Keras Models
===================== =====================
**Author**: `Yuwei Hu <https://Huyuwei.github.io/>`_ **Author**: `Yuwei Hu <https://Huyuwei.github.io/>`_
This article is an introductory tutorial to deploy keras models with NNVM. This article is an introductory tutorial to deploy keras models with Relay.
For us to begin with, keras should be installed. For us to begin with, keras should be installed.
Tensorflow is also required since it's used as the default backend of keras. Tensorflow is also required since it's used as the default backend of keras.
...@@ -18,8 +18,8 @@ A quick solution is to install via pip ...@@ -18,8 +18,8 @@ A quick solution is to install via pip
or please refer to official site or please refer to official site
https://keras.io/#installation https://keras.io/#installation
""" """
import nnvm
import tvm import tvm
import tvm.relay as relay
import keras import keras
import numpy as np import numpy as np
...@@ -66,32 +66,22 @@ data = preprocess_input(data).transpose([0, 3, 1, 2]) ...@@ -66,32 +66,22 @@ data = preprocess_input(data).transpose([0, 3, 1, 2])
print('input_1', data.shape) print('input_1', data.shape)
###################################################################### ######################################################################
# Compile the model on NNVM # Compile the model with Relay
# -------------------------- # ----------------------------
# We should be familiar with the process now. # convert the keras model(NHWC layout) to Relay format(NCHW layout).
shape_dict = {'input_1': data.shape}
# convert the keras model(NHWC layout) to NNVM format(NCHW layout). func, params = relay.frontend.from_keras(keras_resnet50, shape_dict)
sym, params = nnvm.frontend.from_keras(keras_resnet50)
# compile the model # compile the model
target = 'cuda' target = 'cuda'
shape_dict = {'input_1': data.shape} ctx = tvm.gpu(0)
with nnvm.compiler.build_config(opt_level=3): with relay.build_config(opt_level=3):
graph, lib, params = nnvm.compiler.build(sym, target, shape_dict, params=params) executor = relay.build_module.create_executor('graph', func, ctx, target)
###################################################################### ######################################################################
# Execute on TVM # Execute on TVM
# --------------- # ---------------
# The process is no different from other examples. dtype = 'float32'
from tvm.contrib import graph_runtime tvm_out = executor.evaluate(func)(tvm.nd.array(data.astype(dtype)), **params)
ctx = tvm.gpu(0)
m = graph_runtime.create(graph, lib, ctx)
# set inputs
m.set_input('input_1', tvm.nd.array(data.astype('float32')))
m.set_input(**params)
# execute
m.run()
# get outputs
tvm_out = m.get_output(0)
top1_tvm = np.argmax(tvm_out.asnumpy()[0]) top1_tvm = np.argmax(tvm_out.asnumpy()[0])
##################################################################### #####################################################################
...@@ -106,7 +96,7 @@ synset_name = 'synset.txt' ...@@ -106,7 +96,7 @@ synset_name = 'synset.txt'
download(synset_url, synset_name) download(synset_url, synset_name)
with open(synset_name) as f: with open(synset_name) as f:
synset = eval(f.read()) synset = eval(f.read())
print('NNVM top-1 id: {}, class name: {}'.format(top1_tvm, synset[top1_tvm])) print('Relay top-1 id: {}, class name: {}'.format(top1_tvm, synset[top1_tvm]))
# confirm correctness with keras output # confirm correctness with keras output
keras_out = keras_resnet50.predict(data.transpose([0, 2, 3, 1])) keras_out = keras_resnet50.predict(data.transpose([0, 2, 3, 1]))
top1_keras = np.argmax(keras_out) top1_keras = np.argmax(keras_out)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment