高通AI应用程序开发3:网络模型(三)模型转换

TFLite 模型转换

机器学习框架具有用于存储神经网络模型的特定格式。Qualcomm®神经处理SDK通过将这些模型转换为框架中立的深度学习容器(DLC)格式来支持这些不同的模型。DLC文件由Qualcomm®神经处理SDK运行时用于执行神经网络。

可以使用以下指令将训练好的Tensorflow模型转换为TFLte模型(.tflite)文件

snpe-tflite-to-dlc 工具将tflite模型转换为等效的Qualcomm®神经处理SDK dlc文件。以下命令将Inception v3 TFLite模型转换为Qualcomm®神经处理SDK DLC文件。

snpe-tflite-to-dlc --input_network inception_v3.tflite
                   --input_dim input "1,299,299,3"
                   --output_path inception_v3.dlc

Inception v3模型文件可以从以下网址获得 https://tfhub.dev/tensorflow/lite-model/inception_v3/1/default/1

注:

要查看当前支持的TFlite操作列表,请参阅操作支持表

Qualcomm®神经处理SDK和TFlite Converter目前仅支持浮点输入数据类型。

基于MLIR的TFLite转换器的某些旧版本存在一些已知问题,可能会导致加载模型失败。

snpe-tflite-to-dlc

snpe-tflite-to-dlc将tflite模型转换为dlc文件

usage: snpe-tflite-to-dlc [-d INPUT_NAME INPUT_DIM] [--signature_name SIGNATURE_NAME]
                          [--out_node OUT_NAMES] [--input_type INPUT_NAME INPUT_TYPE]
                          [--input_dtype INPUT_NAME INPUT_DTYPE] [--input_encoding  ...]
                          [--input_layout INPUT_NAME INPUT_LAYOUT] [--custom_io CUSTOM_IO]
                          [--preserve_io [PRESERVE_IO [PRESERVE_IO ...]]] [--dump_relay DUMP_RELAY]
                          [--quantization_overrides QUANTIZATION_OVERRIDES]
                          [--keep_quant_nodes] [--disable_batchnorm_folding]
                          [--expand_lstm_op_structure]
                          [--keep_disconnected_nodes] --input_network INPUT_NETWORK [-h]
                          [--debug [DEBUG]] [-o OUTPUT_PATH] [--copyright_file COPYRIGHT_FILE]
                          [--float_bitwidth FLOAT_BITWIDTH] [--float_bw FLOAT_BW]
                          [--float_bias_bw FLOAT_BIAS_BW] [--model_version MODEL_VERSION]
                          [--validation_target RUNTIME_TARGET PROCESSOR_TARGET] [--strict]
                          [--udo_config_paths CUSTOM_OP_CONFIG_PATHS [CUSTOM_OP_CONFIG_PATHS ...]]
                          [--op_package_lib OP_PACKAGE_LIB]
                          [--converter_op_package_lib CONVERTER_OP_PACKAGE_LIB]
                          [-p PACKAGE_NAME | --op_package_config CUSTOM_OP_CONFIG_PATHS [CUSTOM_OP_CONFIG_PATHS ...]]

Script to convert TFLite model into DLC

required arguments:
    --input_network INPUT_NETWORK, -i INPUT_NETWORK
                        Path to the source framework model.

optional arguments:
    -d INPUT_NAME INPUT_DIM, --input_dim INPUT_NAME INPUT_DIM
                        The names and dimensions of the network input layers specified in the format
                        [input_name comma-separated-dimensions], for example:
                            'data' 1,224,224,3
                        Note that the quotes should always be included in order to handlespecial
                        characters, spaces, etc.
                        For multiple inputs specify multiple --input_dim on the command line like:
                            --input_dim 'data1' 1,224,224,3 --input_dim 'data2' 1,50,100,3
    --signature_name SIGNATURE_NAME, -sn SIGNATURE_NAME
                        Specifies a specific subgraph signature to convert.
    --out_node OUT_NAMES, --out_name OUT_NAMES
                        Name of the graph's output Tensor Names. Multiple output names should be
                        provided separately like:
                            --out_name out_1 --out_name out_2
    --input_type INPUT_NAME INPUT_TYPE, -t INPUT_NAME INPUT_TYPE
                        Type of data expected by each input op/layer. Type for each input is
                        |default| if not specified. For example: "data" image.Note that the quotes
                        should always be included in order to handle special characters, spaces,etc.
                        For multiple inputs specify multiple --input_type on the command line.
                        Eg:
                           --input_type "data1" image --input_type "data2" opaque
                        These options get used by DSP runtime and following descriptions state how
                        input will be handled for each option.
                        Image:
                        Input is float between 0-255 and the input's mean is 0.0f and the input's
                        max is 255.0f. We will cast the float to uint8ts and pass the uint8ts to the
                        DSP.
                        Default:
                        Pass the input as floats to the dsp directly and the DSP will quantize it.
                        Opaque:
                        Assumes input is float because the consumer layer(i.e next layer) requires
                        it as float, therefore it won't be quantized.
                        Choices supported:
                           image
                           default
                           opaque
    --input_dtype INPUT_NAME INPUT_DTYPE
                        The names and datatype of the network input layers specified in the format
                        [input_name datatype], for example:
                            'data' 'float32'
                        Default is float32 if not specified
                        Note that the quotes should always be included in order to handlespecial
                        characters, spaces, etc.
                        For multiple inputs specify multiple --input_dtype on the command line like:
                            --input_dtype 'data1' 'float32' --input_dtype 'data2' 'float32'
    --input_encoding INPUT_ENCODING [INPUT_ENCODING ...], -e INPUT_ENCODING [INPUT_ENCODING ...]
                        Usage:     --input_encoding "INPUT_NAME" INPUT_ENCODING_IN
                        [INPUT_ENCODING_OUT]
                        Input encoding of the network inputs. Default is bgr.
                        e.g.
                           --input_encoding "data" rgba
                        Quotes must wrap the input node name to handle special characters,
                        spaces, etc. To specify encodings for multiple inputs, invoke
                        --input_encoding for each one.
                        e.g.
                            --input_encoding "data1" rgba --input_encoding "data2" other
                        Optionally, an output encoding may be specified for an input node by
                        providing a second encoding. The default output encoding is bgr.
                        e.g.
                            --input_encoding "data3" rgba rgb
                        Input encoding types:
                             image color encodings: bgr,rgb, nv21, nv12, ...
                            time_series: for inputs of rnn models;
                            other: not available above or is unknown.
                        Supported encodings:
                            bgr
                            rgb
                            rgba
                            argb32
                            nv21
                            nv12
                            time_series
                            other
    --input_layout INPUT_NAME INPUT_LAYOUT, -l INPUT_NAME INPUT_LAYOUT
                        Layout of each input tensor. If not specified, it will use the default
                        based on the Source Framework, shape of input and input encoding.
                        Accepted values are-
                            NCDHW, NDHWC, NCHW, NHWC, NFC, NCF, NTF, TNF, NF, NC, F, NONTRIVIAL
                        N = Batch, C = Channels, D = Depth, H = Height, W = Width, F = Feature, T = Time
                        NDHWC/NCDHW used for 5d inputs
                        NHWC/NCHW used for 4d image-like inputs
                        NFC/NCF used for inputs to Conv1D or other 1D ops
                        NTF/TNF used for inputs with time steps like the ones used for LSTM op
                        NF used for 2D inputs, like the inputs to Dense/FullyConnected layers
                        NC used for 2D inputs with 1 for batch and other for Channels (rarely used)
                        F used for 1D inputs, e.g. Bias tensor
                        NONTRIVIAL for everything elseFor multiple inputs specify multiple
                        --input_layout on the command line.
                        Eg:
                            --input_layout "data1" NCHW --input_layout "data2" NCHW
                        Note: This flag does not set the layout of the input tensor in the converted DLC.
    --custom_io CUSTOM_IO
                        Use this option to specify a yaml file for custom IO.
    --preserve_io [PRESERVE_IO [PRESERVE_IO ...]]
                        Use this option to preserve IO layout and datatype. The different ways of
                        using this option are as follows:
                            --preserve_io layout <space separated list of names of inputs and
                        outputs of the graph>
                            --preserve_io datatype <space separated list of names of inputs and
                        outputs of the graph>
                        In this case, user should also specify the string - layout or datatype in
                        the command to indicate that converter needs to
                        preserve the layout or datatype. e.g.
                           --preserve_io layout input1 input2 output1
                           --preserve_io datatype input1 input2 output1
                        Optionally, the user may choose to preserve the layout and/or datatype for
                        all the inputs and outputs of the graph.
                        This can be done in the following two ways:
                            --preserve_io layout
                            --preserve_io datatype
                        Additionally, the user may choose to preserve both layout and datatypes for
                        all IO tensors by just passing the option as follows:
                            --preserve_io
                        Note: Only one of the above usages are allowed at a time.
                        Note: --custom_io gets higher precedence than --preserve_io.
    --dump_relay DUMP_RELAY
                        Dump Relay ASM and Params at the path provided with the argument
                        Usage: --dump_relay <path_to_dump>
    --disable_batchnorm_folding
    --expand_lstm_op_structure
                        Enables optimization that breaks the LSTM op to equivalent math ops
    --keep_disconnected_nodes
                        Disable Optimization that removes Ops not connected to the main graph.
                        This optimization uses output names provided over commandline OR
                        inputs/outputs extracted from the Source model to determine the main graph
    -h, --help          show this help message and exit
    --debug [DEBUG]     Run the converter in debug mode.
    -o OUTPUT_PATH, --output_path OUTPUT_PATH
                        Path where the converted Output model should be saved.If not specified, the
                        converter model will be written to a file with same name as the input model
    --copyright_file COPYRIGHT_FILE
                        Path to copyright file. If provided, the content of the file will be added
                        to the output model.
    --float_bitwidth FLOAT_BITWIDTH
                        Use the --float_bitwidth option to select the bitwidth to use when using
                        float for parameters(weights/bias) and activations for all ops  or specific
                        Op (via encodings) selected through encoding, either 32 (default) or 16.
    --float_bw FLOAT_BW
                        Note: --float_bw is deprecated, use --float_bitwidth.
    --float_bias_bw FLOAT_BIAS_BW
                        Use the --float_bias_bw option to select the bitwidth to use for the float
                        bias tensor
    --model_version MODEL_VERSION
                        User-defined ASCII string to identify the model, only first 64 bytes will be
                        stored
    --validation_target RUNTIME_TARGET PROCESSOR_TARGET
                        A combination of processor and runtime target against which model will be
                        validated.
                        Choices for RUNTIME_TARGET:
                           {cpu, gpu, dsp}.
                        Choices for PROCESSOR_TARGET:
                           {snapdragon_801, snapdragon_820, snapdragon_835}.
                        If not specified, will validate model against {snapdragon_820,
                        snapdragon_835} across all runtime targets.
    --strict            If specified, will validate in strict mode whereby model will not
                        be produced if it violates constraints of the specified validation target. If
                        not specified, will validate model in permissive mode against the specified
                        validation target.
    --udo_config_paths CUSTOM_OP_CONFIG_PATHS [CUSTOM_OP_CONFIG_PATHS ...], -udo CUSTOM_OP_CONFIG_PATHS
                        [CUSTOM_OP_CONFIG_PATHS ...]
                        Path to the UDO configs (space separated, if multiple)

Quantizer Options:
    --quantization_overrides QUANTIZATION_OVERRIDES
                        Use this option to specify a json file with parameters to use for
                        quantization. These will override any quantization data carried from
                        conversion (eg TF fake quantization) or calculated during the normal
                        quantization process. Format defined as per AIMET specification.
    --keep_quant_nodes  Use this option to keep activation quantization nodes in the graph rather
                        than stripping them.

Custom Op Package Options:
    --op_package_lib OP_PACKAGE_LIB, -opl OP_PACKAGE_LIB
                        Use this argument to pass an op package library for quantization. Must be in
                        the form <op_package_lib_path:interfaceProviderName> and be separated by a
                        comma for multiple package libs
    --converter_op_package_lib CONVERTER_OP_PACKAGE_LIB, -cpl CONVERTER_OP_PACKAGE_LIB
                        Path to converter op package library compiled by the OpPackage generator.
    -p PACKAGE_NAME, --package_name PACKAGE_NAME
                        A global package name to be used for each node in the Model.cpp file.
                        Defaults to Qnn header defined package name
    --op_package_config CUSTOM_OP_CONFIG_PATHS [CUSTOM_OP_CONFIG_PATHS ...], -opc CUSTOM_OP_CONFIG_PATHS [CUSTOM_OP_CONFIG_PATHS ...]
                        Path to a Qnn Op Package XML configuration file that contains user defined
                        custom operations.

Note: Only one of: {'package_name', 'op_package_config'} can be specified

必要参数

  • --input_network INPUT_NETWORK, -i INPUT_NETWORK

    • 指定一个深度学习模型或框架的源文件路径

可选参数

  • -d INPUT_NAME INPUT_DIM, --input_dim INPUT_NAME INPUT_DIM

    • 这个参数用于指定网络输入层的名称和维度。格式是 [input_name comma-separated-dimensions],例如 'data' 1,224,224,3 表示有一个名为 data 的输入层,其维度是 [1, 224, 224, 3](通常这代表一个批大小为1,高度为224,宽度为224,通道数为3的图像)。注意,如果输入名称或维度中包含特殊字符或空格,则必须将整个字符串用引号括起来。

    • 如果模型有多个输入层,可以通过在命令行中多次指定 --input_dim 来实现,例如 --input_dim 'data1' 1,224,224,3 --input_dim 'data2' 1,50,100,3

  • --signature_name SIGNATURE_NAME, -sn SIGNATURE_NAME

    • 这个参数用于指定要转换的子图签名(如果模型包含多个签名或子图)。这对于处理具有多个导出签名的TensorFlow SavedModel或类似结构特别有用。

  • --out_node OUT_NAMES, --out_name OUT_NAMES

    • 用于指定图中输出张量的名称。如果有多个输出,需要分别指定,例如 --out_name out_1 --out_name out_2。这告诉转换工具哪些节点是模型输出的关键部分。

  • --input_type INPUT_NAME INPUT_TYPE, -t INPUT_NAME INPUT_TYPE

    • 这个参数指定了每个输入操作/层所期望的数据类型。如果不指定,则默认为 |default|。对于每个输入,都可以指定一个类型,例如 "data" image 表示名为 data 的输入层期望的是图像类型的数据。同样,如果输入名称或类型中包含特殊字符或空格,需要用引号括起来。

    • 支持的数据类型包括 imagedefault 和 opaqueimage 类型的数据通常是在0到255之间的浮点数,且假定其均值为0.0f,最大值为255.0f;default 类型则直接将浮点数输入传递给DSP(数字信号处理器),由DSP进行量化;opaque 类型假定输入已经是浮点数,因为后续的消费者层(即下一层)需要浮点数输入,因此不会被量化。

  • --input_dtype INPUT_NAME INPUT_DTYPE

    • 这个参数用于指定网络输入层的名称和数据类型。格式为 [input_name datatype],例如 'data' 'float32'。如果未指定,则默认数据类型为 float32

    • 引号的使用是必要的,以便能够处理包含特殊字符或空格的输入层名称。

    • 对于多个输入层,需要在命令行中多次指定 --input_dtype,例如 --input_dtype 'data1' 'float32' --input_dtype 'data2' 'float32'

  • --input_encoding INPUT_ENCODING [INPUT_ENCODING ...]

    • 这个参数用于指定网络输入层的编码方式。它支持多种图像颜色编码方式(如 bgrrgbrgba 等),以及时间序列数据(time_series)和其他自定义编码(other)。

    • 使用时,可以指定输入层名称和输入编码(可选地,还可以指定输出编码),例如 --input_encoding "data" rgba 或 --input_encoding "data3" rgba rgb

    • 引号的使用是必要的,以便能够处理包含特殊字符或空格的输入层名称。

    • 对于多个输入层,需要为每个输入层分别指定 --input_encoding

  • --input_layout INPUT_NAME INPUT_LAYOUT

    • 这个参数用于指定每个输入张量的布局。布局定义了张量中数据的排列方式,例如,在图像数据中,布局可以指定通道(C)、高度(H)、宽度(W)等维度的顺序。

    • 如果没有指定,它将使用基于源框架、输入形状和输入编码的默认布局。

    • 接受的布局值包括 NCDHWNDHWCNCHWNHWC 等,这些布局分别表示不同维度的顺序(N=批次,C=通道,D=深度,H=高度,W=宽度)。

    • 对于多个输入层,需要在命令行中多次指定 --input_layout,例如 --input_layout "data1" NCHW --input_layout "data2" NCHW

    • 注意:此标志并不设置转换后的DLC(可能是指某种特定格式的深度学习容器)中输入张量的布局。它主要用于在模型转换过程中确保输入数据的正确理解和处理。

  • --custom_io CUSTOM_IO

    • 用于指定一个YAML文件,该文件定义了自定义的输入输出(IO)配置。

  • --preserve_io [PRESERVE_IO [PRESERVE_IO ...]]

    • 用于保留输入输出(IO)的布局和数据类型。可以指定保留特定的输入输出名称的布局或数据类型,或者为图中所有输入输出保留布局或数据类型,甚至可以同时保留所有IO的布局和数据类型。这个选项有几种使用方式,包括仅指定layoutdatatype来保留相应的特性,或者同时指定layoutdatatype(但不推荐同时指定多个具体名称的保留方式)。

  • --dump_relay DUMP_RELAY

    • 将Relay汇编语言(ASM)和参数(Params)转储到指定的路径。这可以用于调试或进一步分析转换后的模型。

  • --disable_batchnorm_folding

    • 禁用批归一化折叠优化。这可能用于避免某些情况下的性能损失或保持模型的特定结构。

  • --expand_lstm_op_structure

    • 启用优化,将LSTM操作分解为等价的数学操作。这有助于在特定的硬件或框架上更好地利用资源或进行进一步的优化。

  • --keep_disconnected_nodes

    • 禁用移除与主图不连接的操作的优化。默认情况下,转换器可能会移除那些对输出没有贡献的操作,但这个选项可以保留这些节点。

  • -h, --help

    • 显示帮助信息并退出。

  • --debug [DEBUG]

    • 以调试模式运行转换器。这可能会输出额外的信息,有助于调试或理解转换过程中的行为。

  • -o OUTPUT_PATH, --output_path OUTPUT_PATH

    • ​​​​​​​指定转换后的输出模型应该保存的路径。如果没有指定,转换器将使用与输入模型相同的名称创建一个文件来保存转换后的模型。

  • --copyright_file COPYRIGHT_FILE

    • 指定版权文件的路径。如果提供了该文件,其内容将被添加到输出模型中。这通常用于在模型中包含版权信息或版权声明。

  • --float_bitwidth FLOAT_BITWIDTH

    • ​​​​​​​用于选择浮点数(如权重/偏置)和激活函数在所有操作或特定操作(通过编码选择)中使用的位宽。可选的位宽包括32位(默认值)或16位。这有助于控制模型的精度和性能之间的平衡。

  • --float_bw FLOAT_BW

  • 这是一个已弃用的选项,用于选择浮点数的位宽。建议使用--float_bitwidth选项代替。

  • --float_bias_bw FLOAT_BIAS_BW

    • ​​​​​​​用于选择浮点数偏置张量使用的位宽。这对于需要特别关注偏置精度的情况很有用。

  • --model_version MODEL_VERSION

    • ​​​​​​​一个用户定义的ASCII字符串,用于标识模型。只有前64个字节将被存储。这有助于在有多个模型版本时进行区分。

  • --validation_target RUNTIME_TARGET PROCESSOR_TARGET

    • ​​​​​​​指定模型验证将针对的处理器和运行时目标的组合。这包括运行时目标(如CPU、GPU、DSP)和处理器目标(如snapdragon_801、snapdragon_820、snapdragon_835)。如果没有指定,工具将默认对指定的处理器目标(snapdragon_820和snapdragon_835)上的所有运行时目标进行模型验证。

  • --strict

    • 如果指定,将以严格模式进行验证,即如果模型违反了指定验证目标的约束,则不会生成模型。如果没有指定,将以宽松模式对指定验证目标进行模型验证。

  • --udo_config_paths CUSTOM_OP_CONFIG_PATHS [CUSTOM_OP_CONFIG_PATHS ...] 或 -udo CUSTOM_OP_CONFIG_PATHS [CUSTOM_OP_CONFIG_PATHS ...]

    • ​​​​​​​指定自定义操作(UDO)配置文件的路径。如果有多个配置文件,它们之间应该用空格分隔。这些配置文件通常包含了自定义操作的实现细节和配置参数。

量化选项

  • --quantization_overrides QUANTIZATION_OVERRIDES

    • 这个选项允许用户指定一个JSON文件,该文件包含用于量化的参数。这些参数将覆盖从转换过程中(如TensorFlow的模拟量化)带来的任何量化数据,或在正常量化过程中计算得到的数据。文件格式遵循AIMET(可能是某个特定的AI模型优化框架)的规范。

  • --keep_quant_nodes:这个选项用于在图中保留激活量化节点,而不是去除它们。在某些情况下,保留这些节点可能对调试或后续处理有用。

Custom Op Package Options(自定义操作包选项)

  • --op_package_lib OP_PACKAGE_LIB, -opl OP_PACKAGE_LIB

    • ​​​​​​​这个选项用于传递一个或多个自定义操作包库给量化工具。这些库必须以<op_package_lib_path:interfaceProviderName>的形式提供,如果有多个库,需要用逗号分隔。

  • --converter_op_package_lib CONVERTER_OP_PACKAGE_LIB, -cpl CONVERTER_OP_PACKAGE_LIB

    • 指定由OpPackage生成器编译的转换器操作包库的路径。

  • -p PACKAGE_NAME, --package_name PACKAGE_NAME

    • 为Model.cpp文件中的每个节点指定一个全局包名。如果未指定,将使用Qnn头文件中定义的包名作为默认值。

  • --op_package_config CUSTOM_OP_CONFIG_PATHS [CUSTOM_OP_CONFIG_PATHS ...], -opc CUSTOM_OP_CONFIG_PATHS [CUSTOM_OP_CONFIG_PATHS ...]

    • 指定一个或多个Qnn操作包XML配置文件的路径,这些文件包含用户定义的自定义操作。

注意:package_nameop_package_config这两个选项只能指定其中一个。这意味着你不能同时为模型指定一个全局包名和自定义操作的配置文件。你需要根据你的需求选择其中一个选项。

其他详细信息:

input_network

  • 转换器支持单个.tflite文件。
  • 转换器–input_network选项指定.tflite文件的路径。
  • 这个论点是必需的。
  • input_dim参数:
  • 指定图形输入节点的输入维度
  • 转换器需要一个节点名称和维度作为输入,它将使用节点输出张量维度创建输入层。在定义图时,通常有一个占位符名称在图中的训练过程中用作输入。占位符张量名称是必须用作参数的名称。也可以使用其他类型的节点作为输入,但是用作输入的节点不会用作输入层以外的层的一部分。

多输入

  • 具有多个输入的网络必须提供–input_dim input_NAME input_dim,每个输入节点一个。
  • 此参数是可选的。

参考网站:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值