{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%shell\n# Installs the latest dev build of TVM from PyPI. If you wish to build\n# from source, see https://tvm.apache.org/docs/install/from_source.html\npip install apache-tvm --pre" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Compiling and Optimizing a Model with the Python Interface (AutoTVM)\n**Author**:\n[Chris Hoge](https://github.com/hogepodge)\n\nIn the [TVMC Tutorial](tvmc_command_line_driver), we covered how to compile, run, and tune a\npre-trained vision model, ResNet-50 v2 using the command line interface for\nTVM, TVMC. TVM is more that just a command-line tool though, it is an\noptimizing framework with APIs available for a number of different languages\nthat gives you tremendous flexibility in working with machine learning models.\n\nIn this tutorial we will cover the same ground we did with TVMC, but show how\nit is done with the Python API. Upon completion of this section, we will have\nused the Python API for TVM to accomplish the following tasks:\n\n* Compile a pre-trained ResNet-50 v2 model for the TVM runtime.\n* Run a real image through the compiled model, and interpret the output and model\n performance.\n* Tune the model that model on a CPU using TVM.\n* Re-compile an optimized model using the tuning data collected by TVM.\n* Run the image through the optimized model, and compare the output and model\n performance.\n\nThe goal of this section is to give you an overview of TVM's capabilites and\nhow to use them through the Python API.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "TVM is a deep learning compiler framework, with a number of different modules\navailable for working with deep learning models and operators. In this\ntutorial we will work through how to load, compile, and optimize a model\nusing the Python API.\n\nWe begin by importing a number of dependencies, including ``onnx`` for\nloading and converting the model, helper utilities for downloading test data,\nthe Python Image Library for working with the image data, ``numpy`` for pre\nand post-processing of the image data, the TVM Relay framework, and the TVM\nGraph Executor.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import onnx\nfrom tvm.contrib.download import download_testdata\nfrom PIL import Image\nimport numpy as np\nimport tvm.relay as relay\nimport tvm\nfrom tvm.contrib import graph_executor" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Downloading and Loading the ONNX Model\n\nFor this tutorial, we will be working with ResNet-50 v2. ResNet-50 is a\nconvolutional neural network that is 50 layers deep and designed to classify\nimages. The model we will be using has been pre-trained on more than a\nmillion images with 1000 different classifications. The network has an input\nimage size of 224x224. If you are interested exploring more of how the\nResNet-50 model is structured, we recommend downloading\n[Netron](https://netron.app), a freely available ML model viewer.\n\nTVM provides a helper library to download pre-trained models. By providing a\nmodel URL, file name, and model type through the module, TVM will download\nthe model and save it to disk. For the instance of an ONNX model, you can\nthen load it into memory using the ONNX runtime.\n\n
TVM supports many popular model formats. A list can be found in the\n`Compile Deep Learning Models
Specifying the correct target can have a huge impact on the performance of\nthe compiled module, as it can take advantage of hardware features\navailable on the target. For more information, please refer to\n`Auto-tuning a convolutional network for x86 CPU
By default this search is guided using an `XGBoost Grid` algorithm.\nDepending on your model complexity and amount of time available, you might\nwant to choose a different algorithm.
In this example, in the interest of time, we set the number of trials and\nearly stopping to 20 and 100. You will likely see more performance improvements if\nyou set these values to be higher but this comes at the expense of time\nspent tuning. The number of trials required for convergence will vary\ndepending on the specifics of the model and the target platform.