如何解释在tensorflow.js中目标检测模型的输出

huangapple go评论102阅读模式
英文:

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

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

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

问题在于输出似乎与目标检测结果无关,或者我无法理解它。我是否漏掉了什么?请提供您的建议。对于这个长篇帖子,我感到抱歉,但我是 TensorFlow.js 的初学者。

英文:

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=&#39;detection_boxes,detection_scores,detection_classes,num_detections&#39; \
/content/frozen_inference_graph.pb \
/content/web_model

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

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

&lt;script src=&quot;index.js&quot;&gt;&lt;/script&gt;
&lt;/html&gt;

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

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


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

async function predict() {
    const img = webcam.capture();
    console.log(&quot;executing model&quot;);
    const cat = document.getElementById(&#39;image&#39;);
    output = await model.executeAsync(img);
    output.forEach(t =&gt; t.print) // log out the data of all tensors
    const data = []
    for (let i = 0; i &lt; 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: &quot;float32&quot;, size: 400, …}
1: t {kept: false, isDisposedInternal: false, shape: Array(2), dtype: &quot;float32&quot;, size: 100, …}
2: t {kept: false, isDisposedInternal: false, shape: Array(2), dtype: &quot;float32&quot;, size: 100, …}
3: t {kept: false, isDisposedInternal: false, shape: Array(1), dtype: &quot;float32&quot;, 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.

答案1

得分: 3

output 是一个 tf.Tensor。当你调用 console.log(output) 时,它会尝试将对象转换为字符串并打印出其属性。

这个张量还具有方法 print,可以用来记录其数据。

要将张量中的数据作为 JavaScript 数组获取,可以调用诸如 data(或 dataSync)和 dataArray(或 dataArraySync)等方法以异步(或同步)方式检索数据。数据以 typedArray 的形式获取。

output = await model.executeAsync(img);
// output 是一个 tf.tensor 数组。
output.forEach(t => t.print()) // 记录所有张量的数据
const data = []
for (let i = 0; i < output.length; i++)
  data.push(output[i].dataSync())  // 获取数据
英文:

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.

<!-- language: lang-js -->

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

huangapple
  • 本文由 发表于 2020年1月3日 17:13:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定