【问题标题】:How to interpret TFLite output tensor如何解释 TFLite 输出张量
【发布时间】:2020-05-30 21:40:26
【问题描述】:

我正在 python 上执行 TFLite 模型,以便根据输入数据进行预测。该模型已经在 AutoML-Google-API 上进行了训练,然后我下载了它的 TFLite 模型。我使用 tf.lite.Interpreter 加载模型并运行推理,如下所示

input_details = interpreter.get_input_details();print(input_details )
output_details = interpreter.get_output_details();print(output_details )
//...preparing input_data
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index']);print(output_data )

结果如下:

input_details: 
[{'name': 'image',
  'index': 0,
  'shape': array([  1, 224, 224,   3]),
  'dtype': numpy.uint8,
  'quantization': (0.007874015718698502, 128)}]
output_details: 
[{'name': 'scores',
  'index': 173,
  'shape': array([ 1, 10]),
  'dtype': numpy.uint8,
  'quantization': (0.00390625, 0)}]
output_data : 
array([[ 34, 100,  67,  14,  15,  24,  21,  18,  25,  37]], dtype=uint8)

output_data 有一些整数,说“它的最大数的索引对应于预测的标签”是真的吗,如何将这些数字转换为概率?

【问题讨论】:

    标签: python tensorflow google-api tensorflow-lite softmax


    【解决方案1】:

    您可以使用softmax将数字转换为概率分布,使用argmax获取概率最大的标签的索引。

    所以是这样的:

    output_probs = tf.math.softmax(output_data)
    pred_label = tf.math.argmax(output_probs)
    

    【讨论】:

    • 谢谢。对于softmax输出,probs的总和等于1。'output_data'的总和怎么样?当我根据给定的输入图像将 output_data 的值相加时,它会给出不同的值。在上面的例子中,总和是(34+100+67+...=355),但是对于其他样本,我得到了不同的值,例如 345、370、... 正常吗?
    • 是的,这看起来很正常,因为网络的直接输出只是输出层的权重。像你这样的分类器的正常过程是使用 softmax 和 argmax 来获取预测标签的索引。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    • 2021-03-24
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    相关资源
    最近更新 更多