Set the weight file location of tf.loadLayersModel
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});
Yes that isn't something we support. The expectation is that the weight file(s) (note there may be multiple files) are stored in the same location to the json file. Is that particularly prohibitive for your use case?
Hi. @tafsiri. Thank you for your response. There are currently two usecases for me.
- I am creating and running a model locally. When I load the model and weight files in the build tool (parcel), Build Tool changed the file name, but the file name of the weight also changed, so I felt uncomfortable to do any work.
- Only specific extensions are available for cdn, but there is a problem in that we have to use the name of weigth.bin unconditionally because we want to change and use the extension.
Of course, I can solve both problems, but it's inconvenient and I was wondering why I didn't specify the file path of the weight. Thanks
Thanks for the info! The renaming of weight files would be an issue because they are specifically referred to by name in the model.json (and have an ordering that is important to maintain—they aren't always in one weights.bin file). You can pass a weightsPathPrefix to loadLayersModel to set a path to a folder containing weights, but it doesn't support arbitrary names for weight files. If you really wanted to get around that you would have to implement a custom IOHandler that supported your renaming conventions, here is the default HTTP one
Ah ha. Thanks. I think that I can solve this problem because of your info.
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.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 dyas if no further activity occurs. Thank you.
Closing as stale. Please @mention us if this needs more attention.
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.