如何解释 tensorflow.js 中对象检测模型的输出

How to interpret the output of object detection model in tensorflow.js

我正在尝试 运行 在浏览器中自定义对象检测 tensorflow.js 模型。我可以使用以下命令将 tensorflow 模型转换为 tensorflow.js 模型(在 google colab 中):

!tensorflowjs_converter \
--input_format=tf_frozen_model \
--output_node_names='detection_boxes,detection_scores,detection_classes,num_detections' \
/content/frozen_inference_graph.pb \
/content/web_model

我正在分享 inference.html 文件的代码片段:

<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"> </script>
<script src="webcam.js"></script>
</head>
<body>
    <div>
        <div>
            <video autoplay playsinline muted id="wc" width="224" height="224"></video>
        </div>
    </div>
    <button type="button" id="startPredicting" onclick="startPredicting()" >Start Predicting</button>
    <button type="button" id="stopPredicting" onclick="stopPredicting()" >Stop Predicting</button>
    <div id="prediction"></div>
</body>

<script src="index.js"></script>
</html>

index.js文件的代码片段如下:

let model;
const webcam = new Webcam(document.getElementById('wc'));
let isPredicting = false;


async function init(){
        try {
            await webcam.setup();
            model = await tf.loadGraphModel('http://127.0.0.1:8887/model/model.json');
        } catch (err) {
            console.log(err);
        }
}

async function predict() {
    const img = webcam.capture();
    console.log("executing model");
    const cat = document.getElementById('image');
    output = await model.executeAsync(img);
    output.forEach(t => t.print) // log out the data of all tensors
    const data = []
    for (let i = 0; i < output.length; i++){
        data.push(output.dataSync())
    }
    console.log(data);
}

init()


function startPredicting(){
    isPredicting = true;
    predict();
}

function stopPredicting(){
    isPredicting = false;
    predict();
}

当我运行上面的inference.html文件使用网络服务器时,returns输出如下:

(4) [t, t, t, t]
0: t {kept: false, isDisposedInternal: false, shape: Array(3), dtype: "float32", size: 400, …}
1: t {kept: false, isDisposedInternal: false, shape: Array(2), dtype: "float32", size: 100, …}
2: t {kept: false, isDisposedInternal: false, shape: Array(2), dtype: "float32", size: 100, …}
3: t {kept: false, isDisposedInternal: false, shape: Array(1), dtype: "float32", size: 1, …}
length: 4
__proto__: Array(0)

问题是输出似乎无关紧要或者我无法理解。我错过了什么吗?请给我你的建议。很抱歉这么长 post 但我是 tensorflow.js.

的初学者

output 是一个 tf.Tensor。当您调用 console.log(output) 时,它会尝试将对象字符串化并打印出它的 properties

张量也有方法,print 来注销它的数据。

要将tensor中的数据取出为javaScript数组,方法如data(分别为dataSync)和dataArray(分别为dataArraySync) 可以调用以异步(分别同步)检索数据。数据检索为 typedArray

output = await model.executeAsync(img);
// output is an array of tf.tensor.
output.forEach(t => t.print()) // log out the data of all tensors
const data = []
for (let i = 0; i < output.length; i++)
  data.push(output[i].dataSync())  // get the data