question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set the weight file location of tf.loadLayersModel

See original GitHub issue

TensorFlow.js version

1.3.2

Describe the problem or feature request

Hello. As I tried to create and use a simple model, it seems that the location of the weight file can be set automatically or only the prefix of the path can be set. In the case of me, I want to set the position of the weight like setting the position of the model, but I wonder if there is a reason not to set it.

The API I am expecting is as follows.

const loadModel = await tf.loadLayersModel(model.default,{weightPath:weight.default});

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Rcuz8commented, Jul 29, 2020

Tensorflow.js version: 2.0.1

I’m having an issue during this process.

I’m using Firebase Storage, which has a url looking like this:

<APP_URL>/o/directory_1%2Fdirectory_2%2Fdirectory_3%2Fmodel.json?<Query Params>                 => 200 OK
<APP_URL>/o/directory_1%2Fdirectory_2%2Fdirectory_3%2Fgroup1-shard1of1.bin?<Query Params>       => 200 OK
...

This is close to a standard directory-like convention (/ => %2F) but doesn’t conform exactly. When I load the model, the parsed shard URL is:

<APP_URL>/o/group1-shard1of1.bin?<Query Params>

which cuts off the directory structure and yields a 404. The queries on their own, and the model.json query in the program, yield 200 status code responses and the correct results.

I’ve tried loading the model in two ways.

Method 1:

const loadhttp = (url, dir2, dir3) =>
  tf.io.http(url, {
    weightPathPrefix: `directory_1%2F${dir2}%2F${dir3}%2F`,
  });
...

let http = loadhttp(model_url, dir2, dir3);

tf.loadLayersModel(http).then((model) => {
    console.log("Loaded model.");
    console.log(model);
})

An example from the docs specifies the entire path, so I when do that by adjusting the function to:

const loadhttp = (url) =>
  tf.io.http(url, {
    weightPathPrefix: before(url, 'model.json'),
  });

const before = (str, sub) => str.substring(0, str.indexOf(sub))

I still get the same result of:

<APP_URL>/o/group1-shard1of1.bin?<Query Params>          => 404 Not Found

Method 2:

I’ve adjusted the model.json file’s weightsManifest from:

"weightsManifest": [{"paths": [group1-shard1of1.bin"] ...

to

"weightsManifest": [{"paths": ["directory_1%2Fdirectory_2%2Fdirectory_3%2Fgroup1-shard1of1.bin"] ...

(and the JS code to)

const loadhttp = (url) =>
  tf.io.http(url);

And it was surprisingly unsuccessful (same incorrect 404 weights URL). My assumption was this directed the second query, and my only explanation is that this value is being overridden in some way. Although this is the less ideal method, I was fairly certain it would work.

0reactions
JulianKlugcommented, Sep 6, 2020

I am experiencing the same issue as described by @Rcuz8 above. Manually replacing “Functional” by “Model” in the model.json file solved the issue for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Save and load models | TensorFlow.js
Weights : These are binary files that store the weights of a given model in an efficient format. They are generally stored in...
Read more >
Tensorflow.js tf.loadLayersModel() Function - GeeksforGeeks
Weight files are assumed to be co-located with the model.json file by default. Return Value: Promise<tf.LayersModel>. Example 1: Javascript ...
Read more >
TFJS Layers Model Not Loading Weights Correctly
It seems to fetch the weight files in a different order each time it is executed. Does anyone know if this is expected...
Read more >
when loading a model in tensorflow.js for node, the weights ...
The weights aren't though, and the model behaves as if I had just constructed/compiled it. I am using tfjs-node save: const tf =...
Read more >
@tensorflow/tfjs-converter - npm
Use tf.loadLayersModel() to load the model in JavaScript. ... weight manifest file); group1-shard\*of\* (collection of binary weight files).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found