簡體   English   中英

使用TensorflowJS時,model.predict不是函數

[英]model.predict is not a function while using TensorflowJS

我已經瀏覽了互聯網的遠端,如下所示:

所有這些都有類似的模型預測方式:

model.predict()

根據文檔,它應該返回一個帶有預測的對象。 但是,我總是得到一個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();
});

編輯包含模型的代碼

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使用execute()而不是predict()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM