如何解决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版本的错误。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。