Javascript 使用TensorflowJS时,model.predict不是函数
我已经搜索了互联网的各个领域,如下所示:Javascript 使用TensorflowJS时,model.predict不是函数,javascript,tensorflow,tensorflow.js,Javascript,Tensorflow,Tensorflow.js,我已经搜索了互联网的各个领域,如下所示: 所有这些模型都有类似的预测方法: model.predict() 根据文档,它应该返回一个包含预测的对象。然而,我总是得到一个不是函数的错误。下面是我的一段代码 constructor() { console.time('Loading of model'); this.mobileNet = new MobileNet(); this.mobileNet.loadMobilenet(); console.ti
model.predict()
根据文档,它应该返回一个包含预测的对象。然而,我总是得到一个不是函数的错误。下面是我的一段代码
constructor() {
console.time('Loading of model');
this.mobileNet = new MobileNet();
this.mobileNet.loadMobilenet();
console.timeEnd('Loading of model');
}
const result = tfc.tidy(() => {
// tfc.fromPixels() returns a Tensor from an image element.
const raw = tfc.fromPixels(this.CANVAS).toFloat();
const cropped = this.cropImage(raw);
const resized = tfc.image.resizeBilinear(cropped, [this.IMAGE_SIZE, this.IMAGE_SIZE])
// Normalize the image from [0, 255] to [-1, 1].
const offset = tfc.scalar(127);
const normalized = resized.sub(offset).div(offset);
// Reshape to a single-element batch so we can pass it to predict.
const batched = normalized.expandDims(0);
console.log(batched)
// Make a prediction through mobilenet.
return this.mobileNet.model.predict(batched).dataSync();
});
编辑
包括模型的代码
import * as tfc from '@tensorflow/tfjs-core';
import { loadFrozenModel } from '@tensorflow/tfjs-converter';
const MODEL_URL = '/assets/project-gaea/models/web_model.pb';
const WEIGHTS_URL = '/assets/project-gaea/models/weights_manifest.json';
const INPUT_NODE_NAME = 'input';
const OUTPUT_NODE_NAME = 'MobilenetV1/Predictions/Reshape_1';
const PREPROCESS_DIVISOR = tfc.scalar(255 / 2);
export default class MobileNet {
constructor() { }
async loadMobilenet() {
this.model = await loadFrozenModel(MODEL_URL, WEIGHTS_URL);
}
}
loadFrozenModel()
返回一个FrozenModel,而不是tf.model,因此正如您在本文中所看到的,FrozenModel
s使用execute()而不是predict()您能在创建模型或mobileNet变量的地方添加代码吗?@SebastianSpeitel,包括模型的代码段在预测之前是否尝试console.log模型?您是否确保预测是在loadMobilenet()
完成后发生的(因为它是异步的)?是的。我已经对每一件事进行了处理,甚至到了console.log(批处理)的程度。所有这些函数都返回了一个值,直到它命中predict()函数,console.log(this.mobileNet.model)
给了您什么?是的,通过反复试验,我们发现了这一点。不过谢谢你,真的很有帮助。我还认为执行的示例图像需要与声明的图像大小相同。现在只需要弄清楚如何通过网络摄像头传递数据