How to load tensorflow-js weights from express using tf.loadLayersModel()?

♀尐吖头ヾ 提交于 2021-02-05 12:22:22

问题


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.

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

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"))
)

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()
  }, [])

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


回答1:


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

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.

So,

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.



来源:https://stackoverflow.com/questions/62528719/how-to-load-tensorflow-js-weights-from-express-using-tf-loadlayersmodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!