Tfjs: The path to load from must be a file. Loading from a directory is not supported.

Created on 23 Nov 2018  路  4Comments  路  Source: tensorflow/tfjs

To get help from the community, check out our Google group.

TensorFlow.js version

@tensorflow/tfjs: 0.13.5
@tensorflow/tfjs-node-gpu: ^0.1.21

Browser version

node v8.11.1

Describe the problem or feature request

I'm unable to load a model just saved to the file system. It seems to save successfully, but I receive the following error when I call _loadModel_:

Error: The path to load from must be a file. Loading from a directory is not supported.

Am I doing something wrong or it this not yet supported?

Code to reproduce the bug / link to feature request

  const tf = require('@tensorflow/tfjs')
  require('@tensorflow/tfjs-node-gpu')

  let model = tf.sequential();

  const fileurl = 'file://./test.model'

  await model.save(fileurl)

  // # ls ./test.model
  // model.json  weights.bin

  try {
    model = await tf.loadModel(fileurl)
  } catch (error) {
    // (node:62270) UnhandledPromiseRejectionWarning: Error: The path to load from must be a file. 
    // Loading from a directory is not supported.
    console.log(error)    
  }

Most helpful comment

That threw me for a loop as well.
You'll need to do model = await tf.loadModel(path.join(fileurl, "model.json"));

All 4 comments

That threw me for a loop as well.
You'll need to do model = await tf.loadModel(path.join(fileurl, "model.json"));

I tried that before, but since that gives me another error, I figured I'd ask for help. The error when I point it directly to the model.json is:

TypeError: Cannot read property 'className' of undefined
at Sequential.fromConfig (node_modules/@tensorflow/tfjs-layers/dist/models.js:404:29)
at Object.deserializeKerasObject (node_modules/@tensorflow/tfjs-layers/dist/utils/generic_utils.js:174:29)

It seems like it's saving in one format and then trying to load in another, but the examples in the API docs don't demonstrate specifying a format when saving/loading. Any suggestions?

I believe it's because the model doesn't have any layers.

I was able to reproduce your error, but then resolved it by adding a layer (and specifying "model.json" during load).

model.add(tf.layers.conv2d({
  inputShape: [28, 28, 1],
  kernelSize: 5,
  filters: 8,
  strides: 1,
  activation: 'relu',
  kernelInitializer: 'VarianceScaling'
}));

Yep, that was it. Thanks for the help @adwellj !

Was this page helpful?
0 / 5 - 0 ratings