首页 > 解决方案 > 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()

我哪里做错了?由于截止日期快到了,我真的需要帮助......谢谢!

标签: modeltensorflow.js

解决方案


错误很可能是你使用的方式造成的predict

output=model.predict({x:reshapedInput,keep_prob:1.0},OUTPUT_NODE_NAME)

predict将张量或张量数组作为参数。

由于您已经加载了frozenModel,您可以考虑model.execute改用。


推荐阅读