【问题标题】:tensorflow.js :TypeError: Cannot read property 'length' of undefined or null referencetensorflow.js:TypeError:无法读取未定义或空引用的属性“长度”
【发布时间】:2019-07-15 03:48:18
【问题描述】:

我将模型加载到 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()

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

【问题讨论】:

  • 是不是在转换模型的时候或者上面代码的执行过程中出现了错误?

标签: model tensorflow.js


【解决方案1】:

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

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

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

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

【讨论】:

  • 谢谢!您的回答确实帮助我找出了我的错误...我没有将 1.0 转换为张量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多