{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Draw a pipeline\n\nThere is no other way to look into one model stored\nin ONNX format than looking into its node with\n*onnx*. This example demonstrates\nhow to draw a model and to retrieve it in *json*\nformat.\n\n## Retrieve a model in JSON format\n\nThat's the most simple way.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from onnxruntime.datasets import get_example\n\nexample1 = get_example(\"mul_1.onnx\")\n\nimport onnx\n\nmodel = onnx.load(example1)  # model is a ModelProto protobuf message\n\nprint(model)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Draw a model with ONNX\nWe use [net_drawer.py](https://github.com/onnx/onnx/blob/main/onnx/tools/net_drawer.py)\nincluded in *onnx* package.\nWe use *onnx* to load the model\nin a different way than before.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from onnx import ModelProto\n\nmodel = ModelProto()\nwith open(example1, \"rb\") as fid:\n    content = fid.read()\n    model.ParseFromString(content)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We convert it into a graph.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from onnx.tools.net_drawer import GetOpNodeProducer, GetPydotGraph\n\npydot_graph = GetPydotGraph(\n    model.graph, name=model.graph.name, rankdir=\"LR\", node_producer=GetOpNodeProducer(\"docstring\")\n)\npydot_graph.write_dot(\"graph.dot\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Then into an image\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\n\nos.system(\"dot -O -Tpng graph.dot\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Which we display...\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nimage = plt.imread(\"graph.dot.png\")\nplt.imshow(image)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}