diff --git a/notebooks/FINN-HowToAnalysisPass.ipynb b/notebooks/FINN-HowToAnalysisPass.ipynb index a99f5af944ac7817509d695920426c94b867cf47..2bf36f163503c03298661aa83b20245432aef22c 100644 --- a/notebooks/FINN-HowToAnalysisPass.ipynb +++ b/notebooks/FINN-HowToAnalysisPass.ipynb @@ -5,6 +5,7 @@ "metadata": {}, "source": [ "# FINN - Analysis passes\n", + "--------------------------------------\n", "\n", "* traverses the graph structure and produces information about certain properties\n", "* input: ModelWrapper\n", @@ -15,57 +16,68 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Example - Quantity analysis of nodes in onnx graph" + "## Example - Quantity analysis of nodes in onnx graph\n", + "----------------------------------------------------------------------" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "import onnx\n", + "onnx_model = onnx.load('LFC.onnx')" + ] + }, + { + "cell_type": "code", + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "import torch\n", - "from models.LFC import LFC\n", - "import brevitas.onnx as bo\n", "from finn.core.modelwrapper import ModelWrapper\n", - "\n", - "\n", - "trained_lfc_w1a1_checkpoint = (\n", - " \"/workspace/brevitas_cnv_lfc/pretrained_models/LFC_1W1A/checkpoints/best.tar\"\n", - ")\n", - "export_onnx_path = \"test_output_lfc.onnx\"\n", - "lfc = LFC(weight_bit_width=1, act_bit_width=1, in_bit_width=1)\n", - "checkpoint = torch.load(trained_lfc_w1a1_checkpoint, map_location=\"cpu\")\n", - "lfc.load_state_dict(checkpoint[\"state_dict\"])\n", - "bo.export_finn_onnx(lfc, (1, 1, 28, 28), export_onnx_path)\n", - "model = ModelWrapper(export_onnx_path)\n" + "onnx_model = ModelWrapper(onnx_model)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Here a picture or a call for onnx_model in netron**" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def count_equal_nodes(model):\n", + " count_dict = {}\n", + " for node in model.graph.node:\n", + " if node.op_type in count_dict:\n", + " count_dict[node.op_type] +=1\n", + " else:\n", + " count_dict[node.op_type] = 1\n", + " return count_dict" + ] + }, + { + "cell_type": "code", + "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "<finn.core.modelwrapper.ModelWrapper object at 0x7f0c3311fda0>\n" + "{'Shape': 1, 'Gather': 1, 'Unsqueeze': 5, 'Concat': 1, 'Reshape': 1, 'Mul': 5, 'Sub': 1, 'Sign': 4, 'MatMul': 4, 'BatchNormalization': 3, 'Squeeze': 3}\n" ] } ], "source": [ - "print(model)" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [], - "source": [ - "import onnx\n", - "onnx.save(model.model, 'test.onnx')\n" + "print(count_equal_nodes(onnx_model))" ] }, { diff --git a/notebooks/LFC.onnx b/notebooks/LFC.onnx new file mode 100644 index 0000000000000000000000000000000000000000..1247f9224a67d9285616fd3ff8038265ac96bcae Binary files /dev/null and b/notebooks/LFC.onnx differ