You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2020/05/22 19:42:33 UTC
[GitHub] [incubator-tvm] tom-gall opened a new pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
tom-gall opened a new pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655
Add MicroTVM tutorial using the STM32F746 discovery board
with a sample tflite model
Signed-off-by: Tom Gall <to...@linaro.org>
Thanks for contributing to TVM! Please refer to guideline https://tvm.apache.org/docs/contribute/ for useful information and tips. After the pull request is submitted, please request code reviews from [Reviewers](https://github.com/apache/incubator-tvm/blob/master/CONTRIBUTORS.md#reviewers) by @ them in the pull request thread.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tom-gall commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tom-gall commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r438434494
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
+# ----------------------------------------------
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+import requests
+
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+
+######################################################################
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
+model_file = 'sine_model.tflite'
+r = requests.get(model_url, allow_redirects=True)
+
+open(model_file,'wb').write(r.content)
+######################################################################
+# Uncomment the following code to load the model from a local
+# directory
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+# model_dir ="./"
+# tflite_model_file = os.path.join(model_dir, "sine_model.tflite")
+tflite_model_buf = open(model_file, "rb").read()
+
+######################################################################
+# Using the buffer, transform into a tflite model python object
+try:
+ import tflite
+ tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)
+except AttributeError:
+ import tflite.Model
+ tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)
+
+######################################################################
+# Print out the version of the model
+version = tflite_model.Version()
+print ("Model Version: " + str(version))
+
+
+######################################################################
+# Setup the device config which is what will be used to communicate
+# with the microcontroller (a STM32F746 Discovery board)
+TARGET = 'c -device=micro_dev'
+dev_config = micro.device.arm.stm32f746xx.generate_config("127.0.0.1", 6666)
+
+
+######################################################################
+# Parse the python model object to convert it into a relay module
+# and weights
+# It is important to note that the input tensor name must match what
+# is contained in the model.
+# If you are unsure what that might be, this can be discovered by using
+# the visualize.py script within the Tensorflow project.
+# See : How do I inspect a .tflite file? https://www.tensorflow.org/lite/guide/faq
+input_tensor = "dense_4_input"
+input_shape = (1,)
+input_dtype = "float32"
+
+mod, params = relay.frontend.from_tflite(tflite_model,
+ shape_dict={input_tensor: input_shape},
+ dtype_dict={input_tensor: input_dtype})
+
+######################################################################
+# You'll need to uncomment the following blocks of code for the
+# example to run on device.
+# Next with the dev_config, we establish a micro session and create
+# a context
+#
+# .. code-block:: python
+#
+# with micro.Session(dev_config) as sess:
+# ctx = tvm.micro_dev(0)
+
+######################################################################
+# Now we create a build config for relay. turning off two options
+# and then calling relay.build which will result in a C source
+#
+# .. code-block:: python
+#
+# disable_vectorize = tvm.target.build_config(disable_vectorize=True)
+# disable_fusion = relay.build_config(disabled_pass={'FuseOps'})
+# with disable_vectorize, disable_fusion:
+# graph, c_mod, params = relay.build(mod, target=TARGET, params=params)
+
+######################################################################
+# With the c_mod that is the handle to our c sourcecode, we create a
+# micro module, followed by a compiled object which behind the scenes
+# is linked to the microTVM runtime for running on the target board
+# .. code-block:: python
+#
+# micro_mod = micro.create_micro_mod(c_mod, dev_config)
+# mod = graph_runtime.create(graph, micro_mod, ctx)
+
+######################################################################
+# Pass the weights to get ready to do some inference
+# .. code-block:: python
+#
+# ``mod.set_input(**params)``
+
+######################################################################
+# The model consumes a single float32. Construct a tvm.nd.array object
+# with a single contrived number as input. For this model values of
+# 0 to 2Pi are acceptible.
Review comment:
Fixed, Thanks!
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tom-gall commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tom-gall commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r438437168
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
Review comment:
Interestingly make html dies with an exception :
python3 -m sphinx -b html -d _build/doctrees . _build/html
Running Sphinx v3.1.0
loading pickled environment... done
[autosummary] generating autosummary for: api/links.rst, api/python/autotvm.rst, api/python/contrib.rst, api/python/driver.rst, api/python/error.rst, api/python/graph_runtime.rst, api/python/index.rst, api/python/ir.rst, api/python/micro.rst, api/python/ndarray.rst, ..., vta/index.rst, vta/install.rst, vta/tutorials/autotvm/tune_relay_vta.rst, vta/tutorials/frontend/deploy_classification.rst, vta/tutorials/frontend/deploy_detection.rst, vta/tutorials/index.rst, vta/tutorials/matrix_multiply.rst, vta/tutorials/optimize/convolution_opt.rst, vta/tutorials/optimize/matrix_multiply_opt.rst, vta/tutorials/vta_get_started.rst
generating gallery...
generating gallery for tutorials... [ 33%] relay_quick_start.py
Extension error:
Handler <function generate_gallery_rst at 0x7f23e55b04d0> for event 'builder-inited' threw an exception
make: *** [Makefile:70: html] Error 2
I'm using ./tests/scripts/task_sphinx_precheck.sh which seems to generate less than perfect html when viewed elsewhere. I'm less than confident that the html generated is acceptable :-/
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] areusch commented on pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
areusch commented on pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#issuecomment-637760600
Hey Tom, to get this to build further along in the CI, I think you can add the following line to docs/conf.py:
'../tutorials/micro'
I added this around line 168
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tom-gall commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tom-gall commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r438405649
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
+# ----------------------------------------------
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+import requests
+
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+
+######################################################################
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
+model_file = 'sine_model.tflite'
+r = requests.get(model_url, allow_redirects=True)
+
+open(model_file,'wb').write(r.content)
+######################################################################
+# Uncomment the following code to load the model from a local
+# directory
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+# model_dir ="./"
+# tflite_model_file = os.path.join(model_dir, "sine_model.tflite")
+tflite_model_buf = open(model_file, "rb").read()
+
+######################################################################
+# Using the buffer, transform into a tflite model python object
+try:
+ import tflite
+ tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)
+except AttributeError:
+ import tflite.Model
+ tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)
+
+######################################################################
+# Print out the version of the model
+version = tflite_model.Version()
+print ("Model Version: " + str(version))
+
+
+######################################################################
+# Setup the device config which is what will be used to communicate
+# with the microcontroller (a STM32F746 Discovery board)
+TARGET = 'c -device=micro_dev'
+dev_config = micro.device.arm.stm32f746xx.generate_config("127.0.0.1", 6666)
+
+
+######################################################################
+# Parse the python model object to convert it into a relay module
+# and weights
+# It is important to note that the input tensor name must match what
+# is contained in the model.
+# If you are unsure what that might be, this can be discovered by using
+# the visualize.py script within the Tensorflow project.
+# See : How do I inspect a .tflite file? https://www.tensorflow.org/lite/guide/faq
+input_tensor = "dense_4_input"
+input_shape = (1,)
+input_dtype = "float32"
+
+mod, params = relay.frontend.from_tflite(tflite_model,
+ shape_dict={input_tensor: input_shape},
+ dtype_dict={input_tensor: input_dtype})
+
+######################################################################
Review comment:
I like idea! I'll do that.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tom-gall commented on pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tom-gall commented on pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#issuecomment-632915897
@areusch
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tom-gall commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tom-gall commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r439184763
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
+# ----------------------------------------------
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+import requests
+
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+
+######################################################################
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
Review comment:
I've gone ahead with this version and moved to the use of download_testdata. I've included the very simple model as part of this pull request, placing it in the same directory as the tutorial. Is this the right place to put it?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tom-gall commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tom-gall commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r445128504
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,216 @@
+# 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.
+"""
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and a TFLite
+model with Relay.
+"""
+
+# %%
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+#
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
Review comment:
I've left this as it came from another TVM tutorial. It's formatted this way in the other tutorial. Suspect consistency is the right course?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tqchen commented on pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tqchen commented on pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#issuecomment-649111624
Thanks @tom-gall @areusch @liangfu !
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tom-gall edited a comment on pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tom-gall edited a comment on pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#issuecomment-632915897
@areusch @u99127
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tom-gall commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tom-gall commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r438436184
##########
File path: docs/conf.py
##########
@@ -203,7 +203,8 @@
'../tutorials/deployment',
'../vta/tutorials/frontend',
'../vta/tutorials/optimize',
- '../vta/tutorials/autotvm'])
+ '../vta/tutorials/autotvm',
+ '../tutorials/micro'])
Review comment:
Reordered. Thanks!
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tqchen commented on pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tqchen commented on pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#issuecomment-637756370
@tom-gall please look into the CI failure
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tqchen commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tqchen commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r438463038
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
+# ----------------------------------------------
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+import requests
+
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+
+######################################################################
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
Review comment:
https://github.com/apache/incubator-tvm/blob/master/tutorials/frontend/from_mxnet.py#L60
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tom-gall commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tom-gall commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r438434392
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
+# ----------------------------------------------
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+import requests
+
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+
+######################################################################
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
+model_file = 'sine_model.tflite'
+r = requests.get(model_url, allow_redirects=True)
+
+open(model_file,'wb').write(r.content)
+######################################################################
+# Uncomment the following code to load the model from a local
+# directory
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+# model_dir ="./"
+# tflite_model_file = os.path.join(model_dir, "sine_model.tflite")
+tflite_model_buf = open(model_file, "rb").read()
+
+######################################################################
+# Using the buffer, transform into a tflite model python object
+try:
+ import tflite
+ tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)
+except AttributeError:
+ import tflite.Model
+ tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)
+
+######################################################################
+# Print out the version of the model
+version = tflite_model.Version()
+print ("Model Version: " + str(version))
+
+
+######################################################################
+# Setup the device config which is what will be used to communicate
+# with the microcontroller (a STM32F746 Discovery board)
+TARGET = 'c -device=micro_dev'
+dev_config = micro.device.arm.stm32f746xx.generate_config("127.0.0.1", 6666)
+
+
+######################################################################
+# Parse the python model object to convert it into a relay module
+# and weights
+# It is important to note that the input tensor name must match what
+# is contained in the model.
+# If you are unsure what that might be, this can be discovered by using
+# the visualize.py script within the Tensorflow project.
+# See : How do I inspect a .tflite file? https://www.tensorflow.org/lite/guide/faq
+input_tensor = "dense_4_input"
+input_shape = (1,)
+input_dtype = "float32"
+
+mod, params = relay.frontend.from_tflite(tflite_model,
+ shape_dict={input_tensor: input_shape},
+ dtype_dict={input_tensor: input_dtype})
+
+######################################################################
+# You'll need to uncomment the following blocks of code for the
+# example to run on device.
+# Next with the dev_config, we establish a micro session and create
+# a context
+#
+# .. code-block:: python
+#
+# with micro.Session(dev_config) as sess:
+# ctx = tvm.micro_dev(0)
+
+######################################################################
+# Now we create a build config for relay. turning off two options
+# and then calling relay.build which will result in a C source
+#
+# .. code-block:: python
+#
+# disable_vectorize = tvm.target.build_config(disable_vectorize=True)
+# disable_fusion = relay.build_config(disabled_pass={'FuseOps'})
+# with disable_vectorize, disable_fusion:
+# graph, c_mod, params = relay.build(mod, target=TARGET, params=params)
+
+######################################################################
+# With the c_mod that is the handle to our c sourcecode, we create a
+# micro module, followed by a compiled object which behind the scenes
+# is linked to the microTVM runtime for running on the target board
+# .. code-block:: python
+#
+# micro_mod = micro.create_micro_mod(c_mod, dev_config)
+# mod = graph_runtime.create(graph, micro_mod, ctx)
+
+######################################################################
+# Pass the weights to get ready to do some inference
+# .. code-block:: python
+#
+# ``mod.set_input(**params)``
Review comment:
I am in error on this one. Appears this was triggered by something else. I've removed. Thanks for the feedback.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tom-gall commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tom-gall commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r438437559
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
+# ----------------------------------------------
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+import requests
+
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+
+######################################################################
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
+model_file = 'sine_model.tflite'
+r = requests.get(model_url, allow_redirects=True)
+
+open(model_file,'wb').write(r.content)
+######################################################################
+# Uncomment the following code to load the model from a local
+# directory
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+# model_dir ="./"
+# tflite_model_file = os.path.join(model_dir, "sine_model.tflite")
+tflite_model_buf = open(model_file, "rb").read()
+
+######################################################################
+# Using the buffer, transform into a tflite model python object
+try:
+ import tflite
+ tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)
+except AttributeError:
+ import tflite.Model
+ tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)
+
+######################################################################
+# Print out the version of the model
+version = tflite_model.Version()
+print ("Model Version: " + str(version))
+
+
+######################################################################
+# Setup the device config which is what will be used to communicate
+# with the microcontroller (a STM32F746 Discovery board)
+TARGET = 'c -device=micro_dev'
+dev_config = micro.device.arm.stm32f746xx.generate_config("127.0.0.1", 6666)
+
+
+######################################################################
+# Parse the python model object to convert it into a relay module
+# and weights
+# It is important to note that the input tensor name must match what
+# is contained in the model.
+# If you are unsure what that might be, this can be discovered by using
+# the visualize.py script within the Tensorflow project.
+# See : How do I inspect a .tflite file? https://www.tensorflow.org/lite/guide/faq
+input_tensor = "dense_4_input"
+input_shape = (1,)
+input_dtype = "float32"
+
+mod, params = relay.frontend.from_tflite(tflite_model,
+ shape_dict={input_tensor: input_shape},
+ dtype_dict={input_tensor: input_dtype})
+
+######################################################################
+# You'll need to uncomment the following blocks of code for the
+# example to run on device.
+# Next with the dev_config, we establish a micro session and create
+# a context
+#
+# .. code-block:: python
+#
+# with micro.Session(dev_config) as sess:
+# ctx = tvm.micro_dev(0)
+
+######################################################################
+# Now we create a build config for relay. turning off two options
+# and then calling relay.build which will result in a C source
+#
+# .. code-block:: python
+#
+# disable_vectorize = tvm.target.build_config(disable_vectorize=True)
+# disable_fusion = relay.build_config(disabled_pass={'FuseOps'})
+# with disable_vectorize, disable_fusion:
+# graph, c_mod, params = relay.build(mod, target=TARGET, params=params)
+
+######################################################################
+# With the c_mod that is the handle to our c sourcecode, we create a
+# micro module, followed by a compiled object which behind the scenes
+# is linked to the microTVM runtime for running on the target board
+# .. code-block:: python
+#
+# micro_mod = micro.create_micro_mod(c_mod, dev_config)
+# mod = graph_runtime.create(graph, micro_mod, ctx)
+
+######################################################################
+# Pass the weights to get ready to do some inference
+# .. code-block:: python
Review comment:
Fixed. Thanks!
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] liangfu commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
liangfu commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r433008686
##########
File path: tutorials/micro/README.txt
##########
@@ -0,0 +1,4 @@
+.. _tutorial-micro:
+
+Micro TVM
+----------------------------
Review comment:
alignment to the above
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tqchen merged pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tqchen merged pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] areusch edited a comment on pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
areusch edited a comment on pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#issuecomment-637760600
Hey Tom, to get this to build further along in the CI, I think you can add the following line to docs/conf.py:
'../tutorials/micro'
I added this around line 168 and it kept building
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tqchen commented on pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tqchen commented on pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#issuecomment-641678850
@tom-gall please followup on @liangfu 's comment
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tqchen commented on pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tqchen commented on pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#issuecomment-647014339
@tom-gall please followup on @areusch 's last comments
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tom-gall commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tom-gall commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r438438414
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
Review comment:
I chopped this out in favor of two sections which are directly above the docs or code they
are for.
Recreating your own Pre-Trained TFLite model
Load and prepare the Pre-Trained Model
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tqchen commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tqchen commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r438462908
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
+# ----------------------------------------------
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+import requests
+
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+
+######################################################################
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
Review comment:
It is fine, we need to follow this function https://github.com/apache/incubator-tvm/blob/master/tutorials/frontend/from_mxnet.py#L60 so the content is cached.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tom-gall commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tom-gall commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r438382565
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
+# ----------------------------------------------
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+import requests
+
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+
+######################################################################
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
+model_file = 'sine_model.tflite'
+r = requests.get(model_url, allow_redirects=True)
+
+open(model_file,'wb').write(r.content)
+######################################################################
+# Uncomment the following code to load the model from a local
+# directory
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+# model_dir ="./"
+# tflite_model_file = os.path.join(model_dir, "sine_model.tflite")
+tflite_model_buf = open(model_file, "rb").read()
+
+######################################################################
+# Using the buffer, transform into a tflite model python object
+try:
+ import tflite
+ tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)
+except AttributeError:
+ import tflite.Model
+ tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)
+
+######################################################################
+# Print out the version of the model
+version = tflite_model.Version()
+print ("Model Version: " + str(version))
+
+
+######################################################################
+# Setup the device config which is what will be used to communicate
+# with the microcontroller (a STM32F746 Discovery board)
+TARGET = 'c -device=micro_dev'
+dev_config = micro.device.arm.stm32f746xx.generate_config("127.0.0.1", 6666)
+
+
+######################################################################
+# Parse the python model object to convert it into a relay module
+# and weights
+# It is important to note that the input tensor name must match what
+# is contained in the model.
+# If you are unsure what that might be, this can be discovered by using
+# the visualize.py script within the Tensorflow project.
+# See : How do I inspect a .tflite file? https://www.tensorflow.org/lite/guide/faq
+input_tensor = "dense_4_input"
+input_shape = (1,)
+input_dtype = "float32"
+
+mod, params = relay.frontend.from_tflite(tflite_model,
+ shape_dict={input_tensor: input_shape},
+ dtype_dict={input_tensor: input_dtype})
+
+######################################################################
+# You'll need to uncomment the following blocks of code for the
+# example to run on device.
+# Next with the dev_config, we establish a micro session and create
+# a context
+#
+# .. code-block:: python
+#
+# with micro.Session(dev_config) as sess:
+# ctx = tvm.micro_dev(0)
+
+######################################################################
+# Now we create a build config for relay. turning off two options
+# and then calling relay.build which will result in a C source
+#
+# .. code-block:: python
+#
+# disable_vectorize = tvm.target.build_config(disable_vectorize=True)
+# disable_fusion = relay.build_config(disabled_pass={'FuseOps'})
+# with disable_vectorize, disable_fusion:
+# graph, c_mod, params = relay.build(mod, target=TARGET, params=params)
+
+######################################################################
+# With the c_mod that is the handle to our c sourcecode, we create a
+# micro module, followed by a compiled object which behind the scenes
+# is linked to the microTVM runtime for running on the target board
+# .. code-block:: python
+#
+# micro_mod = micro.create_micro_mod(c_mod, dev_config)
+# mod = graph_runtime.create(graph, micro_mod, ctx)
+
+######################################################################
+# Pass the weights to get ready to do some inference
+# .. code-block:: python
+#
+# ``mod.set_input(**params)``
Review comment:
'' is needed otherwise sphinx will error out due to the usage of **. Ugly work around I know. Is there perhaps a better way?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tom-gall commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tom-gall commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r441792526
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
+# ----------------------------------------------
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+import requests
+
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+
+######################################################################
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
Review comment:
ok! Thanks for the comment, removed the binary file from this commit, still making use of download_testdata but pulling the model now from a linaro file system.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tqchen commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tqchen commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r439508442
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
+# ----------------------------------------------
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+import requests
+
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+
+######################################################################
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
Review comment:
We cannot check in binary files into the codebase, please place the model elsewhere where we can download via download_testdata. The usage of download_testdata looks good
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] areusch commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
areusch commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r438270610
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
Review comment:
it might read better if we add a header "Download Pre-Trained TFLite Model" before this line.
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
Review comment:
This text renders as a header--I think you'll need to indent here to mark this text as a large code block.
Here, I think it would make sense to include another header akin to "Import the TFLite Model."
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
+# ----------------------------------------------
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+import requests
+
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+
+######################################################################
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
Review comment:
During the CI, all of this code will get executed. @tqchen i'm not sure we'd like to include external dependencies in the CI. Is it okay to place this ~2.5kB file in the tutorials dir?
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
+# ----------------------------------------------
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+import requests
+
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+
+######################################################################
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
+model_file = 'sine_model.tflite'
+r = requests.get(model_url, allow_redirects=True)
+
+open(model_file,'wb').write(r.content)
+######################################################################
+# Uncomment the following code to load the model from a local
+# directory
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+# model_dir ="./"
+# tflite_model_file = os.path.join(model_dir, "sine_model.tflite")
+tflite_model_buf = open(model_file, "rb").read()
+
+######################################################################
+# Using the buffer, transform into a tflite model python object
+try:
+ import tflite
+ tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)
+except AttributeError:
+ import tflite.Model
+ tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)
+
+######################################################################
+# Print out the version of the model
+version = tflite_model.Version()
+print ("Model Version: " + str(version))
+
+
+######################################################################
+# Setup the device config which is what will be used to communicate
+# with the microcontroller (a STM32F746 Discovery board)
+TARGET = 'c -device=micro_dev'
+dev_config = micro.device.arm.stm32f746xx.generate_config("127.0.0.1", 6666)
+
+
+######################################################################
+# Parse the python model object to convert it into a relay module
+# and weights
+# It is important to note that the input tensor name must match what
+# is contained in the model.
+# If you are unsure what that might be, this can be discovered by using
+# the visualize.py script within the Tensorflow project.
+# See : How do I inspect a .tflite file? https://www.tensorflow.org/lite/guide/faq
+input_tensor = "dense_4_input"
+input_shape = (1,)
+input_dtype = "float32"
+
+mod, params = relay.frontend.from_tflite(tflite_model,
+ shape_dict={input_tensor: input_shape},
+ dtype_dict={input_tensor: input_dtype})
+
+######################################################################
Review comment:
here you could add another section "Run the imported model on device"
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tom-gall commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tom-gall commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r438436066
##########
File path: tutorials/micro/README.txt
##########
@@ -0,0 +1,4 @@
+.. _tutorial-micro:
Review comment:
Removed
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] liangfu commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
liangfu commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r437896159
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
+# ----------------------------------------------
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+import requests
+
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+
+######################################################################
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
+model_file = 'sine_model.tflite'
+r = requests.get(model_url, allow_redirects=True)
+
+open(model_file,'wb').write(r.content)
+######################################################################
+# Uncomment the following code to load the model from a local
+# directory
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+# model_dir ="./"
+# tflite_model_file = os.path.join(model_dir, "sine_model.tflite")
+tflite_model_buf = open(model_file, "rb").read()
+
+######################################################################
+# Using the buffer, transform into a tflite model python object
+try:
+ import tflite
+ tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)
+except AttributeError:
+ import tflite.Model
+ tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)
+
+######################################################################
+# Print out the version of the model
+version = tflite_model.Version()
+print ("Model Version: " + str(version))
+
+
+######################################################################
+# Setup the device config which is what will be used to communicate
+# with the microcontroller (a STM32F746 Discovery board)
+TARGET = 'c -device=micro_dev'
+dev_config = micro.device.arm.stm32f746xx.generate_config("127.0.0.1", 6666)
+
+
+######################################################################
+# Parse the python model object to convert it into a relay module
+# and weights
+# It is important to note that the input tensor name must match what
+# is contained in the model.
+# If you are unsure what that might be, this can be discovered by using
+# the visualize.py script within the Tensorflow project.
+# See : How do I inspect a .tflite file? https://www.tensorflow.org/lite/guide/faq
+input_tensor = "dense_4_input"
+input_shape = (1,)
+input_dtype = "float32"
+
+mod, params = relay.frontend.from_tflite(tflite_model,
+ shape_dict={input_tensor: input_shape},
+ dtype_dict={input_tensor: input_dtype})
+
+######################################################################
+# You'll need to uncomment the following blocks of code for the
+# example to run on device.
+# Next with the dev_config, we establish a micro session and create
+# a context
+#
+# .. code-block:: python
+#
+# with micro.Session(dev_config) as sess:
+# ctx = tvm.micro_dev(0)
+
+######################################################################
+# Now we create a build config for relay. turning off two options
+# and then calling relay.build which will result in a C source
+#
+# .. code-block:: python
+#
+# disable_vectorize = tvm.target.build_config(disable_vectorize=True)
+# disable_fusion = relay.build_config(disabled_pass={'FuseOps'})
+# with disable_vectorize, disable_fusion:
+# graph, c_mod, params = relay.build(mod, target=TARGET, params=params)
+
+######################################################################
+# With the c_mod that is the handle to our c sourcecode, we create a
+# micro module, followed by a compiled object which behind the scenes
+# is linked to the microTVM runtime for running on the target board
+# .. code-block:: python
+#
+# micro_mod = micro.create_micro_mod(c_mod, dev_config)
+# mod = graph_runtime.create(graph, micro_mod, ctx)
+
+######################################################################
+# Pass the weights to get ready to do some inference
+# .. code-block:: python
+#
+# ``mod.set_input(**params)``
+
+######################################################################
+# The model consumes a single float32. Construct a tvm.nd.array object
+# with a single contrived number as input. For this model values of
+# 0 to 2Pi are acceptible.
Review comment:
typo: acceptible -> acceptable
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
+# ----------------------------------------------
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+import requests
+
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+
+######################################################################
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
+model_file = 'sine_model.tflite'
+r = requests.get(model_url, allow_redirects=True)
+
+open(model_file,'wb').write(r.content)
+######################################################################
+# Uncomment the following code to load the model from a local
+# directory
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+# model_dir ="./"
+# tflite_model_file = os.path.join(model_dir, "sine_model.tflite")
+tflite_model_buf = open(model_file, "rb").read()
+
+######################################################################
+# Using the buffer, transform into a tflite model python object
+try:
+ import tflite
+ tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)
+except AttributeError:
+ import tflite.Model
+ tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)
+
+######################################################################
+# Print out the version of the model
+version = tflite_model.Version()
+print ("Model Version: " + str(version))
+
+
+######################################################################
+# Setup the device config which is what will be used to communicate
+# with the microcontroller (a STM32F746 Discovery board)
+TARGET = 'c -device=micro_dev'
+dev_config = micro.device.arm.stm32f746xx.generate_config("127.0.0.1", 6666)
+
+
+######################################################################
+# Parse the python model object to convert it into a relay module
+# and weights
+# It is important to note that the input tensor name must match what
+# is contained in the model.
+# If you are unsure what that might be, this can be discovered by using
+# the visualize.py script within the Tensorflow project.
+# See : How do I inspect a .tflite file? https://www.tensorflow.org/lite/guide/faq
+input_tensor = "dense_4_input"
+input_shape = (1,)
+input_dtype = "float32"
+
+mod, params = relay.frontend.from_tflite(tflite_model,
+ shape_dict={input_tensor: input_shape},
+ dtype_dict={input_tensor: input_dtype})
+
+######################################################################
+# You'll need to uncomment the following blocks of code for the
+# example to run on device.
+# Next with the dev_config, we establish a micro session and create
+# a context
+#
+# .. code-block:: python
+#
+# with micro.Session(dev_config) as sess:
+# ctx = tvm.micro_dev(0)
+
+######################################################################
+# Now we create a build config for relay. turning off two options
+# and then calling relay.build which will result in a C source
+#
+# .. code-block:: python
+#
+# disable_vectorize = tvm.target.build_config(disable_vectorize=True)
+# disable_fusion = relay.build_config(disabled_pass={'FuseOps'})
+# with disable_vectorize, disable_fusion:
+# graph, c_mod, params = relay.build(mod, target=TARGET, params=params)
+
+######################################################################
+# With the c_mod that is the handle to our c sourcecode, we create a
+# micro module, followed by a compiled object which behind the scenes
+# is linked to the microTVM runtime for running on the target board
+# .. code-block:: python
+#
+# micro_mod = micro.create_micro_mod(c_mod, dev_config)
+# mod = graph_runtime.create(graph, micro_mod, ctx)
+
+######################################################################
+# Pass the weights to get ready to do some inference
+# .. code-block:: python
+#
+# ``mod.set_input(**params)``
Review comment:
Please remove ``
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
Review comment:
Please follow RST syntax to put the extra indentation, otherwise it's not rendered as code. Again, try running `make html` locally.
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
+# ----------------------------------------------
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+import requests
+
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+
+######################################################################
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
+model_file = 'sine_model.tflite'
+r = requests.get(model_url, allow_redirects=True)
+
+open(model_file,'wb').write(r.content)
+######################################################################
+# Uncomment the following code to load the model from a local
+# directory
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+# model_dir ="./"
+# tflite_model_file = os.path.join(model_dir, "sine_model.tflite")
+tflite_model_buf = open(model_file, "rb").read()
+
+######################################################################
+# Using the buffer, transform into a tflite model python object
+try:
+ import tflite
+ tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)
+except AttributeError:
+ import tflite.Model
+ tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)
+
+######################################################################
+# Print out the version of the model
+version = tflite_model.Version()
+print ("Model Version: " + str(version))
+
+
+######################################################################
+# Setup the device config which is what will be used to communicate
+# with the microcontroller (a STM32F746 Discovery board)
+TARGET = 'c -device=micro_dev'
+dev_config = micro.device.arm.stm32f746xx.generate_config("127.0.0.1", 6666)
+
+
+######################################################################
+# Parse the python model object to convert it into a relay module
+# and weights
+# It is important to note that the input tensor name must match what
+# is contained in the model.
+# If you are unsure what that might be, this can be discovered by using
+# the visualize.py script within the Tensorflow project.
+# See : How do I inspect a .tflite file? https://www.tensorflow.org/lite/guide/faq
+input_tensor = "dense_4_input"
+input_shape = (1,)
+input_dtype = "float32"
+
+mod, params = relay.frontend.from_tflite(tflite_model,
+ shape_dict={input_tensor: input_shape},
+ dtype_dict={input_tensor: input_dtype})
+
+######################################################################
+# You'll need to uncomment the following blocks of code for the
+# example to run on device.
+# Next with the dev_config, we establish a micro session and create
+# a context
+#
+# .. code-block:: python
+#
+# with micro.Session(dev_config) as sess:
+# ctx = tvm.micro_dev(0)
Review comment:
I think the indentation here should be 4 spaces.
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
+# ----------------------------------------------
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+import requests
+
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+
+######################################################################
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
+model_file = 'sine_model.tflite'
+r = requests.get(model_url, allow_redirects=True)
+
+open(model_file,'wb').write(r.content)
+######################################################################
+# Uncomment the following code to load the model from a local
+# directory
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+# model_dir ="./"
+# tflite_model_file = os.path.join(model_dir, "sine_model.tflite")
+tflite_model_buf = open(model_file, "rb").read()
+
+######################################################################
+# Using the buffer, transform into a tflite model python object
+try:
+ import tflite
+ tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)
+except AttributeError:
+ import tflite.Model
+ tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)
+
+######################################################################
+# Print out the version of the model
+version = tflite_model.Version()
+print ("Model Version: " + str(version))
+
+
+######################################################################
+# Setup the device config which is what will be used to communicate
+# with the microcontroller (a STM32F746 Discovery board)
+TARGET = 'c -device=micro_dev'
+dev_config = micro.device.arm.stm32f746xx.generate_config("127.0.0.1", 6666)
+
+
+######################################################################
+# Parse the python model object to convert it into a relay module
+# and weights
+# It is important to note that the input tensor name must match what
+# is contained in the model.
+# If you are unsure what that might be, this can be discovered by using
+# the visualize.py script within the Tensorflow project.
+# See : How do I inspect a .tflite file? https://www.tensorflow.org/lite/guide/faq
+input_tensor = "dense_4_input"
+input_shape = (1,)
+input_dtype = "float32"
+
+mod, params = relay.frontend.from_tflite(tflite_model,
+ shape_dict={input_tensor: input_shape},
+ dtype_dict={input_tensor: input_dtype})
+
+######################################################################
+# You'll need to uncomment the following blocks of code for the
+# example to run on device.
+# Next with the dev_config, we establish a micro session and create
+# a context
+#
+# .. code-block:: python
+#
+# with micro.Session(dev_config) as sess:
Review comment:
Please follow RST syntax to put the extra indentation.
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
Review comment:
Please follow RST syntax to put the extra indentation, otherwise it's not rendered as code.
Please try reproduce the the rendered html by running
```bash
make html
```
in `<tvm_root>/docs` directory, so that the tutorial could be more reader friendly.
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
Review comment:
This looks wield in the code block. Please put extra indentation, otherwise it's not rendered as code.
##########
File path: tutorials/micro/README.txt
##########
@@ -0,0 +1,4 @@
+.. _tutorial-micro:
Review comment:
if this directive is unused, please remove.
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
Review comment:
place an empty line before this directive
##########
File path: docs/conf.py
##########
@@ -203,7 +203,8 @@
'../tutorials/deployment',
'../vta/tutorials/frontend',
'../vta/tutorials/optimize',
- '../vta/tutorials/autotvm'])
+ '../vta/tutorials/autotvm',
+ '../tutorials/micro'])
Review comment:
move this entry above the vta tutorials
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+#
+# Next we need to download a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like Mobilenet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. code-block:: bash
+#
+# if you download the example pretrained model from
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# unzip hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+# I've saved an older version of the pre-trailed model and made it available on linaro.org
+
+######################################################################
+# Python imports for tvm, numpy etc
+# ----------------------------------------------
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+import requests
+
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+
+######################################################################
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
+model_file = 'sine_model.tflite'
+r = requests.get(model_url, allow_redirects=True)
+
+open(model_file,'wb').write(r.content)
+######################################################################
+# Uncomment the following code to load the model from a local
+# directory
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+# model_dir ="./"
+# tflite_model_file = os.path.join(model_dir, "sine_model.tflite")
+tflite_model_buf = open(model_file, "rb").read()
+
+######################################################################
+# Using the buffer, transform into a tflite model python object
+try:
+ import tflite
+ tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)
+except AttributeError:
+ import tflite.Model
+ tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)
+
+######################################################################
+# Print out the version of the model
+version = tflite_model.Version()
+print ("Model Version: " + str(version))
+
+
+######################################################################
+# Setup the device config which is what will be used to communicate
+# with the microcontroller (a STM32F746 Discovery board)
+TARGET = 'c -device=micro_dev'
+dev_config = micro.device.arm.stm32f746xx.generate_config("127.0.0.1", 6666)
+
+
+######################################################################
+# Parse the python model object to convert it into a relay module
+# and weights
+# It is important to note that the input tensor name must match what
+# is contained in the model.
+# If you are unsure what that might be, this can be discovered by using
+# the visualize.py script within the Tensorflow project.
+# See : How do I inspect a .tflite file? https://www.tensorflow.org/lite/guide/faq
+input_tensor = "dense_4_input"
+input_shape = (1,)
+input_dtype = "float32"
+
+mod, params = relay.frontend.from_tflite(tflite_model,
+ shape_dict={input_tensor: input_shape},
+ dtype_dict={input_tensor: input_dtype})
+
+######################################################################
+# You'll need to uncomment the following blocks of code for the
+# example to run on device.
+# Next with the dev_config, we establish a micro session and create
+# a context
+#
+# .. code-block:: python
+#
+# with micro.Session(dev_config) as sess:
+# ctx = tvm.micro_dev(0)
+
+######################################################################
+# Now we create a build config for relay. turning off two options
+# and then calling relay.build which will result in a C source
+#
+# .. code-block:: python
+#
+# disable_vectorize = tvm.target.build_config(disable_vectorize=True)
+# disable_fusion = relay.build_config(disabled_pass={'FuseOps'})
+# with disable_vectorize, disable_fusion:
+# graph, c_mod, params = relay.build(mod, target=TARGET, params=params)
+
+######################################################################
+# With the c_mod that is the handle to our c sourcecode, we create a
+# micro module, followed by a compiled object which behind the scenes
+# is linked to the microTVM runtime for running on the target board
+# .. code-block:: python
+#
+# micro_mod = micro.create_micro_mod(c_mod, dev_config)
+# mod = graph_runtime.create(graph, micro_mod, ctx)
+
+######################################################################
+# Pass the weights to get ready to do some inference
+# .. code-block:: python
Review comment:
Please put an empty line above the RST directive, otherwise RST can't recognize this directive.
Again, try running `make html` locally.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] tom-gall commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
tom-gall commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r438437454
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
Review comment:
Fixed.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] areusch commented on a change in pull request #5655: Add MicroTVM tutorial using the STM32F746 discovery board
Posted by GitBox <gi...@apache.org>.
areusch commented on a change in pull request #5655:
URL: https://github.com/apache/incubator-tvm/pull/5655#discussion_r442953950
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,216 @@
+# 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.
+"""
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and a TFLite
+model with Relay.
+"""
+
+# %%
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+#
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
Review comment:
nitpick: unindent this line.
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,216 @@
+# 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.
+"""
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and a TFLite
+model with Relay.
+"""
+
+# %%
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+#
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add the current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+
+# %%
+# Recreating your own Pre-Trained TFLite model
+# --------------------------------------------
+#
+# The tutorial downloads a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like MobileNet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. note::
+#
+# If you accidentally download the example pretrained model from:
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+from tvm.contrib.download import download_testdata
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+# %%
+# Load and prepare the Pre-Trained Model
+# --------------------------------------
+#
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
+model_file = 'sine_model.tflite'
+model_path = download_testdata(model_url, model_file, module='data')
+
+tflite_model_buf = open(model_path, "rb").read()
+
+######################################################################
+# Using the buffer, transform into a tflite model python object
+try:
+ import tflite
+ tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)
+except AttributeError:
+ import tflite.Model
+ tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)
+
+######################################################################
+# Print out the version of the model
+version = tflite_model.Version()
+print ("Model Version: " + str(version))
+
+######################################################################
+# Parse the python model object to convert it into a relay module
+# and weights
Review comment:
add a period at the end
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,219 @@
+# 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.
+"""
+.. _tutorial-micro-tflite:
+
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and TFLite models with Relay.
+"""
+######################################################################
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
Review comment:
I was able to get this to work by deleting the other tutorials. I haven't seen that specific exception, though. I did build your tutorial locally and I think it looks okay now. i'll add a couple of small nitpicks which you could fix if you want to be a perfectionist about it.
##########
File path: tutorials/micro/micro_tflite.py
##########
@@ -0,0 +1,216 @@
+# 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.
+"""
+Micro TVM with TFLite Models
+============================
+**Author**: `Tom Gall <https://github.com/tom-gall>`_
+
+This tutorial is an introduction to working with MicroTVM and a TFLite
+model with Relay.
+"""
+
+# %%
+# Setup
+# -----
+#
+# To get started, TFLite package needs to be installed as prerequisite.
+#
+# install tflite
+#
+# .. code-block:: bash
+#
+# pip install tflite=2.1.0 --user
+#
+# or you could generate TFLite package yourself. The steps are the following:
+#
+# Get the flatc compiler.
+# Please refer to https://github.com/google/flatbuffers for details
+# and make sure it is properly installed.
+#
+# .. code-block:: bash
+#
+# flatc --version
+#
+# Get the TFLite schema.
+#
+# .. code-block:: bash
+#
+# wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs
+#
+# Generate TFLite package.
+#
+# .. code-block:: bash
+#
+# flatc --python schema.fbs
+#
+# Add the current folder (which contains generated tflite module) to PYTHONPATH.
+#
+# .. code-block:: bash
+#
+# export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd)
+#
+# To validate that the TFLite package was installed successfully, ``python -c "import tflite"``
+#
+# CMSIS needs to be downloaded and the CMSIS_ST_PATH environment variable setup
+# This tutorial only supports the STM32F7xx series of boards.
+# Download from : https://www.st.com/en/embedded-software/stm32cubef7.html
+# After you've expanded the zip file
+#
+# .. code-block:: bash
+#
+# export CMSIS_ST_PATH=/path/to/STM32Cube_FW_F7_V1.16.0/Drivers/CMSIS
+
+# %%
+# Recreating your own Pre-Trained TFLite model
+# --------------------------------------------
+#
+# The tutorial downloads a pretrained TFLite model. When working with microcontrollers
+# you need to be mindful these are highly resource constrained devices as such standard
+# models like MobileNet may not fit into their modest memory.
+#
+# For this tutorial, we'll make use of one of the TF Micro example models.
+#
+# If you wish to replicate the training steps see:
+# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/hello_world/train
+#
+# .. note::
+#
+# If you accidentally download the example pretrained model from:
+# wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/micro/hello_world_2020_04_13.zip
+# this will fail due to an unimplemented opcode (114)
+
+import os
+import numpy as np
+import tvm
+import tvm.micro as micro
+from tvm.contrib.download import download_testdata
+from tvm.contrib import graph_runtime, util
+from tvm import relay
+
+# %%
+# Load and prepare the Pre-Trained Model
+# --------------------------------------
+#
+# Load the pretrained TFLite model from a file in your current
+# directory into a buffer
+
+model_url = 'https://people.linaro.org/~tom.gall/sine_model.tflite'
+model_file = 'sine_model.tflite'
+model_path = download_testdata(model_url, model_file, module='data')
+
+tflite_model_buf = open(model_path, "rb").read()
+
+######################################################################
+# Using the buffer, transform into a tflite model python object
+try:
+ import tflite
+ tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)
+except AttributeError:
+ import tflite.Model
+ tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)
+
+######################################################################
+# Print out the version of the model
+version = tflite_model.Version()
+print ("Model Version: " + str(version))
+
+######################################################################
+# Parse the python model object to convert it into a relay module
+# and weights
+# It is important to note that the input tensor name must match what
+# is contained in the model.
+# If you are unsure what that might be, this can be discovered by using
+# the visualize.py script within the Tensorflow project.
+# See : How do I inspect a .tflite file? https://www.tensorflow.org/lite/guide/faq
Review comment:
nitpick: add another line break before this line, and convert to an external hyperlink: https://thomas-cokelaer.info/tutorials/sphinx/rest_syntax.html#internal-and-external-links
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org