从express加载tensorflow

从express加载tensorflow

本文介绍了如何使用tf.loadLayersModel()从express加载tensorflow-js权重?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误- RangeError:当我尝试将tf-js模型加载到Reactjs中时,尝试在ArrayBuffer上构造出界TypedArray.

I get the error - RangeError: attempting to construct out-of-bounds TypedArray on ArrayBuffer when I try to load a tf-js model into Reactjs.

我正在使用express.js发送json + bin文件来做出反应,以便可以在浏览器本身中进行推断.

I'm using express.js to send the json+bin files to react so that I can run inference in the browser itself.

这是相关的Express.js代码.json + bin文件都在同一文件夹中.

Here's the relevant Express.js code. The json+bin files are all in the same folder.

app.use(
  "/api/pokeml/classify",
  express.static(path.join(__dirname, "classifier_models/original/model.json"))
)

这是我如何在React中加载此内容-

Here's how I'm loading this in React -

import * as tf from "@tensorflow/tfjs"

  useEffect(() => {
    async function fetchModel() {
      // const mobileNetModel = await mobilenet.load()
      // const classifierModel = await axios.get("api/pokeml/classify")
      const classifierModel = await tf.loadLayersModel(
        "http://localhost:3001/api/pokeml/classify"
      )
      setModel(classifierModel)
    }
    fetchModel()
  }, [])

model.json文件正确加载,但分片无法加载-

The model.json file loads correctly but the shards do not -

推荐答案

我在沙箱中重新创建了您的问题.服务器 |客户端

I re-created your problem in a sandbox. Server | Client

碎片是从与调用模型相同的终结点中获取的,因此一旦拥有 model.json ,它将返回以获取 api/pokeml/group1-shard2of2.bin 当前无法访问.

The shards are being sourced from the same endpoint that you used to call the model, so once you have the model.json, it goes back to fetch api/pokeml/group1-shard2of2.bin which is currently not accessible.

所以

app.use(
  "/api/pokeml/classify",
  express.static(path.join(__dirname, "classifier_models/original/model.json"))
);

// add this,
// to allow access to the `original` folder from `api/pokeml` for
// the shards to be accessible
app.use(
  "/api/pokeml",
  express.static(path.join(__dirname, "classifier_models/original"))
);

如果这仍然不能解决您的问题,则可以尝试替换沙盒.这样一来,您就可以检测出您的模型是否格式错误.

If this still doesn't solve your problem, you could try replacing the model/shards in the sandbox. That would allow you to detect if your model is malformed.

这篇关于如何使用tf.loadLayersModel()从express加载tensorflow-js权重?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:59