Tensorflow tfjs AutoML TypeError:无法读取未定义的属性“读取”

问题描述

GCP的对象检测模型已导出为tensorflow.js模型。

模型已正确加载,但model.detect()函数返回

Uncaught (in promise) TypeError: Cannot read property 'read' of undefined

以下是使用的代码

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <Meta charset="UTF-8">
    <Meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>Webpage</title>
</head>

<body>
    <img src="./img.jpg" id="salad">

    <button type="button" id="predict" onclick="predict()">Predict</button>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-automl/dist/tf-automl.min.js"></script>
    <script>
        async function predict() {

            // Used localhost URL because the path has to be an absolute URL
            const model = await tf.automl.loadobjectDetection('http://localhost:5500/dist/model.json');
            console.log(model);
            const img = document.getElementById('salad');
            const options = {
                score: 0.5,IoU: 0.5,topk: 20
            };
            const predictions = await model.detect(img,options);
            console.log(predictions);
            // Show the resulting object on the page.
            const pre = document.createElement('pre');
            pre.textContent = JSON.stringify(predictions,null,2);
            document.body.append(pre);
        }
        predict();
    </script>
</body>

</html>

控制台成功记录了模型变量,它显示了模型的所有正确属性

这是程序遇到model.detect()时抛出的错误堆栈:

Uncaught (in promise) TypeError: Cannot read property 'read' of undefined
    at control_executor.js:184
    at u (runtime.js:45)
    at Generator._invoke (runtime.js:274)
    at Generator.forEach.t.<computed> [as next] (runtime.js:97)
    at Wm (runtime.js:728)
    at o (runtime.js:728)
    at runtime.js:728
    at new Promise (<anonymous>)
    at runtime.js:728
    at control_executor.js:376
    at operation_executor.js:61
    at tM (operation_executor.js:52)
    at h (graph_executor.js:453)
    at t.e.processstack (graph_executor.js:439)
    at t.<anonymous> (graph_executor.js:401)
    at u (runtime.js:45)
    at Generator._invoke (runtime.js:274)
    at Generator.forEach.t.<computed> [as next] (runtime.js:97)
    at Wm (runtime.js:728)
    at o (runtime.js:728)

解决方法

设置“ @ tensorflow / tfjs-node”的版本:“ 2.0.0”已为我修复

显然,这是当前2.1.0版本的错误。