Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import tensorflow in javascript? Import in file, served by local http-server

I'm following this tutorial: https://codelabs.developers.google.com/codelabs/tfjs-training-classfication/index.html#2

I have set up a local HTTP-server, and that work and the app is running. However, when I try to execute step 3 (Load the data), I get the following error when loading the data:

Uncaught TypeError: Failed to resolve module specifier "@tensorflow/tfjs". Relative references must start with either "/", "./", or "../".

If i comment out the import statment:

import * as tf from '@tensorflow/tfjs';

the page actually loads and shows the sidebar with 16 handwritten digits.

  1. Does this mean that TensorFlow is not loaded?
  2. Is TensorFlow loaded, and I do not need this import statement?

  3. Or maybe most important, why does the import not work??

like image 833
otk Avatar asked Sep 26 '19 09:09

otk


1 Answers

you don't need to import the tfjs libraries if they are loaded as modules

in the html file put:

<html>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">

<head>
    ...
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"></script>
    <script type="module" src="YOUR_CODE.js"></script>
    ...
</head>
​

<body>
    ...
</body>
​

</html>

And in YOUR_CODE.js simply put:

async function testModel() {
    const model = await tf.loadGraphModel(...);
    ...
    model.execute(pic);
    ...
}


testModel();
like image 69
Romeo Kienzler Avatar answered Nov 05 '22 17:11

Romeo Kienzler