model - tensorflow.js:TypeError:无法读取未定义或空引用的属性“长度”
问题描述
我将模型加载到 tensorflow.js 中以对图像进行分类,但它返回:
"Unable to get property 'length' of undefined or null reference", message: "Unable to get property 'length' of undefined or null reference", number: -2146823281, stack: "TypeError: Unable to get property 'length' of undefined or null reference at Anonymous function(https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-converter:17:131397)
我使用的浏览器是chrome,模型是通过python中的tensorflow获取的
这是我的进口
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"> </script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-converter">`</script>
这是我的代码
<script>
const MODEL_URL = './tensorflowjs_model.pb'
const WEIGHTS_URL = './weights_manifest.json'
const INPUT_NODE_NAME = 'x';
const OUTPUT_NODE_NAME = 'prediction';
const PREPROCESS_DIVISOR = tf.scalar(255);
const foot=document.getElementById('foot')
async function fun() {
const resultElement=document.getElementById('result')
resultElement.innerText = 'Loading MobileNet...'
const model=await tf.loadFrozenModel(MODEL_URL, WEIGHTS_URL)
const pixels = tf.browser.fromPixels(foot);
var preprocessedInput = tf.div(pixels.asType('float32'), PREPROCESS_DIVISOR);
reshapedInput=tf.image.resizeNearestNeighbor(preprocessedInput,[28,28])
reshapedInput=reshapedInput.reshape([-1,28,28,3])
try{
var output;
output=model.predict({x:reshapedInput,keep_prob:1.0},OUTPUT_NODE_NAME)
console.log(output)
}
catch(err){
console.log(err)
}
console.log("hello")
};
fun()
我哪里做错了?由于截止日期快到了,我真的需要帮助......谢谢!
解决方案
错误很可能是你使用的方式造成的predict
output=model.predict({x:reshapedInput,keep_prob:1.0},OUTPUT_NODE_NAME)
predict将张量或张量数组作为参数。
由于您已经加载了frozenModel,您可以考虑model.execute
改用。
推荐阅读
- java - 网络申请预订表格
- android - 扫描时自动更改屏幕方向(使用 ZXING 库)
- android - 底部导航视图不起作用
- java - 文件夹中的文件存储在 _nXm_ 矩阵中,其中 n 给出行数,m 给出列数
- javascript - Webpack:如何更改导出模块的全局变量名称?
- angular - 使用 Get 而不是 Post 的 Angular 方式数据表
- ios - 如何将一个 Storyboard 的 ContainerView 链接到另一个 Storyboard 中的 ViewController?
- c# - 有没有办法将自定义枚举集合转换为另一个自定义枚举集合?
- python - 计算最大和最小特征值
- java - Primefaces 数据表不适用于过滤器 + 排序