gpt4 book ai didi

javascript - model.predict 在使用 TensorflowJS 时不是一个函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:54:03 25 4
gpt4 key购买 nike

我已经像下面这样搜索了互联网的最远范围:

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

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

关于javascript - model.predict 在使用 TensorflowJS 时不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50576110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com