运行TFLite模型遇TypeError:仅尺寸为1的数组可转为Python标量
问题描述
用TensorFlow官网的示例代码运行自行构建的TFLite模型时,始终触发如下错误:
Traceback (most recent call last):
File "label_image.py", line 133, in
print('{:08.6f}: {}'.format(float(results[i]), labels[i]))
TypeError: only size-1 arrays can be converted to Python scalars
推测是模型输出格式问题,已尝试用NXP的iIQ Toolkit创建TFLite模型,将输出数据类型设置为int8、uint8和float32,但问题未解决。
附完整运行代码:
# Copyright 2018 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """label_image for tflite.""" import argparse import time import numpy as np from PIL import Image import tflite_runtime.interpreter as tflite import sys np.set_printoptions(threshold=sys.maxsize) def load_labels(filename): with open(filename, 'r') as f: return [line.strip() for line in f.readlines()] if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument( '-i', '--image', default='Images/sh_17_11_23_s70m_0048.JPG', help='image to be classified') parser.add_argument( '-m', '--model_file', default='detection-balanced-mcu-2024-02-09T11-36-31.710Z_in-uint8.tflite', help='.tflite model to be executed') parser.add_argument( '-l', '--label_file', default='labels.txt', help='name of file containing labels') parser.add_argument( '--input_mean', default=127.5, type=float, help='input_mean') parser.add_argument( '--input_std', default=127.5, type=float, help='input standard deviation') parser.add_argument( '--num_threads', default=None, type=int, help='number of threads') parser.add_argument( '-e', '--ext_delegate', help='external_delegate_library path') parser.add_argument( '-o', '--ext_delegate_options', help='external delegate options, \ format: "option1: value1; option2: value2"') args = parser.parse_args() ext_delegate = None ext_delegate_options = {} # parse extenal delegate options if args.ext_delegate_options is not None: options = args.ext_delegate_options.split(';') for o in options: kv = o.split(':') if (len(kv) == 2): ext_delegate_options[kv[0].strip()] = kv[1].strip() else: raise RuntimeError('Error parsing delegate option: ' + o) # load external delegate if args.ext_delegate is not None: print('Loading external delegate from {} with args: {}'.format( args.ext_delegate, ext_delegate_options)) ext_delegate = [ tflite.load_delegate(args.ext_delegate, ext_delegate_options) ] interpreter = tflite.Interpreter( model_path=args.model_file, experimental_delegates=ext_delegate, num_threads=args.num_threads) interpreter.allocate_tensors() input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() # check the type of the input tensor floating_model = input_details[0]['dtype'] == np.float32 # NxHxWxC, H:1, W:2 height = input_details[0]['shape'][1] width = input_details[0]['shape'][2] img = Image.open(args.image).resize((width, height)) # add N dim input_data = np.expand_dims(img, axis=0) if floating_model: input_data = (np.float32(input_data) - args.input_mean) / args.input_std interpreter.set_tensor(input_details[0]['index'], input_data) # ignore the 1st invoke startTime = time.time() interpreter.invoke() delta = time.time() - startTime print("Warm-up time:", '%.1f' % (delta * 1000), "ms\n") startTime = time.time() interpreter.invoke() delta = time.time() - startTime print("Inference time:", '%.1f' % (delta * 1000), "ms\n") output_data = interpreter.get_tensor(output_details[0]['index']) results = np.squeeze(output_data) top_k = results.argsort()[-5:][::-1] labels = load_labels(args.label_file) for i in top_k: if floating_model: print('{:08.6f}: {}'.format(float(results[i]), labels[i])) else: print('{:08.6f}: {}'.format(float(results[i] / 255.0), labels[i]))
非常规解决思路
- 强制压平输出维度:在
results = np.squeeze(output_data)之后,添加一行results = np.ravel(results),确保输出彻底变为一维数组,避免嵌套数组的情况。 - 校验输出维度结构:在获取
output_data后,直接打印print(output_data.shape)和print(results.shape),确认输出维度是否符合预期。如果输出是[1, N, 1]这类格式,squeeze后仍为二维,需手动选取对应维度(比如results = results[0])。 - 兼容输出处理逻辑:把循环里的
float(results[i])改成float(results[i].flatten()[0]),直接提取子数组中的唯一元素,兼容输出存在单元素子数组的情况。 - 排查模型导出后处理:确认iIQ Toolkit导出模型时是否自动添加了冗余的后处理节点(如多余维度的softmax、批量归一化),尝试在导出时禁用不必要的后处理,或手动在模型末尾添加维度压缩层。
- 解析模型输出元信息:使用
tflite_inspect model --model_file=你的模型.tflite命令,查看模型输出张量的形状和数据类型,确认是否和代码预期一致,排除导出时的维度配置错误。
内容的提问来源于stack exchange,提问作者fhwfz
相关产品推荐
相关产品推荐

