使用 Tensorflow.js 运行对象检测 [英] Running object detection using Tensorflow.js

查看:81
本文介绍了使用 Tensorflow.js 运行对象检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on object detection using 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='detection_boxes,detection_scores,detection_classes,num_detections' \
/content/frozen_inference_graph.pb \
/content/web_model

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

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

<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"> </script>
<!--<script src="webcam.js"></script>-->
      <img id="img" src="257.jpg" width="300" height="300"/>

</head>
<body>
    <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 文件的代码片段如下[更新]:

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

let model;
let isPredicting = false;

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

async function predict() {
        console.log("executing model");
        const img = document.getElementById('img');

        tf_img = tf.browser.fromPixels(img);
        tf.print(tf_img)

        tf_img = tf_img.expandDims(0);

        console.log(tf_img.shape)  // Image dimension is  [1, 300, 300, 3]

         let output = await model.executeAsync(
        { 'image_tensor' : tf_img},
        [ 'detection_boxes','detection_scores','detection_classes','num_detections']);


        for (let i = 0; i < output.length; i++){
            console.log(output[i].dataSync())
        }

 }

init()


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

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

它产生以下输出[更新]:

我查看了上面的输出,但我无法获得类标签等.如何提取 detection_classes、detection_scores 和 detection_boxes?该模型与 python 代码一起正常工作.

I looked at the above output but I couldn't get class labels etc. How can I extract detection_classes, detection_scores, and detection_boxes? This model works properly with python code.

[更新]:看起来,我在提供 [1,300,300,3] 图像作为模型输入后得到了输出.

[Updated]: It seems like, I am getting the output after providing [1,300,300,3] image as input to the model.

你能指导我吗?我错过了什么吗?

Could you please guide me? Am I missing something?

推荐答案

终于找到了问题所在,它与输入框的大小有关.

Finally, I could figure out the problem and it was related to the size of an input frame.

SSD 模型需要 [1,300,300,3] 图像/帧的形状作为输入.我在我的代码中添加了这个并得到了解决方案.使用以下行(在 inference.html 中),我们可以将 (300,300,3) 形状的图像作为模型的输入:

SSD model needs shape of [1,300,300,3] image/frame as input. I added this in my code and got the solution. Using the following line (in inference.html), we can feed (300,300,3) shape of image as an input to the model:

 <img id="img" src="257.jpg" width="300" height="300"/>

index.js 中使用以下几行:

Using the following lines in index.js:

 tf_img = tf_img.expandDims(0);
 console.log(tf_img.shape)  // Image dimension is  [1, 300, 300, 3]

我们得到SSD需要的[1,300,300,3]的图像形状.

We obtain image shape of [1,300,300,3] which is needed by SSD.

这篇关于使用 Tensorflow.js 运行对象检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆