工具/软件:
在我们的图像分类流水线中、TI model-zoo 模型需要 fp32 输入。
我们目前正在将 Uint8 图像数据转换为 ARM 处理器上的 fp32、对其进行标准化、然后将其传递到深度学习加速器。
如果深度学习加速器能够执行归一化、我们可以绕过 fp32 转换并直接输入 Uint8 图像、这样应该会减少延迟。
这种做法是否可行?
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.
Hi Mitani-san、
我可能不理解您的问题、但模型使用 uint/int8 在本机运行。 图像数据通常是 8 位数据的三个通道、即 R 0-255、G 0-255 和 B 0-255。 因此数据应保持为 INT8 格式。 如果需要对其进行标准化、假设一个通道与其他通道明显偏离(这也很不寻常)、您可以显式添加批标准化层或调整导入配置文件(TIDLRT 语法,如下所示)。
#inDataNorm = 1
#inscale = 0.00392156862745098 0.00392156862745098 0.00392156862745098
#inMean = 0 0 0
#inQuantFactor = 255 255 255
您可能 只需要 inDataNorm =1。 当然,如果您需要比例和平均值,数字会根据您的输入数据而变化。
此致、
Chris
感谢您始终如此迅速地回应。
很抱歉没有提供足够的解释。 例如、当您 在 Netron 中显示来自 TI Model Zoo (software-dl.ti.com/.../resnet18.onnx) 的 ResNet18 时、结果就是这样。
使用 onnxruntime 的代码如下所示、但如果输入“x"在“在 fp32 中不是张量、则会导致错误。
将 onnxruntime 作为 ort 导入
SOO = ort.SessionOptions()
ep_list =['TIDLExecutionProvider'、'CPUExecutionProvider']
会话= ort.ConferenceSession (model_path、provider=ep_list、provider_options=[provider_options、{}]、sess_options=so)
out = session.run(None、{input_name:np.array (x)})
这就是我推断模型的输入格式是 fp32 的原因,但如果我误解了任何内容,请告诉我。
谢谢你。
Mitani-san,
我看到您现在正在主机上运行这个、所以回答正确。 在编译 ONNX 模型之前、它是本机 float32。 您首先需要在图像文件中读取(在本例中,png 为 3、r、g、b)
调用 GET_RGB_CHANNES(文件)后、数组中将有一个红色、绿色和蓝色 8 位通道。 若要将其转换为 float32、只需取 8 位数组并通过以下方式进行转换:
float32_red= red.astype (np.float32)
则可以将其用作模型的输入。
此外、以下是一些使用随机数据(无需图像)对此进行测试的代码:
SOO = ort.SessionOptions()
ep_list =['TIDLExecutionProvider'、'CPUExecutionProvider']
会话= ort.ConferenceSession (model_path、provider=ep_list、provider_options=[provider_options、{}]、sess_options=so)
input_details =session.get_inputs()
output_details =session.get_outputs()
Input_dict ={}
output_dict ={}
对于 I IN 范围 (len (input_details)):
np.random.seed(0)
if (input_details[i]。type =='tensor (float)'):
input_data = np.random.randn (* input_details[i]。shape).astype (np.float32)
elif (input_details[i]。type =='tensor (int64)'):
input_data = np.random.randn (* input_details[i]。shape).astype (np.int64)
elif (input_details[i]。type =='tensor (uint8)'):
input_data = np.random.randn (* input_details[i]。shape).astype (np.uint8)
elif (input_details[i]。type =='tensor (int32)'):
input_data = np.random.randn (* input_details[i]。shape).astype (np.int32)
否则:
input_data = np.random.randn (* input_details[i]。shape).astype (np.float32)
Input_dict[input_details[i]。name]= input_data
start_time = time.time ()
输出= list (session.run(None、input_dict)
对于 I IN 范围 (len (output_details)):
output_dict[output_details[i]。name]= output[i]
Hi Mitani-san、
您可以将 TIDLRT 中的 numParamBits 设置为 32 或在 OSRT 设置 tensor_bits = 32。 在 examples/osrt_python/common_utils.py 中、将 tensort_bits = 8 更改为 16。 对于 TIDLRT、在导入文件中添加如下行:
numParamBits = 16
或
numParamBits = 32
请注意、执行此操作后性能会大幅下降。 一种更好的方法是识别需要更高分辨率的层、并通过以下方式将其设置为 16 位模式:
params16bitNamesList =“layer1、layer2、LayerN“
此致、
Chris