Last active
October 20, 2022 12:10
-
-
Save josephrocca/3f3d940091c3a6e105155e4b77226487 to your computer and use it in GitHub Desktop.
lyra-v2-soundstream-tflite-to-onnx.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"collapsed_sections": [], | |
"name": "lyra-v2-soundstream-tflite-to-onnx.ipynb", | |
"authorship_tag": "ABX9TyM6H26ubs6lm4nSrFzhRTIo", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
}, | |
"gpuClass": "standard" | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/josephrocca/3f3d940091c3a6e105155e4b77226487/notebook.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"!pip install git+https://github.com/onnx/tensorflow-onnx.git" | |
], | |
"metadata": { | |
"id": "_Our00mFGN_J" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"!wget https://huggingface.co/rocca/lyra-v2-soundstream/resolve/main/tflite/quantizer.tflite" | |
], | |
"metadata": { | |
"id": "go1tTAjRF3KO" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# By default it exports the \"decode\" graph:\n", | |
"!python -m tf2onnx.convert --opset 17 --tflite quantizer.tflite --output dequantizer.onnx --verbose --debug" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "BC4yz6N-MhzZ", | |
"outputId": "fca99469-b565-42b0-bb07-ac620b7e5100" | |
}, | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"/usr/lib/python3.7/runpy.py:125: RuntimeWarning: 'tf2onnx.convert' found in sys.modules after import of package 'tf2onnx', but prior to execution of 'tf2onnx.convert'; this may result in unpredictable behaviour\n", | |
" warn(RuntimeWarning(msg))\n", | |
"2022-10-20 12:09:39,310 - INFO - tf2onnx: inputs: None\n", | |
"2022-10-20 12:09:39,311 - INFO - tf2onnx: outputs: None\n", | |
"2022-10-20 12:09:39,444 - INFO - tf2onnx.tfonnx: Using tensorflow=2.9.2, onnx=1.12.0, tf2onnx=1.12.0/ddca3a\n", | |
"2022-10-20 12:09:39,444 - INFO - tf2onnx.tfonnx: Using opset <onnx, 17>\n", | |
"INFO: Created TensorFlow Lite XNNPACK delegate for CPU.\n", | |
"2022-10-20 12:09:39,847 - VERBOSE - tf2onnx.tfonnx: Mapping TF node to ONNX node(s)\n", | |
"2022-10-20 12:09:40,100 - VERBOSE - tf2onnx.tfonnx: Mapping TF node to ONNX node(s)\n", | |
"2022-10-20 12:09:40,389 - VERBOSE - tf2onnx.tfonnx: Summay Stats:\n", | |
"\ttensorflow ops: Counter({'Const': 147, 'TFL_RESHAPE': 92, 'TFL_SUB': 91, 'TFL_STRIDED_SLICE': 46, 'TFL_SQUARED_DIFFERENCE': 46, 'TFL_SUM': 46, 'TFL_ARG_MIN': 46, 'TFL_ONE_HOT': 46, 'TFL_MUL': 46, 'TFL_ADD': 46, 'TFL_GATHER': 45, 'TFL_CAST': 2, 'Placeholder': 2, 'Identity': 2, 'TFL_LESS': 1, 'TFL_PACK': 1, 'TFL_ARG_MAX': 1})\n", | |
"\ttensorflow attr: Counter({'fused_activation_function': 183, 'value': 147, 'pot_scale_int16': 137, 'axis': 92, 'output_type': 47, 'begin_mask': 46, 'ellipsis_mask': 46, 'end_mask': 46, 'new_axis_mask': 46, 'shrink_axis_mask': 46, 'keep_dims': 46, 'batch_dims': 45, 'values_count': 1})\n", | |
"\tonnx mapped: Counter({'Const': 192, 'Reshape': 92, 'Sub': 91, 'SquaredDifference': 46, 'StridedSlice': 46, 'Sum': 46, 'ArgMin': 46, 'OneHot': 46, 'Mul': 46, 'Add': 46, 'GatherV2': 45, 'Placeholder': 2, 'Cast': 2, 'Less': 1, 'Pack': 1, 'ArgMax': 1})\n", | |
"\tonnx unmapped: Counter()\n", | |
"2022-10-20 12:09:40,399 - VERBOSE - tf2onnx.tfonnx: Mapping TF node to ONNX node(s)\n", | |
"2022-10-20 12:09:40,561 - VERBOSE - tf2onnx.tfonnx: Mapping TF node to ONNX node(s)\n", | |
"2022-10-20 12:09:40,687 - VERBOSE - tf2onnx.tfonnx: Summay Stats:\n", | |
"\ttensorflow ops: Counter({'Const': 188, 'TFL_STRIDED_SLICE': 93, 'TFL_GATHER': 46, 'TFL_MUL': 46, 'TFL_ADD': 45, 'TFL_MAXIMUM': 1, 'TFL_NOT_EQUAL': 1, 'TFL_CAST': 1, 'Placeholder': 1, 'Identity': 1})\n", | |
"\ttensorflow attr: Counter({'value': 188, 'begin_mask': 93, 'ellipsis_mask': 93, 'end_mask': 93, 'new_axis_mask': 93, 'shrink_axis_mask': 93, 'fused_activation_function': 91, 'axis': 46, 'batch_dims': 46, 'pot_scale_int16': 45})\n", | |
"\tonnx mapped: Counter({'Const': 234, 'StridedSlice': 93, 'GatherV2': 46, 'Mul': 46, 'Add': 45, 'Placeholder': 1, 'NotEqual': 1, 'Maximum': 1, 'Cast': 1})\n", | |
"\tonnx unmapped: Counter()\n", | |
"2022-10-20 12:09:40,688 - INFO - tf2onnx.optimizer: Optimizing ONNX model\n", | |
"2022-10-20 12:09:40,690 - VERBOSE - tf2onnx.optimizer: Apply optimize_transpose\n", | |
"2022-10-20 12:09:40,761 - VERBOSE - tf2onnx.optimizer.TransposeOptimizer: Const -186 (606->420)\n", | |
"2022-10-20 12:09:40,762 - VERBOSE - tf2onnx.optimizer: Apply remove_redundant_upsample\n", | |
"2022-10-20 12:09:40,816 - VERBOSE - tf2onnx.optimizer.UpsampleOptimizer: no change\n", | |
"2022-10-20 12:09:40,816 - VERBOSE - tf2onnx.optimizer: Apply fold_constants\n", | |
"2022-10-20 12:09:41,000 - VERBOSE - tf2onnx.optimizer.ConstFoldOptimizer: no change\n", | |
"2022-10-20 12:09:41,000 - VERBOSE - tf2onnx.optimizer: Apply const_dequantize_optimizer\n", | |
"2022-10-20 12:09:41,068 - VERBOSE - tf2onnx.optimizer.ConstDequantizeOptimizer: no change\n", | |
"2022-10-20 12:09:41,068 - VERBOSE - tf2onnx.optimizer: Apply loop_optimizer\n", | |
"2022-10-20 12:09:41,123 - VERBOSE - tf2onnx.optimizer.LoopOptimizer: no change\n", | |
"2022-10-20 12:09:41,123 - VERBOSE - tf2onnx.optimizer: Apply merge_duplication\n", | |
"2022-10-20 12:09:41,191 - VERBOSE - tf2onnx.optimizer.MergeDuplicatedNodesOptimizer: Const -230 (420->190)\n", | |
"2022-10-20 12:09:41,191 - VERBOSE - tf2onnx.optimizer: Apply reshape_optimizer\n", | |
"2022-10-20 12:09:41,227 - VERBOSE - tf2onnx.optimizer.ReshapeOptimizer: no change\n", | |
"2022-10-20 12:09:41,227 - VERBOSE - tf2onnx.optimizer: Apply global_pool_optimizer\n", | |
"2022-10-20 12:09:41,278 - VERBOSE - tf2onnx.optimizer.GlobalPoolOptimizer: no change\n", | |
"2022-10-20 12:09:41,278 - VERBOSE - tf2onnx.optimizer: Apply q_dq_optimizer\n", | |
"2022-10-20 12:09:41,318 - VERBOSE - tf2onnx.optimizer.QDQOptimizer: no change\n", | |
"2022-10-20 12:09:41,318 - VERBOSE - tf2onnx.optimizer: Apply remove_identity\n", | |
"2022-10-20 12:09:41,356 - VERBOSE - tf2onnx.optimizer.IdentityOptimizer: Identity -1 (1->0)\n", | |
"2022-10-20 12:09:41,356 - VERBOSE - tf2onnx.optimizer: Apply remove_back_to_back\n", | |
"2022-10-20 12:09:41,393 - VERBOSE - tf2onnx.optimizer.BackToBackOptimizer: no change\n", | |
"2022-10-20 12:09:41,393 - VERBOSE - tf2onnx.optimizer: Apply einsum_optimizer\n", | |
"2022-10-20 12:09:41,429 - VERBOSE - tf2onnx.optimizer.EinsumOptimizer: no change\n", | |
"2022-10-20 12:09:41,429 - VERBOSE - tf2onnx.optimizer: Apply optimize_transpose\n", | |
"2022-10-20 12:09:41,475 - VERBOSE - tf2onnx.optimizer.TransposeOptimizer: no change\n", | |
"2022-10-20 12:09:41,476 - VERBOSE - tf2onnx.optimizer: Apply remove_redundant_upsample\n", | |
"2022-10-20 12:09:41,513 - VERBOSE - tf2onnx.optimizer.UpsampleOptimizer: no change\n", | |
"2022-10-20 12:09:41,513 - VERBOSE - tf2onnx.optimizer: Apply fold_constants\n", | |
"2022-10-20 12:09:41,555 - VERBOSE - tf2onnx.optimizer.ConstFoldOptimizer: no change\n", | |
"2022-10-20 12:09:41,555 - VERBOSE - tf2onnx.optimizer: Apply const_dequantize_optimizer\n", | |
"2022-10-20 12:09:41,592 - VERBOSE - tf2onnx.optimizer.ConstDequantizeOptimizer: no change\n", | |
"2022-10-20 12:09:41,592 - VERBOSE - tf2onnx.optimizer: Apply loop_optimizer\n", | |
"2022-10-20 12:09:41,655 - VERBOSE - tf2onnx.optimizer.LoopOptimizer: no change\n", | |
"2022-10-20 12:09:41,655 - VERBOSE - tf2onnx.optimizer: Apply merge_duplication\n", | |
"2022-10-20 12:09:41,724 - VERBOSE - tf2onnx.optimizer.MergeDuplicatedNodesOptimizer: no change\n", | |
"2022-10-20 12:09:41,724 - VERBOSE - tf2onnx.optimizer: Apply reshape_optimizer\n", | |
"2022-10-20 12:09:41,791 - VERBOSE - tf2onnx.optimizer.ReshapeOptimizer: no change\n", | |
"2022-10-20 12:09:41,791 - VERBOSE - tf2onnx.optimizer: Apply global_pool_optimizer\n", | |
"2022-10-20 12:09:41,886 - VERBOSE - tf2onnx.optimizer.GlobalPoolOptimizer: no change\n", | |
"2022-10-20 12:09:41,886 - VERBOSE - tf2onnx.optimizer: Apply q_dq_optimizer\n", | |
"2022-10-20 12:09:41,955 - VERBOSE - tf2onnx.optimizer.QDQOptimizer: no change\n", | |
"2022-10-20 12:09:41,955 - VERBOSE - tf2onnx.optimizer: Apply remove_identity\n", | |
"2022-10-20 12:09:41,996 - VERBOSE - tf2onnx.optimizer.IdentityOptimizer: no change\n", | |
"2022-10-20 12:09:41,997 - VERBOSE - tf2onnx.optimizer: Apply remove_back_to_back\n", | |
"2022-10-20 12:09:42,210 - VERBOSE - tf2onnx.optimizer.BackToBackOptimizer: no change\n", | |
"2022-10-20 12:09:42,210 - VERBOSE - tf2onnx.optimizer: Apply einsum_optimizer\n", | |
"2022-10-20 12:09:42,246 - VERBOSE - tf2onnx.optimizer.EinsumOptimizer: no change\n", | |
"2022-10-20 12:09:42,257 - INFO - tf2onnx.optimizer: After optimization: Const -416 (606->190), Identity -1 (1->0)\n", | |
"2022-10-20 12:09:42,293 - INFO - tf2onnx: \n", | |
"2022-10-20 12:09:42,293 - INFO - tf2onnx: Successfully converted TensorFlow model quantizer.tflite to ONNX\n", | |
"2022-10-20 12:09:42,293 - INFO - tf2onnx: Model inputs: ['decode_encoding_indices:0']\n", | |
"2022-10-20 12:09:42,294 - INFO - tf2onnx: Model outputs: ['StatefulPartitionedCall:0']\n", | |
"2022-10-20 12:09:42,294 - INFO - tf2onnx: ONNX model is saved at dequantizer.onnx\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# If I try to specify the inputs and outputs of the \"encode\" graph it gives an error:\n", | |
"!python -m tf2onnx.convert --opset 17 --tflite quantizer.tflite --inputs encode_input_frames:0,encode_num_quantizers:0 --outputs StatefulPartitionedCall_1:1,StatefulPartitionedCall_1:0 --output quantizer.onnx --verbose --debug" | |
], | |
"metadata": { | |
"id": "taxo4wAwNGQo", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "cf576639-9d58-48b3-e0f8-25cfd833348b" | |
}, | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"/usr/lib/python3.7/runpy.py:125: RuntimeWarning: 'tf2onnx.convert' found in sys.modules after import of package 'tf2onnx', but prior to execution of 'tf2onnx.convert'; this may result in unpredictable behaviour\n", | |
" warn(RuntimeWarning(msg))\n", | |
"2022-10-20 12:09:46,376 - INFO - tf2onnx: inputs: ['encode_input_frames:0', 'encode_num_quantizers:0']\n", | |
"2022-10-20 12:09:46,377 - INFO - tf2onnx: outputs: ['StatefulPartitionedCall_1:1', 'StatefulPartitionedCall_1:0']\n", | |
"2022-10-20 12:09:46,391 - INFO - tf2onnx.tfonnx: Using tensorflow=2.9.2, onnx=1.12.0, tf2onnx=1.12.0/ddca3a\n", | |
"2022-10-20 12:09:46,391 - INFO - tf2onnx.tfonnx: Using opset <onnx, 17>\n", | |
"INFO: Created TensorFlow Lite XNNPACK delegate for CPU.\n", | |
"2022-10-20 12:09:46,727 - ERROR - /usr/local/lib/python3.7/dist-packages/tf2onnx/utils.py: \n", | |
"Failed to convert: inputs/outputs specified do not exist, make sure your passedin format: input/output_node_name:port_id. Problematic inputs/outputs are: {'StatefulPartitionedCall_1:1', 'encode_input_frames:0', 'StatefulPartitionedCall_1:0', 'encode_num_quantizers:0'} \n", | |
"\n", | |
"Traceback (most recent call last):\n", | |
" File \"/usr/lib/python3.7/runpy.py\", line 193, in _run_module_as_main\n", | |
" \"__main__\", mod_spec)\n", | |
" File \"/usr/lib/python3.7/runpy.py\", line 85, in _run_code\n", | |
" exec(code, run_globals)\n", | |
" File \"/usr/local/lib/python3.7/dist-packages/tf2onnx/convert.py\", line 706, in <module>\n", | |
" main()\n", | |
" File \"/usr/local/lib/python3.7/dist-packages/tf2onnx/convert.py\", line 291, in main\n", | |
" output_path=args.output)\n", | |
" File \"/usr/local/lib/python3.7/dist-packages/tf2onnx/convert.py\", line 165, in _convert_common\n", | |
" custom_op_handlers=custom_op_handlers, **kwargs)\n", | |
" File \"/usr/local/lib/python3.7/dist-packages/tf2onnx/tfonnx.py\", line 453, in process_tf_graph\n", | |
" main_g, subgraphs = graphs_from_tflite(tflite_path, input_names, output_names)\n", | |
" File \"/usr/local/lib/python3.7/dist-packages/tf2onnx/tflite_utils.py\", line 158, in graphs_from_tflite\n", | |
" utils.check_io(input_names, output_names, output_shapes.keys())\n", | |
" File \"/usr/local/lib/python3.7/dist-packages/tf2onnx/utils.py\", line 316, in check_io\n", | |
" raise ValueError(\"Inputs/Outputs Not Found\")\n", | |
"ValueError: Inputs/Outputs Not Found\n" | |
] | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment