aku*_*uba 2 tensorflow tensorflow-lite
我尝试将 TensorflowLite 与来自https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md 的ssdlite_mobilenet_v2_coco 模型一起使用,该模型转换为 tflite 文件,以从我的 Android 应用程序(java )。我执行
interpreter.run(input, output);
Run Code Online (Sandbox Code Playgroud)
其中输入是转换为 ByteBuffer 的图像,输出是浮点数组 - 大小 [1][10][4] 以匹配张量。
如何将此浮点数组转换为一些可读的输出?- 例如获取边界框坐标、对象名称、概率。
好吧,我想通了。首先我在python中运行以下命令:
>>> import tensorflow as tf
>>> interpreter = tf.contrib.lite.Interpreter("detect.tflite")
Run Code Online (Sandbox Code Playgroud)
然后加载 Tflite 模型:
>>> interpreter.allocate_tensors()
>>> input_details = interpreter.get_input_details()
>>> output_details = interpreter.get_output_details()
Run Code Online (Sandbox Code Playgroud)
现在我已经详细了解了输入和输出应该是什么样子
>>> input_details
[{'name': 'normalized_input_image_tensor', 'index': 308, 'shape': array([ 1, 300, 300, 3], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}]
Run Code Online (Sandbox Code Playgroud)
所以输入是转换后的图像 - 形状 300 x 300
>>> output_details
[{'name': 'TFLite_Detection_PostProcess', 'index': 300, 'shape': array([ 1, 10, 4], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}, {'name': 'TFLite_Detection_PostProcess:1', 'index': 301, 'shape': array([ 1, 10], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}, {'name': 'TFLite_Detection_PostProcess:2', 'index': 302, 'shape': array([ 1, 10], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}, {'name': 'TFLite_Detection_PostProcess:3', 'index': 303, 'shape': array([1], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}]
Run Code Online (Sandbox Code Playgroud)
现在我在这个模型中得到了多个输出的规格。我需要改变
interpreter.run(input, output)
Run Code Online (Sandbox Code Playgroud)
到
interpreter.runForMultipleInputsOutputs(inputs, map_of_indices_to_outputs);
Run Code Online (Sandbox Code Playgroud)
其中“输入”是:
private Object[1] inputs;
inputs[0] = imgData; //imgData - image converted to bytebuffer
Run Code Online (Sandbox Code Playgroud)
而 map_of_indices_to_outputs 是:
private Map<Integer, Object> output_map = new TreeMap<>();
private float[1][10][4] boxes;
private float[1][10] scores;
private float[1][10] classes;
output_map.put(0, boxes);
output_map.put(1, classes);
output_map.put(2, scores);
Run Code Online (Sandbox Code Playgroud)
现在运行后,我在框中获得了 10 个对象的坐标,类中的对象索引(在 coco 标签文件中),您必须添加 1 才能获得正确的键!和分数的概率。
希望这对将来的人有所帮助。
| 归档时间: |
|
| 查看次数: |
3925 次 |
| 最近记录: |