Skip to content
Snippets Groups Projects
Commit 01a5c097 authored by auphelia's avatar auphelia
Browse files

[jupyter notebook] Added more explanations to make it easier to understand the procedure

parent 2999b501
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# FINN - Analysis passes
--------------------------------------
* traverses the graph structure and produces information about certain properties
* input: ModelWrapper
* return dictionary of named properties that the analysis extracts
* <font size="3">traverses the graph structure and produces information about certain properties</font>
* <font size="3">input: ModelWrapper</font>
* <font size="3">return dictionary of named properties that the analysis extracts</font>
%% Cell type:markdown id: tags:
## Example - Quantity analysis of nodes in onnx graph
----------------------------------------------------------------------
<font size="3">Purpose of this analysis pass is to return the number of similar nodes in a dictionary. So first an onnx model is loaded. In this example a trained brevitas model is used. It was exported from brevitas and saved as .onnx file. With the help of `import onnx` the load function can be accessed. As argument it takes the model path.</font>
%% Cell type:code id: tags:
``` python
import onnx
onnx_model = onnx.load('LFC.onnx')
onnx_model = onnx.load('LFCW1A1.onnx')
```
%% Cell type:markdown id: tags:
**Here a picture or a call for onnx_model in netron**
%% Cell type:markdown id: tags:
<font size="3">The onnx model has to be converted to a format that can be processed by FINN. This is done with ModelWrapper. As described in the short introduction, this is the format an analysis pass takes as input.</font>
%% Cell type:code id: tags:
``` python
from finn.core.modelwrapper import ModelWrapper
onnx_model = ModelWrapper(onnx_model)
```
%% Cell type:markdown id: tags:
**Here a picture or a call for onnx_model in netron**
<font size="3">The idea is to count all nodes that have the same operation type. The result should contain the operation types and the corresponding number of nodes that occur in the model. At the beginning an empty dictionary is created which is filled by the function and returned as result to the user at the end of the analysis.</font>
%% Cell type:code id: tags:
``` python
def count_equal_nodes(model):
count_dict = {}
for node in model.graph.node:
if node.op_type in count_dict:
count_dict[node.op_type] +=1
else:
count_dict[node.op_type] = 1
return count_dict
```
%% Cell type:markdown id: tags:
<font size="3">The function takes the model as input and iterates over the nodes. Then it is checked whether there is already an entry for the operation type in the dictionary. If this is not the case, an entry is created and set to `1`. If there is already an entry, it is incremented. If all nodes in the model have been iterated, the filled dictionary is returned.</font>
%% Cell type:code id: tags:
``` python
print(count_equal_nodes(onnx_model))
```
%% Output
{'Shape': 1, 'Gather': 1, 'Unsqueeze': 5, 'Concat': 1, 'Reshape': 1, 'Mul': 5, 'Sub': 1, 'Sign': 4, 'MatMul': 4, 'BatchNormalization': 3, 'Squeeze': 3}
%% Cell type:code id: tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment