.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "how_to/compile_models/from_pytorch.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_how_to_compile_models_from_pytorch.py: Compile PyTorch Models ====================== **Author**: `Alex Wong `_ This article is an introductory tutorial to deploy PyTorch models with Relay. For us to begin with, PyTorch should be installed. TorchVision is also required since we will be using it as our model zoo. A quick solution is to install via pip .. code-block:: bash pip install torch==1.7.0 pip install torchvision==0.8.1 or please refer to official site https://pytorch.org/get-started/locally/ PyTorch versions should be backwards compatible but should be used with the proper TorchVision version. Currently, TVM supports PyTorch 1.7 and 1.4. Other versions may be unstable. .. GENERATED FROM PYTHON SOURCE LINES 43-56 .. code-block:: default import tvm from tvm import relay import numpy as np from tvm.contrib.download import download_testdata # PyTorch imports import torch import torchvision .. GENERATED FROM PYTHON SOURCE LINES 62-64 Load a pretrained PyTorch model ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 64-73 .. code-block:: default model_name = "resnet18" model = getattr(torchvision.models, model_name)(pretrained=True) model = model.eval() # We grab the TorchScripted model via tracing input_shape = [1, 3, 224, 224] input_data = torch.randn(input_shape) scripted_model = torch.jit.trace(model, input_data).eval() .. rst-class:: sphx-glr-script-out .. code-block:: none Downloading: "https://download.pytorch.org/models/resnet18-f37072fd.pth" to /workspace/.cache/torch/hub/checkpoints/resnet18-f37072fd.pth 0%| | 0.00/44.7M [00:00` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: from_pytorch.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_