.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "how_to/compile_models/from_oneflow.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_oneflow.py: Compile OneFlow Models ====================== **Author**: `Xiaoyu Zhang `_ This article is an introductory tutorial to deploy OneFlow models with Relay. For us to begin with, OneFlow package should be installed. A quick solution is to install via pip .. code-block:: bash pip install flowvision==0.1.0 python3 -m pip install -f https://release.oneflow.info oneflow==0.7.0+cpu or please refer to official site: https://github.com/Oneflow-Inc/oneflow Currently, TVM supports OneFlow 0.7.0. Other versions may be unstable. .. GENERATED FROM PYTHON SOURCE LINES 38-53 .. code-block:: default import os, math from matplotlib import pyplot as plt import numpy as np from PIL import Image # oneflow imports import flowvision import oneflow as flow import oneflow.nn as nn import tvm from tvm import relay from tvm.contrib.download import download_testdata .. rst-class:: sphx-glr-script-out .. code-block:: none /usr/local/lib/python3.7/dist-packages/oneflow/framework/dtype.py:49: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here. Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations oneflow.bool: np.bool, /usr/local/lib/python3.7/dist-packages/flowvision/transforms/functional_pil.py:193: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BILINEAR instead. def resize(img, size, interpolation=Image.BILINEAR): /usr/local/lib/python3.7/dist-packages/flowvision/transforms/functional.py:65: DeprecationWarning: NEAREST is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.NEAREST or Dither.NONE instead. Image.NEAREST: "nearest", /usr/local/lib/python3.7/dist-packages/flowvision/transforms/functional.py:66: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BILINEAR instead. Image.BILINEAR: "bilinear", /usr/local/lib/python3.7/dist-packages/flowvision/transforms/functional.py:67: DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BICUBIC instead. Image.BICUBIC: "bicubic", /usr/local/lib/python3.7/dist-packages/flowvision/transforms/functional.py:68: DeprecationWarning: BOX is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BOX instead. Image.BOX: "box", /usr/local/lib/python3.7/dist-packages/flowvision/transforms/functional.py:69: DeprecationWarning: HAMMING is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.HAMMING instead. Image.HAMMING: "hamming", /usr/local/lib/python3.7/dist-packages/flowvision/transforms/functional.py:70: DeprecationWarning: LANCZOS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.LANCZOS instead. Image.LANCZOS: "lanczos", /usr/local/lib/python3.7/dist-packages/flowvision/data/auto_augment.py:28: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BILINEAR instead. _RANDOM_INTERPOLATION = (Image.BILINEAR, Image.BICUBIC) /usr/local/lib/python3.7/dist-packages/flowvision/data/auto_augment.py:28: DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BICUBIC instead. _RANDOM_INTERPOLATION = (Image.BILINEAR, Image.BICUBIC) .. GENERATED FROM PYTHON SOURCE LINES 59-61 Load a pretrained OneFlow model and save model ---------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 61-69 .. code-block:: default model_name = "resnet18" model = getattr(flowvision.models, model_name)(pretrained=True) model = model.eval() model_dir = "resnet18_model" if not os.path.exists(model_dir): flow.save(model.state_dict(), model_dir) .. rst-class:: sphx-glr-script-out .. code-block:: none Downloading: "https://oneflow-public.oss-cn-beijing.aliyuncs.com/model_zoo/flowvision/classification/ResNet/resnet18.zip" to /workspace/.oneflow/flowvision_cache/resnet18.zip 0%| | 0.00/41.5M [00:00 (1, 3, 224, 224) .. GENERATED FROM PYTHON SOURCE LINES 134-137 Look up synset name ------------------- Look up prediction top 1 index in 1000 class synset. .. GENERATED FROM PYTHON SOURCE LINES 137-184 .. code-block:: default synset_url = "".join( [ "https://raw.githubusercontent.com/Cadene/", "pretrained-models.pytorch/master/data/", "imagenet_synsets.txt", ] ) synset_name = "imagenet_synsets.txt" synset_path = download_testdata(synset_url, synset_name, module="data") with open(synset_path) as f: synsets = f.readlines() synsets = [x.strip() for x in synsets] splits = [line.split(" ") for line in synsets] key_to_classname = {spl[0]: " ".join(spl[1:]) for spl in splits} class_url = "".join( [ "https://raw.githubusercontent.com/Cadene/", "pretrained-models.pytorch/master/data/", "imagenet_classes.txt", ] ) class_name = "imagenet_classes.txt" class_path = download_testdata(class_url, class_name, module="data") with open(class_path) as f: class_id_to_key = f.readlines() class_id_to_key = [x.strip() for x in class_id_to_key] # Get top-1 result for TVM top1_tvm = np.argmax(tvm_output.numpy()[0]) tvm_class_key = class_id_to_key[top1_tvm] # Convert input to OneFlow variable and get OneFlow result for comparison with flow.no_grad(): torch_img = flow.from_numpy(img) output = model(torch_img) # Get top-1 result for OneFlow top_oneflow = np.argmax(output.numpy()) oneflow_class_key = class_id_to_key[top_oneflow] print("Relay top-1 id: {}, class name: {}".format(top1_tvm, key_to_classname[tvm_class_key])) print( "OneFlow top-1 id: {}, class name: {}".format(top_oneflow, key_to_classname[oneflow_class_key]) ) .. rst-class:: sphx-glr-script-out .. code-block:: none Relay top-1 id: 281, class name: tabby, tabby cat OneFlow top-1 id: 281, class name: tabby, tabby cat .. _sphx_glr_download_how_to_compile_models_from_oneflow.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: from_oneflow.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: from_oneflow.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_