我遇到了 Uncaught TypeError: Cannot read property '0' of null on prediction using tensorflowjs(tfjs) 的问题。
我检查了“批处理”是正确的数组作为 dnn 模型的输入。
代码是
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="http://127.0.0.1:8080/dist/node_modules/@tensorflow/tfjs/dist/tf.min.js"></script>
</head>
<body>
<img id="cat_leaf" src="http://127.0.0.1:8080/dist/test_data/test03.JPG" width=256 height=256 alt="My Image" />
<input type = "button" value = "Image GOGO!" onclick= "img_pred()" />
<script>
async function img_pred(){
const catElement = document.getElementById("cat_leaf");
const img = tf.browser.fromPixels(catElement).toFloat();
const offset = tf.scalar(225.0);
// Normalize the image from
const normalized = img.div(offset);
// Reshape to a single-element batch so we can pass it to predict.
const batched = normalized.reshape([1, 256, 256, 3]);
//console.log('batched1',batched.toString())
const gt_mode = await tf.loadLayersModel('http://127.0.0.1:8080/dist/tea_model02/model.json');
const gopred =gt_mode.predict(batched); //occurred error here!!
const y_pred__ = gopred.toString() ;
console.log('result**',y_pred__);
//return gt_mode.predict(batched);
}
</script>
</body>
</html>