|
1 | 1 | Visualizing an ONNX Model |
2 | 2 | ========================= |
3 | 3 |
|
4 | | -To visualize an onnx model, we can use the [net drawer tool](https://github.com/onnx/onnx/blob/master/onnx/tools/net_drawer.py). This tool takes in a serialized ONNX model and produces a directed graph representation. The graph contains this information: |
| 4 | +To visualize an ONNX model, we can use the [net drawer tool](https://github.com/onnx/onnx/blob/master/onnx/tools/net_drawer.py). This tool takes in a serialized ONNX model and produces a directed graph representation. The graph contains the following information: |
5 | 5 |
|
6 | 6 | * Tensors |
7 | 7 | * Input/output tensors |
8 | 8 | * Intermediate tensors |
9 | | -* Ops |
| 9 | +* Operators (ops) |
10 | 10 | * Op type |
11 | 11 | * Op number |
12 | 12 | * Input tensor names |
13 | 13 | * Output tensor names |
14 | | - * Docstrings (Pytorch exports stack traces here, so this is a good way to get your bearings about the network topology) |
| 14 | + * Docstrings (PyTorch exports stack traces, so this is a good way to become familiarized with the network topology) |
15 | 15 |
|
16 | 16 | ## SqueezeNet Example |
17 | 17 |
|
18 | | -Let's walk through an example visualizing a [SqueezeNet](https://arxiv.org/abs/1602.07360) model exported from [Pytorch](https://github.com/bwasti/AICamera/blob/master/Exporting%20Squeezenet%20to%20mobile.ipynb). An example visualization: |
| 18 | +Let's walk through an example visualizing a [SqueezeNet](https://arxiv.org/abs/1602.07360) model exported from [Pytorch](https://github.com/bwasti/AICamera/blob/master/Exporting%20Squeezenet%20to%20mobile.ipynb). Here's an example visualization: |
19 | 19 |
|
20 | 20 |  |
21 | 21 |
|
22 | | -#### Prerequisites |
23 | | -* You will need [Graphviz](http://www.graphviz.org/), and particularly the `dot` command-line utility. |
| 22 | +**Prerequisites** |
| 23 | +* You will need [Graphviz](http://www.graphviz.org/) – specifically, the `dot` command-line utility. |
24 | 24 | * You'll need the `pydot` Python package. |
25 | | -* For the net drawer, you will need [ONNX](https://github.com/onnx/onnx), both installed and cloned somewhere (so you have access to the net_drawer.py file). |
26 | | -* For the optional part (i.e. experimentation) you'll need Pytorch and Numpy |
| 25 | +* For the net drawer, you will need [ONNX](https://github.com/onnx/onnx), both installed and cloned somewhere (so that you have access to the `net_drawer.py` file). |
| 26 | +* For the optional part (i.e., experimentation), you'll need PyTorch and Numpy. |
27 | 27 |
|
28 | | -#### Convert an exported ONNX model to a Graphviz representation |
| 28 | +### Convert an exported ONNX model to a Graphviz representation |
29 | 29 |
|
30 | | -In the `assets` folder you should find a file named `squeezenet.onnx`. This is a serialized SqueezeNet model that was exported to ONNX from Pytorch. Go into your ONNX repository and run the following command: |
| 30 | +In the `assets` folder, you should find a file named `squeezenet.onnx`. This is a serialized SqueezeNet model that was exported to ONNX from PyTorch. Go into your ONNX repository and run the following: |
31 | 31 |
|
32 | 32 | python onnx/tools/net_drawer.py --input <path to squeezenet.onnx> --output squeezenet.dot --embed_docstring |
33 | | - |
34 | | -The command line flags are as follows: |
35 | 33 |
|
36 | | -- `input` specifies the input filename, i.e. the serialized ONNX model you'd like to visualize |
| 34 | +The command line flags are described below: |
| 35 | + |
| 36 | +- `input` specifies the input filename (i.e., the serialized ONNX model you would like to visualize). |
37 | 37 | - `output` specifies where to write the Graphviz `.dot` file. |
38 | | -- `embed_docstring` specifies that you'd like to embed the doc_string for each node in the graph visualization. This is implemented as a javascript alert() that is fired when you click on the node. |
| 38 | +- `embed_docstring` specifies that you'd like to embed the doc_string for each node in the graph visualization. This is implemented as a JavaScript alert() that occurs when you click on the node. |
39 | 39 |
|
40 | 40 | Now, we have a Graphviz file `squeezenet.dot`. We need to convert it into a viewable format. Let's convert this into an `svg` file like so: |
41 | 41 |
|
42 | 42 | dot -Tsvg squeezenet.dot -o squeezenet.svg |
43 | | - |
44 | | -You should now have an `svg` file named `squeezenet.svg`. Now open this file in a web browser (I've tried Chrome and Firefox and they both work). |
45 | 43 |
|
46 | | -#### Interpreting the graph |
| 44 | +You should now have an `svg` file named `squeezenet.svg`. Open this file in a web browser. |
| 45 | + |
| 46 | +### Interpreting the graph |
47 | 47 |
|
48 | | -Within the graph, white hexagons represent tensors and green rectangles represent ops. Within the op nodes, inputs are listed in order and outputs are listed in order. Note that the position of the Hexagons with respect to the ops does NOT represent input order. Finally, clicking on each op node will bring up an alert that contains the doc string (stack trace for Pytorch) that may have useful information about each node. |
| 48 | +Within the graph, white hexagons represent tensors and green rectangles represent ops. Within the op nodes, inputs and outputs are listed in order. Note that the position of the hexagons with respect to the ops does NOT represent input order. Clicking on each op node will bring up an alert that contains the doc string (stack trace for PyTorch), and may have useful information about each node. |
49 | 49 |
|
50 | | -#### (Optional) Exporting the ONNX model |
| 50 | +### (Optional) Exporting the ONNX model |
51 | 51 |
|
52 | | -This is the code that I used to create the exported model. You can put this into a Python script if you'd like to experiment: |
| 52 | +To create the exported model, you can put this into a Python script: |
53 | 53 |
|
54 | 54 | ```python |
55 | 55 | # Some standard imports |
|
0 commit comments