Tensorflow JS tfjs | Unable to load model using tf.loadLayersModel

Viewed 2079

While executing the following code embedded in html using WAMP stack

const model = tf.loadLayersModel('js/model.json');

I encounter the following errors in chrome

> Uncaught (in promise) TypeError: Failed to fetch

> platform_browser.ts:28 GET http://localhost/poemgenerator/js/group1-shard3of22.bin net::ERR_EMPTY_RESPONSE

> Uncaught (in promise) TypeError: Failed to fetch

I have all the group1-shard__of22.bin present at the mentioned location

During every run of the code, the ERR_EMPTY_RESPONSE is shown on different files.

The tfjs loaded using

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.7.2/dist/tf.min.js"></script>


PS:

While executing the code IDM starts downloading all the group1-shard__of22.bin files

I The model being loaded was created in python and is a tf.keras model. which has been converted using tfjs converter


Update :

I used the replaced the above code with

async function predict(){
  const model = await tf.loadLayersModel('js/model.json');
  model.summary()
}

And also removed IDM but it showed another errors :

> errors.ts:48 Uncaught (in promise) Error: Provided weight data has no target variable: lstm_3/lstm_cell_3/kernel

> (index):68 Uncaught TypeError: model.summary is not a function

1 Answers

Since Tensorflow works best in an asynchronous way, you should use an async function with await when loading the model:

async function predict(){
  const model = await tf.loadLayersModel('js/model.json');
  // do prediction
}
Related