This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] SK-AM62A-LP:带模型分析器的 TFlite 人脸检测模型

Guru**** 2426510 points


请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/processors-group/processors/f/processors-forum/1537408/sk-am62a-lp-tflite-face-detection-model-with-model-analyzer

器件型号:SK-AM62A-LP


工具/软件:

我计划针对 AM62Ax 进行开发、现在我将运行模型分析器。

我尝试编译自定义模型。 我当前使用的模型是“face_detection_front_128_integer_quant.tflite",“,可、可从以下 URL 获取:
https://s3.ap-northeast-2.wasabisys.com/pinto-model-zoo/030_BlazeFace/resources.tar.gz

我已经检查了这个模型在 CPU 上工作正常。 我已经根据示例笔记本多次尝试编译它、但编译失败、并出现以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[3], line 1
----> 1 interpreter = tflite.Interpreter(model_path=tflite_model_path, experimental_delegates=tidl_delegate)
      2 #interpreter = tflite.Interpreter(model_path=tflite_model_path)

File /usr/local/lib/python3.10/dist-packages/tflite_runtime/interpreter.py:489, in Interpreter.__init__(self, model_path, model_content, experimental_delegates, num_threads, experimental_op_resolver_type, experimental_preserve_all_tensors)
    487   self._delegates = experimental_delegates
    488   for delegate in self._delegates:
--> 489     self._interpreter.ModifyGraphWithDelegate(
    490         delegate._get_native_delegate_pointer())  # pylint: disable=protected-access
    491 #self._signature_defs = self.get_signature_list() #PC-- commented for now. Workaround. Needs to be added to interpreter_wrapper2
    493 self._metrics = metrics.TFLiteMetrics()

ValueError: basic_string::_M_create

---------------------------------------------------------------------------
MemoryError                               Traceback (most recent call last)
Cell In[6], line 1
----> 1 interpreter = tflite.Interpreter(model_path=tflite_model_path, experimental_delegates=tidl_delegate)
      2 #interpreter = tflite.Interpreter(model_path=tflite_model_path)

File /usr/local/lib/python3.10/dist-packages/tflite_runtime/interpreter.py:489, in Interpreter.__init__(self, model_path, model_content, experimental_delegates, num_threads, experimental_op_resolver_type, experimental_preserve_all_tensors)
    487   self._delegates = experimental_delegates
    488   for delegate in self._delegates:
--> 489     self._interpreter.ModifyGraphWithDelegate(
    490         delegate._get_native_delegate_pointer())  # pylint: disable=protected-access
    491 #self._signature_defs = self.get_signature_list() #PC-- commented for now. Workaround. Needs to be added to interpreter_wrapper2
    493 self._metrics = metrics.TFLiteMetrics()

MemoryError: std::bad_alloc

日志:e2e.ti.com/.../74862.custon_2D00_model_2D00_tfl_5F00_out.log

另一方面、我能够在 Ubuntu 22.04 上使用 EdgeAI-TIDLP-TOOLS 10_01 编译和运行该模型。

为什么将 EdgeAI 工具更改为较新的工具会使编译成功? (我认识到,模型分析器使用 EdgeAI-tools 09_02 或 10_00、因为是 SDK 版本。 是 9.2)
是否有办法在模型分析器上成功编译此模型?

Python 代码:

import sys
import time
import os
import cv2
import numpy as np
import tflite_runtime.interpreter as tflite
from utils import loggerWriter, plot_TI_performance_data, get_benchmark_output
from PIL import Image
import matplotlib.pyplot as plt

output_dir = 'face_detection_quant'
tflite_model_path = './face_detection_front_128_integer_quant.tflite'
debug_level = 0
num_bits = 8
accuracy = 1
compile_options = {
    'tidl_tools_path' : os.environ['TIDL_TOOLS_PATH'],
    'artifacts_folder' : output_dir,
    'tensor_bits' : num_bits,
    'accuracy_level' : accuracy,
    'debug_level' : debug_level,
    'advanced_options:calibration_frames' : 1,
    'advanced_options:calibration_iterations' : 3,
    'advanced_options:add_data_convert_ops' : 1,
}
os.makedirs(output_dir, exist_ok=True)
for root, dirs, files in os.walk(output_dir, topdown=False):
    [os.remove(os.path.join(root, f)) for f in files]
    [os.rmdir(os.path.join(root, d)) for d in dirs]

tidl_delegate = [tflite.load_delegate(os.path.join(os.environ['TIDL_TOOLS_PATH'], 'tidl_model_import_tflite.so'), compile_options)]
interpreter = tflite.Interpreter(model_path=tflite_model_path, experimental_delegates=tidl_delegate)


谢谢

Fumiya

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    我的编译失败已解决。
    我注意到当处理某些层时编译过程会终止。

    在编译选项中添加“deny_list":"2,22"“:"2,22"将“将导致“导致成功编译。

    我遇到了另一个问题。
    所有填充层都在 CPU 上操作、原因是“仅支持填充宽度/高度轴“。
    该模型具有许多填充层、因此在不进行卸载的情况下运行模型需要很长时间。

    我是否可以修改模型以便卸载填充层?

    Total Nodes = 90
    -------------------------------------------------------------------------------
    |          Core           |      No. of Nodes       |   Number of Subgraphs   |
    -------------------------------------------------------------------------------
    | C7x                     |                      73 |                      12 |
    | CPU                     |                      17 |                       x |
    -------------------------------------------------------------------------------
    -------------------------------------------------------------------------------
    | Node |      Node Name     |                     Reason                      |
    -------------------------------------------------------------------------------
    | PAD  | channel_padding    | Padding is only supported for width/height axes |
    | PAD  | channel_padding_1  | Padding is only supported for width/height axes |
    | PAD  | channel_padding_2  | Padding is only supported for width/height axes |
    | PAD  | channel_padding_3  | Padding is only supported for width/height axes |
    | PAD  | channel_padding_4  | Padding is only supported for width/height axes |
    | PAD  | channel_padding_5  | Padding is only supported for width/height axes |
    | PAD  | channel_padding_6  | Padding is only supported for width/height axes |
    | PAD  | channel_padding_7  | Padding is only supported for width/height axes |
    | PAD  | channel_padding_8  | Padding is only supported for width/height axes |
    | PAD  | channel_padding_9  | Padding is only supported for width/height axes |
    | PAD  | channel_padding_10 | Padding is only supported for width/height axes |
    -------------------------------------------------------------------------------

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Fumiya:

    很高兴您已经解决了使用模型分析器时遇到的问题。  

    [引述 userid=“661369" url="“ url="~“~/support/processors-group/processors/f/processors-forum/1537408/sk-am62a-lp-tflite-face-detection-model-with-model-analyzer/5915266 #5915266“]

    我是否可以修改模型以便卸载填充层?

    [/报价]

    根据尝试实现填充的图层、您可以通过在填充图层之前和之后添加整形图层来更新模型、以便填充仍在宽度/高度访问中。  

    请告诉我这个解决方案对于您的模型是否可行。

    此致、

    Qutaiba

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Qutaiba:

    感谢您的答复和信息。
    这似乎是可行的。

    我调查了在处理整形图层时编译终止的原因。
    似乎有两种方法可以指定整形图层的输出形状:作为属性或作为输入参数。

    https://github.com/tensorflow/tensorflow/issues/45150


    我的模型使用了前一个模型、但似乎没有 TIDL 导入的支持。 后者得到支持。
    通过修改模型以使用后一种方法、不再需要为“deny_layer"指定“指定整形图层。

    然后我会尝试你的建议。  结果将在稍后报告。

    此致、

    Fumiya

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Fumiya:

    感谢您分享信息。 我们很高兴在需要时提供帮助。  

    此致、

    Qutaiba

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    很抱歉晚才回复。
    根据您的建议、我能够修改模型、以便卸载所有层。

    但是、卸载模型会返回意外结果。
    此模型在 CPU 上运行良好、因此我认为我的编译设置错误。
    修改后的模型和编译生成的 SVG 文件: e2e.ti.com/.../face_5F00_landmark_5F00_float32.zip

    我的编译设置:

    compile_options = {
        'tidl_tools_path' : os.environ['TIDL_TOOLS_PATH'],
        'artifacts_folder' : output_dir,
        'tensor_bits' : 16,
        'accuracy_level' : 9,
        'debug_level' : 2,
        'advanced_options:calibration_frames' : 1,
        'advanced_options:quantization_scale_type' : 4,
        'advanced_options:channel_wise_quantization' : 1,
    }

    为什么造成差异?

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Fumiya:

    请考虑以下几点:

    此致、

    Qutaiba

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好、 Fumiya、

    我将从我们这边帮助确保准确性  

    您能否确认、  无论是 TIDL(使用 tensor_bits=32 编译)还是标准 Arm 后端、网络中都有完全相同的输入? 如果您使用的是 edgeai-tidl-tools/example/osrt_python/tbl/tflrt_delegen.py、则使用-d 标签 (disable_offload) 将确保这一点。

    • 否则、此处精度的巨大差异表明我们的 32 位浮点基准模式(即 tensor_bits=32)存在问题。 我们可以预计任何量化版本 (tensor_bits=8 或 16) 也将是错误的。

    ------------------------------------------------------------------------------------------------------------------------------------------------

    我注意到您的模型包括带有斜率参数的 PreLU 图层。 TIDL 不支持该层类型[0]的此斜率参数。 这似乎是精度问题最可能的罪魁祸首。 由于 TIDL 不支持斜率、因此它的作用类似于普通的 RELU

    您的模型是您在网上找到的预训练网络、还是您自己进行了培训? 可能需要将 PRELU 替换为另一个激活功能、这将需要重新培训。 您可以尝试拒绝列出这些图层、但这将对性能产生巨大影响、并否定加速器的大部分性能提升。  

    我记得谷歌的人脸网状网络面临的类似挑战——这看起来像一个类似的 模型体系结构

    也就是说、您的模型中的 PreLU 图层不受支持。 这些激活需要替换为 TIDL 的另一个激活、以提供适当的精度。 更换激活通常需要重新培训。  

    [0] https://github.com/TexasInstruments/edgeai-tidl-tools/blob/master/docs/supported_ops_rts_versions.md --见 PreLU

    BR、
    Reese

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Reese:

    输入是相同的。

    如您所说、该模型基于中点面网格模型。
    我对其进行了一些修改以避免编译错误。

    面网格的训练数据集似乎不可用。
    我需要找到一个没有 PRELU 图层的模型吗?
    或者、是否有办法利用 TIDL 对模型进行精细的运行?

    此致、
    Fumiya

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好、Fumiya、  

    好的、这就是我的怀疑。 这在过去也带来了类似的挑战(特别是因为 Google 尚未发布其数据集),不幸的是,与我合作的开发人员需要找到一个替代模型。  

    是的、您应该找到一个不包含 PreLU 图层的模型(或者,如果包含这些图层,则不使用斜率参数)。 由于这是一个激活功能并在网络中多次使用、因此我们无法将其指定为在 Arm 内核上运行而不会造成巨大的性能损失。 在为此层类型添加更好的支持之前、我目前看不到在 TIDL 中运行该网络的可行路径。

    目前正在调查这一问题、但支持这一层的时间表不够明确、不能更加具体。  

    BR、
    Reese

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好 Reese、

    感谢你的帮助。 我将尝试找到符合要求的模型。

    此致、
    Fumiya