javascript – 使用TensorflowJS时,model.predict不是函数

我已经浏览了互联网的远端,如下所示:

> https://beta.observablehq.com/@cedrickchee/load-and-serve-a-pre-trained-model-in-javascript-with-tensor
> https://github.com/google/emoji-scavenger-hunt
> https://medium.com/tensorflow/a-gentle-introduction-to-tensorflow-js-dba2e5257702

所有这些都有类似的模型预测方式:

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,因此你可以在这个example中看到,FrozenModels使用execute()而不是predict()

相关文章

MNIST数据集可以说是深度学习的入门,但是使用模型预测单张M...
1、新建tensorflow环境(1)打开anacondaprompt,输入命令行...
这篇文章主要介绍“张量tensor是什么”,在日常操作中,相信...
tensorflow中model.fit()用法model.fit()方法用于执行训练过...
https://blog.csdn.net/To_be_little/article/details/12443...
根据身高推测体重const$=require('jquery');const...