使用浏览器检测图像中的Tensorflow JS错误

问题描述

我正在尝试使用tensorflow js检测图像中的对象。

在这里Tensorflow Object Detection with Tensorflow 2: Creating a custom model

中遵循了教程

因此,一切都可以在google collab中正常运行,而对象检测也可以正常运行。

我得到了一个saveed_model,现在我想在浏览器中使用它。

所以我使用转换模型

tensorflowjs_converter  --input_format=tf_saved_model --output_format=tfjs_graph_model --signature_name=serving_default --saved_model_tags=serve /content/inference_graph/saved_model /mobilenet/web_model 

然后我下载相同的文件

我现在正在尝试使用以下html在浏览器中使用它

<!DOCTYPE html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <Meta name="viewport" content="width=device-width,initial-scale=1.0">
    <Meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"> </script>
    <style>
        .container {
            display: flex;
            flex-wrap: wrap;
        }
    </style>
</head>
<body>

    <div class="container">
        <div>
            <img id="test1" src="test/IMG_20181228_102636.jpg" width="300" height="300" />
            <div id="test1_pred">WORKING</div>        
        </div>
        
    </div>

    <script>
        async function predict() {
            const model = await tf.loadGraphModel('http://localhost:8000/mobilenet/web_model/model.json');
            const img = document.getElementById('test1');
            const tfImg = tf.browser.fromPixels(img);
            const smalImg = tf.image.resizeBilinear(tfImg,[300,300]);
            const resized = tf.cast(smalImg,'int32');
            const t4d = tf.tensor4d(Array.from(smalImg.dataSync()),[1,300,3]);
            let predictions = await model.executeAsync(tf.cast(t4d,"int32"));
            console.log(predictions);
        }

        setTimeout(function() {
            predict();
        },1000);
    </script>

</body>
</html>

我在控制台上看到的错误

tfjs:17 Uncaught (in promise) Error: Error in matMul: inputs must have the same rank of at least 2,got ranks 5 and 2.
    at xv (tfjs:17)
    at matMul_ (tfjs:17)
    at matMul__op (tfjs:17)
    at tfjs:17
    at tfjs:17
    at tfjs:17
    at t.e.scopedRun (tfjs:17)
    at t.e.tidy (tfjs:17)
    at $b (tfjs:17)
    at tfjs:17

我对这里的错误不知所措。似乎与矩阵中的秩有关,但不确定是什么原因导致了不匹配。

我应该如何调整图像大小以使其适合模型使用?

我还在下面的model.json

中发布了我的模型。

任何帮助将不胜感激

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)