如何解释 tensorflow.js 中对象检测模型的输出 [英] How to interpret the output of object detection model in tensorflow.js

查看:33
本文介绍了如何解释 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 \/内容/网络模型

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

<头><script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"><script src="webcam.js"></script><身体><div><div><视频自动播放内联静音 id="wc" width="224" height="224"></video>

<button type="button" id="startPredicting" onclick="startPredicting()" >开始预测</button><button type="button" id="stopPredicting" onclick="stopPredicting()" >停止预测</button><div id="预测"></div><script src="index.js"></script></html>

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

let 模型;const webcam = new Webcam(document.getElementById('wc'));让 isPredicting = false;异步函数 init(){尝试 {等待 webcam.setup();模型 = 等待 tf.loadGraphModel('http://127.0.0.1:8887/model/model.json');} 抓住(错误){控制台日志(错误);}}异步函数预测(){const img = webcam.capture();console.log("执行模型");const cat = document.getElementById('image');输出 = 等待 model.executeAsync(img);output.forEach(t => t.print)//注销所有张量的数据常量数据 = []for (let i = 0; i < output.length; i++){data.push(output.dataSync())}控制台日志(数据);}在里面()函数开始预测(){isPredicting = true;预测();}函数停止预测(){isPredicting = false;预测();}

当我使用 Web 服务器在 inference.html 文件上方运行时,它返回以下输出:

(4) [t, t, t, t]0: t {kept: false, isDisposedInternal: false, shape: Array(3), dtype: "float32", size: 400, ...}1:t {保持:假,isDisposedInternal:假,形状:数组(2),数据类型:float32",大小:100,...}2:t {保持:假,isDisposedInternal:假,形状:数组(2),数据类型:float32",大小:100,...}3:t {保持:假,isDisposedInternal:假,形状:数组(1),数据类型:float32",大小:1,...}长度:4__proto__:数组(0)

问题是输出似乎无关或我无法理解.我错过了什么吗?请向我提供您的建议.我很抱歉这篇长文章,但我是 tensorflow.js 的初学者.

解决方案

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

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

从张量中取出数据作为javaScript数组,方法如data(分别为dataSync)和dataArray(分别为<可以调用 code>dataArraySync) 以异步(分别同步)检索数据.数据作为 typedArray 检索.

output = await model.executeAsync(img);//输出是一个 tf.tensor 数组.output.forEach(t => t.print())//注销所有张量的数据常量数据 = []for (让 i = 0; i < output.length; i++)data.push(output[i].dataSync())//获取数据

I am trying to run custom object detection tensorflow.js model in a browser. I could able to convert tensorflow model to tensorflow.js model (in google colab) using the following command:

!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

I am sharing the code snippet of inference.html file:

<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>

The code snippet of index.js file is as follow:

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();
}

When I run above inference.html file using web server, it returns the following output:

(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)

The problem is output seems to be irrelevant or I can't understand it. Am I missing something? Please provide me your suggestions. I am sorry for the long post but I am beginner in tensorflow.js.

解决方案

output is a tf.Tensor. When you called console.log(output), it tries to stringify the object and prints out its properties.

The tensor also has the method, print to log out its data.

To get the data out of the tensor as a javaScript array, method such as data (respectively dataSync) and dataArray (respectively dataArraySync) can be called to retrieve the data aynchronously (respectively synchronously). The data is retrieved as a 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

这篇关于如何解释 tensorflow.js 中对象检测模型的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆