使用 TensorflowJS 时 model.predict 不是函数

oni*_*oss 5 javascript tensorflow tensorflow.js

我已经搜索了互联网的各个角落,如下所示:

它们都有类似的从模型预测的方法:

model.predict()
Run Code Online (Sandbox Code Playgroud)

根据文档,它应该返回一个包含预测的对象。但是,我总是遇到is not a function错误。下面是我拥有的一段代码。

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();
});
Run Code Online (Sandbox Code Playgroud)

编辑 包括模型的代码

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Seb*_*tel 4

loadFrozenModel()返回一个 FrozenModel,而不是 tf.model,因此正如您在本中看到的,FrozenModels 使用execute() 而不是predict()