4

I don't know javascript and so I wanted to move a HUB model that's only available in JS to a SavedModel format.

I copied this script from a tutorial and tried to add the model.save function, but it's not working.

Here's the script:


    <html><head>
    <!-- Load the latest version of TensorFlow.js -->
    <script src="https://unpkg.com/@tensorflow/tfjs"></script>
    <script src="https://unpkg.com/@tensorflow-models/mobilenet"></script>

    </head>
    <body>
      <div id="console"></div>
      <!-- Add an image that we will use to test -->
      <img id="img" src="https://i.imgur.com/JlUvsxa.jpg" width="227" height="227">

      <script>
         let net;

         async function app() {
         console.log('Loading mobilenet..');

         // Load the model.
         net = await mobilenet.load();
         console.log('Successfully loaded model');

         // Make a prediction through the model on our image.
         const imgEl = document.getElementById('img');
         const result = await net.classify(imgEl);
         console.log(result);

         console.log('Saving mobilenet...');
         const saveResults = await net.save('downloads://my-model-1');
         console.log('Mobilenet saved');
       }
       app();

     </script>
    </body></html>

And here's the error I get:

Uncaught (in promise) TypeError: net.save is not a function
at app (TFjsmodelSaver.html:27)
app @ TFjsmodelSaver.html:27
async function (async)
app @ TFjsmodelSaver.html:19
(anonymous) @ TFjsmodelSaver.html:30

The error clearly says that net.save isn't a function at app, but at the same time net.classify works, and save is in the tfjs: https://js.tensorflow.org/api/0.12.5/#tf.Model.save

What am I missing?

BTW, if there's a way of getting the HUB models in SavedModel without going through this, please point me to it. I'd assume the models were created in TF first and then ported to TFJS, so they might be available somewhere...

oguz ismail
  • 1
  • 16
  • 47
  • 69

1 Answers1

2

mobilenet.load() returns a promise of type MobileNet. Here is the interface definition:

export interface MobileNet {
  load(): Promise<void>;
  infer(
      img: tf.Tensor|ImageData|HTMLImageElement|HTMLCanvasElement|
      HTMLVideoElement,
      embedding?: boolean): tf.Tensor;
  classify(
      img: tf.Tensor3D|ImageData|HTMLImageElement|HTMLCanvasElement|
      HTMLVideoElement,
      topk?: number): Promise<Array<{className: string, probability: number}>>;
}

The loaded model does not contain a save method thus throwing the error.

save is not a function

Is it worth it saving the model ? The loaded model is not used for training. So each time it is needed it can be loaded using mobilenet.load.

The mobilenet package is just a wrapper around the mobilet savedModel. The github repo contains the url of different version of mobilenet from which the savedModel can be downloaded. The model can be loaded locally using tf.loadGraphModel. But this locally loaded model will be of type tf.GraphModel and will not contain the methods classify and infer

A next release will provide the ability to save tf.GraphModel

Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
edkeveked
  • 17,989
  • 10
  • 55
  • 93
  • 1
    Thanks a lot for the response, I was giving up on getting this answered. 1) Is it worth it to download the model? I need to use it in TF 2.0 (python) in order to train other models. I was downloading it to convert it to a SavedModel using the TFJS converter. I looked at the GraphModel and it seems I can't (directly) convert it to a SavedModel or use it in TF 2.0. Am I wrong? – Kathy Jones Jan 22 '20 at 06:07
  • 1
    Sorry, I didn't spot the question early because `tensorflow.js` tag was not used. To answer your question, Currently, it is not possible to train savedModel in JavaScript. Besides, the converter can't convert a js savedModel to python savedModel. If the goal is to use a savedModel in Python, it will be better to do everything in python – edkeveked Jan 22 '20 at 08:43
  • Thanks. I guess I need to find a pre-trained SavedModel somewhere else... – Kathy Jones Jan 29 '20 at 05:34
  • Are there any ideas on downloading a model trained in browser after transfer learning (e.g. combining a mobilenet backbone with a KNN classifier) on data provided through upload in the browser? I feel then downloading the merged model would definitely be worth it. (I am currently struggling with that here https://stackoverflow.com/questions/69163291/merge-and-save-model-in-tensorflow-js-after-training-in-browser) - in case you want to take a look. Thanks – mrk Sep 17 '21 at 09:04